2024-02-25 20:55:30 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import clsx from 'clsx';
|
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
|
2024-04-01 21:45:10 +03:00
|
|
|
|
import { urls } from '@/app/urls';
|
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-03-20 15:27:32 +03:00
|
|
|
|
import DataLoader from '@/components/wrap/DataLoader';
|
2024-02-25 20:55:30 +03:00
|
|
|
|
import { useAuth } from '@/context/AuthContext';
|
|
|
|
|
import { useConceptNavigation } from '@/context/NavigationContext';
|
|
|
|
|
import useQueryStrings from '@/hooks/useQueryStrings';
|
2024-05-26 14:54:02 +03:00
|
|
|
|
import { IPasswordTokenData, IResetPasswordData } from '@/models/user';
|
2024-02-25 20:55:30 +03:00
|
|
|
|
|
|
|
|
|
function PasswordChangePage() {
|
|
|
|
|
const router = useConceptNavigation();
|
|
|
|
|
const token = useQueryStrings().get('token');
|
|
|
|
|
|
|
|
|
|
const { validateToken, resetPassword, loading, error, setError } = useAuth();
|
|
|
|
|
|
|
|
|
|
const [isTokenValid, setIsTokenValid] = useState(false);
|
|
|
|
|
const [newPassword, setNewPassword] = useState('');
|
|
|
|
|
const [newPasswordRepeat, setNewPasswordRepeat] = useState('');
|
|
|
|
|
|
|
|
|
|
const passwordColor = useMemo(() => {
|
|
|
|
|
if (!!newPassword && !!newPasswordRepeat && newPassword !== newPasswordRepeat) {
|
|
|
|
|
return 'clr-warning';
|
|
|
|
|
} else {
|
|
|
|
|
return 'clr-input';
|
|
|
|
|
}
|
|
|
|
|
}, [newPassword, newPasswordRepeat]);
|
|
|
|
|
|
|
|
|
|
const canSubmit = useMemo(() => {
|
|
|
|
|
return !!newPassword && !!newPasswordRepeat && newPassword === newPasswordRepeat;
|
|
|
|
|
}, [newPassword, newPasswordRepeat]);
|
|
|
|
|
|
|
|
|
|
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (!loading) {
|
|
|
|
|
const data: IResetPasswordData = {
|
|
|
|
|
password: newPassword,
|
|
|
|
|
token: token!
|
|
|
|
|
};
|
|
|
|
|
resetPassword(data, () => {
|
2024-04-01 21:45:10 +03:00
|
|
|
|
router.replace(urls.home);
|
|
|
|
|
router.push(urls.login);
|
2024-02-25 20:55:30 +03:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setError(undefined);
|
|
|
|
|
}, [newPassword, newPasswordRepeat, setError]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const data: IPasswordTokenData = {
|
|
|
|
|
token: token ?? ''
|
|
|
|
|
};
|
|
|
|
|
validateToken(data, () => setIsTokenValid(true));
|
|
|
|
|
}, [token, validateToken]);
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return <ProcessError error={error} />;
|
|
|
|
|
}
|
|
|
|
|
return (
|
2024-12-12 13:19:12 +03:00
|
|
|
|
<DataLoader isLoading={loading} hasNoData={!isTokenValid}>
|
|
|
|
|
<form className={clsx('cc-fade-in cc-column', 'w-[24rem] mx-auto', 'px-6 mt-3')} onSubmit={handleSubmit}>
|
2024-02-25 20:55:30 +03:00
|
|
|
|
<TextInput
|
|
|
|
|
id='new_password'
|
|
|
|
|
type='password'
|
|
|
|
|
label='Новый пароль'
|
2024-03-18 16:21:39 +03:00
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
allowEnter
|
2024-02-25 20:55:30 +03:00
|
|
|
|
colors={passwordColor}
|
|
|
|
|
value={newPassword}
|
|
|
|
|
onChange={event => {
|
|
|
|
|
setNewPassword(event.target.value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='new_password_repeat'
|
|
|
|
|
type='password'
|
|
|
|
|
label='Повторите новый'
|
2024-03-18 16:21:39 +03:00
|
|
|
|
autoComplete='new-password'
|
|
|
|
|
allowEnter
|
2024-02-25 20:55:30 +03:00
|
|
|
|
colors={passwordColor}
|
|
|
|
|
value={newPasswordRepeat}
|
|
|
|
|
onChange={event => {
|
|
|
|
|
setNewPasswordRepeat(event.target.value);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<SubmitButton
|
|
|
|
|
text='Установить пароль'
|
|
|
|
|
className='self-center w-[12rem] mt-3'
|
|
|
|
|
loading={loading}
|
|
|
|
|
disabled={!canSubmit}
|
|
|
|
|
/>
|
|
|
|
|
{error ? <ProcessError error={error} /> : null}
|
|
|
|
|
</form>
|
|
|
|
|
</DataLoader>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default PasswordChangePage;
|
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 === 404) {
|
2024-06-05 19:42:48 +03:00
|
|
|
|
return <div className='mx-auto mt-6 text-sm select-text clr-text-red'>Данная ссылка не действительна</div>;
|
2024-06-05 12:28:08 +03:00
|
|
|
|
} else {
|
|
|
|
|
return <InfoError error={error} />;
|
|
|
|
|
}
|
|
|
|
|
}
|