diff --git a/apps/spa/src/components/CartDrawer.tsx b/apps/spa/src/components/CartDrawer.tsx index 93152da..6bfe149 100644 --- a/apps/spa/src/components/CartDrawer.tsx +++ b/apps/spa/src/components/CartDrawer.tsx @@ -124,6 +124,16 @@ function CartDrawerFooter({ subtotal, onClose }: { subtotal: number; onClose: () const [discountAmount, setDiscountAmount] = React.useState(0) const [isFreeShipping, setIsFreeShipping] = React.useState(false) const [couponValid, setCouponValid] = React.useState(false) + const [activePromos, setActivePromos] = React.useState([]) + + React.useEffect(() => { + fetch(`${API_BASE}/promotions/active`) + .then(res => res.json()) + .then(data => { + if (Array.isArray(data)) setActivePromos(data) + }) + .catch(console.error) + }, []) React.useEffect(() => { const code = localStorage.getItem('homaroma_active_coupon') @@ -132,7 +142,7 @@ function CartDrawerFooter({ subtotal, onClose }: { subtotal: number; onClose: () fetch(`${API_BASE}/coupons/validate`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ code, cartTotal: subtotal }) + body: JSON.stringify({ code, cartTotal: subtotalAfterL2 }) }) .then(r => r.json()) .then(data => { @@ -146,7 +156,7 @@ function CartDrawerFooter({ subtotal, onClose }: { subtotal: number; onClose: () }) .catch(() => setCouponValid(false)) } - }, [subtotal]) + }, [subtotal, activePromos]) const removeCoupon = () => { localStorage.removeItem('homaroma_active_coupon') @@ -156,7 +166,22 @@ function CartDrawerFooter({ subtotal, onClose }: { subtotal: number; onClose: () setCouponValid(false) } + // Calculate Level 1 Promo + const promoLevel1 = activePromos.find(p => p.level === 1 && subtotal >= p.minPurchase) + const promoLevel1Disc = promoLevel1 + ? (promoLevel1.type === 'PERCENTAGE' ? subtotal * (promoLevel1.value / 100) : promoLevel1.value) + : 0 + const subtotalAfterL1 = Math.max(0, subtotal - promoLevel1Disc) + + // Calculate Level 2 Promo + const promoLevel2 = activePromos.find(p => p.level === 2 && subtotalAfterL1 >= p.minPurchase) + const promoLevel2Disc = promoLevel2 + ? (promoLevel2.type === 'PERCENTAGE' ? subtotalAfterL1 * (promoLevel2.value / 100) : promoLevel2.value) + : 0 + + const subtotalAfterL2 = Math.max(0, subtotalAfterL1 - promoLevel2Disc) + const total = Math.max(0, subtotalAfterL2 - discountAmount) return (
@@ -167,6 +192,18 @@ function CartDrawerFooter({ subtotal, onClose }: { subtotal: number; onClose: () ราคาสินค้า {formatPrice(subtotal)}
+ {promoLevel1Disc > 0 && ( +
+ {promoLevel1?.name} + - {formatPrice(promoLevel1Disc)} +
+ )} + {promoLevel2Disc > 0 && ( +
+ {promoLevel2?.name} + - {formatPrice(promoLevel2Disc)} +
+ )} {(discountAmount > 0 || isFreeShipping) && savedCoupon && (
@@ -194,7 +231,7 @@ function CartDrawerFooter({ subtotal, onClose }: { subtotal: number; onClose: () )}
รวมสุทธิ - {formatPrice(Math.max(0, subtotal - discountAmount))} + {formatPrice(total)}