From dc334fa49ad13959b6a0d572fd56a4cde38176fa Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Tue, 11 Jul 2023 15:29:03 +0700 Subject: get voucher --- src/lib/checkout/api/getVoucher.js | 7 +- src/lib/checkout/components/Checkout.jsx | 230 ++++++++++++++++++++++--------- 2 files changed, 168 insertions(+), 69 deletions(-) (limited to 'src/lib') diff --git a/src/lib/checkout/api/getVoucher.js b/src/lib/checkout/api/getVoucher.js index 68185021..03ac3d6d 100644 --- a/src/lib/checkout/api/getVoucher.js +++ b/src/lib/checkout/api/getVoucher.js @@ -1,8 +1,11 @@ import odooApi from '@/core/api/odooApi' -const getVoucher = async () => { +export const getVoucher = async () => { const dataVoucher = await odooApi('GET', `/api/v1/voucher`) return dataVoucher } -export default getVoucher +export const findVoucher = async (code) => { + const dataVoucher = await odooApi('GET', `/api/v1/voucher?code=${code}`) + return dataVoucher +} diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index 629ec5bc..dcc00eb8 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -24,7 +24,7 @@ import whatsappUrl from '@/core/utils/whatsappUrl' import { createSlug } from '@/core/utils/slug' import BottomPopup from '@/core/components/elements/Popup/BottomPopup' import { gtagPurchase } from '@/core/utils/googleTag' -import getVoucher from '../api/getVoucher' +import { findVoucher, getVoucher } from '../api/getVoucher' const SELF_PICKUP_ID = 32 @@ -84,11 +84,22 @@ const Checkout = () => { const [etdFix, setEtdFix] = useState(null) const [bottomPopup, SetBottomPopup] = useState(null) const [listVouchers, SetListVoucher] = useState(null) + const [activeVoucher, SetActiveVoucher] = useState(null) + const [discountVoucher, SetDiscountVoucher] = useState(null) + const [codeVoucher, SetCodeVoucher] = useState(null) const voucher = async () => { let dataVoucher = await getVoucher() SetListVoucher(dataVoucher) } + const VoucherCode = async (code) => { + let dataVoucher = await findVoucher(code) + if (!dataVoucher) return + + let addNewLine = dataVoucher[0] + SetListVoucher((prevList) => [addNewLine, ...prevList]) + SetActiveVoucher(addNewLine.code) + } useEffect(() => { const loadExpedisi = async () => { let dataExpedisi = await ExpedisiList() @@ -103,7 +114,27 @@ const Checkout = () => { voucher() }, []) - console.log('ini voucher', listVouchers) + useEffect(() => { + if (!listVouchers) return + if (!activeVoucher) return + + let dataVoucherIndex = listVouchers.findIndex((vocuher) => vocuher.code == activeVoucher) + let dataActiveVocuher = listVouchers[dataVoucherIndex] + let countDiscount = 0 + + if (dataActiveVocuher.discountType === 'percentage') { + countDiscount = (totalAmount - totalDiscountAmount) * 0.1 + if ( + dataActiveVocuher.maxDiscountAmount > 0 && + countDiscount > dataActiveVocuher.maxDiscountAmount + ) { + countDiscount = dataActiveVocuher.maxDiscountAmount + } + } else { + countDiscount = dataActiveVocuher.discountAmount + } + SetDiscountVoucher(countDiscount) + }, [activeVoucher, listVouchers]) useEffect(() => { const loadProducts = async () => { @@ -266,92 +297,137 @@ const Checkout = () => { event_callback: midtrans }) } + + const handlingActivateCode = async () => { + VoucherCode(codeVoucher) + } const taxTotal = (totalAmount - totalDiscountAmount) * 0.11 return ( <> - SetBottomPopup(false)} title='Gunakan Promo'> + SetBottomPopup(false)} + title='Gunakan Promo' + >
SetCodeVoucher(e.target.value)} />
-
+ {codeVoucher && activeVoucher == codeVoucher ? ( +
+ + Voucher berhasil ditambahkan{' '} + +
+ ) : ( +
+ + Kode voucher salah / sudah tidak berlaku lagi. Coba voucher lainnya, ya. + +
+ )}

Promo yang tersedia

{listVouchers?.map((item) => ( -
-
-
- {item.name} -
-
-
-
-

{item.name}

-
- {item.description} +
+ {totalAmount - totalDiscountAmount < item.minPurchaseAmount && ( +
+ )} + +
+
+
+ {item.name} +
+
+
+
+

{item.name}

+
+ {item.description} +
+
+
+
-
- +
+
+

+ Kode Voucher :{' '} + {item.code} +

+

+ {activeVoucher === item.code && ( + + Voucher digunakan{' '} + + )} +

-
-

- Kode Voucher :{' '} - {item.code} -

-
-
-

- Voucher ini dapat anda gunakan untuk pengguna pertama{' '} -

-
-
- - - Berakhir dalam {item.endTime} lagi !{' '} - - Lihat Detail - - +
+

+ {totalAmount - totalDiscountAmount < item.minPurchaseAmount + ? 'Tambah ' + + currencyFormat( + item.minPurchaseAmount - (totalAmount - totalDiscountAmount) + ) + + ' untuk pakai promo ini' + : 'Voucher ini dapat anda gunakan untuk pengguna pertama'} +

+
+
+ + + Berakhir dalam {item.remainingTime}{' '} + lagi{' '} + +
@@ -462,9 +538,18 @@ const Checkout = () => { className='text-gray-900 flex items-center justify-center rounded-lg bg-white border font-medium border-gray-300 hover:bg-gray-100 py-2.5 h-[50px] w-[100%]' > % - - Hemat belanja dengan promo - + {activeVoucher ? ( +
+ + Kamu dapat potogan {discountVoucher} + + Voucher berhasil digunakan +
+ ) : ( + + Hemat belanja dengan promo + + )} {'>'}
@@ -700,12 +785,23 @@ const Checkout = () => {
-- cgit v1.2.3