From b3855694c2723168d1c7ad949d892649fff220b8 Mon Sep 17 00:00:00 2001
From: "HATEC\\SPVDEV001" <tri.susilo@altama.co.id>
Date: Tue, 11 Jul 2023 17:05:43 +0700
Subject: find voucher

---
 src/lib/checkout/components/Checkout.jsx | 40 ++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 5 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 dcc00eb8..f19b6054 100644
--- a/src/lib/checkout/components/Checkout.jsx
+++ b/src/lib/checkout/components/Checkout.jsx
@@ -87,6 +87,8 @@ const Checkout = () => {
   const [activeVoucher, SetActiveVoucher] = useState(null)
   const [discountVoucher, SetDiscountVoucher] = useState(null)
   const [codeVoucher, SetCodeVoucher] = useState(null)
+  const [findCodeVoucher, SetFindVoucher] = useState(null)
+  const [selisihHargaCode, SetSelisihHargaCode] = useState(null)
 
   const voucher = async () => {
     let dataVoucher = await getVoucher()
@@ -94,9 +96,21 @@ const Checkout = () => {
   }
   const VoucherCode = async (code) => {
     let dataVoucher = await findVoucher(code)
-    if (!dataVoucher) return
+    if (dataVoucher.length <= 0) {
+      SetFindVoucher(1)
+      return
+    }
 
     let addNewLine = dataVoucher[0]
+    if (totalAmount - totalDiscountAmount < addNewLine.minPurchaseAmount) {
+      SetSelisihHargaCode(
+        currencyFormat(addNewLine.minPurchaseAmount - (totalAmount - totalDiscountAmount))
+      )
+      SetFindVoucher(2)
+      return
+    } else {
+      SetFindVoucher(3)
+    }
     SetListVoucher((prevList) => [addNewLine, ...prevList])
     SetActiveVoucher(addNewLine.code)
   }
@@ -301,6 +315,14 @@ const Checkout = () => {
   const handlingActivateCode = async () => {
     VoucherCode(codeVoucher)
   }
+
+  const handleUseVoucher = async (code) => {
+    SetActiveVoucher(code)
+    SetFindVoucher(null)
+    // document.getElementById('uniqCode').val('')
+  }
+  
+  console.log('ini find voucher', findCodeVoucher)
   const taxTotal = (totalAmount - totalDiscountAmount) * 0.11
 
   return (
@@ -334,19 +356,27 @@ const Checkout = () => {
               </button>
             </div>
           </div>
-          {codeVoucher && activeVoucher == codeVoucher ? (
+          {findCodeVoucher === 3 && activeVoucher === codeVoucher && (
             <div className='mt-2'>
               <span className='text-caption-1 mt-2 text-green-600'>
                 Voucher berhasil ditambahkan{' '}
               </span>
             </div>
-          ) : (
+          )}
+          {findCodeVoucher === 1 && (
             <div className='mt-2'>
-              <span className='text-caption-1 mt-2 text-green-600'>
+              <span className='text-caption-1 mt-2 text-red-600'>
                 Kode voucher salah / sudah tidak berlaku lagi. Coba voucher lainnya, ya.
               </span>
             </div>
           )}
+          {findCodeVoucher === 2 && (
+            <div className='mt-2'>
+              <span className='text-caption-1 mt-2 text-red-600'>
+                {'Tambah ' + selisihHargaCode + ' untuk pakai promo ini'}
+              </span>
+            </div>
+          )}
 
           <hr className='mt-14 my-4 border-gray_r-10' />
           <div className=''>
@@ -374,7 +404,7 @@ const Checkout = () => {
                           <button
                             className='btn-solid-red w-[102px]  md:flex-none rounded-md'
                             type='button'
-                            onClick={() => SetActiveVoucher(item.code)}
+                            onClick={() => handleUseVoucher(item.code)}
                             disabled={activeVoucher === item.code}
                           >
                             {activeVoucher === item.code ? 'Terpakai' : 'Pakai'}
-- 
cgit v1.2.3

