ConceptPortal-public/rsconcept/frontend/src/pages/UserProfilePage/EditorPassword.tsx

103 lines
3.5 KiB
TypeScript
Raw Normal View History

import axios from 'axios';
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';
import BackendError, { ErrorInfo } 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-09-11 20:31:54 +03:00
import { IUserUpdatePassword } from '../../models/library';
2023-08-10 13:53:19 +03:00
function ProcessError({error}: {error: ErrorInfo}): React.ReactElement {
if (axios.isAxiosError(error) && error.response && error.response.status === 400) {
return (
<div className='text-sm select-text text-warning'>
Неверно введен старый пароль
</div>
);
} else {
return (<BackendError error={error} />);
}
}
2023-08-10 13:53:19 +03:00
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(
() => {
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 (
<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'>
<div className='flex flex-col gap-3'>
2023-08-16 17:54:59 +03:00
<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}
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}
label='Повторите новый'
2023-08-16 17:54:59 +03:00
value={newPasswordRepeat}
onChange={event => {
setNewPasswordRepeat(event.target.value);
}}
/>
</div>
{ error && <ProcessError error={error} />}
2023-08-26 17:26:49 +03:00
<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;