From ab0782b5cf7b65930b0b40528b9205f3f0dfc3a0 Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Thu, 12 Sep 2024 10:11:02 +0700
Subject: <iman> update switch account

---
 src/lib/auth/components/CompanyProfile.jsx | 168 ++++++++++++++++++-----------
 1 file changed, 103 insertions(+), 65 deletions(-)

(limited to 'src/lib/auth/components/CompanyProfile.jsx')

diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx
index e37decc9..20be6829 100644
--- a/src/lib/auth/components/CompanyProfile.jsx
+++ b/src/lib/auth/components/CompanyProfile.jsx
@@ -1,18 +1,18 @@
-import odooApi from '@/core/api/odooApi'
-import HookFormSelect from '@/core/components/elements/Select/HookFormSelect'
-import useAuth from '@/core/hooks/useAuth'
-import addressApi from '@/lib/address/api/addressApi'
-import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline'
-import { useEffect, useState } from 'react'
-import { Controller, useForm } from 'react-hook-form'
-import { toast } from 'react-hot-toast'
-import BottomPopup from '@/core/components/elements/Popup/BottomPopup'
+import odooApi from '@/core/api/odooApi';
+import HookFormSelect from '@/core/components/elements/Select/HookFormSelect';
+import useAuth from '@/core/hooks/useAuth';
+import addressApi from '@/lib/address/api/addressApi';
+import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline';
+import { useEffect, useState } from 'react';
+import { Controller, useForm } from 'react-hook-form';
+import { toast } from 'react-hot-toast';
+import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
 
 const CompanyProfile = () => {
-  const [changeConfirmation, setChangeConfirmation] = useState(false)
-  const auth = useAuth()
-  const [isOpen, setIsOpen] = useState(false)
-  const toggle = () => setIsOpen(!isOpen)
+  const [changeConfirmation, setChangeConfirmation] = useState(false);
+  const auth = useAuth();
+  const [isOpen, setIsOpen] = useState(false);
+  const toggle = () => setIsOpen(!isOpen);
   const { register, setValue, control, handleSubmit } = useForm({
     defaultValues: {
       industry: '',
@@ -20,64 +20,77 @@ const CompanyProfile = () => {
       name: '',
       taxName: '',
       npwp: '',
-      alamat_wajib_pajak:'',
-    }
-  })
+      alamat_wajib_pajak: '',
+    },
+  });
 
-  const [industries, setIndustries] = useState([])
+  const [industries, setIndustries] = useState([]);
   useEffect(() => {
     const loadIndustries = async () => {
-      const dataIndustries = await odooApi('GET', '/api/v1/partner/industry')
-      setIndustries(dataIndustries?.map((o) => ({ value: o.id, label: o.name })))
-    }
-    loadIndustries()
-  }, [])
+      const dataIndustries = await odooApi('GET', '/api/v1/partner/industry');
+      setIndustries(
+        dataIndustries?.map((o) => ({ value: o.id, label: o.name }))
+      );
+    };
+    loadIndustries();
+  }, []);
 
