From f3aa76b1810b3bc8b25bd02c76b50384893fc453 Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Tue, 6 Aug 2024 15:12:56 +0700 Subject: voucher --- src/lib/checkout/components/Checkout.jsx | 118 ++++++++++++++++++++++--------- 1 file changed, 86 insertions(+), 32 deletions(-) (limited to 'src/lib/checkout/components') diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index 09a791ee..84f68dab 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -131,6 +131,7 @@ const Checkout = () => { setLoadingVoucher(true); let dataVoucher = await getVoucher(auth?.id, { source: query, + type: 'all,brand', }); SetListVoucher(dataVoucher); @@ -146,40 +147,71 @@ const Checkout = () => { }; const VoucherCode = async (code) => { - const source = 'code=' + code + '&source=' + query; // let dataVoucher = await findVoucher(code, auth.id, query); - let dataVoucher = await getVoucherNew(source); + let dataVoucher = await getVoucher(auth?.id, { + source: query, + code: code + }); if (dataVoucher.length <= 0) { SetFindVoucher(1); return; } let addNewLine = dataVoucher[0]; - let checkList = listVouchers?.findIndex( - (voucher) => voucher.code == addNewLine.code - ); - if (checkList >= 0) { - if (listVouchers[checkList].canApply) { - ToggleSwitch(code); - SetCodeVoucher(null); - } else { - SetSelisihHargaCode(listVouchers[checkList].differenceToApply); + console.log('addNewLine', addNewLine); + if (addNewLine.applyType != 'shipping') { + let checkList = listVouchers?.findIndex( + (voucher) => voucher.code == addNewLine.code + ); + if (checkList >= 0) { + if (listVouchers[checkList].canApply) { + ToggleSwitch(code); + SetCodeVoucher(null); + } else { + SetSelisihHargaCode(listVouchers[checkList].differenceToApply); + SetFindVoucher(2); + } + return; + } + if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) { + SetSelisihHargaCode( + currencyFormat(addNewLine.minPurchaseAmount - cartCheckout?.subtotal) + ); SetFindVoucher(2); + return; + } else { + SetFindVoucher(3); + SetButtonTerapkan(true); } - return; - } - if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) { - SetSelisihHargaCode( - currencyFormat(addNewLine.minPurchaseAmount - cartCheckout?.subtotal) - ); - SetFindVoucher(2); - return; + SetListVoucher((prevList) => [addNewLine, ...prevList]); + SetActiveVoucher(addNewLine.code); } else { - SetFindVoucher(3); - SetButtonTerapkan(true); + let checkList = listVoucherShippings?.findIndex( + (voucher) => voucher.code == addNewLine.code + ); + if (checkList >= 0) { + if (listVoucherShippings[checkList].canApply) { + ToggleSwitch(code); + SetCodeVoucher(null); + } else { + SetSelisihHargaCode(listVoucherShippings[checkList].differenceToApply); + SetFindVoucher(2); + } + return; + } + if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) { + SetSelisihHargaCode( + currencyFormat(addNewLine.minPurchaseAmount - cartCheckout?.subtotal) + ); + SetFindVoucher(2); + return; + } else { + SetFindVoucher(3); + SetButtonTerapkan(true); + } + SetListVoucherShipping((prevList) => [addNewLine, ...prevList]); + setActiveVoucherShipping(addNewLine.code); } - SetListVoucher((prevList) => [addNewLine, ...prevList]); - SetActiveVoucher(addNewLine.code); }; useEffect(() => { @@ -187,7 +219,7 @@ const Checkout = () => { }, [bottomPopup]); useEffect(() => { - voucher(); + // voucher(); const loadExpedisi = async () => { let dataExpedisi = await ExpedisiList(); dataExpedisi = dataExpedisi.map((expedisi) => ({ @@ -210,13 +242,24 @@ const Checkout = () => { }; }, []); - const hitungDiscountVoucher = (code) => { - let dataVoucherIndex = listVouchers.findIndex( - (voucher) => voucher.code == code - ); - let dataActiveVoucher = listVouchers[dataVoucherIndex]; - - let countDiscount = dataActiveVoucher.discountVoucher; + const hitungDiscountVoucher = (code, source) => { + let countDiscount = 0; + if(source === 'voucher'){ + let dataVoucherIndex = listVouchers.findIndex( + (voucher) => voucher.code == code + ); + let dataActiveVoucher = listVouchers[dataVoucherIndex]; + + countDiscount = dataActiveVoucher.discountVoucher; + }else{ + let dataVoucherIndex = listVoucherShippings.findIndex( + (voucher) => voucher.code == code + ); + let dataActiveVoucher = listVoucherShippings[dataVoucherIndex]; + + countDiscount = dataActiveVoucher.discountVoucher; + } + /*if (dataActiveVoucher.discountType === 'percentage') { countDiscount = cartCheckout?.subtotal * (dataActiveVoucher.discountAmount / 100) @@ -237,11 +280,22 @@ const Checkout = () => { if (!listVouchers) return; if (!activeVoucher) return; - const countDiscount = hitungDiscountVoucher(activeVoucher); + console.log('voucher') + const countDiscount = hitungDiscountVoucher(activeVoucher, 'voucher'); SetDiscountVoucher(countDiscount); }, [activeVoucher, listVouchers]); + useEffect(() => { + if (!listVoucherShippings) return; + if (!activeVoucherShipping) return; + + console.log('voucher shipping') + const countDiscount = hitungDiscountVoucher(activeVoucherShipping, 'voucher_shipping'); + + SetDiscountVoucher(countDiscount); + }, [activeVoucherShipping, listVoucherShippings]); + useEffect(() => { if (qVoucher === 'PASTIHEMAT' && listVouchers) { let code = qVoucher; -- cgit v1.2.3 From 1bfd8851c66f4029ee6e5088b3776819bf61f4b7 Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Wed, 28 Aug 2024 14:58:42 +0700 Subject: voucher shipping code --- src/lib/checkout/components/Checkout.jsx | 206 ++++++++++++++++++------------- 1 file changed, 121 insertions(+), 85 deletions(-) (limited to 'src/lib/checkout/components') diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index 384e7105..2e3d4e58 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -150,68 +150,88 @@ const Checkout = () => { // let dataVoucher = await findVoucher(code, auth.id, query); let dataVoucher = await getVoucher(auth?.id, { source: query, - code: code + code: code, }); if (dataVoucher.length <= 0) { SetFindVoucher(1); return; } - let addNewLine = dataVoucher[0]; - console.log('addNewLine', addNewLine); - if (addNewLine.applyType != 'shipping') { - let checkList = listVouchers?.findIndex( - (voucher) => voucher.code == addNewLine.code - ); - if (checkList >= 0) { - if (listVouchers[checkList].canApply) { - ToggleSwitch(code); - SetCodeVoucher(null); - } else { - SetSelisihHargaCode(listVouchers[checkList].differenceToApply); + dataVoucher.forEach((addNewLine) => { + if (addNewLine.applyType !== 'shipping') { + // Mencari voucher dalam listVouchers + let checkList = listVouchers?.findIndex( + (voucher) => voucher.code === addNewLine.code + ); + + if (checkList >= 0) { + if (listVouchers[checkList].canApply) { + ToggleSwitch(addNewLine.code); // Perbaikan: Gunakan code voucher yang benar + SetCodeVoucher(null); + } else { + SetSelisihHargaCode(listVouchers[checkList].differenceToApply); + SetFindVoucher(2); + } + return; // Hentikan eksekusi lebih lanjut pada iterasi ini + } + + // Memeriksa apakah subtotal memenuhi syarat minimal pembelian + if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) { + SetSelisihHargaCode( + currencyFormat( + addNewLine.minPurchaseAmount - cartCheckout?.subtotal + ) + ); SetFindVoucher(2); + return; + } else { + SetFindVoucher(3); + SetButtonTerapkan(true); } - return; - } - if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) { - SetSelisihHargaCode( - currencyFormat(addNewLine.minPurchaseAmount - cartCheckout?.subtotal) - ); - SetFindVoucher(2); - return; + + // Tambahkan voucher ke list dan set voucher aktif + SetListVoucher((prevList) => [addNewLine, ...prevList]); + SetActiveVoucher(addNewLine.code); } else { - SetFindVoucher(3); - SetButtonTerapkan(true); - } - SetListVoucher((prevList) => [addNewLine, ...prevList]); - SetActiveVoucher(addNewLine.code); - } else { - let checkList = listVoucherShippings?.findIndex( - (voucher) => voucher.code == addNewLine.code - ); - if (checkList >= 0) { - if (listVoucherShippings[checkList].canApply) { - ToggleSwitch(code); - SetCodeVoucher(null); - } else { - SetSelisihHargaCode(listVoucherShippings[checkList].differenceToApply); + // Mencari voucher dalam listVoucherShippings + let checkList = listVoucherShippings?.findIndex( + (voucher) => voucher.code === addNewLine.code + ); + + if (checkList >= 0) { + if (listVoucherShippings[checkList].canApply) { + ToggleSwitch(addNewLine.code); // Perbaikan: Gunakan code voucher yang benar + SetCodeVoucher(null); + } else { + SetSelisihHargaCode( + listVoucherShippings[checkList].differenceToApply + ); + SetFindVoucher(2); + } + return; // Hentikan eksekusi lebih lanjut pada iterasi ini + } + + // Memeriksa apakah subtotal memenuhi syarat minimal pembelian + if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) { + SetSelisihHargaCode( + currencyFormat( + addNewLine.minPurchaseAmount - cartCheckout?.subtotal + ) + ); SetFindVoucher(2); + return; + } else { + SetFindVoucher(3); + SetButtonTerapkan(true); } - return; - } - if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) { - SetSelisihHargaCode( - currencyFormat(addNewLine.minPurchaseAmount - cartCheckout?.subtotal) - ); - SetFindVoucher(2); - return; - } else { - SetFindVoucher(3); - SetButtonTerapkan(true); + + // Tambahkan voucher ke list pengiriman dan set voucher aktif pengiriman + SetListVoucherShipping((prevList) => [addNewLine, ...prevList]); + setActiveVoucherShipping(addNewLine.code); } - SetListVoucherShipping((prevList) => [addNewLine, ...prevList]); - setActiveVoucherShipping(addNewLine.code); - } + }); + + // let addNewLine = dataVoucher[0]; }; useEffect(() => { @@ -244,22 +264,21 @@ const Checkout = () => { const hitungDiscountVoucher = (code, source) => { let countDiscount = 0; - if(source === 'voucher'){ + if (source === 'voucher') { let dataVoucherIndex = listVouchers.findIndex( (voucher) => voucher.code == code ); let dataActiveVoucher = listVouchers[dataVoucherIndex]; - + countDiscount = dataActiveVoucher.discountVoucher; - }else{ + } else { let dataVoucherIndex = listVoucherShippings.findIndex( (voucher) => voucher.code == code ); let dataActiveVoucher = listVoucherShippings[dataVoucherIndex]; - + countDiscount = dataActiveVoucher.discountVoucher; } - /*if (dataActiveVoucher.discountType === 'percentage') { countDiscount = cartCheckout?.subtotal * (dataActiveVoucher.discountAmount / 100) @@ -276,25 +295,24 @@ const Checkout = () => { return countDiscount; }; - useEffect(() => { - if (!listVouchers) return; - if (!activeVoucher) return; + // useEffect(() => { + // if (!listVouchers) return; + // if (!activeVoucher) return; - console.log('voucher') - const countDiscount = hitungDiscountVoucher(activeVoucher, 'voucher'); + // console.log('voucher') + // const countDiscount = hitungDiscountVoucher(activeVoucher, 'voucher'); - SetDiscountVoucher(countDiscount); - }, [activeVoucher, listVouchers]); + // SetDiscountVoucher(countDiscount); + // }, [activeVoucher, listVouchers]); - useEffect(() => { - if (!listVoucherShippings) return; - if (!activeVoucherShipping) return; + // useEffect(() => { + // if (!listVoucherShippings) return; + // if (!activeVoucherShipping) return; - console.log('voucher shipping') - const countDiscount = hitungDiscountVoucher(activeVoucherShipping, 'voucher_shipping'); + // const countDiscount = hitungDiscountVoucher(activeVoucherShipping, 'voucher_shipping'); - SetDiscountVoucher(countDiscount); - }, [activeVoucherShipping, listVoucherShippings]); + // SetDiscountVoucherOngkir(countDiscount); + // }, [activeVoucherShipping, listVoucherShippings]); useEffect(() => { if (qVoucher === 'PASTIHEMAT' && listVouchers) { @@ -553,6 +571,10 @@ const Checkout = () => { const finalShippingAmt = biayaKirim - discShippingAmt; + const totalDiscountVoucher = + cartCheckout?.discountVoucher + + (cartCheckout?.discountVoucherShipping || 0); + return ( <> { )}
+ {/* {!loadingVoucher && + listVouchers?.length === 1 && + listVoucherShippings?.length === 1} + { +
+
+

Tidak ada voucher tersedia

+

+ Maaf, saat ini tidak ada voucher yang tersedia. +

+
+
+ } */} {listVoucherShippings && listVoucherShippings?.length > 0 && (
@@ -801,16 +836,7 @@ const Checkout = () => {
- {!loadingVoucher && listVouchers?.length === 0 ? ( -
-
-

Tidak ada voucher tersedia

-

- Maaf, saat ini tidak ada voucher yang tersedia. -

-
-
- ) : ( + {loadingVoucher && listVouchers?.length > 0 && (

Promo Khusus Untuk {auth?.name}

@@ -1074,7 +1100,12 @@ const Checkout = () => {
{!!products && snakecaseKeys(products).map((item, index) => ( - + ))}
@@ -1137,7 +1168,7 @@ const Checkout = () => {
Diskon Voucher
- - {currencyFormat(discountVoucher)} + - {currencyFormat(cartCheckout?.discountVoucher)}
)} @@ -1365,7 +1396,12 @@ const Checkout = () => {
{!!products && snakecaseKeys(products).map((item, index) => ( - + ))}
@@ -1432,7 +1468,7 @@ const Checkout = () => {
Diskon Voucher
- - {currencyFormat(discountVoucher)} + - {currencyFormat(cartCheckout?.discountVoucher)}
)} @@ -1501,10 +1537,10 @@ const Checkout = () => { className='object-contain object-center h-6 w-full rounded-md' /> - {activeVoucher ? ( + {activeVoucher || activeVoucherShipping ? (
- Hemat {currencyFormat(discountVoucher)} + Hemat {currencyFormat(totalDiscountVoucher)}
Voucher berhasil digunakan -- cgit v1.2.3 From 6786d929b549d10ed1abc11f42d31af84d91bd38 Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Wed, 28 Aug 2024 15:36:04 +0700 Subject: voucher --- src/lib/checkout/components/Checkout.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib/checkout/components') diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index 2e3d4e58..2d0f0f62 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -836,7 +836,7 @@ const Checkout = () => {
- {loadingVoucher && listVouchers?.length > 0 && ( + {!loadingVoucher && listVouchers?.length > 0 && (

Promo Khusus Untuk {auth?.name}

-- cgit v1.2.3 From 59327836c5e200e5eb474929f45e6e2a03eecfbe Mon Sep 17 00:00:00 2001 From: trisusilo48 Date: Wed, 28 Aug 2024 15:51:23 +0700 Subject: feedback renca --- src/lib/checkout/components/Checkout.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib/checkout/components') diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index 2d0f0f62..54acdf7c 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -701,7 +701,7 @@ const Checkout = () => { {listVoucherShippings && listVoucherShippings?.length > 0 && (
-

Promo Gratis Ongkir

+

Promo Extra Potongan Ongkir

{listVoucherShippings?.map((item) => (