ConceptPortal-public/rsconcept/frontend/src/pages/PasswordChangePage.tsx

103 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-02-25 20:55:30 +03:00
'use client';
import axios from 'axios';
import clsx from 'clsx';
import { useEffect, useState } from 'react';
2024-02-25 20:55:30 +03:00
2025-01-27 15:50:37 +03:00
import { useConceptNavigation } from '@/app/Navigation/NavigationContext';
import { urls } from '@/app/urls';
import { useResetPassword } from '@/backend/auth/useResetPassword';
2025-01-30 11:38:49 +03:00
import InfoError, { ErrorData } from '@/components/info/InfoError';
2025-02-07 10:54:47 +03:00
import { SubmitButton } from '@/components/ui/Control';
import { TextInput } from '@/components/ui/Input';
import Loader from '@/components/ui/Loader';
2024-02-25 20:55:30 +03:00
import useQueryStrings from '@/hooks/useQueryStrings';
2025-01-29 23:18:20 +03:00
export function Component() {
2024-02-25 20:55:30 +03:00
const router = useConceptNavigation();
2025-01-30 11:38:49 +03:00
const token = useQueryStrings().get('token') ?? '';
2024-02-25 20:55:30 +03:00
const { validateToken, resetPassword, isPending, error: serverError } = useResetPassword();
2024-02-25 20:55:30 +03:00
const [isTokenValidated, setIsTokenValidated] = useState(false);
2024-02-25 20:55:30 +03:00
const [newPassword, setNewPassword] = useState('');
const [newPasswordRepeat, setNewPasswordRepeat] = useState('');
const passwordColor =
!!newPassword && !!newPasswordRepeat && newPassword !== newPasswordRepeat ? 'bg-warn-100' : 'clr-input';
const canSubmit = !!newPassword && !!newPasswordRepeat && newPassword === newPasswordRepeat;
2024-02-25 20:55:30 +03:00
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
if (!isPending) {
2025-01-30 11:38:49 +03:00
resetPassword(
{
password: newPassword,
token: token
},
() => {
router.replace(urls.home);
router.push(urls.login);
}
);
2024-02-25 20:55:30 +03:00
}
}
useEffect(() => {
if (!isTokenValidated && !isPending) {
2025-01-30 11:38:49 +03:00
validateToken({ token: token });
setIsTokenValidated(true);
2025-01-30 11:38:49 +03:00
}
}, [token, validateToken, isTokenValidated, isPending]);
2025-01-30 11:38:49 +03:00
if (isPending) {
return <Loader />;
}
2024-02-25 20:55:30 +03:00
return (
<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
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'
loading={isPending}
disabled={!canSubmit}
/>
{serverError ? <ServerError error={serverError} /> : null}
</form>
2024-02-25 20:55:30 +03:00
);
}
// ====== Internals =========
function ServerError({ error }: { error: ErrorData }): React.ReactElement {
if (axios.isAxiosError(error) && error.response && error.response.status === 404) {
return <div className='mx-auto mt-6 text-sm select-text text-warn-600'>Данная ссылка не действительна</div>;
}
2025-01-30 11:38:49 +03:00
return <InfoError error={error} />;
}