From f42fbce8242d89bec250cab37bab7d6f67625134 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Sat, 25 Oct 2025 13:53:40 +0700 Subject: coba lagi --- .../product-detail/components/ProductDetail.tsx | 70 +++++++++++++++------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/src-migrate/modules/product-detail/components/ProductDetail.tsx b/src-migrate/modules/product-detail/components/ProductDetail.tsx index ef609fcf..ad6d4cd8 100644 --- a/src-migrate/modules/product-detail/components/ProductDetail.tsx +++ b/src-migrate/modules/product-detail/components/ProductDetail.tsx @@ -109,6 +109,7 @@ const ProductDetail = ({ product }: Props) => { })(); const [mainImage, setMainImage] = useState(allImages[0] || ''); + const [discount, setDiscount] = useState(0); useEffect(() => { if (!allImages.includes(mainImage)) { @@ -153,31 +154,55 @@ const ProductDetail = ({ product }: Props) => { // normalisasi nama properti (camelCase vs snake_case) const discountType = voucher?.discountType ?? voucher?.discount_type ?? ''; // 'percentage' atau 'fixed' + console.log('discountType:', discountType); const discountAmount = Number( voucher?.discountAmount ?? voucher?.discount_amount ?? 0 ); + console.log('discountAmount:', discountAmount); const maxDiscount = Number( voucher?.maxDiscount ?? voucher?.max_discount ?? 0 ); + // console.log('maxDiscount:', maxDiscount); // base price -> samakan dengan ProductCard: pakai price_discount const basePrice = - Number(product?.lowest_price?.price_discount ?? 0) || - Number(product?.lowest_price?.price ?? 0); + Number(product?.lowest_price.price ?? 0) || + Number(product.lowest_price.price_discount ?? 0); + + // console.log('basePrice:', basePrice); let voucherDiscount = 0; - if (discountType === 'percentage') { - voucherDiscount = basePrice * (discountAmount / 100); - if (maxDiscount > 0 && voucherDiscount > maxDiscount) { - voucherDiscount = maxDiscount; + const hitungDiscountVoucher = () => { + let countDiscount = 0; + if (discountType === 'percentage') { + countDiscount = basePrice * (discountAmount / 100); + console.log('countDiscount awal:', countDiscount); + if (maxDiscount > 0 && countDiscount > maxDiscount) { + countDiscount = maxDiscount; + } + } else { + countDiscount = discountAmount; + console.log('countDiscount:', countDiscount); } - } else { - // fixed - voucherDiscount = discountAmount; - } - voucherDiscount = Math.max(0, Math.floor(voucherDiscount)); + console.log('countDiscount final:', countDiscount); - const priceAfterVoucher = Math.max(0, basePrice - voucherDiscount); + setDiscount(countDiscount); + }; + + useEffect(() => { + hitungDiscountVoucher(); + }, []); + + // console.log('voucherDiscount:', voucherDiscount); + // if (discountType === 'percentage') { + // voucherDiscount = basePrice * (discountAmount / 100); + // if (maxDiscount > 0 && voucherDiscount > maxDiscount) { + // voucherDiscount = maxDiscount; + // } + // } else { + // voucherDiscount = discountAmount; + // } + // voucherDiscount = Math.max(0, Math.floor(voucherDiscount)); return ( <> @@ -290,17 +315,16 @@ const ProductDetail = ({ product }: Props) => { {/* ===== Kolom kanan: info ===== */}
- {isMobile && ( -
- {' '} - Pakai{' '} - - {' '} - Voucher belanja hemat {currencyFormat(voucherDiscount)} - {' '} - saat checkout -
- )} + {/* {isMobile && ( */} +
+ Pakai{' '} + + {' '} + Voucher belanja hemat {currencyFormat(discount)} + {' '} + saat checkout +
+ {/* )} */}

{product.name}

-- cgit v1.2.3