diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/auth/api/switchAccountApi.js | 14 | ||||
| -rw-r--r-- | src/lib/auth/api/switchAccountProgresApi.js | 13 | ||||
| -rw-r--r-- | src/lib/auth/components/CompanyProfile.jsx | 4 | ||||
| -rw-r--r-- | src/lib/auth/components/PersonalProfile.jsx | 106 | ||||
| -rw-r--r-- | src/lib/auth/components/StatusSwitchAccount.jsx | 5 | ||||
| -rw-r--r-- | src/lib/auth/components/SwitchAccount.jsx | 206 | ||||
| -rw-r--r-- | src/pages/my/profile.jsx | 107 |
7 files changed, 411 insertions, 44 deletions
diff --git a/src/lib/auth/api/switchAccountApi.js b/src/lib/auth/api/switchAccountApi.js new file mode 100644 index 00000000..79ca2553 --- /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.partnerId}/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..6289a5dd --- /dev/null +++ b/src/lib/auth/api/switchAccountProgresApi.js @@ -0,0 +1,13 @@ +import odooApi from '@/core/api/odooApi'; +import { getAuth } from '@/core/utils/auth'; + +const switchAccountProgresApi = async () => { + const auth = getAuth(); + const switchAccount = await odooApi( + 'GET', + `/api/v1/user/${auth.partnerId}/switch_progres` + ); + return switchAccount; +}; + +export default switchAccountProgresApi; diff --git a/src/lib/auth/components/CompanyProfile.jsx b/src/lib/auth/components/CompanyProfile.jsx index 7bda992f..7bebb02f 100644 --- a/src/lib/auth/components/CompanyProfile.jsx +++ b/src/lib/auth/components/CompanyProfile.jsx @@ -53,7 +53,9 @@ const CompanyProfile = () => { useEffect(() => { const loadProfile = async () => { - const dataProfile = await addressApi({ id: auth.parentId }); + const dataProfile = await addressApi({ + id: auth.parentId ? auth.parentId : auth.parent_id, + }); setValue('name', dataProfile.name); setValue('industry', dataProfile.industryId); setValue('companyType', dataProfile.companyTypeId); diff --git a/src/lib/auth/components/PersonalProfile.jsx b/src/lib/auth/components/PersonalProfile.jsx index b9fb3f5f..727785bf 100644 --- a/src/lib/auth/components/PersonalProfile.jsx +++ b/src/lib/auth/components/PersonalProfile.jsx @@ -1,56 +1,63 @@ -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.partnerId ? auth.partnerId : 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 ( <> - <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 Akun</div> <div className='text-gray_r-11'> - Dibawah ini adalah data diri yang anda masukan, periksa kembali data diri anda + Dibawah ini adalah data diri yang anda masukan, periksa kembali data + diri anda </div> </div> <div className='ml-auto p-2 bg-gray_r-3 rounded'> @@ -60,19 +67,35 @@ const PersonalProfile = () => { </button> {isOpen && ( - <form className='p-4 border-t border-gray_r-6' onSubmit={handleSubmit(onSubmitHandler)}> + <form + className='p-4 border-t border-gray_r-6' + onSubmit={handleSubmit(onSubmitHandler)} + > <div className='grid grid-cols-1 sm:grid-cols-2 gap-4'> <div> <label>Email</label> - <input {...register('email')} type='text' disabled className='form-input mt-3' /> + <input + {...register('email')} + type='text' + disabled + className='form-input mt-3' + /> </div> <div> <label>Nama Lengkap</label> - <input {...register('name')} type='text' className='form-input mt-3' /> + <input + {...register('name')} + type='text' + className='form-input mt-3' + /> </div> <div> <label>No. Handphone</label> - <input {...register('mobile')} type='tel' className='form-input mt-3' /> + <input + {...register('mobile')} + type='tel' + className='form-input mt-3' + /> </div> <div> <label>Kata Sandi</label> @@ -84,13 +107,16 @@ const PersonalProfile = () => { /> </div> </div> - <button type='submit' className='btn-yellow w-full sm:w-fit sm:ml-auto mt-6'> + <button + type='submit' + className='btn-yellow w-full sm:w-fit sm:ml-auto mt-6' + > Simpan </button> </form> )} </> - ) -} + ); +}; -export default PersonalProfile +export default PersonalProfile; diff --git a/src/lib/auth/components/StatusSwitchAccount.jsx b/src/lib/auth/components/StatusSwitchAccount.jsx new file mode 100644 index 00000000..73316616 --- /dev/null +++ b/src/lib/auth/components/StatusSwitchAccount.jsx @@ -0,0 +1,5 @@ +const StatusSwitchAccount = ({ status }) => { + return <>Perpindahan akun anda masih {status}</>; +}; + +export default StatusSwitchAccount; diff --git a/src/lib/auth/components/SwitchAccount.jsx b/src/lib/auth/components/SwitchAccount.jsx new file mode 100644 index 00000000..18c6076f --- /dev/null +++ b/src/lib/auth/components/SwitchAccount.jsx @@ -0,0 +1,206 @@ +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, useMemo } from 'react'; +import { useForm } from 'react-hook-form'; +import { toast } from 'react-hot-toast'; +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'; +import { useRegisterStore } from '~/modules/register/stores/useRegisterStore.ts'; +import { registerUser } from '~/services/auth'; +import { useMutation } from 'react-query'; +import { isValid } from 'zod'; +import useDevice from '@/core/hooks/useDevice'; +const SwitchAccount = () => { + const { isDesktop, isMobile } = useDevice(); + const auth = useAuth(); + const [isOpen, setIsOpen] = useState(true); + const toggle = () => setIsOpen(!isOpen); + const [isPKP, setIsPKP] = useState(true); + const [isTerdaftar, setIsTerdaftar] = useState(false); + const [isChecked, setIsChecked] = useState(false); + const [selectedValueBisnis, setSelectedValueBisnis] = useState('false'); + const [selectedValue, setSelectedValue] = useState('PKP'); + const [buttonSubmitClick, setButtonSubmitClick] = useState(false); + const { register, setValue, handleSubmit } = useForm({ + defaultValues: { + email: '', + name: '', + phone: '', + password: '', + }, + }); + const mutation = useMutation({ + mutationFn: (data) => registerUser(data), + }); + const [notValid, setNotValid] = useState(false); + const { form, isCheckedTNC, isValidCaptcha, errors, validate, updateForm } = + useRegisterStore(); + const isFormValid = useMemo(() => Object.keys(errors).length === 0, [errors]); + useEffect(() => { + const loadProfile = async () => { + const dataProfile = await addressApi({ id: auth.partnerId }); + setValue('email', dataProfile?.email); + setValue('name', dataProfile?.name); + setValue('phone', dataProfile?.phone); + }; + if (auth) loadProfile(); + }, [auth, setValue]); + + useEffect(() => { + if (selectedValue === 'PKP') { + updateForm('is_pkp', 'true'); + validate(); + } else { + updateForm('is_pkp', 'false'); + validate(); + } + }, [selectedValue]); + + useEffect(() => { + if (isTerdaftar) { + updateForm('is_terdaftar', 'true'); + validate(); + } else { + updateForm('is_terdaftar', 'false'); + validate(); + } + }, [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') { + validate(); + setIsTerdaftar(true); + } else { + validate(); + setIsTerdaftar(false); + } + }; + const handleChange = (value) => { + setSelectedValue(value); + if (value === 'PKP') { + validate(); + setIsPKP(true); + } else { + validate(); + setIsPKP(false); + setIsPKP(false); + } + }; + const onSubmitHandler = async (values) => { + // let data = { ...form, id: `${auth.partnerId}` }; + const data = form; + if (!isFormValid) { + setNotValid(true); + setButtonSubmitClick(!buttonSubmitClick); + 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; + } + toast.error('Terjadi kesalahan internal'); + }; + + return ( + <> + <button + type='button' + onClick={toggle} + className='p-4 flex items-center text-left w-full' + > + <div + className={`flex ${ + isDesktop ? 'flex-row' : 'flex-col gap-y-2' + } items-start justify-start bg-slate-50`} + > + <div className='flex font-semibold mr-2'>Informasi Bisnis</div> + <div className='text-red-500 text-xs'> + *Perubahan akun tidak dapat diubah kembali + </div> + </div> + <div className='ml-auto p-2 bg-gray_r-3 rounded'> + {!isOpen && <ChevronDownIcon className='w-6' />} + {isOpen && <ChevronUpIcon className='w-6' />} + </div> + </button> + + {isOpen && ( + <div className='p-4'> + <div> + <p className='text-black font-bold mb-2'> + Bisnis Terdaftar di Indoteknik? + </p> + <RadioGroup + onChange={handleChangeBisnis} + value={selectedValueBisnis} + > + <Stack direction='row'> + <Radio colorScheme='red' value='true'> + Sudah Terdaftar + </Radio> + <Radio colorScheme='red' value='false' className='ml-2'> + Belum Terdaftar + </Radio> + </Stack> + </RadioGroup> + </div> + <div className='mt-4'> + <p className='text-black font-bold mb-2'>Tipe Bisnis</p> + <RadioGroup onChange={handleChange} value={selectedValue}> + <Stack direction='row' className='font-bold'> + <Radio colorScheme='red' value='PKP'> + PKP + </Radio> + <Radio colorScheme='red' value='Non-PKP' className='ml-4'> + Non-PKP + </Radio> + </Stack> + </RadioGroup> + </div> + <FormBisnis + type={isDesktop ? 'profil' : 'bisnis'} + required={isTerdaftar} + isPKP={isPKP} + chekValid={notValid} + buttonSubmitClick={buttonSubmitClick} + /> + <div className='flex justify-end mb-4 mr-4'> + <button + type='submit' + onClick={onSubmitHandler} + className='btn-yellow w-full sm:w-fit sm:ml-auto mt-6' + > + {mutation.isLoading ? 'Loading...' : 'Simpan Perubahan'} + </button> + </div> + </div> + )} + </> + ); +}; + +export default SwitchAccount; diff --git a/src/pages/my/profile.jsx b/src/pages/my/profile.jsx index 7cf1bcbb..f6063ff2 100644 --- a/src/pages/my/profile.jsx +++ b/src/pages/my/profile.jsx @@ -5,19 +5,96 @@ import DesktopView from '@/core/components/views/DesktopView'; import MobileView from '@/core/components/views/MobileView'; import useAuth from '@/core/hooks/useAuth'; import CompanyProfile from '@/lib/auth/components/CompanyProfile'; +import SwitchAccount from '@/lib/auth/components/SwitchAccount'; import IsAuth from '@/lib/auth/components/IsAuth'; import Menu from '@/lib/auth/components/Menu'; import PersonalProfile from '@/lib/auth/components/PersonalProfile'; -import Seo from '@/core/components/Seo'; - +import StatusSwitchAccount from '@/lib/auth/components/StatusSwitchAccount'; +import { Checkbox } from '@chakra-ui/react'; +import { useState, useEffect } from 'react'; +import switchAccountProgresApi from '@/lib/auth/api/switchAccountProgresApi.js'; +import BottomPopup from '@/core/components/elements/Popup/BottomPopup'; export default function Profile() { const auth = useAuth(); + const [isChecked, setIsChecked] = useState(false); + const [ubahAkun, setUbahAkun] = useState(false); + const [isAprove, setIsAprove] = useState('unknown'); + const [changeConfirmation, setChangeConfirmation] = useState(false); + const handleChange = async () => { + if (isChecked) { + setIsChecked(!isChecked); + } else { + setChangeConfirmation(true); + } + }; + useEffect(() => { + const loadPromo = async () => { + const progresSwitchAccount = await switchAccountProgresApi(); + if (progresSwitchAccount) { + setIsAprove(progresSwitchAccount.status); + setUbahAkun(progresSwitchAccount.status === 'unknown'); + } + }; + loadPromo(); + }, []); + const handleConfirmSubmit = () => { + setChangeConfirmation(false); + setIsChecked(true); + }; return ( <> - <Seo title='Profile - Indoteknik.com' /> + <BottomPopup + active={changeConfirmation} + close={() => setChangeConfirmation(false)} // Menutup popup + title='Ubah type akun' + > + <div className='leading-7 text-gray_r-12/80'> + Anda akan mengubah type akun anda? + </div> + <div className='flex mt-6 gap-x-4 md:justify-end'> + <button + className='btn-solid-red flex-1 md:flex-none' + type='button' + onClick={handleConfirmSubmit} + > + Yakin + </button> + <button + className='btn-light flex-1 md:flex-none' + type='button' + onClick={() => setChangeConfirmation(false)} + > + Batal + </button> + </div> + </BottomPopup> <IsAuth> <MobileView> <AppLayout title='Akun Saya'> + {!auth?.parentId && ubahAkun && ( + <div className='text-sm p-4 flex items-center'> + <Checkbox + borderColor='gray.600' + colorScheme='red' + size='lg' + isChecked={isChecked} + onChange={handleChange} + /> + <p className='ml-2'>Ubah ke akun bisnis</p> + </div> + )} + {isChecked && ( + <div> + <SwitchAccount /> + <Divider /> + </div> + )} + {!auth?.parentId + ? auth?.parentId + : auth?.parent_id && + isAprove != 'unknown' && ( + <StatusSwitchAccount status={isAprove} /> + )} <PersonalProfile /> <Divider /> {auth?.parentId && <CompanyProfile />} @@ -31,6 +108,30 @@ export default function Profile() { <Menu /> </div> <div className='w-9/12 bg-white border border-gray_r-6 rounded'> + {!auth?.parentId && ubahAkun && ( + <div className='text-sm p-4 flex items-center'> + <Checkbox + borderColor='gray.600' + colorScheme='red' + size='lg' + isChecked={isChecked} + onChange={handleChange} + /> + <p className='ml-2'>Ubah ke akun bisnis</p> + </div> + )} + {isChecked && ( + <div> + <SwitchAccount /> + <Divider /> + </div> + )} + {!auth?.parentId + ? auth?.parentId + : auth?.parent_id && + isAprove != 'unknown' && ( + <StatusSwitchAccount status={isAprove} /> + )} <PersonalProfile /> <Divider /> {auth?.parentId && <CompanyProfile />} |
