2024-02-25 20:55:30 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import clsx from 'clsx';
|
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
2024-03-20 15:27:32 +03:00
|
|
|
|
import InfoError, { ErrorData } from '@/components/info/InfoError';
|
2024-02-25 20:55:30 +03:00
|
|
|
|
import SubmitButton from '@/components/ui/SubmitButton';
|
|
|
|
|
import TextInput from '@/components/ui/TextInput';
|
2024-01-04 19:38:12 +03:00
|
|
|
|
import TextURL from '@/components/ui/TextURL';
|
2024-03-20 15:27:32 +03:00
|
|
|
|
import AnimateFade from '@/components/wrap/AnimateFade';
|
2024-02-25 20:55:30 +03:00
|
|
|
|
import { useAuth } from '@/context/AuthContext';
|
2024-05-26 14:54:02 +03:00
|
|
|
|
import { IRequestPasswordData } from '@/models/user';
|
2024-02-25 20:55:30 +03:00
|
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
|
function RestorePasswordPage() {
|
2024-02-25 20:55:30 +03:00
|
|
|
|
const { requestPasswordReset, loading, error, setError } = useAuth();
|
|
|
|
|
|
|
|
|
|
const [isCompleted, setIsCompleted] = useState(false);
|
|
|
|
|
const [email, setEmail] = useState('');
|
|
|
|
|
|
|
|
|
|
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (!loading) {
|
|
|
|
|
const data: IRequestPasswordData = {
|
|
|
|
|
email: email
|
|
|
|
|
};
|
|
|
|
|
requestPasswordReset(data, () => setIsCompleted(true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setError(undefined);
|
|
|
|
|
}, [email, setError]);
|
|
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
|
return (
|
2024-02-25 20:55:30 +03:00
|
|
|
|
<AnimateFade>
|
|
|
|
|
{!isCompleted ? (
|
2024-06-05 19:42:48 +03:00
|
|
|
|
<form className={clsx('cc-column', 'w-[24rem] mx-auto', 'px-6 mt-3')} onSubmit={handleSubmit}>
|
2024-02-25 20:55:30 +03:00
|
|
|
|
<TextInput
|
|
|
|
|
id='email'
|
2024-06-05 19:42:48 +03:00
|
|
|
|
autoComplete='email'
|
|
|
|
|
required
|
2024-02-25 20:55:30 +03:00
|
|
|
|
allowEnter
|
|
|
|
|
label='Электронная почта'
|
|
|
|
|
value={email}
|
|
|
|
|
onChange={event => setEmail(event.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<SubmitButton
|
|
|
|
|
text='Запросить пароль'
|
|
|
|
|
className='self-center w-[12rem] mt-3'
|
|
|
|
|
loading={loading}
|
|
|
|
|
disabled={!email}
|
|
|
|
|
/>
|
|
|
|
|
{error ? <ProcessError error={error} /> : null}
|
|
|
|
|
</form>
|
|
|
|
|
) : null}
|
|
|
|
|
{isCompleted ? (
|
|
|
|
|
<div className='flex flex-col items-center gap-1 mt-3'>
|
|
|
|
|
<p>На указанную почту отправлены инструкции по восстановлению пароля.</p>
|
|
|
|
|
<TextURL text='Войти в Портал' href='/login' />
|
|
|
|
|
<TextURL text='Начальная страница' href='/' />
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
2024-01-07 03:29:16 +03:00
|
|
|
|
</AnimateFade>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
export default RestorePasswordPage;
|
2024-06-05 12:28:08 +03:00
|
|
|
|
|
|
|
|
|
// ====== Internals =========
|
|
|
|
|
function ProcessError({ error }: { error: ErrorData }): React.ReactElement {
|
|
|
|
|
if (axios.isAxiosError(error) && error.response && error.response.status === 400) {
|
2024-06-05 19:42:48 +03:00
|
|
|
|
return (
|
|
|
|
|
<div className='mx-auto mt-6 text-sm select-text clr-text-red'>Данный email не используется на Портале.</div>
|
|
|
|
|
);
|
2024-06-05 12:28:08 +03:00
|
|
|
|
} else {
|
|
|
|
|
return <InfoError error={error} />;
|
|
|
|
|
}
|
|
|
|
|
}
|