diff options
| author | Miqdad <ahmadmiqdad27@gmail.com> | 2025-10-25 13:53:40 +0700 |
|---|---|---|
| committer | Miqdad <ahmadmiqdad27@gmail.com> | 2025-10-25 13:53:40 +0700 |
| commit | f42fbce8242d89bec250cab37bab7d6f67625134 (patch) | |
| tree | 882351a9acfa66e368d4cdb91b0dc6ca3074a868 | |
| parent | 88339f8b4afa2eb840220fa573a5fa4599702fab (diff) | |
<Miqdad> coba lagi
| -rw-r--r-- | src-migrate/modules/product-detail/components/ProductDetail.tsx | 70 |
1 files 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 ===== */} <div className='md:w-8/12 px-4 md:pl-6'> <div className='h-6 md:h-0' /> - {isMobile && ( - <div className='text text-sm font-medium'> - <TicketIcon className='inline text-yellow-300 w-5 h-5' />{' '} - Pakai{' '} - <span className='text-green-600 font-extrabold'> - {' '} - Voucher belanja hemat {currencyFormat(voucherDiscount)} - </span>{' '} - saat checkout - </div> - )} + {/* {isMobile && ( */} + <div className='text text-sm font-medium'> + <TicketIcon className='inline text-yellow-300 w-5 h-5' /> Pakai{' '} + <span className='text-green-600 font-extrabold'> + {' '} + Voucher belanja hemat {currencyFormat(discount)} + </span>{' '} + saat checkout + </div> + {/* )} */} <h1 className={style['title']}>{product.name}</h1> <div className='h-3 md:h-0' /> <Information product={product} /> |
