From c527fa8fe9772b456a11a52f52c7ee091504d734 Mon Sep 17 00:00:00 2001
From: "Indoteknik ." <andrifebriyadiputra@gmail.com>
Date: Tue, 12 Aug 2025 17:31:31 +0700
Subject: (andri) fix switch account ke bisnis

---
 src/lib/auth/components/SwitchAccount.jsx | 67 +++++++++++++++++++++----------
 1 file changed, 45 insertions(+), 22 deletions(-)

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

diff --git a/src/lib/auth/components/SwitchAccount.jsx b/src/lib/auth/components/SwitchAccount.jsx
index 46e57348..80bc584f 100644
--- a/src/lib/auth/components/SwitchAccount.jsx
+++ b/src/lib/auth/components/SwitchAccount.jsx
@@ -13,6 +13,7 @@ import { useRegisterStore } from '~/modules/register/stores/useRegisterStore.ts'
 import { registerUser } from '~/services/auth';
 import { useMutation } from 'react-query';
 import { isValid } from 'zod';
+import Spinner from "@/core/components/elements/Spinner/LogoSpinner";
 import useDevice from '@/core/hooks/useDevice';
 import BottomPopup from '@/core/components/elements/Popup/BottomPopup';
 const SwitchAccount = ({ company_type }) => {
@@ -27,6 +28,8 @@ const SwitchAccount = ({ company_type }) => {
   const [selectedValue, setSelectedValue] = useState('PKP');
   const [buttonSubmitClick, setButtonSubmitClick] = useState(false);
   const [changeConfirmation, setChangeConfirmation] = useState(false);
+  const [isLoading, setIsLoading] = useState(false);
+  const [isLoadingPopup, setIsLoadingPopup] = useState(false);
   const { register, setValue, handleSubmit } = useForm({
     defaultValues: {
       email: '',
@@ -131,35 +134,44 @@ const SwitchAccount = ({ company_type }) => {
       setIsPKP(false);
     }
   };
-  const onSubmitHandler = async (values) => {
-    toast.loading('Mengubah status akun...');
+  const onSubmitHandler = async () => {
+    setChangeConfirmation(false); // Tutup popup konfirmasi
+    setIsLoadingPopup(true); // Munculkan popup loading
+
     updateForm('parent_id', `${auth.parentId}`);
-    setChangeConfirmation(false);
-    // let data = { ...form, id: `${auth.partnerId}` };
+
     const data = form;
     if (!isFormValid) {
       setNotValid(true);
       setButtonSubmitClick(!buttonSubmitClick);
+      toast.error('Form belum valid. Mohon periksa kembali input Anda.');
+      setIsLoadingPopup(false);
       return;
-    } else {
-      setButtonSubmitClick(!buttonSubmitClick);
-      setNotValid(false);
     }
-    // if (!values.password) delete data.password;
-    const isUpdated = await switchAccountApi({ data });
 
-    if (isUpdated?.switch === 'Pending') {
-      // setAuth(isUpdated.user);
-      // setValue('password', '');
-      toast.success('Berhasil mengubah akun', { duration: 1500 });
-      setTimeout(() => {
-        window.location.reload();
-      }, 1500);
-      return;
+    try {
+      const isUpdated = await switchAccountApi({ data });
+
+      if (isUpdated?.switch === 'Pending') {
+        toast.success('Berhasil mengajukan ubah akun', { duration: 1500 });
+        if (typeof window !== 'undefined') {
+          localStorage.setItem('autoCheckProfile', 'true');
+        }
+        setTimeout(() => {
+          window.location.reload();
+        }, 1500);
+      } else {
+        toast.error('Gagal mengubah akun. Silakan coba lagi nanti.');
+        setIsLoadingPopup(false);
+      }
+    } catch (error) {
+      console.error(error);
+      toast.error('Terjadi kesalahan saat menghubungi server.');
+      setIsLoadingPopup(false);
     }
-    toast.error('Terjadi kesalahan internal');
   };
 
+
   const onSubmitHandlerCancel = async (values) => {
     window.location.reload();
   };
@@ -172,26 +184,37 @@ const SwitchAccount = ({ company_type }) => {
         title='Ubah profil Bisnis'
       >
         <div className='leading-7 text-gray_r-12/80'>
-          Anda yakin akan merubah profil bisnis anda dari INDIVIDU menjadi{' '}
-          {selectedValue}?
+          Anda yakin akan merubah profil bisnis anda dari INDIVIDU menjadi {selectedValue}?
         </div>
         <div className='flex mt-6 gap-x-4 md:justify-end'>
           <button
-            className='btn-solid-red flex-1 md:flex-none'
+            className='btn-solid-red flex-1 md:flex-none flex items-center justify-center'
             type='button'
             onClick={onSubmitHandler}
+            disabled={isLoading}
           >
-            Ya, Ubah
+            {isLoading && <Spinner className="w-4 h-4 mr-2 text-white" />}
+            {isLoading ? 'Menyimpan...' : 'Ya, Ubah'}
           </button>
           <button
             className='btn-light flex-1 md:flex-none'
             type='button'
             onClick={() => setChangeConfirmation(false)}
+            disabled={isLoading}
           >
             Batal
           </button>
         </div>
       </BottomPopup>
+      <BottomPopup active={isLoadingPopup} close="">
+        <div className="leading-7 text-gray_r-12/80 flex justify-center">
+          Mengubah status akun...
+        </div>
+        <div className="container flex justify-center my-4">
+          <Spinner width={48} height={48} />
+        </div>
+      </BottomPopup>
+
       {/* <div type='button' className='ml-4 flex items-center text-left w-full'>
         <div
           className={`flex ${
-- 
cgit v1.2.3

