From b94c0181d2e8b3b1f8a48879a059813d213b09d5 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Mon, 25 Nov 2024 14:01:45 +0700
Subject: <iman> update data

---
 src/lib/pengajuan-tempo/component/Pengiriman.jsx | 118 +++++++++++++++++------
 1 file changed, 87 insertions(+), 31 deletions(-)

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

diff --git a/src/lib/pengajuan-tempo/component/Pengiriman.jsx b/src/lib/pengajuan-tempo/component/Pengiriman.jsx
index bae3214b..0c96c060 100644
--- a/src/lib/pengajuan-tempo/component/Pengiriman.jsx
+++ b/src/lib/pengajuan-tempo/component/Pengiriman.jsx
@@ -39,24 +39,43 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
     ['Surat Terima Barang (STB)', '2'],
     ['Lembar Penerimaan Barang (LPB)', '3'],
   ];
+  const Dokumen_invoice = [
+    ['Invoice Pembelian', '0'],
+    ['Surat Jalan', '1'],
+    ['Berita Acara Serah Terima (BAST)', '2'],
+    ['Faktur Pajak', '3'],
+    ['Good Receipt (GR)', '4'],
+  ];
   const [selectedIds, setSelectedIds] = useState(() => {
     if (formPengiriman.dokumenPengiriman) {
       const selectedDocuments = formPengiriman.dokumenPengiriman
         .split(',')
         .map((item) => item.trim());
-      return dokumen_kirim
-        .filter(([name]) => selectedDocuments.includes(name))
-        .map(([, id]) => Number(id));
+
+      return selectedDocuments.map((docName) => {
+        const match = dokumen_kirim.find(([name]) => name === docName);
+        return match ? Number(match[1]) : 5; // Jika cocok, gunakan ID; jika tidak, gunakan 5
+      });
     }
     return [];
   });
-  console.log('selectedIds', selectedIds);
-  const [selectedIdsDokumenInvoice, setSelectedIdsselectedIdsDokumenInvoice] =
-    useState(
-      formPengiriman.dokumenPengirimanInvoice
-        ? formPengiriman.dokumenPengirimanInvoice.split(',').map(Number)
-        : []
-    );
+
+  const [selectedIdsDokumenInvoice, setSelectedIdsDokumenInvoice] = useState(
+    () => {
+      if (formPengiriman.dokumenPengirimanInvoice) {
+        // Pisahkan string dokumen berdasarkan koma dan hilangkan spasi ekstra
+        const selectedDocuments = formPengiriman.dokumenPengirimanInvoice
+          .split(',')
+          .map((item) => item.trim());
+        // Cari ID berdasarkan posisi nama dokumen di array Dokumen_invoice
+        return selectedDocuments.map((docName) => {
+          const match = Dokumen_invoice.find(([name]) => name === docName);
+          return match ? Number(match[1]) : 5; // Jika cocok, gunakan ID; jika tidak, gunakan 5
+        });
+      }
+      return [];
+    }
+  );
 
   const handleCheckboxChange = (id) => {
     const updatedSelected = selectedIds.includes(id)
@@ -75,7 +94,7 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
       ? selectedIdsDokumenInvoice.filter((selectedId) => selectedId !== id)
       : [...selectedIdsDokumenInvoice, id];
 
-    setSelectedIdsselectedIdsDokumenInvoice(updatedSelected);
+    setSelectedIdsDokumenInvoice(updatedSelected);
 
     // Jika checkbox 'Lainnya' dipilih, aktifkan input custom
 
@@ -83,16 +102,39 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
     validatePengiriman();
   };
 
-  // useEffect(() => {
-  //   if (formPengiriman.dokumenPengiriman) {
-  //     setSelectedIds(formPengiriman.dokumenPengiriman.split(',').map(Number)); // Parse string menjadi array angka
-  //   }
-  // }, [formPengiriman.dokumenPengiriman]);
+  useEffect(() => {
+    if (formPengiriman.dokumenPengiriman) {
+      // Pisahkan string berdasarkan koma dan trim spasi
+      const selectedDocuments = formPengiriman.dokumenPengiriman
+        .split(',')
+        .map((item) => item.trim());
+
+      // Map setiap dokumen, cocokkan nama dokumen dengan ID yang sesuai, atau set ke 5 jika tidak cocok
+      const matchingIds = selectedDocuments.map((docName) => {
+        const match = dokumen_kirim.find(([name]) => name === docName);
+        return match ? Number(match[1]) : 5; // Jika cocok gunakan ID; jika tidak, gunakan 5
+      });
+
+      // Set hasil ID yang sesuai atau default ke state selectedIds
+      setSelectedIds(matchingIds);
+    }
+  }, [formPengiriman.dokumenPengiriman]);
+
   useEffect(() => {
     if (formPengiriman.dokumenPengirimanInvoice) {
-      setSelectedIdsselectedIdsDokumenInvoice(
-        formPengiriman.dokumenPengirimanInvoice.split(',').map(Number)
-      ); // Parse string menjadi array angka
+      // Pisahkan string berdasarkan koma dan trim spasi
+      const selectedDocuments = formPengiriman.dokumenPengirimanInvoice
+        .split(',')
+        .map((item) => item.trim());
+
+      // Temukan ID berdasarkan posisi dokumen di dalam array Dokumen_invoice
+      const matchingIds = selectedDocuments.map((docName) => {
+        const match = Dokumen_invoice.find(([name]) => name === docName);
+        return match ? Number(match[1]) : 5; // Jika cocok gunakan ID; jika tidak, gunakan 5
+      });
+
+      // Set hasil ID yang cocok ke state selectedIdsDokumenInvoice
+      setSelectedIdsDokumenInvoice(matchingIds);
     }
   }, [formPengiriman.dokumenPengirimanInvoice]);
 
