From bd276355e5b2090628d3ad10e95f2c118e883ab2 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Wed, 10 Sep 2025 15:57:43 +0700
Subject: <hafid> fix address override

---
 src/lib/address/components/EditAddress.jsx | 54 ++++++++++++++++--------------
 1 file changed, 28 insertions(+), 26 deletions(-)

(limited to 'src/lib/address/components/EditAddress.jsx')

diff --git a/src/lib/address/components/EditAddress.jsx b/src/lib/address/components/EditAddress.jsx
index d9908726..d5f22670 100644
--- a/src/lib/address/components/EditAddress.jsx
+++ b/src/lib/address/components/EditAddress.jsx
@@ -121,7 +121,18 @@ const EditAddress = ({ id, defaultValues }) => {
     }
   }, [getValues, resetPin, setAddressMaps, setSelectedPosition]);
 
-
+  useEffect(() => {
+    if (Object.keys(detailAddress).length > 0) {
+      setValue('zip', detailAddress.postalCode);
+      const selectedState = states.find(
+        (state) =>
+          detailAddress?.province.includes(state.label) ||
+        state.label.includes(detailAddress?.province)
+      );
+      setValue('state', selectedState?.value);  
+      setValue('street', detailAddress?.street);      
+    }
+  }, [detailAddress, setValue]);
 
   useEffect(() => {
     const loadProfile = async () => {
@@ -234,29 +245,24 @@ const EditAddress = ({ id, defaultValues }) => {
       loadDistricts();
     }
   }, [watchCity, setValue, getValues]);
-
+  
   useEffect(() => {
     if (!isPinned) return; // skip kalau belum pin
-
-    // jangan override kalau form sudah punya nilai district
     if (getValues("district")) return;
 
-    if (Object.keys(detailAddress || {}).length > 0) {
+    if (Object.keys(detailAddress).length > 0) {
       const selectedDistrict = districts.find(
         (district) =>
-          detailAddress?.subDistrict
-            ?.toLowerCase()
+          detailAddress.subDistrict
+            .toLowerCase()
             .includes(district.label.toLowerCase()) ||
           district.label
             .toLowerCase()
-            .includes(detailAddress?.subDistrict?.toLowerCase())
+            .includes(detailAddress.subDistrict.toLowerCase())
       );
-
-      if (selectedDistrict) {
-        setValue("district", selectedDistrict.value);
-      }
+      setValue('district', selectedDistrict?.value);
     }
-  }, [districts, detailAddress, isPinned, getValues, setValue]);
+  }, [districts, detailAddress, setValue]);
   
 
   const watchDistrict = watch('district');
@@ -284,26 +290,22 @@ const EditAddress = ({ id, defaultValues }) => {
 
   useEffect(() => {
     if (!isPinned) return; // skip kalau belum pin
-
-    // jangan override kalau form sudah punya nilai subDistrict
     if (getValues("subDistrict")) return;
-
-    if (Object.keys(detailAddress || {}).length > 0) {
+    
+    if (Object.keys(detailAddress).length > 0) {
       const selectedSubDistrict = subDistricts.find(
         (district) =>
-          detailAddress?.village
-            ?.toLowerCase()
+          detailAddress.village
+            .toLowerCase()
             .includes(district.label.toLowerCase()) ||
-          district.label
+            district.label
             .toLowerCase()
-            .includes(detailAddress?.village?.toLowerCase())
-      );
+            .includes(detailAddress.village.toLowerCase())
+          );
 
-      if (selectedSubDistrict) {
-        setValue("subDistrict", selectedSubDistrict.value);
-      }
+          setValue('subDistrict', selectedSubDistrict?.value);
     }
-  }, [subDistricts, detailAddress, isPinned, getValues, setValue]);
+  }, [subDistricts, detailAddress, setValue]);
 
   useEffect(() => {
     if (id) {
-- 
cgit v1.2.3

