From 2e507ccbc92f18a0d7f8ebc59112a0337a0bd678 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Wed, 11 Jun 2025 13:40:45 +0700
Subject: <hafid> Pinpoint done

---
 src/lib/address/components/EditAddress.jsx | 150 +++++++++++++++++------------
 1 file changed, 90 insertions(+), 60 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 9f038b74..cfb35fbd 100644
--- a/src/lib/address/components/EditAddress.jsx
+++ b/src/lib/address/components/EditAddress.jsx
@@ -8,6 +8,7 @@ import districtApi from '../api/districtApi';
 import subDistrictApi from '../api/subDistrictApi';
 import addressApi from '@/lib/address/api/addressApi';
 import editAddressApi from '../api/editAddressApi';
+import editPartnerApi from '../api/editPartnerApi';
 import HookFormSelect from '@/core/components/elements/Select/HookFormSelect';
 import { toast } from 'react-hot-toast';
 import Menu from '@/lib/auth/components/Menu';
@@ -20,6 +21,7 @@ import { useMaps } from '../../maps/stores/useMaps';
 
 import PinPointMap from '../../maps/components/PinPointMap';
 import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
+import { data } from 'autoprefixer';
 
 const EditAddress = ({ id, defaultValues }) => {
   const auth = useAuth();
@@ -41,12 +43,14 @@ const EditAddress = ({ id, defaultValues }) => {
   const [cities, setCities] = useState([]);
   const [districts, setDistricts] = useState([]);
   const [subDistricts, setSubDistricts] = useState([]);
-  const [pinedMaps, setPinedMaps] = useState(false);
   const [tempAddress, setTempAddress] = useState(getValues('addressMap'));
-  const { addressMaps, selectedPosition, detailAddress } = useMaps();
-
-  console.log('ini adalah',);
+  const { addressMaps,
+    selectedPosition,
+    detailAddress,
+    pinedMaps,
+    setPinedMaps } = useMaps();
 
+  
   useEffect(() => {
     if (addressMaps) {
       setTempAddress(addressMaps);
@@ -55,20 +59,20 @@ const EditAddress = ({ id, defaultValues }) => {
       setValue('latitude', selectedPosition.lat);
     }
   }, [addressMaps, selectedPosition, setValue]);
-
+  
   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)
+        state.label.includes(detailAddress?.province)
       );
       setValue('state', selectedState?.value);  
-      setValue('street', detailAddress?.route);      
+      setValue('street', detailAddress?.street);      
     }
   }, [detailAddress, setValue]);
