2024-06-07 20:17:03 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
2024-12-13 21:30:49 +03:00
|
|
|
|
import { useEffect, useState } from 'react';
|
2025-02-12 21:36:03 +03:00
|
|
|
|
import clsx from 'clsx';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
|
import { urls, useConceptNavigation } from '@/app';
|
2025-02-12 21:36:03 +03:00
|
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
|
import { isAxiosError } from '@/backend/apiTransport';
|
|
|
|
|
import { SubmitButton } from '@/components/Control';
|
2025-02-20 20:22:05 +03:00
|
|
|
|
import { type ErrorData, InfoError } from '@/components/InfoError';
|
2025-02-10 01:32:16 +03:00
|
|
|
|
import { TextInput } from '@/components/Input';
|
|
|
|
|
import { Loader } from '@/components/Loader';
|
2025-02-19 23:29:45 +03:00
|
|
|
|
import { useQueryStrings } from '@/hooks/useQueryStrings';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
|
import { useResetPassword } from '../backend/useResetPassword';
|
|
|
|
|
|
2025-01-29 23:18:08 +03:00
|
|
|
|
export function Component() {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
const router = useConceptNavigation();
|
2025-01-30 11:38:13 +03:00
|
|
|
|
const token = useQueryStrings().get('token') ?? '';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2025-02-04 20:35:18 +03:00
|
|
|
|
const { validateToken, resetPassword, isPending, error: serverError } = useResetPassword();
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2025-02-11 20:15:34 +03:00
|
|
|
|
const [isTokenValidating, setIsTokenValidating] = useState(false);
|
2024-06-07 20:17:03 +03:00
|
|
|
|
const [newPassword, setNewPassword] = useState('');
|
|
|
|
|
const [newPasswordRepeat, setNewPasswordRepeat] = useState('');
|
|
|
|
|
|
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) {
|
2025-02-11 20:15:34 +03:00
|
|
|
|
void resetPassword({
|
|
|
|
|
password: newPassword,
|
|
|
|
|
token: token
|
|
|
|
|
}).then(() => {
|
|
|
|
|
router.replace(urls.home);
|
|
|
|
|
router.push(urls.login);
|
|
|
|
|
});
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-02-11 20:15:34 +03:00
|
|
|
|
if (!isTokenValidating && !isPending) {
|
|
|
|
|
void validateToken({ token: token });
|
|
|
|
|
setIsTokenValidating(true);
|
2025-01-30 11:38:13 +03:00
|
|
|
|
}
|
2025-02-11 20:15:34 +03:00
|
|
|
|
}, [token, validateToken, isTokenValidating, isPending]);
|
2025-01-29 16:17:50 +03:00
|
|
|
|
|
2025-01-30 11:38:13 +03:00
|
|
|
|
if (isPending) {
|
2025-01-29 16:17:50 +03:00
|
|
|
|
return <Loader />;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 20:17:03 +03:00
|
|
|
|
return (
|
2025-01-29 16:17:50 +03:00
|
|
|
|
<form className={clsx('cc-fade-in cc-column', 'w-[24rem] mx-auto', 'px-6 mt-3')} onSubmit={handleSubmit}>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='new_password'
|
|
|
|
|
type='password'
|
|
|
|
|
label='Новый пароль'
|
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
allowEnter
|
|
|
|
|
value={newPassword}
|
|
|
|
|
onChange={event => {
|
|
|
|
|
setNewPassword(event.target.value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='new_password_repeat'
|
|
|
|
|
type='password'
|
|
|
|
|
label='Повторите новый'
|
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
allowEnter
|
|
|
|
|
value={newPasswordRepeat}
|
|
|
|
|
onChange={event => {
|
|
|
|
|
setNewPasswordRepeat(event.target.value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<SubmitButton
|
|
|
|
|
text='Установить пароль'
|
|
|
|
|
className='self-center w-[12rem] mt-3'
|
|
|
|
|
loading={isPending}
|
|
|
|
|
disabled={!canSubmit}
|
|
|
|
|
/>
|
2025-02-04 20:35:18 +03:00
|
|
|
|
{serverError ? <ServerError error={serverError} /> : null}
|
2025-01-29 16:17:50 +03:00
|
|
|
|
</form>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ====== Internals =========
|
2025-02-04 20:35:18 +03:00
|
|
|
|
function ServerError({ error }: { error: ErrorData }): React.ReactElement {
|
2025-02-10 01:32:16 +03:00
|
|
|
|
if (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
|
|
|
|
}
|
2025-01-30 11:38:13 +03:00
|
|
|
|
return <InfoError error={error} />;
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|