diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2025-08-15 10:23:38 +0000 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2025-08-15 10:23:38 +0000 |
| commit | 5ef267f76486ed590c00d3c37cccde5c713491cd (patch) | |
| tree | 4a3cf919743ac45d4ecb8d01fdf7ef51685ca16a /src/lib/auth/components | |
| parent | 4d7e2f7b6e92cbe700e5b8fcea006b3d70ed81f9 (diff) | |
| parent | b1bdfbb9f780a1a1305e02c4b9c338b98c7a4556 (diff) | |
Merged in tempo-v2 (pull request #442)
Tempo v2
Diffstat (limited to 'src/lib/auth/components')
| -rw-r--r-- | src/lib/auth/components/SwitchAccount.jsx | 71 |
1 files changed, 48 insertions, 23 deletions
diff --git a/src/lib/auth/components/SwitchAccount.jsx b/src/lib/auth/components/SwitchAccount.jsx index 46e57348..840758c9 100644 --- a/src/lib/auth/components/SwitchAccount.jsx +++ b/src/lib/auth/components/SwitchAccount.jsx @@ -13,9 +13,10 @@ 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 }) => { +const SwitchAccount = ({ company_type, setIsAprove, setUbahAkun }) => { const { isDesktop, isMobile } = useDevice(); const auth = useAuth(); const [isOpen, setIsOpen] = useState(true); @@ -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,46 @@ 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(() => { + setIsAprove('pending'); + setUbahAkun('pending'); + window.location.reload(); + }, 1500); + } else { + toast.error('Gagal mengubah akun. Silakan coba lagi nanti atau hubungi admin jika masalah tetap terjadi.'); + setIsLoadingPopup(false); + } + } catch (error) { + console.error(error); + toast.error('Terjadi kesalahan saat menghubungi server, silahkan cek internet Anda atau hubungi admin Indoteknik.'); + setIsLoadingPopup(false); } - toast.error('Terjadi kesalahan internal'); }; + const onSubmitHandlerCancel = async (values) => { window.location.reload(); }; @@ -172,26 +186,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 ${ |
