-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8팀 김도운] [Chapter 2-1] 클린코드와 리팩토링 #42
base: main
Are you sure you want to change the base?
Conversation
export function CartSummary() { | ||
const element = document.createElement('div'); | ||
element.id = 'cart-total'; | ||
element.className = 'text-xl font-bold my-4'; | ||
|
||
const { getCart } = useCart(); | ||
|
||
const calSummary = () => { | ||
const cart = getCart(); | ||
let subtotal = 0; | ||
let totalWithQuantityDiscount = 0; | ||
let totalQuantity = 0; | ||
|
||
// 각 상품의 수량별 할인 계산 | ||
cart.forEach(({ product, quantity }) => { | ||
const itemTotal = product.price * quantity; | ||
subtotal += itemTotal; | ||
totalQuantity += quantity; | ||
|
||
if (quantity >= 10) { | ||
totalWithQuantityDiscount += itemTotal * (1 - product.discountRate); | ||
} else { | ||
totalWithQuantityDiscount += itemTotal; | ||
} | ||
}); | ||
|
||
// 최종 금액 계산 (수량 할인 vs 대량 구매 할인) | ||
let finalTotal = totalWithQuantityDiscount; | ||
let discountRate = (subtotal - totalWithQuantityDiscount) / subtotal || 0; | ||
|
||
// 30개 이상 구매시 25% 할인과 비교 | ||
if (totalQuantity >= 30) { | ||
const bulkDiscountTotal = subtotal * 0.75; // 25% 할인 | ||
if (bulkDiscountTotal < finalTotal) { | ||
finalTotal = bulkDiscountTotal; | ||
discountRate = 0.25; | ||
} | ||
} | ||
|
||
// 화요일 추가 할인 | ||
if (new Date().getDay() === 2) { | ||
const tuesdayDiscount = 0.1; | ||
discountRate = Math.max(discountRate, tuesdayDiscount); | ||
finalTotal = subtotal * (1 - discountRate); | ||
} | ||
|
||
const points = Math.floor(finalTotal / 1000); | ||
|
||
return { | ||
total: Math.round(finalTotal), | ||
points, | ||
discountRate, | ||
}; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 주석이 적절한 곳에 있어서 읽기 좋네요!👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 주석이 적절한 곳에 있어서 읽기 좋네요!👍
저도 그렇게 느꼈어요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
와 도운님 기본 과제에서 정말 리액트 도입 직전까지 작업하신 것 같아요!👍 매번 느끼는 거지만 도운님 정말 잘 하시네요!👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
많이 배우고 갑니다.
const element = document.createElement('div'); | ||
element.className = 'flex justify-between items-center mb-2'; | ||
element.id = product.id; | ||
const { updateItemQuantity, removeFromCart } = useCart(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리액트 커스텀 훅으로 교체되기 좋은 패턴인 것 같습니다.
도운님 코드는 항상 감동이 있네요..
<button class="quantity-change bg-blue-500 text-white px-2 py-1 rounded mr-1" data-product-id=${product.id} data-change="1">+</button> | ||
<button class="remove-item bg-red-500 text-white px-2 py-1 rounded">삭제</button> | ||
</div> | ||
`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 여기만 innerHtml형식으로 작업하신 이유가 뭘까요?(시간 이슈..?)
export function CartSummary() { | ||
const element = document.createElement('div'); | ||
element.id = 'cart-total'; | ||
element.className = 'text-xl font-bold my-4'; | ||
|
||
const { getCart } = useCart(); | ||
|
||
const calSummary = () => { | ||
const cart = getCart(); | ||
let subtotal = 0; | ||
let totalWithQuantityDiscount = 0; | ||
let totalQuantity = 0; | ||
|
||
// 각 상품의 수량별 할인 계산 | ||
cart.forEach(({ product, quantity }) => { | ||
const itemTotal = product.price * quantity; | ||
subtotal += itemTotal; | ||
totalQuantity += quantity; | ||
|
||
if (quantity >= 10) { | ||
totalWithQuantityDiscount += itemTotal * (1 - product.discountRate); | ||
} else { | ||
totalWithQuantityDiscount += itemTotal; | ||
} | ||
}); | ||
|
||
// 최종 금액 계산 (수량 할인 vs 대량 구매 할인) | ||
let finalTotal = totalWithQuantityDiscount; | ||
let discountRate = (subtotal - totalWithQuantityDiscount) / subtotal || 0; | ||
|
||
// 30개 이상 구매시 25% 할인과 비교 | ||
if (totalQuantity >= 30) { | ||
const bulkDiscountTotal = subtotal * 0.75; // 25% 할인 | ||
if (bulkDiscountTotal < finalTotal) { | ||
finalTotal = bulkDiscountTotal; | ||
discountRate = 0.25; | ||
} | ||
} | ||
|
||
// 화요일 추가 할인 | ||
if (new Date().getDay() === 2) { | ||
const tuesdayDiscount = 0.1; | ||
discountRate = Math.max(discountRate, tuesdayDiscount); | ||
finalTotal = subtotal * (1 - discountRate); | ||
} | ||
|
||
const points = Math.floor(finalTotal / 1000); | ||
|
||
return { | ||
total: Math.round(finalTotal), | ||
points, | ||
discountRate, | ||
}; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 주석이 적절한 곳에 있어서 읽기 좋네요!👍
저도 그렇게 느꼈어요.
element.innerHTML = products | ||
.filter((product) => product.quantity === 0) | ||
.map((product) => `<span>${product.name}: 품절</span>`) | ||
.join(''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
체이닝 메소드를 잘 쓰셨네요.
getMessage: (product) => `${product.name}은(는) 어떠세요? 지금 구매하시면 5% 추가 할인!`, | ||
priceRate: 0.05, | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
속성 네이밍이나 객체 분리가 너무 좋은 것 같아요.
객체 변수명은 Config가 들어가는 이유가 무엇일까요??
let instance = null; | ||
|
||
export function useCart() { | ||
if (instance) return instance; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
싱글톤으로 상태공유를 구현하신 건가요?
과제 체크포인트
기본과제
심화과제
과제 셀프회고
과제에서 좋았던 부분
과제를 하면서 새롭게 알게된 점
과제를 진행하면서 아직 애매하게 잘 모르겠다 하는 점, 혹은 뭔가 잘 안되서 아쉬운 것들
리뷰 받고 싶은 내용이나 궁금한 것에 대한 질문