From e9cd13307f0095dc4edc5048752675d80c551564 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Fri, 6 Dec 2024 17:06:26 +0700 Subject: pengajuan tempo --- src/lib/address/components/EditAddress.jsx | 53 ++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 6 deletions(-) (limited to 'src/lib/address/components') 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) => ( - + )} />
@@ -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'), }); -- cgit v1.2.3