import { TextField } from '@mui/material'; import { useForm, Controller } from 'react-hook-form'; import { Button } from '@mui/material'; import { useUserStore } from '../store'; import { useEffect, useRef, useState } from 'react'; import { useShallow } from 'zustand/react/shallow'; import { isObjectNull } from '@/utils/is-null'; import { useLayoutStore } from '@/modules/layout/store'; import UploadOutlined from '@ant-design/icons/UploadOutlined'; import PandaPNG from '@/assets/panda.png'; import { FileUpload } from '../module/FileUpload'; import { useTranslation } from 'react-i18next'; export const Profile = () => { const { t } = useTranslation(); const { control, handleSubmit, setValue, reset, formState, getValues } = useForm({ defaultValues: { username: '', avatar: '', description: '', }, }); const ref = useRef(null); const userStore = useUserStore( useShallow((state) => { return { showEdit: state.showEdit, setShowEdit: state.setShowEdit, formData: state.formData, updateData: state.updateData, setFormData: state.setFormData, updateSelf: state.updateSelf, }; }), ); const [avatar, setAvatar] = useState(''); const layoutStore = useLayoutStore( useShallow((state) => { return { me: state.me, setMe: state.setMe, }; }), ); useEffect(() => { const fromData = layoutStore.me; if (isObjectNull(fromData)) { reset({ username: '', avatar: '', description: '', }); } else { reset({ username: fromData.username, avatar: fromData.avatar || '', description: fromData.description || '', }); } setAvatar(fromData.avatar || ''); }, [layoutStore.me, setValue]); const onChange = (path: string) => { const url = path + '?t=' + new Date().getTime(); setAvatar(url); setValue('avatar', url); const values = getValues(); onFinish(values); }; const onFinish = async (values) => { const newMe = await userStore.updateSelf(values); if (newMe) { layoutStore.setMe(newMe); } }; return (
{t('Profile')}
{t('Edit your profile')}
{avatar && avatar} {!avatar && avatar}
} /> ( { ref.current?.open?.(); }} />
), }, }} /> )} /> } />
); };