diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-22 14:00:00 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-02-22 14:00:00 +0700 |
| commit | 7265295454801c1d921385a4b67fb3780b46771e (patch) | |
| tree | a45e7c0add3641d6a234c3b610adc72a08631616 /src/lib/checkout/components/Checkout.jsx | |
| parent | 757b69f4d814ec4890c209f7a9fdf3d9940157d9 (diff) | |
fix
Diffstat (limited to 'src/lib/checkout/components/Checkout.jsx')
| -rw-r--r-- | src/lib/checkout/components/Checkout.jsx | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx index 0a3949c3..8416cd9e 100644 --- a/src/lib/checkout/components/Checkout.jsx +++ b/src/lib/checkout/components/Checkout.jsx @@ -9,11 +9,15 @@ import VariantCard from '@/lib/variant/components/VariantCard' import { ExclamationCircleIcon } from '@heroicons/react/24/outline' import { useEffect, useRef, useState } from 'react' import _ from 'lodash' -import { getCart, getItemCart } from '@/core/utils/cart' +import { deleteItemCart, getCart, getItemCart } from '@/core/utils/cart' import currencyFormat from '@/core/utils/currencyFormat' import { toast } from 'react-hot-toast' +import getFileBase64 from '@/core/utils/getFileBase64' +import checkoutApi from '../api/checkoutApi' +import { useRouter } from 'next/router' const Checkout = () => { + const router = useRouter() const auth = useAuth() const [selectedAddress, setSelectedAddress] = useState({ shipping: null, @@ -96,6 +100,33 @@ const Checkout = () => { toast.error('Pilih metode pembayaran', { position: 'bottom-center' }) return } + const file = poFile.current.files[0] + if (typeof file !== 'undefined' && file.size > 5000000) { + toast.error('Maksimal ukuran file adalah 5MB', { position: 'bottom-center' }) + return + } + setIsLoading(true) + const productOrder = products.map((product) => ({ + product_id: product.id, + quantity: product.quantity + })) + let data = { + partner_shipping_id: selectedAddress.shipping.id, + partner_invoice_id: selectedAddress.invoicing.id, + order_line: JSON.stringify(productOrder), + type: 'sale_order' + } + if (poNumber.current.value) data.po_number = poNumber.current.value + if (typeof file !== 'undefined') data.po_file = await getFileBase64(file) + + const isCheckouted = await checkoutApi({ data }) + setIsLoading(false) + if (isCheckouted?.id) { + for (const product of products) deleteItemCart({ productId: product.id }) + router.push(`/shop/checkout/finish?id=${isCheckouted.id}`) + return + } + toast.error('Gagal melakukan transaksi, terjadi kesalahan internal') } return ( @@ -232,8 +263,7 @@ const Checkout = () => { <div className='flex gap-x-3 p-4'> <button className='flex-1 btn-yellow' onClick={checkout} disabled={isLoading}> - {isLoading && 'Loading...'} - {!isLoading && 'Bayar'} + {isLoading ? 'Loading...' : 'Bayar'} </button> </div> </> |
