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/CreateAddress.jsx | 71 ++++++++++++++++++----------
 1 file changed, 45 insertions(+), 26 deletions(-)

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

diff --git a/src/lib/address/components/CreateAddress.jsx b/src/lib/address/components/CreateAddress.jsx
index fcfad056..68e72c62 100644
--- a/src/lib/address/components/CreateAddress.jsx
+++ b/src/lib/address/components/CreateAddress.jsx
@@ -58,30 +58,6 @@ const CreateAddress = () => {
     resetPin();
   }, [resetPin]);
 
-
-  useEffect(() => {
-    if (defaultValues?.zip) {
-      setValue("zip", defaultValues.zip);
-    }
-
-    // set state/province
-    if (defaultValues?.province) {
-      const selectedState = states.find(
-        (state) =>
-          defaultValues.province.includes(state.label) ||
-          state.label.includes(defaultValues.province)
-      );
-      if (selectedState) {
-        setValue("state", selectedState.value);
-      }
-    }
-
-    // set street
-    if (defaultValues?.street) {
-      setValue("street", defaultValues.street);
-    }
-  }, [states, setValue, defaultValues]);
-
   useEffect(() => {
     const loadState = async () => {
       let dataState = await stateApi({ tempo: false });
@@ -95,6 +71,20 @@ const CreateAddress = () => {
     setAddressMaps('');
   }, []);
 
+   useEffect(() => {
+    if (detailAddress) {
+      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]);
+
   const watchState = watch('state');
   useEffect(() => {
     setValue('city', '');
@@ -130,6 +120,21 @@ const CreateAddress = () => {
     }
   }, [auth]);
 
+  useEffect(() => {
+    if (detailAddress && Object.keys(detailAddress).length > 0) {
+      const selectedCities = cities.find(
+        (city) =>
+          detailAddress?.district
+            .toLowerCase()
+            .includes(city.label.toLowerCase()) ||
+          city.label
+            .toLowerCase()
+            .includes(detailAddress?.district.toLowerCase())
+      );
+      setValue('city', selectedCities?.value);
+    }
+  }, [cities, detailAddress, setValue]);
+
   const watchCity = watch('city');
   useEffect(() => {
     setValue('district', '');
@@ -197,9 +202,23 @@ const CreateAddress = () => {
 
   useEffect(() => {
     const currentCity = cities.find((c) => c.value === watch("city"))?.label || "";
-    setSelectedCityName(currentCity);
-  }, [watch("city"), cities]);
 
+    let normalized = currentCity.toLowerCase().trim();
+
+    const parts = normalized.split(" ");
+
+    if (parts.length >= 3) {
+      // hapus prefix kabupaten/kota kalau ada
+      normalized = normalized
+        .replace(/^kabupaten\s+/i, "")
+        .replace(/^kota\s+/i, "")
+        .trim();
+    }
+
+    setSelectedCityName(normalized);
+  }, [watch("city"), cities]);
+  
+  console.log(selectedCityName);
   const onSubmitHandler = async (values) => {
     if (detailAddress?.district) {
       if (
-- 
cgit v1.2.3

