2023-08-26 17:26:49 +03:00
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
2023-08-10 13:53:19 +03:00
|
|
|
import { toast } from 'react-toastify';
|
|
|
|
|
2023-08-11 17:36:09 +03:00
|
|
|
import BackendError from '../../components/BackendError';
|
2023-08-26 17:26:49 +03:00
|
|
|
import SubmitButton from '../../components/Common/SubmitButton';
|
2023-08-10 13:53:19 +03:00
|
|
|
import TextInput from '../../components/Common/TextInput';
|
2023-08-11 17:36:09 +03:00
|
|
|
import { useAuth } from '../../context/AuthContext';
|
2023-09-05 00:23:53 +03:00
|
|
|
import { useConceptNavigation } from '../../context/NagivationContext';
|
2023-08-10 13:53:19 +03:00
|
|
|
import { IUserUpdatePassword } from '../../utils/models';
|
|
|
|
|
|
|
|
|
2023-08-26 17:26:49 +03:00
|
|
|
function EditorPassword() {
|
2023-09-05 00:23:53 +03:00
|
|
|
const { navigateTo } = useConceptNavigation();
|
2023-08-26 17:26:49 +03:00
|
|
|
const { updatePassword, error, setError, loading } = useAuth();
|
2023-08-10 13:53:19 +03:00
|
|
|
|
2023-08-11 17:36:09 +03:00
|
|
|
const [oldPassword, setOldPassword] = useState('');
|
|
|
|
const [newPassword, setNewPassword] = useState('');
|
|
|
|
const [newPasswordRepeat, setNewPasswordRepeat] = useState('');
|
|
|
|
|
2023-08-26 17:26:49 +03:00
|
|
|
const passwordColor = useMemo(
|
|
|
|
() => {
|
2023-09-03 18:26:50 +03:00
|
|
|
return !!newPassword && !!newPasswordRepeat && newPassword !== newPasswordRepeat ? 'clr-warning' : 'clr-input';
|
2023-08-11 17:36:09 +03:00
|
|
|
}, [newPassword, newPasswordRepeat]);
|
2023-08-26 17:26:49 +03:00
|
|
|
|
|
|
|
const canSubmit = useMemo(
|
|
|
|
() => {
|
|
|
|
return !!oldPassword && !!newPassword && !!newPasswordRepeat && newPassword === newPasswordRepeat;
|
|
|
|
}, [newPassword, newPasswordRepeat, oldPassword]);
|
2023-08-10 13:53:19 +03:00
|
|
|
|
|
|
|
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
|
|
event.preventDefault();
|
2023-08-11 17:36:09 +03:00
|
|
|
if (newPassword !== newPasswordRepeat) {
|
2023-08-10 13:53:19 +03:00
|
|
|
toast.error('Пароли не совпадают');
|
2023-08-26 17:26:49 +03:00
|
|
|
return;
|
2023-08-10 13:53:19 +03:00
|
|
|
}
|
|
|
|
const data: IUserUpdatePassword = {
|
2023-08-11 17:36:09 +03:00
|
|
|
old_password: oldPassword,
|
|
|
|
new_password: newPassword,
|
2023-08-10 13:53:19 +03:00
|
|
|
};
|
2023-08-26 17:26:49 +03:00
|
|
|
updatePassword(data, () => {
|
|
|
|
toast.success('Изменения сохранены');
|
2023-09-05 00:23:53 +03:00
|
|
|
navigateTo('/login');
|
2023-08-26 17:26:49 +03:00
|
|
|
});
|
2023-08-10 13:53:19 +03:00
|
|
|
}
|
|
|
|
|
2023-08-26 17:26:49 +03:00
|
|
|
useEffect(() => {
|
|
|
|
setError(undefined);
|
|
|
|
}, [newPassword, oldPassword, newPasswordRepeat, setError]);
|
|
|
|
|
2023-08-10 13:53:19 +03:00
|
|
|
return (
|
2023-09-03 18:26:50 +03:00
|
|
|
<div className='flex py-2 border-l-2 max-w-[14rem]'>
|
2023-08-26 17:26:49 +03:00
|
|
|
<form onSubmit={handleSubmit} className='flex flex-col justify-between px-6 min-w-fit'>
|
2023-08-16 17:54:59 +03:00
|
|
|
<div>
|
|
|
|
<TextInput id='old_password'
|
|
|
|
type='password'
|
2023-08-26 17:26:49 +03:00
|
|
|
label='Старый пароль'
|
2023-08-16 17:54:59 +03:00
|
|
|
value={oldPassword}
|
|
|
|
onChange={event => setOldPassword(event.target.value)}
|
|
|
|
/>
|
2023-08-26 17:26:49 +03:00
|
|
|
<TextInput id='new_password' type='password'
|
|
|
|
colorClass={passwordColor}
|
2023-09-03 18:26:50 +03:00
|
|
|
label='Новый пароль'
|
2023-08-16 17:54:59 +03:00
|
|
|
value={newPassword}
|
|
|
|
onChange={event => {
|
|
|
|
setNewPassword(event.target.value);
|
|
|
|
}}
|
|
|
|
/>
|
2023-08-26 17:26:49 +03:00
|
|
|
<TextInput id='new_password_repeat' type='password'
|
|
|
|
colorClass={passwordColor}
|
2023-09-03 18:26:50 +03:00
|
|
|
label='Повторите новый'
|
2023-08-16 17:54:59 +03:00
|
|
|
value={newPasswordRepeat}
|
|
|
|
onChange={event => {
|
|
|
|
setNewPasswordRepeat(event.target.value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-08-26 17:26:49 +03:00
|
|
|
{ error && <BackendError error={error} />}
|
|
|
|
<div className='flex justify-center w-full'>
|
|
|
|
<SubmitButton
|
|
|
|
disabled={!canSubmit}
|
|
|
|
loading={loading}
|
|
|
|
text='Сменить пароль'
|
|
|
|
/>
|
2023-08-10 13:53:19 +03:00
|
|
|
</div>
|
2023-08-16 17:54:59 +03:00
|
|
|
</form>
|
|
|
|
</div>
|
2023-08-26 17:26:49 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default EditorPassword;
|