From 752e55686dfee0d536f9e4e128336e91681ba794 Mon Sep 17 00:00:00 2001 From: it-fixcomart Date: Wed, 11 Sep 2024 17:07:49 +0700 Subject: update switch account --- src/lib/auth/api/switchAccountApi.js | 14 ++++ src/lib/auth/api/switchAccountProgresApi.js | 15 ++++ src/lib/auth/components/PersonalProfile.jsx | 104 +++++++++++++++++----------- src/lib/auth/components/SwitchAccount.jsx | 37 ++++++---- src/pages/my/profile.jsx | 17 ++++- 5 files changed, 132 insertions(+), 55 deletions(-) create mode 100644 src/lib/auth/api/switchAccountApi.js create mode 100644 src/lib/auth/api/switchAccountProgresApi.js (limited to 'src') diff --git a/src/lib/auth/api/switchAccountApi.js b/src/lib/auth/api/switchAccountApi.js new file mode 100644 index 00000000..9b772d20 --- /dev/null +++ b/src/lib/auth/api/switchAccountApi.js @@ -0,0 +1,14 @@ +import odooApi from '@/core/api/odooApi'; +import { getAuth } from '@/core/utils/auth'; + +const switchAccountApi = async ({ data }) => { + const auth = getAuth(); + const switchAccount = await odooApi( + 'PUT', + `/api/v1/user/${auth.partner_id}/switch`, + data + ); + return switchAccount; +}; + +export default switchAccountApi; diff --git a/src/lib/auth/api/switchAccountProgresApi.js b/src/lib/auth/api/switchAccountProgresApi.js new file mode 100644 index 00000000..4005ce3c --- /dev/null +++ b/src/lib/auth/api/switchAccountProgresApi.js @@ -0,0 +1,15 @@ +import odooApi from '@/core/api/odooApi'; +import { getAuth } from '@/core/utils/auth'; + +const switchAccountProgresApi = async () => { + const auth = getAuth(); + console.log('auth', auth); + const switchAccount = await odooApi( + 'PUT', + `/api/v1/user/${auth.partner_id}/switch_progres` + ); + console.log('switchAccount', switchAccount); + return switchAccount; +}; + +export default switchAccountProgresApi; diff --git a/src/lib/auth/components/PersonalProfile.jsx b/src/lib/auth/components/PersonalProfile.jsx index b9fb3f5f..7ef3af2d 100644 --- a/src/lib/auth/components/PersonalProfile.jsx +++ b/src/lib/auth/components/PersonalProfile.jsx @@ -1,56 +1,61 @@ -import useAuth from '@/core/hooks/useAuth' -import { setAuth } from '@/core/utils/auth' -import addressApi from '@/lib/address/api/addressApi' -import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline' -import { useEffect, useState } from 'react' -import { useForm } from 'react-hook-form' -import { toast } from 'react-hot-toast' -import editPersonalProfileApi from '../api/editPersonalProfileApi' +import useAuth from '@/core/hooks/useAuth'; +import { setAuth } from '@/core/utils/auth'; +import addressApi from '@/lib/address/api/addressApi'; +import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline'; +import { useEffect, useState } from 'react'; +import { useForm } from 'react-hook-form'; +import { toast } from 'react-hot-toast'; +import editPersonalProfileApi from '../api/editPersonalProfileApi'; const PersonalProfile = () => { - const auth = useAuth() - const [isOpen, setIsOpen] = useState(true) - const toggle = () => setIsOpen(!isOpen) + const auth = useAuth(); + const [isOpen, setIsOpen] = useState(true); + const toggle = () => setIsOpen(!isOpen); const { register, setValue, handleSubmit } = useForm({ defaultValues: { email: '', name: '', mobile: '', - password: '' - } - }) + password: '', + }, + }); useEffect(() => { const loadProfile = async () => { - const dataProfile = await addressApi({ id: auth.partnerId }) - setValue('email', dataProfile?.email) - setValue('name', dataProfile?.name) - setValue('mobile', dataProfile?.mobile) - } - if (auth) loadProfile() - }, [auth, setValue]) + const dataProfile = await addressApi({ id: auth.partner_id }); + setValue('email', dataProfile?.email); + setValue('name', dataProfile?.name); + setValue('mobile', dataProfile?.mobile); + }; + if (auth) loadProfile(); + }, [auth, setValue]); const onSubmitHandler = async (values) => { - let data = values - if (!values.password) delete data.password - const isUpdated = await editPersonalProfileApi({ data }) + let data = values; + if (!values.password) delete data.password; + const isUpdated = await editPersonalProfileApi({ data }); if (isUpdated?.user) { - setAuth(isUpdated.user) - setValue('password', '') - toast.success('Berhasil mengubah profil', { duration: 1500 }) - return + setAuth(isUpdated.user); + setValue('password', ''); + toast.success('Berhasil mengubah profil', { duration: 1500 }); + return; } - toast.error('Terjadi kesalahan internal') - } + toast.error('Terjadi kesalahan internal'); + }; return ( <> - {isOpen && ( -
+
- +
- +
- +
@@ -84,13 +105,16 @@ const PersonalProfile = () => { />
-
)} - ) -} + ); +}; -export default PersonalProfile +export default PersonalProfile; diff --git a/src/lib/auth/components/SwitchAccount.jsx b/src/lib/auth/components/SwitchAccount.jsx index 71571bd7..3146a3da 100644 --- a/src/lib/auth/components/SwitchAccount.jsx +++ b/src/lib/auth/components/SwitchAccount.jsx @@ -5,7 +5,7 @@ import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline'; import { useEffect, useState, useMemo } from 'react'; import { useForm } from 'react-hook-form'; import { toast } from 'react-hot-toast'; -import editPersonalProfileApi from '../api/editPersonalProfileApi'; +import switchAccountApi from '../api/switchAccountApi'; import FormBisnis from '~/modules/register/components/FormBisnis.tsx'; import RegistrasiBisnis from '~/modules/register/components/RegistrasiBisnis.tsx'; import { Radio, RadioGroup, Stack, Divider, Button } from '@chakra-ui/react'; @@ -68,6 +68,14 @@ const SwitchAccount = () => { } }, [isTerdaftar]); + useEffect(() => { + updateForm('name', '-'); + updateForm('email', 'example@mail.com'); + updateForm('password', 'example@mail.com'); + updateForm('phone', '081234567890'); + validate(); + }, []); + const handleChangeBisnis = (value) => { setSelectedValueBisnis(value); if (value === 'true') { @@ -89,26 +97,31 @@ const SwitchAccount = () => { setIsPKP(false); } }; - + console.log('auth', auth); const onSubmitHandler = async (values) => { - let data = values; + let data = form; console.log('data', data); if (!isFormValid) { + console.log('masih ada yang belum valid'); setNotValid(true); return; } else { setNotValid(false); } // if (!values.password) delete data.password; - // const isUpdated = await editPersonalProfileApi({ data }); + const isUpdated = await switchAccountApi({ data }); + console.log('isupdate', isUpdated); - // if (isUpdated?.user) { - // setAuth(isUpdated.user); - // setValue('password', ''); - // toast.success('Berhasil mengubah profil', { duration: 1500 }); - // return; - // } - // toast.error('Terjadi kesalahan internal'); + if (isUpdated.switch === 'Pending') { + // setAuth(isUpdated.user); + // setValue('password', ''); + toast.success('Berhasil mengubah akun', { duration: 1500 }); + setTimeout(() => { + window.location.reload(); + }, 1500); + return; + } + toast.error('Terjadi kesalahan internal'); }; return ( @@ -171,7 +184,7 @@ const SwitchAccount = () => { /> )} -
+
- {!auth?.parentId && ( + {!auth?.parentId && !isAprove && (