From 9fbc3c44f745efb4ea8a5c119dde7027b65451d8 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Thu, 11 Sep 2025 09:22:34 +0700
Subject: <hafid> fix address edit and create bug override

---
 src/lib/address/components/EditAddress.jsx | 66 ++++++++++++++++--------------
 1 file changed, 36 insertions(+), 30 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 d5f22670..75f1a89a 100644
--- a/src/lib/address/components/EditAddress.jsx
+++ b/src/lib/address/components/EditAddress.jsx
@@ -51,6 +51,7 @@ const EditAddress = ({ id, defaultValues }) => {
   const [showValidationPopup, setShowValidationPopup] = useState(false);
   const [popupMessage, setPopupMessage] = useState("");
   const [selectedCityName, setSelectedCityName] = useState("");
+  const [normalizedDistrict, setNormalizedDistrict] = useState("");
 
   const {
     addressMaps,
@@ -61,6 +62,14 @@ const EditAddress = ({ id, defaultValues }) => {
     getDefaultCenter, // penting untuk deteksi default center
   } = useMaps();
 
+  const normalizeName = (name = "") => {
+    return name
+      .toLowerCase()
+      .replace(/\bkabupaten\b/gi, "")
+      .replace(/\bkota\b/gi, "")
+      .trim();
+  };
+
   // Helper: cek apakah benar2 sudah PIN (bukan default center & ada addressMaps)
   const isPinned = useMemo(() => {
     if (!selectedPosition) return false;
@@ -149,24 +158,35 @@ const EditAddress = ({ id, defaultValues }) => {
   }, [auth?.parentId, setValue]);
 
   // Isi ZIP/Prov dari detailAddress (JANGAN isi street)
-  useEffect(() => {
-    const zip = getValues("zip");
-    const province = getValues("state"); 
-    if (!zip && defaultValues?.zip) {
+useEffect(() => {
+  const zip = getValues("zip");
+  const province = getValues("state"); 
+  const street = getValues("street");
+
+  // set zip dari DB kalau kosong
+  if (!zip && defaultValues?.zip) {
       setValue("zip", defaultValues.zip);
     }
 
-    if (!getValues("state") && province) {
+    // set state dari DB kalau kosong
+    if (!province && defaultValues?.state) {
       const selectedState = states.find(
         (state) =>
-          province.includes(state.label) || state.label.includes(province)
+          defaultValues.state.includes(state.label) ||
+          state.label.includes(defaultValues.state)
       );
       if (selectedState) {
         setValue("state", selectedState.value);
       }
     }
+
+    // set street dari DB kalau kosong
+    if (!street && defaultValues?.street) {
+      setValue("street", defaultValues.street);
+    }
   }, [states, setValue, getValues, defaultValues]);
 
+
   useEffect(() => {
     const loadStates = async () => {
       let dataStates = await stateApi({ tempo: false });
@@ -313,38 +333,24 @@ const EditAddress = ({ id, defaultValues }) => {
     }
   }, [id, setValue]);
 
-  useEffect(() => {
-    const currentCity = cities.find((c) => c.value === watch("city"))?.label || "";
-
-    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(defaultValues);
-  
-  // console.log(selectedCityName, '=', detailAddress?.district);
   const onSubmitHandler = async (values) => {
     if (addressMaps) {
       if (!detailAddress){
         if (defaultValues?.oldCity !== values.city) {
           setPopupMessage("Titik Koordinat tidak sesuai dengan Kota yang dipilih");
           setShowValidationPopup(true);
-          console.log(detailAddress)
+          // console.log(detailAddress)
           return;
         }
       }
-      if(detailAddress){
-        if (selectedCityName && selectedCityName !== detailAddress?.district?.toLowerCase()) {
+      if (detailAddress) {
+        const cityName = normalizeName(
+          cities.find((c) => c.value === watch("city"))?.label || ""
+        );
+        const districtName = normalizeName(detailAddress?.district || "");
+        // console.log(cityName, '=', districtName);
+
+        if (cityName && cityName !== districtName) {
           setPopupMessage("Titik Koordinat tidak sesuai dengan Kota yang dipilih");
           setShowValidationPopup(true);
           return;
@@ -433,7 +439,7 @@ const EditAddress = ({ id, defaultValues }) => {
     }
 
     const dataProfile = await addressApi({ id: auth.partnerId });
-    console.log('ini adalah', dataProfile);
+    // console.log('ini adalah', dataProfile);
   };
   // console.log('ini adalah', detailAddress);
   return (
-- 
cgit v1.2.3