-  const [companyTypes, setCompanyTypes] = useState([])
+  const [companyTypes, setCompanyTypes] = useState([]);
   useEffect(() => {
     const loadCompanyTypes = async () => {
-      const dataCompanyTypes = await odooApi('GET', '/api/v1/partner/company_type')
-      setCompanyTypes(dataCompanyTypes?.map((o) => ({ value: o.id, label: o.name })))
-    }
-    loadCompanyTypes()
-  }, [])
+      const dataCompanyTypes = await odooApi(
+        'GET',
+        '/api/v1/partner/company_type'
+      );
+      setCompanyTypes(
+        dataCompanyTypes?.map((o) => ({ value: o.id, label: o.name }))
+      );
+    };
+    loadCompanyTypes();
+  }, []);
 
   useEffect(() => {
     const loadProfile = async () => {
-      const dataProfile = await addressApi({ id: auth.parentId })
-      setValue('name', dataProfile.name)
-      setValue('industry', dataProfile.industryId)
-      setValue('companyType', dataProfile.companyTypeId)
-      setValue('taxName', dataProfile.taxName)
-      setValue('npwp', dataProfile.npwp)
-      setValue('alamat_wajib_pajak', dataProfile.alamatWajibPajak)
-    }
-    if (auth) loadProfile()
-  }, [auth, setValue])
+      const dataProfile = await addressApi({
+        id: auth.parentId ? auth.parentId : auth.parent_id,
+      });
+      setValue('name', dataProfile.name);
+      setValue('industry', dataProfile.industryId);
+      setValue('companyType', dataProfile.companyTypeId);
+      setValue('taxName', dataProfile.taxName);
+      setValue('npwp', dataProfile.npwp);
+      setValue('alamat_wajib_pajak', dataProfile.alamatWajibPajak);
+    };
+    if (auth) loadProfile();
+  }, [auth, setValue]);
 
   const onSubmitHandler = async (values) => {
-    if(changeConfirmation){
+    if (changeConfirmation) {
       const data = {
         ...values,
-        id_user:auth.partnerId,
+        id_user: auth.partnerId,
         company_type_id: values.companyType,
         industry_id: values.industry,
         tax_name: values.taxName,
-        alamat_lengkap_text:values.alamat_wajib_pajak
-      }
-      const isUpdated = await odooApi('PUT', `/api/v1/partner/${auth.parentId}`, data)
+        alamat_lengkap_text: values.alamat_wajib_pajak,
+      };
+      const isUpdated = await odooApi(
+        'PUT',
+        `/api/v1/partner/${auth.parentId}`,
+        data
+      );
       if (isUpdated?.id) {
-        toast.success('Berhasil mengubah profil', { duration: 1500 })
-        return
+        toast.success('Berhasil mengubah profil', { duration: 1500 });
+        return;
       }
-      toast.error('Terjadi kesalahan internal')
+      toast.error('Terjadi kesalahan internal');
     }
-  }
+  };
 
   const handleConfirmSubmit = () => {
-    setChangeConfirmation(false)
-    handleSubmit(onSubmitHandler)()
-  }
+    setChangeConfirmation(false);
+    handleSubmit(onSubmitHandler)();
+  };
 
   return (
     <>
@@ -106,11 +119,16 @@ const CompanyProfile = () => {
           </button>
         </div>
       </BottomPopup>
-      <button type='button' onClick={toggle} className='p-4 flex items-center text-left w-full'>
+      <button
+        type='button'
+        onClick={toggle}
+        className='p-4 flex items-center text-left w-full'
+      >
         <div>
           <div className='font-semibold mb-2'>Informasi Usaha</div>
           <div className='text-gray_r-11'>
-            Dibawah ini adalah data usaha yang anda masukkan, periksa kembali data usaha anda.
+            Dibawah ini adalah data usaha yang anda masukkan, periksa kembali
+            data usaha anda.
           </div>
         </div>
         <div className='ml-auto p-2 bg-gray_r-3 rounded'>
@@ -120,17 +138,22 @@ const CompanyProfile = () => {
       </button>
 
       {isOpen && (
-        <form className='p-4 border-t border-gray_r-6' onSubmit={(e) => {
-          e.preventDefault()
-          setChangeConfirmation(true) 
-        }}>
+        <form
+          className='p-4 border-t border-gray_r-6'
+          onSubmit={(e) => {
+            e.preventDefault();
+            setChangeConfirmation(true);
+          }}
+        >
           <div className='grid grid-cols-1 sm:grid-cols-2 gap-4'>
             <div>
               <label className='block mb-3'>Klasifikasi Jenis Usaha</label>
               <Controller
                 name='industry'
                 control={control}
-                render={(props) => <HookFormSelect {...props} options={industries} />}
+                render={(props) => (
+                  <HookFormSelect {...props} options={industries} />
+                )}
               />
             </div>
             <div className='flex flex-wrap'>
@@ -139,7 +162,9 @@ const CompanyProfile = () => {
                 <Controller
                   name='companyType'
                   control={control}
-                  render={(props) => <HookFormSelect {...props} options={companyTypes} />}
+                  render={(props) => (
+                    <HookFormSelect {...props} options={companyTypes} />
+                  )}
                 />
               </div>
               <div className='w-9/12 pl-1'>
@@ -153,14 +178,27 @@ const CompanyProfile = () => {
             </div>
             <div>
               <label>Nama Wajib Pajak</label>
-              <input {...register('taxName')} type='text' className='form-input mt-3' />
+              <input
+                {...register('taxName')}
+                type='text'
+                className='form-input mt-3'
+              />
             </div>
             <div>
               <label>Nomor NPWP</label>
-              <input {...register('npwp')} type='text' className='form-input mt-3' />
-            </div><div>
+              <input
+                {...register('npwp')}
+                type='text'
+                className='form-input mt-3'
+              />
+            </div>
+            <div>
               <label>Alamat Wajib Pajak</label>
-              <input {...register('alamat_wajib_pajak')} type='text' className='form-input mt-3' />
+              <input
+                {...register('alamat_wajib_pajak')}
+                type='text'
+                className='form-input mt-3'
+              />
             </div>
           </div>
           <button type='submit' className='btn-yellow w-full mt-6'>
@@ -169,7 +207,7 @@ const CompanyProfile = () => {
         </form>
       )}
     </>
-  )
-}
+  );
+};
 
-export default CompanyProfile
+export default CompanyProfile;
-- 
cgit v1.2.3

