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

107 lines
3.3 KiB
TypeScript
Raw Normal View History

'use client';
import axios from 'axios';
import clsx from 'clsx';
2023-07-15 17:46:19 +03:00
import { useEffect, useState } from 'react';
2024-01-07 03:29:16 +03:00
import AnimateFade from '@/components/AnimateFade';
import ExpectedAnonymous from '@/components/ExpectedAnonymous';
import InfoError, { ErrorData } from '@/components/InfoError';
import SubmitButton from '@/components/ui/SubmitButton';
import TextInput from '@/components/ui/TextInput';
import TextURL from '@/components/ui/TextURL';
import { useAuth } from '@/context/AuthContext';
2023-12-26 14:23:51 +03:00
import { useConceptNavigation } from '@/context/NavigationContext';
import useQueryStrings from '@/hooks/useQueryStrings';
import { IUserLoginData } from '@/models/library';
import { classnames, resources } from '@/utils/constants';
2023-12-28 14:04:44 +03:00
function ProcessError({ error }: { error: ErrorData }): React.ReactElement {
if (axios.isAxiosError(error) && error.response && error.response.status === 400) {
return (
<div className='text-sm select-text clr-text-red'>
2023-12-28 14:04:44 +03:00
На Портале отсутствует такое сочетание имени пользователя и пароля
</div>
);
} else {
2023-12-28 14:04:44 +03:00
return <InfoError error={error} />;
}
}
2023-07-15 17:46:19 +03:00
function LoginPage() {
const router = useConceptNavigation();
const query = useQueryStrings();
const userQuery = query.get('username');
2023-12-08 00:33:47 +03:00
const { user, login, loading, error, setError } = useAuth();
2023-12-28 14:04:44 +03:00
const [username, setUsername] = useState(userQuery || '');
2023-07-31 23:47:18 +03:00
const [password, setPassword] = useState('');
2023-07-15 17:46:19 +03:00
useEffect(() => {
setError(undefined);
}, [username, password, setError]);
2023-07-31 23:47:18 +03:00
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
2023-07-15 17:46:19 +03:00
event.preventDefault();
if (!loading) {
const data: IUserLoginData = {
username: username,
password: password
};
2023-09-02 01:11:27 +03:00
login(data, () => {
if (router.canBack()) {
router.back();
2023-09-02 01:11:27 +03:00
} else {
router.push('/library');
2023-09-02 01:11:27 +03:00
}
});
2023-07-15 17:46:19 +03:00
}
2023-07-31 23:47:18 +03:00
}
2023-07-15 17:46:19 +03:00
2023-12-08 00:33:47 +03:00
if (user) {
2023-12-28 14:04:44 +03:00
return <ExpectedAnonymous />;
2023-09-18 15:25:25 +03:00
}
2023-07-15 17:46:19 +03:00
return (
2024-01-07 03:29:16 +03:00
<AnimateFade>
<form className={clsx('w-[24rem]', 'pt-12 pb-6 px-6', classnames.flex_col)} onSubmit={handleSubmit}>
<img alt='Концепт Портал' src={resources.logo} className='max-h-[2.5rem] min-w-[2.5rem] mb-3' />
<TextInput
id='username'
label='Имя пользователя'
autoComplete='username'
autoFocus
required
allowEnter
value={username}
onChange={event => setUsername(event.target.value)}
/>
<TextInput
id='password'
type='password'
label='Пароль'
autoComplete='current-password'
required
allowEnter
value={password}
onChange={event => setPassword(event.target.value)}
/>
2023-07-15 17:46:19 +03:00
<SubmitButton
text='Войти'
className='self-center w-[12rem] mt-3'
loading={loading}
disabled={!username || !password}
/>
<div className='flex flex-col text-sm'>
<TextURL text='Восстановить пароль...' href='/restore-password' />
<TextURL text='Нет аккаунта? Зарегистрируйтесь...' href='/signup' />
</div>
{error ? <ProcessError error={error} /> : null}
</form>
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 LoginPage;