From b5701645ce23deed5e32afc4a0f98bd523c6c92c Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Thu, 31 Oct 2024 13:58:23 +0700
Subject: <iman> update pengajuan tempo

---
 .../pengajuan-tempo/component/PengajuanTempo.jsx   | 146 +++++++++------------
 1 file changed, 60 insertions(+), 86 deletions(-)

(limited to 'src/lib/pengajuan-tempo/component/PengajuanTempo.jsx')

diff --git a/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx b/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx
index 0efea807..11079328 100644
--- a/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx
+++ b/src/lib/pengajuan-tempo/component/PengajuanTempo.jsx
@@ -19,6 +19,9 @@ import {
 } from '../../../../src-migrate/modules/register/stores/usePengajuanTempoStore';
 import { ChevronRightIcon, ChevronLeftIcon } from '@heroicons/react/24/outline';
 import createPengajuanTempoApi from '../api/createPengajuanTempoApi';
+import { Button, Checkbox, Spinner, Tooltip } from '@chakra-ui/react';
+import clsxm from '~/libs/clsxm';
+import { toast } from 'react-hot-toast';
 const PengajuanTempo = () => {
   const [currentStep, setCurrentStep] = React.useState(0);
   const NUMBER_OF_STEPS = 6;
@@ -34,8 +37,14 @@ const PengajuanTempo = () => {
     validateKontakPerson,
     updateFormKontakPerson,
   } = usePengajuanTempoStoreKontakPerson();
-  const { formSupplier, errorsSupplier, validateSupplier, updateFormSupplier } =
-    usePengajuanTempoStoreSupplier();
+  const {
+    formSupplier,
+    errorsSupplier,
+    validateSupplier,
+    updateFormSupplier,
+    hasSavedata,
+    updateHasSave,
+  } = usePengajuanTempoStoreSupplier();
   const {
     formPengiriman,
     errorsPengiriman,
@@ -90,31 +99,6 @@ const PengajuanTempo = () => {
     'Dokumen',
     'Konfirmasi',
   ];
-  const combineForms = () => {
-    return stepDivsForm.reduce((accumulator, currentForm, index) => {
-      if (currentForm && typeof currentForm === 'object') {
-        if (index === 3) {
-          if (!accumulator.formSupplier) {
-            accumulator.formSupplier = [];
-          }
-          accumulator.formSupplier.push(...currentForm);
-        } else {
-          return { ...accumulator, ...currentForm };
-        }
-      }
-      return accumulator;
-    }, {});
-  };
-
-  // const stepLabels = [
-  //   `${auth.partnerId}_informasi_perusahaan`,
-  //   `${auth.partnerId}_kontak_person`,
-  //   `${auth.partnerId}_Pengiriman`,
-  //   `${auth.partnerId}_Referensi`,
-  //   `${auth.partnerId}_Dokumen`,
-  //   `${auth.partnerId}_Konfirmasi`,
-  // ];
-
   const isFormValid = useMemo(
     () => Object.keys(stepDivsError[currentStep]).length === 0,
     [stepDivsError[currentStep]]
@@ -124,6 +108,7 @@ const PengajuanTempo = () => {
     validateKontakPerson();
     validatePengiriman();
     validateDokumen();
+    updateHasSave(false);
 
     window.scrollTo({
       top: 0,
@@ -134,11 +119,9 @@ const PengajuanTempo = () => {
   useEffect(() => {
     const cachedData = getFromLocalStorage(stepLabels[currentStep]);
     if (cachedData) {
-      // const formData = JSON.parse(cachedData);
       if (currentStep == 3) {
         stepDivsUpdateForm[currentStep](cachedData);
       } else if (currentStep == 4) {
-        // Memanggil updateFormDokumen dengan parameter yang benar
         Object.keys(cachedData).forEach((key) => {
           const { name, format, base64 } = cachedData[key];
           stepDivsUpdateForm[currentStep](key, name, format, base64);
@@ -149,8 +132,6 @@ const PengajuanTempo = () => {
         });
       }
     }
-    if (formSupplier) {
-    }
   }, [currentStep]);
   const goToNextStep = () => {
     if (!isFormValid) {
@@ -167,43 +148,39 @@ const PengajuanTempo = () => {
   const handleDaftarTempo = async () => {
     for (const error of stepDivsError) {
       if (error.length > 0) {
-        console.log('ada yang salah');
-        return; // Keluar dari fungsi jika ada error
+        return;
       }
     }
-    const combinedData = combineForms();
-    console.log('combinedData', combinedData);
-    const data = {
-      ...combinedData,
-      parent_id: auth.partnerId,
-    };
-
     const productOrder = formSupplier.map((product) => ({
       supplier: product.supplier,
       pic: product.pic,
       telepon: product.telepon,
-      quantitydurasiTempo: product.durasiTempo,
+      durasiTempo: product.durasiTempo,
       creditLimit: product.creditLimit,
     }));
+
+    const formattedDokumen = Object.entries(formDokumen).map(([key, doc]) => ({
+      documentName: key,
+      details: {
+        name: doc.name,
+        format: doc.format,
+        base64: doc.base64,
+      },
+    }));
     const data2 = {
-      form: JSON.stringify(productOrder),
-      parent_id: auth.partnerId,
+      ...form,
+      ...formKontakPerson,
+      ...formPengiriman,
+      formDocs: JSON.stringify(formattedDokumen),
+      formSupplier: JSON.stringify(productOrder),
+      user_id: auth.id,
     };
-    const address = await createPengajuanTempoApi({ combinedData });
-
-    console.log('Sukses Daftar');
-    // router.push('/pengajuan-tempo/finish?tempo_id=SO-2023-06480');
-
-    // if (!isFormValid) {
-    //   setNotValid(true);
-    //   setButtonSubmitClick(!buttonSubmitClick);
-    //   return;
-    // } else {
-    //   saveToLocalStorage(stepLabels[currentStep], stepDivsForm[currentStep]);
-    //   setButtonSubmitClick(!buttonSubmitClick);
-    //   setNotValid(false);
-    // }
-    // setCurrentStep((prev) => (prev === NUMBER_OF_STEPS - 1 ? prev : prev + 1));
+    const address = await createPengajuanTempoApi(data2);
+    if (address) {
+      removeFromLocalStorage();
+      toast.success('Pengajuan tempo berhasil dilakukan', { duration: 4000 });
+      router.push('/pengajuan-tempo/finish?tempo_id=SO-2023-06480');
+    }
   };
 
   const goToPreviousStep = () => {
@@ -221,6 +198,11 @@ const PengajuanTempo = () => {
     const item = JSON.parse(itemStr);
     return item;
   };
+  const removeFromLocalStorage = () => {
+    for (const key of stepLabels) {
+      localStorage.removeItem(key);
+    }
+  };
 
   return (
     <>
@@ -241,43 +223,35 @@ const PengajuanTempo = () => {
           <Stepper currentStep={currentStep} numberOfSteps={NUMBER_OF_STEPS} />
         </div>
         <div>{stepDivs[currentStep]}</div>
-        <section className='flex gap-10 mt-10'>
-          {/* <button
-            onClick={goToNextStep}
-            className='bg-blue-600 text-white p-2 rounded-md'
-            disabled={currentStep === NUMBER_OF_STEPS - 1} // Disable if on the last step
-            >
-            Next step
-            </button> */}
-        </section>
+        <section className='flex gap-10 mt-10'></section>
         <div className='flex flex-row justify-end items-center gap-4 mb-8'>
           <span className='text-xs opacity-60'>
             *Pastikan data yang anda masukan sudah benar dan sesuai
           </span>
-          {/* <button
-            onClick={goToPreviousStep}
-            className='bg-red-600 border border-red-600 rounded-md text-sm text-white p-2 h-11 mb-1 content-center flex flex-row justify-center items-center'
-            disabled={currentStep === 0} // Disable if on the first step
-          >
-            {<ChevronLeftIcon className='w-5' />}
-            Langkah Sebelumnya
-          </button> */}
           {currentStep < 5 && (
-            <button
-              onClick={goToNextStep}
-              disabled={currentStep === NUMBER_OF_STEPS - 1}
-              className='bg-red-600 border border-red-600 rounded-md text-sm text-white p-2 h-11 mb-1 content-center flex flex-row justify-center items-center'
+            <Tooltip
+              label={clsxm({
+                'Klik simpan data terlebih dahulu':
+                  currentStep === 3 && !hasSavedata,
+              })}
             >
-              Langkah Selanjutnya {<ChevronRightIcon className='w-5' />}
-            </button>
+              <Button
+                colorScheme='red'
+                w='fit'
+                isDisabled={
+                  (currentStep === 3 && !hasSavedata) ||
+                  currentStep === NUMBER_OF_STEPS - 1
+                }
+                onClick={goToNextStep}
+              >
+                Langkah Selanjutnya {<ChevronRightIcon className='w-5' />}
+              </Button>
+            </Tooltip>
           )}
           {currentStep == 5 && (
-            <button
-              onClick={handleDaftarTempo}
-              className='bg-red-600 border border-red-600 rounded-md text-sm text-white p-2 h-11 mb-1 content-center flex flex-row justify-center items-center'
-            >
+            <Button colorScheme='red' w='fit' onClick={handleDaftarTempo}>
               Daftar Tempo {<ChevronRightIcon className='w-5' />}
-            </button>
+            </Button>
           )}
         </div>
       </div>
-- 
cgit v1.2.3

