From 0f84963214ee6dc5b5a44d945540826a66bec9e0 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Wed, 11 Dec 2024 11:51:45 +0700
Subject: <iman> update pengajuan tempo

---
 src/lib/pengajuan-tempo/component/Referensi.jsx | 81 ++++++++++++++++++++-----
 1 file changed, 66 insertions(+), 15 deletions(-)

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

diff --git a/src/lib/pengajuan-tempo/component/Referensi.jsx b/src/lib/pengajuan-tempo/component/Referensi.jsx
index 6d9d0bd6..1b7b0f4c 100644
--- a/src/lib/pengajuan-tempo/component/Referensi.jsx
+++ b/src/lib/pengajuan-tempo/component/Referensi.jsx
@@ -10,8 +10,12 @@ import { PlusCircleIcon } from '@heroicons/react/24/outline';
 import useDevice from '@/core/hooks/useDevice';
 import { Trash2Icon } from 'lucide-react';
 import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline';
+import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
 const initialData = [];
 const Referensi = ({ chekValid, buttonSubmitClick, data }) => {
+  const [changeConfirmation, setChangeConfirmation] = useState(false);
+  const [selectedIndex, setSelectedIndex] = useState(null);
+
   const { isDesktop, isMobile } = useDevice();
   const [openIndexes, setOpenIndexes] = useState([]);
 
@@ -26,7 +30,7 @@ const Referensi = ({ chekValid, buttonSubmitClick, data }) => {
     resolver: yupResolver(validationSchema),
     defaultValues,
   });
-  const { formSupplier, updateFormSupplier, updateHasSave } =
+  const { formSupplier, updateFormSupplier, updateHasSave, hasSavedata } =
     usePengajuanTempoStoreSupplier();
   const { form } = usePengajuanTempoStore();
   const [formData, setFormData] = useState([
@@ -63,9 +67,8 @@ const Referensi = ({ chekValid, buttonSubmitClick, data }) => {
     const editData = supplierData.map((item, i) =>
       i === index ? { ...item, [name]: formattedValue } : item
     );
-
     setSupplierData(editData);
-    if (value == '') {
+    if (value == '' && supplierData.supplier) {
       updateHasSave(true);
     }
   };
@@ -89,12 +92,26 @@ const Referensi = ({ chekValid, buttonSubmitClick, data }) => {
     }
   };
 
-  const handleDeleteSupplier = (index) => {
-    // updateHasSave(false); // Indikasi bahwa data telah berubah
-
-    const updatedData = supplierData.filter((_, i) => i !== index);
-    setSupplierData(updatedData);
-    updateFormSupplier(updatedData); // Update store atau state terkait
+  useEffect(() => {
+    const isAllFieldsEmpty = Object.values(newSupplier).every(
+      (value) => value === ''
+    );
+    if (isAllFieldsEmpty) {
+      updateHasSave(true);
+    } else {
+      updateHasSave(false);
+    }
+  }, [newSupplier]);
+
+  const handleDeleteSupplier = () => {
+    if (selectedIndex !== null) {
+      // Logika untuk menghapus supplier
+      const updatedSuppliers = supplierData.filter(
+        (_, idx) => idx !== selectedIndex
+      );
+      setSupplierData(updatedSuppliers);
+      setChangeConfirmation(false);
+    }
   };
 
   const handleAddNewSupplier = () => {
@@ -181,8 +198,37 @@ const Referensi = ({ chekValid, buttonSubmitClick, data }) => {
     );
   };
 
+  const handleOpenConfirmation = (index) => {
+    setSelectedIndex(index);
+    setChangeConfirmation(true);
+  };
   return (
     <>
+      <BottomPopup
+        active={changeConfirmation}
+        close={() => setChangeConfirmation(false)}
+        title='Ubah profil Bisnis'
+      >
+        <div className='leading-7 text-gray_r-12/80'>
+          Apakah anda yakin menghapus data?
+        </div>
+        <div className='flex mt-6 gap-x-4 md:justify-end'>
+          <button
+            className='btn-solid-red flex-1 md:flex-none'
+            type='button'
+            onClick={handleDeleteSupplier}
+          >
+            Ya, Hapus
+          </button>
+          <button
+            className='btn-light flex-1 md:flex-none'
+            type='button'
+            onClick={() => setChangeConfirmation(false)}
+          >
+            Batal
+          </button>
+        </div>
+      </BottomPopup>
       {isDesktop && (
         <div className='py-4'>
           <div className='flex flex-col justify-start'>
@@ -262,7 +308,8 @@ const Referensi = ({ chekValid, buttonSubmitClick, data }) => {
                       />
                       <Trash2Icon
                         size={18}
-                        onClick={() => handleDeleteSupplier(index)}
+                        onClick={() => handleOpenConfirmation(index)}
+                        className='cursor-pointer'
                       />
                     </td>
                   </tr>
@@ -332,13 +379,15 @@ const Referensi = ({ chekValid, buttonSubmitClick, data }) => {
               className='bg-gray-200 border border-gray-500 rounded-md text-sm text-gray-500 p-2 h-11 mb-1 content-center flex flex-row justify-center items-center'
             >
               {<PlusCircleIcon className='w-5 mr-2' />}
-              {''} Tambah data baru
+              {''} Tambah Data Baru
             </button>
             <button
               onClick={simpanData}
-              className='bg-red-500 border border-red-500 opacity-70 rounded-md text-sm text-white p-2 h-11 mb-1 content-center flex flex-row justify-center items-center'
+              className={`bg-red-500 border border-red-500 rounded-md text-sm text-white p-2 h-11 mb-1 content-center flex flex-row justify-center items-center ${
+                hasSavedata ? 'hidden' : ''
+              }`}
             >
-              simpan data
+              Simpan Data
             </button>
             <span className='text-sm opacity-60 text-red-500'>
               *Klik simpan sebelum lanjut ke tahap selanjutnya
@@ -546,9 +595,11 @@ const Referensi = ({ chekValid, buttonSubmitClick, data }) => {
             </span>
             <button
               onClick={simpanData}
-              className='bg-gray-200 border border-gray-500 rounded-md w-full text-sm text-gray-500 p-2 h-11 mb-1 content-center flex flex-row justify-center items-center'
+              className={`bg-gray-200 border border-gray-500 rounded-md w-full text-sm text-gray-500 p-2 h-11 mb-1 content-center flex flex-row justify-center items-center ${
+                hasSavedata ? 'hidden' : ''
+              }`}
             >
-              simpan data
+              Simpan Data
             </button>
           </div>
         </div>
-- 
cgit v1.2.3

