From 0f84963214ee6dc5b5a44d945540826a66bec9e0 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Wed, 11 Dec 2024 11:51:45 +0700
Subject: <iman> update pengajuan tempo

---
 src/lib/pengajuan-tempo/component/Pengiriman.jsx | 104 +++++++++++++++++------
 1 file changed, 78 insertions(+), 26 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 d32fa226..000f209c 100644
--- a/src/lib/pengajuan-tempo/component/Pengiriman.jsx
+++ b/src/lib/pengajuan-tempo/component/Pengiriman.jsx
@@ -23,6 +23,7 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
   const { form, errors, validate, updateForm } = usePengajuanTempoStore();
   const [states, setState] = useState([]);
   const [cities, setCities] = useState([]);
+  const [citiesInvoice, setCitiesInvoice] = useState([]);
   const [sameAddress, setSameAddress] = useState(false);
   const [sameAddressStreet, setSameAddressStreet] = useState(false);
   const [everyWeekday, setEveryWeekday] = useState(false);
@@ -90,7 +91,7 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
 
   useEffect(() => {
     const loadState = async () => {
-      let dataState = await stateApi();
+      let dataState = await stateApi({ tempo: true });
       dataState = dataState.map((state) => ({
         value: state.id,
         label: state.name,
@@ -125,6 +126,33 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
       validatePengiriman();
     }
   }, [watchCity]);
+
+  const watchStateInvoice = watch('stateInvoice');
+  useEffect(() => {
+    // updateFormPengiriman('cityPengiriman', '');
+    if (watchStateInvoice) {
+      updateFormPengiriman('stateInvoice', `${watchStateInvoice}`);
+      validatePengiriman();
+      const loadCities = async () => {
+        let dataCities = await cityApi({ stateId: watchStateInvoice });
+        dataCities = dataCities.map((city) => ({
+          value: city.id,
+          label: city.name,
+        }));
+        setCitiesInvoice(dataCities);
+      };
+      loadCities();
+    }
+  }, [watchStateInvoice]);
+
+  const watchCityInvoice = watch('cityInvoice');
+  useEffect(() => {
+    if (watchCityInvoice) {
+      updateFormPengiriman('cityInvoice', `${watchCityInvoice}`);
+      validatePengiriman();
+    }
+  }, [watchCityInvoice]);
+
   const handleInputChange = (event) => {
     const { name, value } = event.target;
     updateFormPengiriman(name, value);
@@ -171,23 +199,6 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
     setTukarInvoicePembayaran(!tukarInvoicePembayaran);
   };
 
-  // useEffect(() => {
-  //   updateFormPengiriman('everyWeekday', everyWeekday);
-  //   updateFormPengiriman('everyWeek', everyWeek);
-  //   updateFormPengiriman('tukarInvoice', tukarInvoice);
-  //   updateFormPengiriman('everyWeekdayPembayaran', everyWeekdayPembayaran);
-  //   updateFormPengiriman('everyWeekPembayaran', everyWeekPembayaran);
-  //   updateFormPengiriman('tukarInvoicePembayaran', tukarInvoicePembayaran);
-  //   validatePengiriman();
-  // }, [
-  //   everyWeekday,
-  //   everyWeek,
-  //   tukarInvoice,
-  //   everyWeekdayPembayaran,
-  //   everyWeekPembayaran,
-  //   tukarInvoicePembayaran,
-  // ]);
-
   const isFormValid = useMemo(
     () => Object.keys(errorsPengiriman).length === 0,
     [errorsPengiriman]
@@ -306,6 +317,8 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
   useEffect(() => {
     if (formPengiriman.isSameAddrees == 'true') {
       setSameAddress(true);
+    } else {
+      setSameAddress(false);
     }
   }, [formPengiriman.isSameAddrees]);
 
@@ -371,6 +384,29 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
     formPengiriman.statePengiriman,
     formPengiriman.cityPengiriman,
   ]);
+
+  useEffect(() => {
+    if (sameAddress) {
+      updateFormPengiriman('streetInvoice', formPengiriman.streetPengiriman);
+      updateFormPengiriman('stateInvoice', formPengiriman.statePengiriman);
+      setValue('stateInvoice', parseInt(formPengiriman.statePengiriman));
+      updateFormPengiriman('cityInvoice', formPengiriman.cityPengiriman);
+      setValue('cityInvoice', parseInt(formPengiriman.cityPengiriman));
+      updateFormPengiriman('isSameAddrees', `${sameAddress}`);
+    } else {
+      // updateFormPengiriman('stateInvoice', formPengiriman.stateInvoice);
+      // setValue('stateInvoice', parseInt(formPengiriman.stateInvoice));
+      // updateFormPengiriman('cityInvoice', formPengiriman.cityInvoice);
+      // setValue('cityInvoice', parseInt(formPengiriman.cityInvoice));
+    }
+    validatePengiriman();
+  }, [
+    sameAddress,
+    formPengiriman.streetPengiriman,
+    formPengiriman.statePengiriman,
+    formPengiriman.cityPengiriman,
+  ]);
+
   useEffect(() => {
     if (formPengiriman.sameAddressStreet?.toLowerCase().includes('true')) {
       setSameAddressStreet(true);
@@ -388,6 +424,16 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
       setValue('cityPengiriman', parseInt(formPengiriman.cityPengiriman));
     }
   }, [formPengiriman.cityPengiriman]);
+  useEffect(() => {
+    if (formPengiriman.stateInvoice) {
+      setValue('stateInvoice', parseInt(formPengiriman.stateInvoice));
+    }
+  }, [formPengiriman.stateInvoice]);
+  useEffect(() => {
+    if (formPengiriman.cityInvoice) {
+      setValue('cityInvoice', parseInt(formPengiriman.cityInvoice));
+    }
+  }, [formPengiriman.cityInvoice]);
 
   useEffect(() => {
     if (sameAddressStreet) {
@@ -483,11 +529,19 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
   //   cachedData?.tukarInvoicePembayaran,
   // ]);
   const handleChangeSameAddress = () => {
-    setSameAddress(!sameAddress);
+    // setSameAddress(!sameAddress);
+    // if (sameAddress) {
+    //   console.log('sameAddress adalah', sameAddress);
+    //   updateFormPengiriman('sameAddress', `true`);
+    // } else {
+    //   console.log('sameAddress merupakan', sameAddress);
+    // }
+    updateFormPengiriman('isSameAddrees', `${!sameAddress}`);
+    validate();
   };
   const handleChangeSameAddressStreet = () => {
     // updateFormPengiriman('sameAddressStreet', `${!sameAddressStreet}`);
-    setSameAddressStreet(!sameAddressStreet);
+    // setSameAddressStreet(!sameAddressStreet);
     if (sameAddressStreet == false) {
       updateFormPengiriman('streetPengiriman', '');
       updateFormPengiriman('statePengiriman', '');
@@ -496,9 +550,9 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
       setValue('streetPengiriman', '');
       setValue('statePengiriman', '');
       setValue('cityPengiriman', '');
-      updateFormPengiriman('isSameAddreesStreet', `${sameAddressStreet}`);
-      validate();
     }
+    updateFormPengiriman('isSameAddreesStreet', `${!sameAddressStreet}`);
+    validate();
   };
 
   useEffect(() => {
@@ -546,8 +600,6 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
 
     loadIndustries();
   }, [formPengiriman.zipPengiriman]);
-
-
   return (
     <>
       {isDesktop && (
@@ -829,8 +881,8 @@ const Pengiriman = ({ chekValid, buttonSubmitClick, isKonfirmasi }) => {
                           render={(props) => (
                             <HookFormSelect
                               {...props}
-                              options={cities}
-                              disabled={!watchState || sameAddress}
+                              options={citiesInvoice}
+                              disabled={!watchStateInvoice || sameAddress}
                               placeholder='Kota'
                             />
                           )}
-- 
cgit v1.2.3

