From dd8a56ad12d054e121965c41bcb077158690b208 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Tue, 17 Dec 2024 17:07:44 +0700
Subject: <iman> update code

---
 .../component/informasiPerusahaan.jsx              | 103 +++++++++++++++++----
 1 file changed, 83 insertions(+), 20 deletions(-)

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

diff --git a/src/lib/pengajuan-tempo/component/informasiPerusahaan.jsx b/src/lib/pengajuan-tempo/component/informasiPerusahaan.jsx
index 9850e927..4bc04a3f 100644
--- a/src/lib/pengajuan-tempo/component/informasiPerusahaan.jsx
+++ b/src/lib/pengajuan-tempo/component/informasiPerusahaan.jsx
@@ -73,9 +73,9 @@ const InformasiPerusahaan = ({
 
   const watchState = watch('state');
   useEffect(() => {
-    setValue('city', '');
-    setValue('district', '');
-    setValue('subDistrict', '');
+    // setValue('city', '');
+    // setValue('district', '');
+    // setValue('subDistrict', '');
     if (watchState) {
       updateForm('state', `${watchState}`);
       validate();
@@ -133,6 +133,15 @@ const InformasiPerusahaan = ({
 
   useEffect(() => {
     let kelurahan = '';
+    let kecamatan = '';
+
+    if (watchDistrict) {
+      for (const data in districts) {
+        if (districts[data].value == watchDistrict) {
+          kecamatan = districts[data].label.toLowerCase();
+        }
+      }
+    }
 
     if (watchsubDistrict) {
       updateForm('subDistrict', `${watchsubDistrict}`);
@@ -147,13 +156,30 @@ const InformasiPerusahaan = ({
           `https://alamat.thecloudalert.com/api/cari/index/?keyword=${kelurahan}`
         );
 
+        let dataMasuk = []; // Array untuk menyimpan kode pos yang sudah diproses
+
         const result = await response.json();
-        const dataZips = result.result.map((zip) => ({
-          value: parseInt(zip.kodepos),
-          label: zip.kodepos,
-        }));
-        setZips(dataZips);
+
+        // Filter dan map data
+        const dataZips = result.result
+          .filter((zip) => zip.kecamatan.toLowerCase() === kecamatan) // Filter berdasarkan kecamatan
+          .filter((zip) => {
+            // Pastikan zip.kodepos belum ada di dataMasuk
+            if (dataMasuk.includes(zip.kodepos)) {
+              return false; // Jika sudah ada, maka skip (tidak akan ditambahkan)
+            } else {
+              dataMasuk.push(zip.kodepos); // Tambahkan ke dataMasuk
+              return true; // Tambahkan zip ke hasil
+            }
+          })
+          .map((zip) => ({
+            value: parseInt(zip.kodepos),
+            label: zip.kodepos,
+          }));
+
+        setZips(dataZips); // Set hasil ke state
       };
+
       loadZip();
     }
   }, [watchsubDistrict, setValue, subDistricts]);
@@ -744,14 +770,33 @@ const InformasiPerusahaan = ({
                           name='zip'
                           control={control}
                           render={(props) => (
-                            <HookFormSelect
-                              {...props}
-                              options={zips}
-                              disabled={!watchsubDistrict}
-                              placeholder='Zip'
-                            />
+                            <>
+                              {/* Jika zips tidak kosong, tampilkan dropdown */}
+                              {zips.length > 0 ? (
+                                <HookFormSelect
+                                  {...props}
+                                  options={zips}
+                                  disabled={!watchsubDistrict}
+                                  placeholder='Zip'
+                                />
+                              ) : (
+                                // Jika zips kosong, tampilkan input manual
+                                <input
+                                  id='zip'
+                                  name='zip'
+                                  ref={zipRef}
+                                  placeholder='Kode Pos'
+                                  type='number'
+                                  disabled={!watchsubDistrict}
+                                  value={form.zip}
+                                  className='form-input'
+                                  onChange={handleInputChange}
+                                />
+                              )}
+                            </>
                           )}
                         />
+
                         {chekValid && (
                           <div className='text-caption-2 text-danger-500 mt-1'>
                             {errors.zip}
@@ -1276,12 +1321,30 @@ const InformasiPerusahaan = ({
                         name='zip'
                         control={control}
                         render={(props) => (
-                          <HookFormSelect
-                            {...props}
-                            options={zips}
-                            disabled={!watchsubDistrict}
-                            placeholder='Zip'
-                          />
+                          <>
+                            {/* Jika zips tidak kosong, tampilkan dropdown */}
+                            {zips.length > 0 ? (
+                              <HookFormSelect
+                                {...props}
+                                options={zips}
+                                disabled={!watchsubDistrict}
+                                placeholder='Zip'
+                              />
+                            ) : (
+                              // Jika zips kosong, tampilkan input manual
+                              <input
+                                id='zip'
+                                name='zip'
+                                ref={zipRef}
+                                placeholder='Kode Pos'
+                                type='number'
+                                disabled={!watchsubDistrict}
+                                value={form.zip}
+                                className='form-input'
+                                onChange={handleInputChange}
+                              />
+                            )}
+                          </>
                         )}
                       />
                       {chekValid && (
-- 
cgit v1.2.3

