diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-12-06 17:06:26 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-12-06 17:06:26 +0700 |
| commit | e9cd13307f0095dc4edc5048752675d80c551564 (patch) | |
| tree | ba133ca989725bd225e3ed1517ddece8a33e3f32 /src/lib/address | |
| parent | 9a49b8d84761781531cb417731cb9ef802f63541 (diff) | |
<iman> pengajuan tempo
Diffstat (limited to 'src/lib/address')
| -rw-r--r-- | src/lib/address/components/EditAddress.jsx | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/src/lib/address/components/EditAddress.jsx b/src/lib/address/components/EditAddress.jsx index 23cf72a9..dd5c27fd 100644 --- a/src/lib/address/components/EditAddress.jsx +++ b/src/lib/address/components/EditAddress.jsx @@ -34,7 +34,8 @@ const EditAddress = ({ id, defaultValues }) => { const [states, setStates] = useState([]); const [cities, setCities] = useState([]); const [districts, setDistricts] = useState([]); - const [subDistricts, setSubDistricts] = useState([]); + const [subDistricts, setSubDistricts] = useState([]); + const [isZipTrue, setIsZipTrue] = useState(false); useEffect(() => { const loadProfile = async () => { @@ -60,12 +61,12 @@ const EditAddress = ({ id, defaultValues }) => { setStates(dataStates); }; loadStates(); - },[]) + }, []); const watchState = watch('state'); useEffect(() => { setValue('city', ''); - if(watchState) { + if (watchState) { const loadCities = async () => { let dataCities = await cityApi({ stateId: watchState }); dataCities = dataCities.map((city) => ({ @@ -81,7 +82,6 @@ const EditAddress = ({ id, defaultValues }) => { }; loadCities(); } - }, [watchState, setValue, getValues]); const watchCity = watch('city'); @@ -128,7 +128,44 @@ const EditAddress = ({ id, defaultValues }) => { loadSubDistricts(); } }, [watchDistrict, setValue, getValues]); + + const watchZip = watch('zip'); const onSubmitHandler = async (values) => { + if (watchZip) { + const loadZip = async () => { + try { + const response = await fetch( + `https://alamat.thecloudalert.com/api/cari/index/?keyword=${watchZip}` + ); + + if (!response.ok) { + toast.error('Gagal memuat data alamat pos.'); + setValue('zip', ''); + return false; + } + + const result = await response.json(); + console.log('result', result); + + if (result.length === 0) { + toast.error('Alamat pos salah'); + setValue('zip', ''); + return false; + } + + return true; // Jika valid + } catch (error) { + toast.error('Terjadi kesalahan saat memeriksa alamat pos.'); + console.error('Error:', error); + return false; + } + }; + + const isValidZip = await loadZip(); + if (!isValidZip) { + return; // Menghentikan eksekusi jika alamat salah + } + } const data = { ...values, phone: values.mobile, @@ -287,7 +324,11 @@ const EditAddress = ({ id, defaultValues }) => { name='city' control={control} render={(props) => ( - <HookFormSelect {...props} options={cities} disabled={!watchState} /> + <HookFormSelect + {...props} + options={cities} + disabled={!watchState} + /> )} /> <div className='text-caption-2 text-danger-500 mt-1'> @@ -348,7 +389,7 @@ const validationSchema = Yup.object().shape({ mobile: Yup.string().required('Harus di-isi'), street: Yup.string().required('Harus di-isi'), zip: Yup.string().required('Harus di-isi'), - state : Yup.string().required('Harus di-pilih'), + state: Yup.string().required('Harus di-pilih'), city: Yup.string().required('Harus di-pilih'), district: Yup.string().required('Harus di-pilih'), }); |
