Portal/rsconcept/frontend/src/pages/LoginPage.tsx

109 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
import axios from 'axios';
import clsx from 'clsx';
2025-01-30 21:00:59 +03:00
import { useState } from 'react';
2024-06-07 20:17:03 +03:00
2025-01-27 15:50:15 +03:00
import { useConceptNavigation } from '@/app/Navigation/NavigationContext';
2024-06-07 20:17:03 +03:00
import { urls } from '@/app/urls';
2025-01-30 21:00:59 +03:00
import { UserLoginSchema } from '@/backend/auth/api';
import { useAuthSuspense } from '@/backend/auth/useAuth';
2025-01-21 20:33:05 +03:00
import { useLogin } from '@/backend/auth/useLogin';
import ExpectedAnonymous from '@/components/ExpectedAnonymous';
2025-01-29 23:18:08 +03:00
import { ErrorData } from '@/components/info/InfoError';
2024-06-07 20:17:03 +03:00
import SubmitButton from '@/components/ui/SubmitButton';
import TextInput from '@/components/ui/TextInput';
import TextURL from '@/components/ui/TextURL';
import useQueryStrings from '@/hooks/useQueryStrings';
import { resources } from '@/utils/constants';
function LoginPage() {
const router = useConceptNavigation();
const query = useQueryStrings();
2025-01-30 21:00:59 +03:00
const initialName = query.get('username') ?? '';
2024-06-07 20:17:03 +03:00
const { isAnonymous } = useAuthSuspense();
2025-01-30 21:00:59 +03:00
const { login, isPending, error: loginError, reset } = useLogin();
const [validationError, setValidationError] = useState<ErrorData | undefined>(undefined);
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-01-30 21:00:59 +03:00
const formData = new FormData(event.currentTarget);
const result = UserLoginSchema.safeParse({
username: formData.get('username'),
password: formData.get('password')
2024-06-07 20:17:03 +03:00
});
2025-01-30 21:00:59 +03:00
if (!result.success) {
setValidationError(result.error);
} else {
login(result.data, () => {
if (router.canBack()) {
router.back();
} else {
router.push(urls.library);
}
});
}
2024-06-07 20:17:03 +03:00
}
}
2025-01-30 21:00:59 +03:00
function resetErrors() {
reset();
setValidationError(undefined);
}
if (!isAnonymous) {
2024-06-07 20:17:03 +03:00
return <ExpectedAnonymous />;
}
return (
2024-12-12 13:17:24 +03:00
<form className={clsx('cc-column cc-fade-in', 'w-[24rem] mx-auto', 'pt-12 pb-6 px-6')} onSubmit={handleSubmit}>
<img alt='Концепт Портал' src={resources.logo} className='max-h-[2.5rem] min-w-[2.5rem] mb-3' />
<TextInput
id='username'
2025-01-30 21:00:59 +03:00
name='username'
2024-12-12 13:17:24 +03:00
autoComplete='username'
2025-01-30 21:00:59 +03:00
label='Логин или email'
2024-12-12 13:17:24 +03:00
autoFocus
required
allowEnter
spellCheck={false}
2025-01-30 21:00:59 +03:00
defaultValue={initialName}
onChange={resetErrors}
2024-12-12 13:17:24 +03:00
/>
<TextInput
id='password'
2025-01-30 21:00:59 +03:00
name='password'
2024-12-12 13:17:24 +03:00
type='password'
autoComplete='current-password'
2025-01-30 21:00:59 +03:00
label='Пароль'
2024-12-12 13:17:24 +03:00
required
allowEnter
2025-01-30 21:00:59 +03:00
onChange={resetErrors}
2024-12-12 13:17:24 +03:00
/>
2024-06-07 20:17:03 +03:00
2025-01-30 21:00:59 +03:00
<SubmitButton text='Войти' className='self-center w-[12rem] mt-3' loading={isPending} />
2024-12-12 13:17:24 +03:00
<div className='flex flex-col text-sm'>
<TextURL text='Восстановить пароль...' href='/restore-password' />
<TextURL text='Нет аккаунта? Зарегистрируйтесь...' href='/signup' />
</div>
2025-01-30 21:00:59 +03:00
{!!loginError || !!validationError ? <ProcessError error={loginError ?? validationError} /> : null}
2024-12-12 13:17:24 +03:00
</form>
2024-06-07 20:17:03 +03:00
);
}
export default LoginPage;
// ====== Internals =========
function ProcessError({ error }: { error: ErrorData }): React.ReactElement {
if (axios.isAxiosError(error) && error.response && error.response.status === 400) {
return (
<div className='text-sm select-text text-warn-600'>
2024-06-07 20:17:03 +03:00
На Портале отсутствует такое сочетание имени пользователя и пароля
</div>
);
}
2025-01-29 23:18:08 +03:00
throw error as Error;
2024-06-07 20:17:03 +03:00
}