@@ -859,8 +901,10 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                   <Checkbox
                     colorScheme='red'
                     key='0'
-                    isChecked={isChecked(0)}
-                    onChange={() => handleCheckboxChange(0)}
+                    isChecked={isChecked('Surat Tanda Terima Barang (STTB)')}
+                    onChange={() =>
+                      handleCheckboxChange('Surat Tanda Terima Barang (STTB)')
+                    }
                   >
                     Surat Tanda Terima Barang (STTB)
                   </Checkbox>
@@ -868,7 +912,7 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                     colorScheme='red'
                     key='1'
                     isChecked={isChecked(1)}
-                    onChange={() => handleCheckboxChange(1)}
+                    onChange={() => handleCheckboxChange('Good Receipt (GR)')}
                   >
                     Good Receipt (GR)
                   </Checkbox>
@@ -876,7 +920,9 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                     colorScheme='red'
                     key='2'
                     isChecked={isChecked(2)}
-                    onChange={() => handleCheckboxChange(2)}
+                    onChange={() =>
+                      handleCheckboxChange('Surat Terima Barang (STB)')
+                    }
                   >
                     Surat Terima Barang (STB)
                   </Checkbox>
@@ -884,7 +930,9 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                     colorScheme='red'
                     key='3'
                     isChecked={isChecked(3)}
-                    onChange={() => handleCheckboxChange(3)}
+                    onChange={() =>
+                      handleCheckboxChange('Lembar Penerimaan Barang (LPB)')
+                    }
                   >
                     Lembar Penerimaan Barang (LPB)
                   </Checkbox>
@@ -894,7 +942,7 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                       colorScheme='red'
                       key='4'
                       isChecked={isChecked(4)}
-                      onChange={() => handleCheckboxChange(4)}
+                      onChange={() => handleCheckboxChange('Lainnya')}
                     >
                       Lainnya
                     </Checkbox>
@@ -937,7 +985,9 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                     key='0'
                     isChecked={isCheckedInvoice(0)}
                     onChange={() =>
-                      handleCheckboxChangeDokumenPengirimanInvoice(0)
+                      handleCheckboxChangeDokumenPengirimanInvoice(
+                        'Invoice Pembelian'
+                      )
                     }
                   >
                     Invoice Pembelian
@@ -947,7 +997,9 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                     key='1'
                     isChecked={isCheckedInvoice(1)}
                     onChange={() =>
-                      handleCheckboxChangeDokumenPengirimanInvoice(1)
+                      handleCheckboxChangeDokumenPengirimanInvoice(
+                        ' Surat Jalan'
+                      )
                     }
                   >
                     Surat Jalan
@@ -957,7 +1009,7 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                     key='2'
                     isChecked={isCheckedInvoice(2)}
                     onChange={() =>
-                      handleCheckboxChangeDokumenPengirimanInvoice(2)
+                      handleCheckboxChangeDokumenPengirimanInvoice('')
                     }
                   >
                     Berita Acara Serah Terima (BAST)
@@ -967,7 +1019,9 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                     key='3'
                     isChecked={isCheckedInvoice(3)}
                     onChange={() =>
-                      handleCheckboxChangeDokumenPengirimanInvoice(3)
+                      handleCheckboxChangeDokumenPengirimanInvoice(
+                        'Faktur Pajak'
+                      )
                     }
                   >
                     Faktur Pajak
@@ -977,7 +1031,9 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                     key='4'
                     isChecked={isCheckedInvoice(4)}
                     onChange={() =>
-                      handleCheckboxChangeDokumenPengirimanInvoice(4)
+                      handleCheckboxChangeDokumenPengirimanInvoice(
+                        'Good Receipt (GR)'
+                      )
                     }
                   >
                     Good Receipt (GR)
@@ -989,7 +1045,7 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                       key='5'
                       isChecked={isCheckedInvoice(5)}
                       onChange={() =>
-                        handleCheckboxChangeDokumenPengirimanInvoice(5)
+                        handleCheckboxChangeDokumenPengirimanInvoice('Lainnya')
                       }
                     >
                       Lainnya
-- 
cgit v1.2.3

