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.jsx263
1 files changed, 187 insertions, 76 deletions
diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx
index 52f1b3ed..4c7e852f 100644
--- a/src/lib/checkout/components/Checkout.jsx
+++ b/src/lib/checkout/components/Checkout.jsx
@@ -134,6 +134,7 @@ const Checkout = () => {
setLoadingVoucher(true);
let dataVoucher = await getVoucher(auth?.id, {
source: query,
+ type: 'all,brand',
});
SetListVoucher(dataVoucher);
@@ -149,40 +150,94 @@ 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]);
+ if (addNewLine.canApply) {
+ 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]);
+ if (addNewLine.canApply) {
+ 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(() => {
@@ -190,7 +245,7 @@ const Checkout = () => {
}, [bottomPopup]);
useEffect(() => {
- voucher();
+ // voucher();
const loadExpedisi = async () => {
let dataExpedisi = await ExpedisiList();
dataExpedisi = dataExpedisi.map((expedisi) => ({
@@ -213,13 +268,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];
+
+ countDiscount = dataActiveVoucher.discountVoucher;
+ } else {
+ let dataVoucherIndex = listVoucherShippings.findIndex(
+ (voucher) => voucher.code == code
+ );
+ let dataActiveVoucher = listVoucherShippings[dataVoucherIndex];
- let countDiscount = dataActiveVoucher.discountVoucher;
+ countDiscount = dataActiveVoucher.discountVoucher;
+ }
/*if (dataActiveVoucher.discountType === 'percentage') {
countDiscount = cartCheckout?.subtotal * (dataActiveVoucher.discountAmount / 100)
@@ -236,14 +301,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');
+
+ // SetDiscountVoucher(countDiscount);
+ // }, [activeVoucher, listVouchers]);
- const countDiscount = hitungDiscountVoucher(activeVoucher);
+ // useEffect(() => {
+ // if (!listVoucherShippings) return;
+ // if (!activeVoucherShipping) return;
- SetDiscountVoucher(countDiscount);
- }, [activeVoucher, listVouchers]);
+ // const countDiscount = hitungDiscountVoucher(activeVoucherShipping, 'voucher_shipping');
+
+ // SetDiscountVoucherOngkir(countDiscount);
+ // }, [activeVoucherShipping, listVoucherShippings]);
useEffect(() => {
if (qVoucher === 'PASTIHEMAT' && listVouchers) {
@@ -338,7 +413,7 @@ const Checkout = () => {
Math.round(parseInt(finalShippingAmt * 1.1) / 1000) * 1000;
const finalGT = GT < 0 ? 0 : GT;
setGrandTotal(finalGT);
- }, [biayaKirim, cartCheckout?.grandTotal, activeVoucher]);
+ }, [biayaKirim, cartCheckout?.grandTotal, activeVoucher, activeVoucherShipping]);
const checkout = async () => {
const file = poFile.current.files[0];
@@ -392,14 +467,22 @@ const Checkout = () => {
if (typeof file !== 'undefined') data.po_file = await getFileBase64(file);
const isCheckouted = await checkoutApi({ data });
+
if (!isCheckouted?.id) {
toast.error('Gagal melakukan transaksi, terjadi kesalahan internal');
return;
- }
-
- gtagPurchase(products, biayaKirim, isCheckouted.name);
+ } else {
+ gtagPurchase(products, biayaKirim, isCheckouted.name);
+
+ gtag('event', 'conversion', {
+ send_to: 'AW-954540379/nDymCL3BhaQYENvClMcD',
+ value:
+ cartCheckout?.grandTotal +
+ Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000,
+ currency: 'IDR',
+ transaction_id: isCheckouted.id,
+ });
- const midtrans = async () => {
for (const product of products) deleteItemCart({ productId: product.id });
if (grandTotal > 0) {
const payment = await axios.post(
@@ -415,17 +498,25 @@ const Checkout = () => {
'-'
)}`;
}
- };
+ }
- gtag('event', 'conversion', {
- send_to: 'AW-954540379/nDymCL3BhaQYENvClMcD',
- value:
- cartCheckout?.grandTotal +
- Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000,
- currency: 'IDR',
- transaction_id: isCheckouted.id,
- event_callback: midtrans,
- });
+ /* const midtrans = async () => {
+ for (const product of products) deleteItemCart({ productId: product.id });
+ if (grandTotal > 0) {
+ const payment = await axios.post(
+ `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${isCheckouted.id}`
+ );
+ setIsLoading(false);
+ window.location.href = payment.data.redirectUrl;
+ } else {
+ window.location.href = `${
+ process.env.NEXT_PUBLIC_SELF_HOST
+ }/shop/checkout/success?order_id=${isCheckouted.name.replace(
+ /\//g,
+ '-'
+ )}`;
+ }
+ };*/
};
const handlingActivateCode = async () => {
@@ -486,6 +577,10 @@ const Checkout = () => {
const finalShippingAmt = biayaKirim - discShippingAmt;
+ const totalDiscountVoucher =
+ cartCheckout?.discountVoucher +
+ (cartCheckout?.discountVoucherShipping || 0);
+
return (
<>
<BottomPopup
@@ -596,10 +691,25 @@ 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
@@ -734,16 +844,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>
@@ -935,7 +1036,7 @@ const Checkout = () => {
</div>
<span className='leading-5'>
Jika mengalami kesulitan dalam melakukan pembelian di website
- Indoteknik. Hubungi kami disini
+ Indoteknik. <a href={whatsappUrl()}>Hubungi kami disini</a>
</span>
</Alert>
</div>
@@ -1007,7 +1108,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} />
+ <CartItem
+ key={index}
+ item={item}
+ editable={false}
+ selfPicking={selectedExpedisi === '1,32' ? true : false}
+ />
))}
</div>
@@ -1070,7 +1176,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>
)}
@@ -1086,7 +1192,7 @@ const Checkout = () => {
<div className='text-gray_r-11'>
Biaya Kirim <p className='text-xs mt-1'>{etdFix}</p>
</div>
- <div>{currencyFormat(biayaKirim)}</div>
+ <div>{currencyFormat(Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000)}</div>
</div>
{activeVoucherShipping && voucherShippingAmt && (
<div className='flex gap-x-2 justify-between'>
@@ -1138,10 +1244,10 @@ const Checkout = () => {
className='object-contain object-center h-6 rounded-md'
/>
</span>
- {activeVoucher ? (
+ {activeVoucher || activeVoucherShipping ? (
<div className=''>
<div className='text-left text-sm text-black font-semibold'>
- Potongan Senilai {currencyFormat(discountVoucher)}
+ Potongan Senilai {currencyFormat(totalDiscountVoucher)}
</div>
<div className='text-left mt-1 text-green-600 text-xs'>
Voucher berhasil digunakan
@@ -1298,7 +1404,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} />
+ <CartItem
+ key={index}
+ item={item}
+ editable={false}
+ selfPicking={selectedExpedisi === '1,32' ? true : false}
+ />
))}
</div>
</div>
@@ -1365,7 +1476,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>
)}
@@ -1382,7 +1493,7 @@ const Checkout = () => {
Biaya Kirim
<p className='text-xs mt-1'>{etdFix}</p>
</div>
- <div>{currencyFormat(biayaKirim)}</div>
+ <div>{currencyFormat(Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000) }</div>
</div>
{activeVoucherShipping && voucherShippingAmt && (
<div className='flex gap-x-2 justify-between'>
@@ -1434,10 +1545,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