From c210cdc221db85f6711af92442808e15fc2eb10e Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Thu, 20 Jul 2023 16:41:30 +0700 Subject: voucher get perhitungan dari api --- src/lib/checkout/components/Checkout.jsx | 416 ++++++++++++++++++------------- 1 file changed, 243 insertions(+), 173 deletions(-) (limited to 'src/lib/checkout/components/Checkout.jsx') diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index 44e0b0b6..3686ced9 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -26,6 +26,7 @@ import BottomPopup from '@/core/components/elements/Popup/BottomPopup' import { useQuery } from 'react-query' import { gtagPurchase } from '@/core/utils/googleTag' import { findVoucher, getVoucher } from '../api/getVoucher' +import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner' const SELF_PICKUP_ID = 32 @@ -35,7 +36,12 @@ const { getProductsCheckout } = require('../api/checkoutApi') const Checkout = () => { const router = useRouter() const auth = useAuth() - const { data: cartCheckout } = useQuery('cartCheckout', getProductsCheckout) + + const [activeVoucher, SetActiveVoucher] = useState(null) + + const { data: cartCheckout } = useQuery('cartCheckout-' + activeVoucher, () => + getProductsCheckout(activeVoucher) + ) const [selectedAddress, setSelectedAddress] = useState({ shipping: null, @@ -88,7 +94,6 @@ 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(0) const [codeVoucher, SetCodeVoucher] = useState(null) const [findCodeVoucher, SetFindVoucher] = useState(null) @@ -99,11 +104,11 @@ const Checkout = () => { const expedisiValidation = useRef(null) const voucher = async () => { - let dataVoucher = await getVoucher() + let dataVoucher = await getVoucher(auth?.id) SetListVoucher(dataVoucher) } const VoucherCode = async (code) => { - let dataVoucher = await findVoucher(code) + let dataVoucher = await findVoucher(code, auth.id) if (dataVoucher.length <= 0) { SetFindVoucher(1) return @@ -111,11 +116,18 @@ const Checkout = () => { let addNewLine = dataVoucher[0] let checkList = listVouchers.findIndex((voucher) => voucher.code == addNewLine.code) - if (checkList >= 0) return - if (totalAmount - totalDiscountAmount < addNewLine.minPurchaseAmount) { - SetSelisihHargaCode( - currencyFormat(addNewLine.minPurchaseAmount - (totalAmount - totalDiscountAmount)) - ) + 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 { @@ -125,6 +137,11 @@ const Checkout = () => { SetListVoucher((prevList) => [addNewLine, ...prevList]) SetActiveVoucher(addNewLine.code) } + + useEffect(() => { + SetFindVoucher(null) + }, [bottomPopup]) + useEffect(() => { const loadExpedisi = async () => { let dataExpedisi = await ExpedisiList() @@ -136,7 +153,7 @@ const Checkout = () => { setExpedisi(dataExpedisi) } loadExpedisi() - voucher() + // voucher() }, []) const hitungDiscountVoucher = (code) => { @@ -146,7 +163,7 @@ const Checkout = () => { let countDiscount = 0 if (dataActiveVoucher.discountType === 'percentage') { - countDiscount = (totalAmount - totalDiscountAmount) * (dataActiveVoucher.discountAmount / 100) + countDiscount = cartCheckout?.subtotal * (dataActiveVoucher.discountAmount / 100) if ( dataActiveVoucher.maxDiscountAmount > 0 && countDiscount > dataActiveVoucher.maxDiscountAmount @@ -269,7 +286,7 @@ const Checkout = () => { let data = { partner_shipping_id: auth.partnerId, partner_invoice_id: auth.partnerId, - user_id : auth.id, + user_id: auth.id, order_line: JSON.stringify(productOrder), delivery_amount: biayaKirim, carrier_id: selectedCarrierId, @@ -299,11 +316,7 @@ const Checkout = () => { gtag('event', 'conversion', { send_to: 'AW-954540379/nDymCL3BhaQYENvClMcD', - value: - totalAmount - - totalDiscountAmount + - taxTotal + - Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000, + value: cartCheckout?.grandTotal + Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000, currency: 'IDR', transaction_id: isCheckouted.id, event_callback: midtrans @@ -345,8 +358,6 @@ const Checkout = () => { handleUseVoucher(code, !isChecked) } - console.log('ini adalah', listVouchers) - // const taxTotal = (totalAmount - totalDiscountAmount - discountVoucher) * 0.11 return ( @@ -418,7 +429,7 @@ const Checkout = () => { )} {listVouchers?.map((item) => (
- {totalAmount - totalDiscountAmount < item.minPurchaseAmount && ( + {item.canApply === false && (
)} @@ -464,11 +475,9 @@ const Checkout = () => {

- {totalAmount - totalDiscountAmount < item.minPurchaseAmount + {item.canApply === false ? 'Tambah ' + - currencyFormat( - item.minPurchaseAmount - (totalAmount - totalDiscountAmount) - ) + + currencyFormat(item.differenceToApply) + ' untuk pakai promo ini' : 'Potensi potongan sebesar ' + currencyFormat(hitungDiscountVoucher(item.code))} @@ -567,7 +576,7 @@ const Checkout = () => {

Diskon Produk
-
- {currencyFormat(cartCheckout?.totalDiscount )}
+
- {currencyFormat(cartCheckout?.totalDiscount)}
{activeVoucher && (
@@ -738,133 +747,149 @@ const Checkout = () => { - {products?.map((product) => ( - <> - - -
- {product?.name} -
-
-
- {product?.parent?.name} -
-
- {product?.code}{' '} - {product?.attributes.length > 0 - ? `| ${product?.attributes.join(', ')}` - : ''} -
-
- Berat item : {product?.weight} Kg + {!products ? ( + + +
+
+
+
+
+
+ + + ) : ( + products?.map((product) => ( + <> + + +
+ {product?.name}
-
- - - - - - {product?.price?.discountPercentage > 0 && ( -
-
- {currencyFormat(product?.price?.price)} +
+
+ {product?.parent?.name}
-
- {product?.price?.discountPercentage}% +
+ {product?.code}{' '} + {product?.attributes.length > 0 + ? `| ${product?.attributes.join(', ')}` + : ''} +
+
+ Berat item : {product?.weight} Kg
- )} -
- {product.price.priceDiscount > 0 - ? currencyFormat(product?.price?.priceDiscount) - : 'Call For Price'} -
- - -
- {product.price.priceDiscount > 0 ? ( - currencyFormat(product?.price?.priceDiscount * product?.quantity) - ) : ( - - Call For Price{' '} - - )} -
- - - {product.program && - product.program.items && - product.program.items.map((item) => ( - <> - - -
- {item.name} + + + + + + {product?.price?.discountPercentage > 0 && ( +
+
+ {currencyFormat(product?.price?.price)} +
+
+ {product?.price?.discountPercentage}%
-
-
- - {product.program.type.label} - +
+ )} +
+ {product.price.priceDiscount > 0 + ? currencyFormat(product?.price?.priceDiscount) + : 'Call For Price'} +
+ + +
+ {product.price.priceDiscount > 0 ? ( + currencyFormat(product?.price?.priceDiscount * product?.quantity) + ) : ( + + Call For Price{' '} + + )} +
+ + + {product.program && + product.program.items && + product.program.items.map((item) => ( + <> + + +
+ {item.name}
-
- {item.name} +
+
+ + {product.program.type.label} + +
+
{item.name}
-
- - - - - - {item?.price?.discountPercentage > 0 && ( -
-
- {currencyFormat(product?.price?.price)} + + + + + + {item?.price?.discountPercentage > 0 && ( +
+
+ {currencyFormat(product?.price?.price)} +
+ )} +
+ {item?.price.priceDiscount > 0 ? 'Gratis' : ''}
- )} -
- {item?.price.priceDiscount > 0 ? 'Gratis' : ''} -
- - -
- {item.price.priceDiscount > 0 ? 'Gratis' : ''} -
- - - - - ))} - - ))} + + +
+ {item.price.priceDiscount > 0 ? 'Gratis' : ''} +
+ + + + + ))} + + )) + )}
@@ -878,38 +903,80 @@ const Checkout = () => {

- -
-
-
Total Belanja
-
{currencyFormat(cartCheckout?.totalPurchase)}
-
-
-
Diskon Produk
-
- {currencyFormat(cartCheckout?.totalDiscount )}
+ {!cartCheckout ? ( +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Loading...
- {activeVoucher && ( + ) : ( +
-
Diskon Voucher
-
- {currencyFormat(discountVoucher)}
+
Total Belanja
+
{currencyFormat(cartCheckout?.totalPurchase)}
- )} -
-
Subtotal
-
{currencyFormat(cartCheckout?.subtotal)}
-
-
-
PPN 11%
-
{currencyFormat(cartCheckout?.tax)}
-
-
-
- Biaya Kirim -

{etdFix}

+
+
Diskon Produk
+
+ - {currencyFormat(cartCheckout?.totalDiscount)} +
+
+ {activeVoucher && ( +
+
Diskon Voucher
+
- {currencyFormat(discountVoucher)}
+
+ )} +
+
Subtotal
+
{currencyFormat(cartCheckout?.subtotal)}
+
+
+
PPN 11%
+
{currencyFormat(cartCheckout?.tax)}
+
+
+
+ Biaya Kirim +

{etdFix}

+
+
+ {currencyFormat(Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000)} +
-
{currencyFormat(Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000)}
-
+ )}
@@ -927,7 +994,10 @@ const Checkout = () => {