import axios from 'axios'; import React, { useState } from 'react'; import { useForm } from 'react-hook-form'; import { useTranslation } from 'react-i18next'; import { useRouter } from 'next/router'; import { CustomResponse } from '../../../model/common'; import { User } from '../../../model/user'; import { MyProfileProps, PasswordFormData, ProfileFormData, ProfileParams, } from '../../../model/components/profile'; import Field from '../../common/Field'; import RoundedButton from '../../common/RoundedButton'; import ProfileIcon from '../../icons/Profile'; import logout from '../../../utils/logout'; // import CloseSmall from '../../icons/CloseSmall'; const MyProfile: React.FC = ({ departments, user, onChangeUser, // onChangeTeam, }) => { const { register: profileRegister, formState: { errors: profileErrors }, handleSubmit: handleProfileSubmit, setError: setProfileError, clearErrors: clearProfileErrors, } = useForm(); const { register: passwordRegister, formState: { errors: passwordErrors }, handleSubmit: handlePasswordSubmit, setError: setPasswordError, getValues: getPasswordValues, setValue: setPasswordValue, clearErrors: clearPasswordErrors, } = useForm(); const { t } = useTranslation(); const router = useRouter(); const [editing, setEditing] = useState(false); // const [abandonTeam, setAbandonTeam] = useState(false); // const onConfirmAbandonTeam = async (): Promise => { // await axios // .post>('/api/deleteUserFromTeam', { // userId: user._id, // }) // .then(({ data: { ok } }) => { // if (ok) { // onChangeTeam(null); // } // setAbandonTeam(false); // }); // }; const onProfileSubmit = async (data: ProfileFormData): Promise => { const { email, username, department, licenseNumber } = data; const response = await axios.post>( '/api/updateProfile', { _id: user._id, email, username, licenseNumber, department, } as ProfileParams, ); if (response.data?.ok) { onChangeUser(response.data.data); clearPasswordErrors(); setEditing(false); } else if (response.data?.error) { setProfileError('username', { message: response.data.error }); } }; const onPasswordSubmit = async (data: PasswordFormData): Promise => { const { password, newPassword } = data; const response = await axios.post>( '/api/updatePassword', { _id: user._id, email: user.email, password, newPassword, } as ProfileParams, ); if (response.data?.ok) { clearProfileErrors(); setPasswordValue('password', ''); setPasswordValue('newPassword', ''); setPasswordValue('repeatNewPassword', ''); setEditing(false); } else if (response.data?.error) { setPasswordError('password', { message: response.data.error }); } }; const logOutAndRedirect = (): void => { logout(); router.push('/login'); }; return (

{t('YOUR_PROFILE')}

{editing && ( )}
{editing ? (
value !== getPasswordValues('newPassword') ? 'PASSWORDS_DONT_MATCH' : undefined, })} />
) : null}
{/* {abandonTeam ? ( <> setAbandonTeam(false)} /> ) : ( } onClick={(): void => setAbandonTeam(true)} /> )} */}
); }; export default MyProfile;