From 7265295454801c1d921385a4b67fb3780b46771e Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Wed, 22 Feb 2023 14:00:00 +0700
Subject: fix

---
 src/lib/checkout/components/Checkout.jsx | 36 +++++++++++++++++++++++++++++---
 1 file changed, 33 insertions(+), 3 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 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>
     </>
-- 
cgit v1.2.3

