2023-08-11 17:36:09 +03:00
|
|
|
|
import { useMemo, useState } from 'react';
|
2023-08-10 13:53:19 +03:00
|
|
|
|
import { useNavigate } from 'react-router-dom';
|
|
|
|
|
import { toast } from 'react-toastify';
|
|
|
|
|
|
2023-08-11 17:36:09 +03:00
|
|
|
|
import BackendError from '../../components/BackendError';
|
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-08-10 13:53:19 +03:00
|
|
|
|
import { IUserUpdatePassword } from '../../utils/models';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function ChangePassword() {
|
2023-08-11 17:36:09 +03:00
|
|
|
|
const { updatePassword, error, 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-10 13:53:19 +03:00
|
|
|
|
const navigate = useNavigate();
|
2023-08-11 17:36:09 +03:00
|
|
|
|
|
|
|
|
|
const colorClass = useMemo(() => {
|
2023-08-16 17:54:59 +03:00
|
|
|
|
return !!newPassword && !!newPasswordRepeat && newPassword !== newPasswordRepeat ? 'bg-red-500' : 'clr-input';
|
2023-08-11 17:36:09 +03:00
|
|
|
|
}, [newPassword, newPasswordRepeat]);
|
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('Пароли не совпадают');
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
updatePassword(data, () => {toast.success('Изменения сохранены'); navigate('/login')});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2023-08-16 17:54:59 +03:00
|
|
|
|
<div className='flex max-w-sm px-4 border-l-2'>
|
|
|
|
|
<form onSubmit={handleSubmit} className='flex flex-col justify-between px-6 min-w-fit '>
|
|
|
|
|
<div>
|
|
|
|
|
<TextInput id='old_password'
|
|
|
|
|
type='password'
|
|
|
|
|
label='Введите старый пароль:'
|
|
|
|
|
value={oldPassword}
|
|
|
|
|
onChange={event => setOldPassword(event.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput id='new_password'
|
|
|
|
|
colorClass={colorClass}
|
|
|
|
|
label="Введите новый пароль:"
|
|
|
|
|
value={newPassword}
|
|
|
|
|
onChange={event => {
|
|
|
|
|
setNewPassword(event.target.value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput id='new_password_repeat'
|
|
|
|
|
colorClass={colorClass}
|
|
|
|
|
label="Повторите новый пароль:"
|
|
|
|
|
value={newPasswordRepeat}
|
|
|
|
|
onChange={event => {
|
|
|
|
|
setNewPasswordRepeat(event.target.value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
{ error && <BackendError error={error} />}
|
|
|
|
|
<div className='flex justify-center py-4'>
|
2023-08-10 13:53:19 +03:00
|
|
|
|
<button
|
|
|
|
|
type='submit'
|
2023-08-16 17:54:59 +03:00
|
|
|
|
className={`px-2 py-1 border clr-btn-blue`}
|
2023-08-11 17:36:09 +03:00
|
|
|
|
disabled={loading}>
|
2023-08-10 13:53:19 +03:00
|
|
|
|
<span>Сменить пароль</span>
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2023-08-16 17:54:59 +03:00
|
|
|
|
</form>
|
|
|
|
|
</div>
|
2023-08-11 12:25:16 +03:00
|
|
|
|
)}
|