From dd8a56ad12d054e121965c41bcb077158690b208 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Tue, 17 Dec 2024 17:07:44 +0700 Subject: 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) => ( - + <> + {/* Jika zips tidak kosong, tampilkan dropdown */} + {zips.length > 0 ? ( + + ) : ( + // Jika zips kosong, tampilkan input manual + + )} + )} /> + {chekValid && (
{errors.zip} @@ -1276,12 +1321,30 @@ const InformasiPerusahaan = ({ name='zip' control={control} render={(props) => ( - + <> + {/* Jika zips tidak kosong, tampilkan dropdown */} + {zips.length > 0 ? ( + + ) : ( + // Jika zips kosong, tampilkan input manual + + )} + )} /> {chekValid && ( -- cgit v1.2.3