2024-06-07 20:17:03 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import clsx from 'clsx';
|
2024-12-13 21:30:49 +03:00
|
|
|
|
import { useEffect, useState } from 'react';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import { toast } from 'react-toastify';
|
|
|
|
|
|
2025-01-27 15:50:15 +03:00
|
|
|
|
import { useConceptNavigation } from '@/app/Navigation/NavigationContext';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import { urls } from '@/app/urls';
|
2025-01-21 20:33:05 +03:00
|
|
|
|
import { IChangePasswordDTO } from '@/backend/auth/api';
|
|
|
|
|
import { useChangePassword } from '@/backend/auth/useChangePassword';
|
2025-01-29 23:18:08 +03:00
|
|
|
|
import { ErrorData } from '@/components/info/InfoError';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import FlexColumn from '@/components/ui/FlexColumn';
|
|
|
|
|
import SubmitButton from '@/components/ui/SubmitButton';
|
|
|
|
|
import TextInput from '@/components/ui/TextInput';
|
2025-01-28 19:45:31 +03:00
|
|
|
|
import { errors } from '@/utils/labels';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
function EditorPassword() {
|
|
|
|
|
const router = useConceptNavigation();
|
2025-01-21 20:33:05 +03:00
|
|
|
|
const { changePassword, isPending, error, reset } = useChangePassword();
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
const [oldPassword, setOldPassword] = useState('');
|
|
|
|
|
const [newPassword, setNewPassword] = useState('');
|
|
|
|
|
const [newPasswordRepeat, setNewPasswordRepeat] = useState('');
|
|
|
|
|
|
2024-12-13 21:30:49 +03:00
|
|
|
|
const passwordColor =
|
2024-12-16 23:51:31 +03:00
|
|
|
|
!!newPassword && !!newPasswordRepeat && newPassword !== newPasswordRepeat ? 'bg-warn-100' : 'clr-input';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2024-12-13 21:30:49 +03:00
|
|
|
|
const canSubmit = !!oldPassword && !!newPassword && !!newPasswordRepeat && newPassword === newPasswordRepeat;
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (newPassword !== newPasswordRepeat) {
|
|
|
|
|
toast.error(errors.passwordsMismatch);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-01-21 20:33:05 +03:00
|
|
|
|
const data: IChangePasswordDTO = {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
old_password: oldPassword,
|
|
|
|
|
new_password: newPassword
|
|
|
|
|
};
|
2025-01-28 19:45:31 +03:00
|
|
|
|
changePassword(data, () => router.push(urls.login));
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-01-21 20:33:05 +03:00
|
|
|
|
reset();
|
|
|
|
|
}, [newPassword, oldPassword, newPasswordRepeat, reset]);
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form
|
|
|
|
|
className={clsx('max-w-[14rem]', 'px-6 py-2 flex flex-col justify-between', 'border-l-2')}
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
>
|
|
|
|
|
<FlexColumn>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='old_password'
|
|
|
|
|
type='password'
|
|
|
|
|
label='Старый пароль'
|
|
|
|
|
autoComplete='current-password'
|
|
|
|
|
allowEnter
|
|
|
|
|
value={oldPassword}
|
|
|
|
|
onChange={event => setOldPassword(event.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='new_password'
|
|
|
|
|
type='password'
|
|
|
|
|
label='Новый пароль'
|
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
allowEnter
|
|
|
|
|
colors={passwordColor}
|
|
|
|
|
value={newPassword}
|
|
|
|
|
onChange={event => {
|
|
|
|
|
setNewPassword(event.target.value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='new_password_repeat'
|
|
|
|
|
type='password'
|
|
|
|
|
label='Повторите новый'
|
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
allowEnter
|
|
|
|
|
colors={passwordColor}
|
|
|
|
|
value={newPasswordRepeat}
|
|
|
|
|
onChange={event => {
|
|
|
|
|
setNewPasswordRepeat(event.target.value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
{error ? <ProcessError error={error} /> : null}
|
|
|
|
|
</FlexColumn>
|
2025-01-21 20:33:05 +03:00
|
|
|
|
<SubmitButton text='Сменить пароль' className='self-center' disabled={!canSubmit} loading={isPending} />
|
2024-06-07 20:17:03 +03:00
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default EditorPassword;
|
|
|
|
|
|
|
|
|
|
// ====== Internals =========
|
|
|
|
|
function ProcessError({ error }: { error: ErrorData }): React.ReactElement {
|
|
|
|
|
if (axios.isAxiosError(error) && error.response && error.response.status === 400) {
|
2024-12-16 23:51:31 +03:00
|
|
|
|
return <div className='text-sm select-text text-warn-600'>Неверно введен старый пароль</div>;
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|
2025-01-29 23:18:08 +03:00
|
|
|
|
throw error as Error;
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|