From c527fa8fe9772b456a11a52f52c7ee091504d734 Mon Sep 17 00:00:00 2001 From: "Indoteknik ." Date: Tue, 12 Aug 2025 17:31:31 +0700 Subject: (andri) fix switch account ke bisnis --- src/lib/auth/api/editPersonalProfileApi.js | 2 +- src/lib/auth/api/switchAccountApi.js | 2 +- src/lib/auth/components/SwitchAccount.jsx | 67 +++++++++++++++------- .../pengajuan-tempo/api/createPengajuanTempoApi.js | 2 +- src/pages/my/profile.jsx | 6 +- 5 files changed, 50 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/lib/auth/api/editPersonalProfileApi.js b/src/lib/auth/api/editPersonalProfileApi.js index 39cd44c1..90bda114 100644 --- a/src/lib/auth/api/editPersonalProfileApi.js +++ b/src/lib/auth/api/editPersonalProfileApi.js @@ -3,7 +3,7 @@ import { getAuth } from '@/core/utils/auth' const editPersonalProfileApi = async ({ data }) => { const auth = getAuth() - const dataProfile = await odooApi('PUT', `/api/v1/user/${auth.id}`, data) + const dataProfile = await odooApi('POST', `/api/v1/user/${auth.id}`, data) return dataProfile } diff --git a/src/lib/auth/api/switchAccountApi.js b/src/lib/auth/api/switchAccountApi.js index 79ca2553..f62693f6 100644 --- a/src/lib/auth/api/switchAccountApi.js +++ b/src/lib/auth/api/switchAccountApi.js @@ -4,7 +4,7 @@ import { getAuth } from '@/core/utils/auth'; const switchAccountApi = async ({ data }) => { const auth = getAuth(); const switchAccount = await odooApi( - 'PUT', + 'POST', `/api/v1/user/${auth.partnerId}/switch`, data ); 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' >
- Anda yakin akan merubah profil bisnis anda dari INDIVIDU menjadi{' '} - {selectedValue}? + Anda yakin akan merubah profil bisnis anda dari INDIVIDU menjadi {selectedValue}?
+ +
+ Mengubah status akun... +
+
+ +
+
+ {/*
{ return dataPengajuanTempo; }; -export default createPengajuanTempoApi; +export default createPengajuanTempoApi; \ No newline at end of file diff --git a/src/pages/my/profile.jsx b/src/pages/my/profile.jsx index 663ff97a..887489e0 100644 --- a/src/pages/my/profile.jsx +++ b/src/pages/my/profile.jsx @@ -99,8 +99,7 @@ export default function Profile() { - {auth?.company || - (!ubahAkun && ( + {auth?.company || ((isChecked || (!ubahAkun && ubahAkun !== 'pending')) && (
- {auth?.company || - (!ubahAkun && ( + {auth?.company || ((isChecked || (!ubahAkun && ubahAkun !== 'pending')) && (