-
+  
   useEffect(() => {
     const loadProfile = async () => {
       const dataProfile = await addressApi({ id: auth.partnerId });
@@ -85,7 +89,7 @@ const EditAddress = ({ id, defaultValues }) => {
     };
     if (auth) loadProfile();
   }, [auth?.parentId]);
-
+  
   useEffect(() => {
     const loadStates = async () => {
       let dataStates = await stateApi({ tempo: false });
@@ -97,7 +101,7 @@ const EditAddress = ({ id, defaultValues }) => {
     };
     loadStates();
   }, []);
-
+  
   const watchState = watch('state');
   useEffect(() => {
     setValue('city', '');
@@ -118,7 +122,7 @@ const EditAddress = ({ id, defaultValues }) => {
       loadCities();
     }
   }, [watchState, setValue, getValues]);
-
+  
   useEffect(() => {
     if (Object.keys(detailAddress).length > 0) {
       const selectedCities = cities.find(
@@ -126,23 +130,23 @@ const EditAddress = ({ id, defaultValues }) => {
           detailAddress?.district
             .toLowerCase()
             .includes(city.label.toLowerCase()) ||
-          city.label
+            city.label
             .toLowerCase()
             .includes(detailAddress?.district.toLowerCase())
-      );
-      setValue('city', selectedCities?.value);
-    }
-  }, [cities, detailAddress, setValue]);
-
-  const watchCity = watch('city');
-  useEffect(() => {
-    if (watchCity) {
-      // setValue('district', '');
-      const loadDistricts = async () => {
-        let dataDistricts = await districtApi({ cityId: watchCity });
-        dataDistricts = dataDistricts.map((district) => ({
-          value: district.id,
-          label: district.name,
+          );
+          setValue('city', selectedCities?.value);
+        }
+      }, [cities, detailAddress, setValue]);
+      
+      const watchCity = watch('city');
+      useEffect(() => {
+        if (watchCity) {
+          // setValue('district', '');
+          const loadDistricts = async () => {
+            let dataDistricts = await districtApi({ cityId: watchCity });
+            dataDistricts = dataDistricts.map((district) => ({
+              value: district.id,
+              label: district.name,
         }));
         setDistricts(dataDistricts);
         let oldDistrict = getValues('oldDistrict');
@@ -154,7 +158,7 @@ const EditAddress = ({ id, defaultValues }) => {
       loadDistricts();
     }
   }, [watchCity, setValue, getValues]);
-
+  
   useEffect(() => {
     if (Object.keys(detailAddress).length > 0) {
       const selectedDistrict = districts.find(
@@ -169,9 +173,9 @@ const EditAddress = ({ id, defaultValues }) => {
       setValue('district', selectedDistrict?.value);
     }
   }, [districts, detailAddress, setValue]);
-
-
-
+  
+  
+  
   const watchDistrict = watch('district');
   useEffect(() => {
     if (watchDistrict) {
@@ -204,23 +208,29 @@ const EditAddress = ({ id, defaultValues }) => {
           detailAddress.village
             .toLowerCase()
             .includes(district.label.toLowerCase()) ||
-          district.label
+            district.label
             .toLowerCase()
             .includes(detailAddress.village.toLowerCase())
-      );
+          );
 
-      setValue('subDistrict', selectedSubDistrict?.value);
+          setValue('subDistrict', selectedSubDistrict?.value);
     }
   }, [subDistricts, detailAddress, setValue]);
 
+  useEffect(() => {
+    if (id) {
+      setValue('id', id);
+    }
+  }, [id, setValue]);
+  
   const onSubmitHandler = async (values) => {
     const data = {
       ...values,
       phone: values.mobile,
-      state_id: values.state,
-      city_id: values.city,
-      district_id: values.district,
-      sub_district_id: values.subDistrict,
+      state_id: parseInt(values.state, 10),
+      city_id: parseInt(values.city, 10),
+      district_id: parseInt(values.district, 10),
+      sub_district_id: parseInt(values.subDistrict, 10),
       longtitude: selectedPosition?.lng,
       latitude: selectedPosition?.lat,
       address_map: addressMaps,
@@ -230,37 +240,57 @@ const EditAddress = ({ id, defaultValues }) => {
     }
     try {
       const address = await editAddressApi({ id, data });
-      let dataAlamat;
-      let isUpdated = true;
-      if (auth.company) {
-        if (auth?.partnerId == id) {
-          dataAlamat = {
-            id_user: auth.partnerId,
-            alamat_lengkap_text: values.alamat_wajib_pajak,
-            street: values.street,
-          };
-          isUpdated = await odooApi(
-            'PUT',
-            `/api/v1/partner/${auth.parentId}`,
-            dataAlamat
-          );
-        }
+      console.log('Response address:', address);
+
+      let isUpdated = null;
+
+      // Jika company dan partnerId sama dengan id, maka update data alamat wajib pajak
+      const isCompanyEditingSelf = auth.company && auth.partnerId == id;
+
+      if (isCompanyEditingSelf) {
+        const dataProfile = await addressApi({ id: auth.partnerId });
+        const dataAlamat = {
+          id_user: auth.id,
+          company_type_id: dataProfile.companyTypeId,
+          industry_id: dataProfile.industryId,
+          tax_name: values.taxName,
+          npwp: values.npwp,
+          alamat_lengkap_text: values.alamat_wajib_pajak || values.street,
+          street: values.street,
+          email: values.email,
+          mobile: values.mobile,
+        };
+
+        const isUpdated = await editPartnerApi({
+          id: auth.partnerId,
+          data: dataAlamat,
+        });
+
+        console.log('Response isUpdated:', isUpdated);
       }
-      // if (isUpdated?.id) {
-      if (address?.id && (auth.company && auth?.partnerId == id ? isUpdated?.id : true)) {
+
+      // Validasi kondisi sukses
+      const isSuccess = !!address?.id;
+
+      if (isSuccess) {
         toast.success('Berhasil mengubah alamat');
         router.back();
       } else {
-        toast.error('Terjadi kesalahan internal');
-        router.back();
+        const errorMsg =
+          address?.message ||
+          isUpdated?.message ||
+          'Gagal memperbarui alamat, silakan coba lagi.';
+        toast.error(errorMsg);
       }
     } catch (error) {
-      console.log(error);
-      toast.error('Terjadi kesalahan internal');
-      router.back();
+      console.error('Catch error:', error);
+      toast.error(error?.message || 'Terjadi kesalahan tidak terduga.');
     }
 
-   
+    
+    const dataProfile = await addressApi({ id: auth.partnerId });
+    console.log('ini adalah', dataProfile);
+
 
     // if (isUpdated?.id) {
     // if (address?.id && auth.company ? isUpdated?.id : true) {
-- 
cgit v1.2.3

