From e93a330a399ddd91314dc25651416b62ea2ea9c9 Mon Sep 17 00:00:00 2001 From: Jaeha Lee Date: Sun, 5 Mar 2023 15:22:04 +0900 Subject: [PATCH] Refactor: [cartPage.ts]#145 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 수량 increment, decrement, delete 개별적인 함수를 이벤트와 분리하였고 2. switch문으로 가독성 있게 리팩토링 하였습니다. --- src/ts/page/cartPage/cartPage.ts | 116 +++++++++++++++++-------------- 1 file changed, 63 insertions(+), 53 deletions(-) diff --git a/src/ts/page/cartPage/cartPage.ts b/src/ts/page/cartPage/cartPage.ts index 0058b9a..39123da 100644 --- a/src/ts/page/cartPage/cartPage.ts +++ b/src/ts/page/cartPage/cartPage.ts @@ -31,18 +31,6 @@ export const renderCartTotalPrice = (): number => { return cartTotalPriceReduce; }; -/** 장바구니 비었을 때 '결제하기 버튼' 예외처리 */ -// const handleCartPaymentBtn = () => { -// // const shoppingCartArr = shoppingCartStore.getLocalStorage(); -// const enableBtn = ``; -// const disabledBtn = ``; -// if (shoppingCartStore.getLocalStorage().length > 0) { -// return enableBtn; -// } else if (shoppingCartStore.getLocalStorage().length === 0) { -// return disabledBtn; -// } -// }; - // 장바구니 페이지 초기 렌더링 export const renderInitCartPage = `
@@ -79,8 +67,13 @@ export const renderInitCartPage = `
+ >
+ +
+
@@ -212,56 +205,73 @@ $('.app')?.addEventListener('click', (e: MouseEvent) => { // pushInCart(e); // }); -/** 장바구니 페이지에서 수량 핸들링 */ -$('.app').addEventListener('click', (e: MouseEvent) => { - const id = (e.target as HTMLLIElement).closest('li')?.dataset.productId; - // 구매 수량 + +/** 장바구니 아이템 수량 조절 함수 */ +const handleCartItemQty = ( + id: string, + operation: 'increment' | 'decrement' | 'delete', +) => { let shoppingCartArr = shoppingCartStore.getLocalStorage(); - if ((e.target as HTMLButtonElement).classList.contains('cart-addQtyBtn')) { - storeLocalStorage(id); - // shoppingCartStore.setLocalStorage(shoppingCartArr); - // 카트 페이지 렌더 - renderCartPage(); - return; - } + const existingItem = shoppingCartArr.find( + (item: ShoppingCartStore): boolean => item.id === id, + ); - // 구매 수량 - - if ((e.target as HTMLButtonElement).classList.contains('cart-minusQtyBtn')) { - // let shoppingCartArr = shoppingCartStore.getLocalStorage(); - const existingItem = shoppingCartArr.find( - (item: ShoppingCartStore): boolean => item.id === id, - ); + switch (operation) { + case 'increment': + if (existingItem) { + existingItem.price += existingItem.pricePerOne; + existingItem.qty += 1; + existingItem.count += 1; - if (existingItem) { - if (existingItem.price > existingItem.pricePerOne) { - existingItem.price -= existingItem.pricePerOne; - } - if (existingItem.qty > 1) { - existingItem.qty -= 1; + // + localStorage에 저장 + shoppingCartStore.setLocalStorage(shoppingCartArr); } - if (existingItem.count > 1) { - existingItem.count -= 1; + break; + + case 'decrement': + if (existingItem) { + if (existingItem) { + if (existingItem.price > existingItem.pricePerOne) { + existingItem.price -= existingItem.pricePerOne; + } + if (existingItem.qty > 1) { + existingItem.qty -= 1; + } + if (existingItem.count > 1) { + existingItem.count -= 1; + } + + // - localStorage에 저장 + shoppingCartStore.setLocalStorage(shoppingCartArr); + } } + break; - // 카트 페이지 렌더 + case 'delete': + shoppingCartArr = shoppingCartStore + .getLocalStorage() + .filter((item: ShoppingCartStore): boolean => item.id !== id); shoppingCartStore.setLocalStorage(shoppingCartArr); - renderCartPage(); - } + break; } - // 장바구니에서 삭제 - if ( + countQtyInCart(); + renderCartPage(); +}; + +/** 장바구니 페이지에서 수량 핸들링 이벤트 */ +$('.app').addEventListener('click', (e: MouseEvent) => { + const id = (e.target as HTMLLIElement).closest('li')?.dataset.productId; + + if ((e.target as HTMLButtonElement).classList.contains('cart-addQtyBtn')) { + handleCartItemQty(id, 'increment'); + } else if ( + (e.target as HTMLButtonElement).classList.contains('cart-minusQtyBtn') + ) { + handleCartItemQty(id, 'decrement'); + } else if ( (e.target as HTMLButtonElement).classList.contains('cartProductDeleteBtn') ) { - shoppingCartArr = shoppingCartStore - .getLocalStorage() - .filter((item: ShoppingCartStore): boolean => item.id !== id); - // storeLocalStorage(id); - shoppingCartStore.setLocalStorage(shoppingCartArr); - - countQtyInCart(); - renderCartPage(); - return; + handleCartItemQty(id, 'delete'); } });