From 1bbdc0ad8b5c23b53a64b9880e0acb7e127bf2c4 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Sun, 26 Oct 2025 00:43:19 +0700 Subject: fixed discout prod detail --- .../product-detail/components/ProductDetail.tsx | 88 ++++++++-------------- 1 file changed, 32 insertions(+), 56 deletions(-) (limited to 'src-migrate/modules') diff --git a/src-migrate/modules/product-detail/components/ProductDetail.tsx b/src-migrate/modules/product-detail/components/ProductDetail.tsx index ad6d4cd8..abd7188a 100644 --- a/src-migrate/modules/product-detail/components/ProductDetail.tsx +++ b/src-migrate/modules/product-detail/components/ProductDetail.tsx @@ -137,72 +137,48 @@ const ProductDetail = ({ product }: Props) => { setCurrentIdx(i); setMainImage(allImages[i] || ''); }; - // ====== Ambil voucher & hitung potongan (persis seperti ProductCard) ====== - const voucherRaw = Array.isArray(product?.voucher_pasti_hemat) - ? product.voucher_pasti_hemat[0] - : product?.voucher_pasti_hemat; - - // kalau backend kadang kirim string JSON - let voucher: any = voucherRaw; - if (typeof voucherRaw === 'string') { - try { - voucher = JSON.parse(voucherRaw.replace(/'/g, '"')); - } catch { - voucher = null; - } - } - // normalisasi nama properti (camelCase vs snake_case) - const discountType = voucher?.discountType ?? voucher?.discount_type ?? ''; // 'percentage' atau 'fixed' - console.log('discountType:', discountType); - const discountAmount = Number( + const voucherNew = Array.isArray(product?.new_voucher_pasti_hemat) + ? product.new_voucher_pasti_hemat[0] + : undefined; + + const voucher = voucherNew ?? null; + + const type = voucher?.discount_type ?? ''; // 'percentage' | 'percent' | 'fixed' + const amount = Number( voucher?.discountAmount ?? voucher?.discount_amount ?? 0 ); - console.log('discountAmount:', discountAmount); - const maxDiscount = Number( - voucher?.maxDiscount ?? voucher?.max_discount ?? 0 - ); - // console.log('maxDiscount:', maxDiscount); + const max = Number(voucher?.max_discount ?? 0); + const min = Number(voucher?.min_purchase ?? 0); - // base price -> samakan dengan ProductCard: pakai price_discount const basePrice = - Number(product?.lowest_price.price ?? 0) || - Number(product.lowest_price.price_discount ?? 0); - - // console.log('basePrice:', basePrice); - - let voucherDiscount = 0; - const hitungDiscountVoucher = () => { - let countDiscount = 0; - if (discountType === 'percentage') { - countDiscount = basePrice * (discountAmount / 100); - console.log('countDiscount awal:', countDiscount); - if (maxDiscount > 0 && countDiscount > maxDiscount) { - countDiscount = maxDiscount; - } + Number(product?.lowest_price?.price_discount ?? 0) || + Number(product?.lowest_price?.price ?? 0); + + function calcVoucherDiscount() { + if (!voucher || !basePrice) return 0; + if (min > 0 && basePrice < min) return 0; + + const percent = type.toLowerCase().startsWith('percent') + ? amount <= 1 + ? amount * 100 + : amount + : 0; + + let cut = 0; + if (type.toLowerCase().startsWith('percent')) { + cut = Math.floor(basePrice * (percent / 100)); } else { - countDiscount = discountAmount; - console.log('countDiscount:', countDiscount); + cut = Math.floor(amount || 0); } - console.log('countDiscount final:', countDiscount); - setDiscount(countDiscount); - }; + if (max > 0) cut = Math.min(cut, max); + return Math.max(0, cut); + } 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)); + setDiscount(calcVoucherDiscount()); + }, [product?.lowest_price]); return ( <> -- cgit v1.2.3