summaryrefslogtreecommitdiff
path: root/src/lib/checkout/components/Checkout.jsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/checkout/components/Checkout.jsx')
-rw-r--r--src/lib/checkout/components/Checkout.jsx202
1 files changed, 146 insertions, 56 deletions
diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx
index 58d1403a..54acdf7c 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,91 @@ 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);
+ 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);
+ }
+
+ // Tambahkan voucher ke list dan set voucher aktif
+ SetListVoucher((prevList) => [addNewLine, ...prevList]);
+ SetActiveVoucher(addNewLine.code);
} else {
- SetSelisihHargaCode(listVouchers[checkList].differenceToApply);
- SetFindVoucher(2);
+ // 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);
+ }
+
+ // Tambahkan voucher ke list pengiriman dan set voucher aktif pengiriman
+ SetListVoucherShipping((prevList) => [addNewLine, ...prevList]);
+ setActiveVoucherShipping(addNewLine.code);
}
- return;
- }
- if (cartCheckout?.subtotal < addNewLine.minPurchaseAmount) {
- SetSelisihHargaCode(
- currencyFormat(addNewLine.minPurchaseAmount - cartCheckout?.subtotal)
- );
- SetFindVoucher(2);
- return;
- } else {
- SetFindVoucher(3);
- SetButtonTerapkan(true);
- }
- SetListVoucher((prevList) => [addNewLine, ...prevList]);
- SetActiveVoucher(addNewLine.code);
+ });
+
+ // let addNewLine = dataVoucher[0];
};
useEffect(() => {
@@ -187,7 +239,7 @@ const Checkout = () => {
}, [bottomPopup]);
useEffect(() => {
- voucher();
+ // voucher();
const loadExpedisi = async () => {
let dataExpedisi = await ExpedisiList();
dataExpedisi = dataExpedisi.map((expedisi) => ({
@@ -210,13 +262,23 @@ const Checkout = () => {
};
}, []);
- const hitungDiscountVoucher = (code) => {
- let dataVoucherIndex = listVouchers.findIndex(
- (voucher) => voucher.code == code
- );
- let dataActiveVoucher = listVouchers[dataVoucherIndex];
+ const hitungDiscountVoucher = (code, source) => {
+ let countDiscount = 0;
+ if (source === 'voucher') {
+ let dataVoucherIndex = listVouchers.findIndex(
+ (voucher) => voucher.code == code
+ );
+ let dataActiveVoucher = listVouchers[dataVoucherIndex];
- let countDiscount = dataActiveVoucher.discountVoucher;
+ 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)
@@ -233,14 +295,24 @@ const Checkout = () => {
return countDiscount;
};
- useEffect(() => {
- if (!listVouchers) return;
- if (!activeVoucher) return;
+ // useEffect(() => {
+ // if (!listVouchers) return;
+ // if (!activeVoucher) return;
- const countDiscount = hitungDiscountVoucher(activeVoucher);
+ // console.log('voucher')
+ // const countDiscount = hitungDiscountVoucher(activeVoucher, 'voucher');
- SetDiscountVoucher(countDiscount);
- }, [activeVoucher, listVouchers]);
+ // SetDiscountVoucher(countDiscount);
+ // }, [activeVoucher, listVouchers]);
+
+ // useEffect(() => {
+ // if (!listVoucherShippings) return;
+ // if (!activeVoucherShipping) return;
+
+ // const countDiscount = hitungDiscountVoucher(activeVoucherShipping, 'voucher_shipping');
+
+ // SetDiscountVoucherOngkir(countDiscount);
+ // }, [activeVoucherShipping, listVoucherShippings]);
useEffect(() => {
if (qVoucher === 'PASTIHEMAT' && listVouchers) {
@@ -499,6 +571,10 @@ const Checkout = () => {
const finalShippingAmt = biayaKirim - discShippingAmt;
+ const totalDiscountVoucher =
+ cartCheckout?.discountVoucher +
+ (cartCheckout?.discountVoucherShipping || 0);
+
return (
<>
<BottomPopup
@@ -609,10 +685,23 @@ const Checkout = () => {
)}
<hr className='mt-8 mb-4 border-gray_r-8' />
+ {/* {!loadingVoucher &&
+ listVouchers?.length === 1 &&
+ listVoucherShippings?.length === 1}
+ {
+ <div className='flex items-center justify-center mt-4 mb-4'>
+ <div className='text-center'>
+ <h1 className='font-bold mb-4'>Tidak ada voucher tersedia</h1>
+ <p className='text-gray-500'>
+ Maaf, saat ini tidak ada voucher yang tersedia.
+ </p>
+ </div>
+ </div>
+ } */}
{listVoucherShippings && listVoucherShippings?.length > 0 && (
<div>
- <h3 className='font-semibold mb-4'>Promo Gratis Ongkir</h3>
+ <h3 className='font-semibold mb-4'>Promo Extra Potongan Ongkir</h3>
{listVoucherShippings?.map((item) => (
<div key={item.id} className='relative'>
<div
@@ -747,16 +836,7 @@ const Checkout = () => {
<hr className='mt-8 mb-4 border-gray_r-8' />
<div>
- {!loadingVoucher && listVouchers?.length === 0 ? (
- <div className='flex items-center justify-center mt-4 mb-4'>
- <div className='text-center'>
- <h1 className='font-bold mb-4'>Tidak ada voucher tersedia</h1>
- <p className='text-gray-500'>
- Maaf, saat ini tidak ada voucher yang tersedia.
- </p>
- </div>
- </div>
- ) : (
+ {!loadingVoucher && listVouchers?.length > 0 && (
<h3 className='font-semibold mb-4'>
Promo Khusus Untuk {auth?.name}
</h3>
@@ -1020,7 +1100,12 @@ const Checkout = () => {
<div className='p-4 flex flex-col gap-y-4'>
{!!products &&
snakecaseKeys(products).map((item, index) => (
- <CartItem key={index} item={item} editable={false} selfPicking={selectedExpedisi === '1,32' ? true : false}/>
+ <CartItem
+ key={index}
+ item={item}
+ editable={false}
+ selfPicking={selectedExpedisi === '1,32' ? true : false}
+ />
))}
</div>
@@ -1083,7 +1168,7 @@ const Checkout = () => {
<div className='flex gap-x-2 justify-between'>
<div className='text-gray_r-11'>Diskon Voucher</div>
<div className='text-danger-500'>
- - {currencyFormat(discountVoucher)}
+ - {currencyFormat(cartCheckout?.discountVoucher)}
</div>
</div>
)}
@@ -1311,7 +1396,12 @@ const Checkout = () => {
<div className='flex flex-col gap-y-8 border-t border-gray-300 pt-8'>
{!!products &&
snakecaseKeys(products).map((item, index) => (
- <CartItem key={index} item={item} editable={false} selfPicking={selectedExpedisi === '1,32' ? true : false} />
+ <CartItem
+ key={index}
+ item={item}
+ editable={false}
+ selfPicking={selectedExpedisi === '1,32' ? true : false}
+ />
))}
</div>
</div>
@@ -1378,7 +1468,7 @@ const Checkout = () => {
<div className='flex gap-x-2 justify-between'>
<div className='text-gray_r-11'>Diskon Voucher</div>
<div className='text-danger-500'>
- - {currencyFormat(discountVoucher)}
+ - {currencyFormat(cartCheckout?.discountVoucher)}
</div>
</div>
)}
@@ -1447,10 +1537,10 @@ const Checkout = () => {
className='object-contain object-center h-6 w-full rounded-md'
/>
</span>
- {activeVoucher ? (
+ {activeVoucher || activeVoucherShipping ? (
<div className=''>
<div className='text-left text-sm text-black font-semibold'>
- Hemat {currencyFormat(discountVoucher)}
+ Hemat {currencyFormat(totalDiscountVoucher)}
</div>
<div className='text-left mt-1 text-green-600 text-xs'>
Voucher berhasil digunakan