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/CreateAddress.jsx | 68 +++++++++++++---------------
 1 file changed, 32 insertions(+), 36 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 68e72c62..23238d4b 100644
--- a/src/lib/address/components/CreateAddress.jsx
+++ b/src/lib/address/components/CreateAddress.jsx
@@ -53,6 +53,7 @@ const CreateAddress = () => {
   const [showValidationPopup, setShowValidationPopup] = useState(false);
   const [popupMessage, setPopupMessage] = useState("");
   const [selectedCityName, setSelectedCityName] = useState("");
+  const [normalizedDistrict, setNormalizedDistrict] = useState("");
 
    useEffect(() => {
     resetPin();
@@ -71,12 +72,12 @@ const CreateAddress = () => {
     setAddressMaps('');
   }, []);
 
-   useEffect(() => {
+  useEffect(() => {
     if (detailAddress) {
       setValue('zip', detailAddress.postalCode);
       const selectedState = states.find(
         (state) =>
-          detailAddress?.province.includes(state.label) ||
+          (detailAddress?.province || "").includes(state.label) ||
           state.label.includes(detailAddress?.province)
       );
       setValue('state', selectedState?.value);
@@ -120,18 +121,26 @@ const CreateAddress = () => {
     }
   }, [auth]);
 
+  const normalizeName = (name = "") => {
+    return name
+      .toLowerCase()
+      .replace(/\bkabupaten\b/gi, "")
+      .replace(/\bkota\b/gi, "")
+      .trim();
+  };
+
   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);
+    if (detailAddress?.district) {
+      const normalizedDistrict = normalizeName(detailAddress.district);
+
+      const selectedCity = cities.find((city) => {
+        const normalizedCity = normalizeName(city.label);
+        return normalizedCity === normalizedDistrict;
+      });
+
+      if (selectedCity) {
+        setValue("city", selectedCity.value);
+      }
     }
   }, [cities, detailAddress, setValue]);
 
@@ -200,31 +209,18 @@ const CreateAddress = () => {
     }
   }, [subDistricts, detailAddress, 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(selectedCityName);
+  
+  // console.log(selectedCityName, '=', normalizedDistrict);
   const onSubmitHandler = async (values) => {
-    if (detailAddress?.district) {
-      if (
-        selectedCityName && 
-        selectedCityName.toLowerCase() !== 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;
-- 
cgit v1.2.3

