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(false) const toggle = () => setIsOpen(!isOpen) const { register, setValue, handleSubmit } = useForm({ defaultValues: { email: '', name: '', mobile: '', 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 onSubmitHandler = async (values) => { let data = values if (!values.password) delete data.password const isUpdated = await editPersonalProfileApi({ data }) console.log(isUpdated) if (isUpdated?.user) { setAuth(isUpdated.user) setValue('password', '') setIsOpen(false) toast.success('Berhasil mengubah profil', { duration: 1500 }) return } toast.error('Terjadi kesalahan internal') } return ( <> {isOpen && (
)} ) } export default PersonalProfile