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 { urls } from '@/app/urls';
|
2025-01-21 20:33:05 +03:00
|
|
|
|
import { IPasswordTokenDTO, IResetPasswordDTO } from '@/backend/auth/api';
|
|
|
|
|
import { useResetPassword } from '@/backend/auth/useResetPassword';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import InfoError, { ErrorData } from '@/components/info/InfoError';
|
|
|
|
|
import SubmitButton from '@/components/ui/SubmitButton';
|
|
|
|
|
import TextInput from '@/components/ui/TextInput';
|
|
|
|
|
import DataLoader from '@/components/wrap/DataLoader';
|
2025-01-23 19:41:31 +03:00
|
|
|
|
import { useConceptNavigation } from '@/app/Navigation/NavigationContext';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import useQueryStrings from '@/hooks/useQueryStrings';
|
|
|
|
|
|
|
|
|
|
function PasswordChangePage() {
|
|
|
|
|
const router = useConceptNavigation();
|
|
|
|
|
const token = useQueryStrings().get('token');
|
|
|
|
|
|
2025-01-21 20:33:05 +03:00
|
|
|
|
const { validateToken, resetPassword, isPending, error, reset } = useResetPassword();
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
const [isTokenValid, setIsTokenValid] = useState(false);
|
|
|
|
|
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-12-13 21:30:49 +03:00
|
|
|
|
|
|
|
|
|
const canSubmit = !!newPassword && !!newPasswordRepeat && newPassword === newPasswordRepeat;
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
|
|
|
event.preventDefault();
|
2025-01-21 20:33:05 +03:00
|
|
|
|
if (!isPending) {
|
|
|
|
|
const data: IResetPasswordDTO = {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
password: newPassword,
|
|
|
|
|
token: token!
|
|
|
|
|
};
|
|
|
|
|
resetPassword(data, () => {
|
|
|
|
|
router.replace(urls.home);
|
|
|
|
|
router.push(urls.login);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-01-21 20:33:05 +03:00
|
|
|
|
reset();
|
|
|
|
|
}, [newPassword, newPasswordRepeat, reset]);
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-01-21 20:33:05 +03:00
|
|
|
|
const data: IPasswordTokenDTO = {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
token: token ?? ''
|
|
|
|
|
};
|
|
|
|
|
validateToken(data, () => setIsTokenValid(true));
|
|
|
|
|
}, [token, validateToken]);
|
|
|
|
|
|
|
|
|
|
return (
|
2025-01-21 20:33:05 +03:00
|
|
|
|
<DataLoader isLoading={isPending} hasNoData={!isTokenValid}>
|
2024-12-12 13:17:24 +03:00
|
|
|
|
<form className={clsx('cc-fade-in cc-column', 'w-[24rem] mx-auto', 'px-6 mt-3')} onSubmit={handleSubmit}>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
<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);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<SubmitButton
|
|
|
|
|
text='Установить пароль'
|
|
|
|
|
className='self-center w-[12rem] mt-3'
|
2025-01-21 20:33:05 +03:00
|
|
|
|
loading={isPending}
|
2024-06-07 20:17:03 +03:00
|
|
|
|
disabled={!canSubmit}
|
|
|
|
|
/>
|
|
|
|
|
{error ? <ProcessError error={error} /> : null}
|
|
|
|
|
</form>
|
|
|
|
|
</DataLoader>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default PasswordChangePage;
|
|
|
|
|
|
|
|
|
|
// ====== Internals =========
|
|
|
|
|
function ProcessError({ error }: { error: ErrorData }): React.ReactElement {
|
|
|
|
|
if (axios.isAxiosError(error) && error.response && error.response.status === 404) {
|
2024-12-16 23:51:31 +03:00
|
|
|
|
return <div className='mx-auto mt-6 text-sm select-text text-warn-600'>Данная ссылка не действительна</div>;
|
2024-06-07 20:17:03 +03:00
|
|
|
|
} else {
|
|
|
|
|
return <InfoError error={error} />;
|
|
|
|
|
}
|
|
|
|
|
}
|