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

132 lines
4.1 KiB
TypeScript
Raw Normal View History

import axios from 'axios';
2023-07-15 17:46:19 +03:00
import { useEffect, useState } from 'react';
2023-09-05 00:23:53 +03:00
import { useLocation } from 'react-router-dom';
2023-07-15 17:46:19 +03:00
import BackendError, { ErrorInfo } from '../components/BackendError';
import Form from '../components/common/Form';
import SubmitButton from '../components/common/SubmitButton';
import TextInput from '../components/common/TextInput';
import TextURL from '../components/common/TextURL';
2023-07-25 20:27:29 +03:00
import { useAuth } from '../context/AuthContext';
2023-09-05 00:23:53 +03:00
import { useConceptNavigation } from '../context/NagivationContext';
import { useConceptTheme } from '../context/ThemeContext';
2023-09-11 20:31:54 +03:00
import { IUserLoginData } from '../models/library';
2023-07-15 17:46:19 +03:00
function ProcessError({error}: {error: ErrorInfo}): React.ReactElement {
if (axios.isAxiosError(error) && error.response && error.response.status === 400) {
return (
<div className='text-sm select-text text-warning'>
На Портале отсутствует такое сочетание имени пользователя и пароля
</div>
);
} else {
return ( <BackendError error={error} />);
}
}
2023-07-15 17:46:19 +03:00
function LoginPage() {
const {mainHeight} = useConceptTheme();
2023-09-02 01:11:27 +03:00
const location = useLocation();
2023-09-05 00:23:53 +03:00
const { navigateTo, navigateHistory } = useConceptNavigation();
2023-07-15 17:46:19 +03:00
const search = useLocation().search;
2023-09-18 15:25:25 +03:00
const { user, login, logout, loading, error, setError } = useAuth();
2023-07-31 23:47:18 +03:00
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
2023-07-15 17:46:19 +03:00
useEffect(() => {
const name = new URLSearchParams(search).get('username');
2023-07-25 20:27:29 +03:00
setUsername(name ?? '');
setPassword('');
2023-07-15 17:46:19 +03:00
}, [search]);
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 (location.key !== 'default') {
2023-09-05 00:23:53 +03:00
navigateHistory(-1);
2023-09-02 01:11:27 +03:00
} else {
2023-09-05 00:23:53 +03:00
navigateTo('/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-09-18 15:25:25 +03:00
function logoutAndRedirect() {
logout(() => navigateTo('/login/'));
}
2023-07-15 17:46:19 +03:00
return (
2023-11-05 18:41:28 +03:00
<div className='flex items-start justify-center w-full pt-8 select-none' style={{minHeight: mainHeight}}>
{ user &&
2023-09-04 16:40:40 +03:00
<div className='flex flex-col items-center gap-2'>
<p className='font-semibold'>{`Вы вошли в систему как ${user.username}`}</p>
<p>
<TextURL text='Создать схему' href='/rsform-create'/>
<span> | </span>
<TextURL text='Библиотека' href='/library'/>
<span> | </span>
<TextURL text='Справка' href='/manuals'/>
<span> | </span>
2023-09-18 15:25:25 +03:00
<span
className='cursor-pointer hover:underline text-url'
onClick={logoutAndRedirect}
>
Выйти
</span>
2023-09-04 16:40:40 +03:00
</p>
</div>}
{ !user &&
<Form
onSubmit={handleSubmit}
dimensions='w-[24rem]'
>
2023-11-05 18:41:28 +03:00
<img alt='Концепт Портал'
src='/logo_full.svg'
className='max-h-[2.5rem] min-w-[2.5rem] mt-2 mb-4'
/>
2023-11-05 16:31:49 +03:00
<TextInput id='username' type='text'
label='Имя пользователя'
required
2023-11-05 16:31:49 +03:00
allowEnter
value={username}
autoFocus
onChange={event => setUsername(event.target.value)}
/>
2023-11-05 16:31:49 +03:00
<TextInput id='password' type='password'
label='Пароль'
required
2023-11-05 16:31:49 +03:00
allowEnter
value={password}
onChange={event => setPassword(event.target.value)}
/>
2023-07-15 17:46:19 +03:00
2023-11-05 18:41:28 +03:00
<div className='flex justify-center w-full py-2'>
<SubmitButton
2023-09-29 16:22:49 +03:00
text='Войти'
dimensions='w-[12rem]'
loading={loading}
/>
</div>
<div className='flex flex-col text-sm'>
<TextURL text='Восстановить пароль...' href='/restore-password' />
<TextURL text='Нет аккаунта? Зарегистрируйтесь...' href='/signup' />
</div>
{ error && <ProcessError error={error} />}
</Form>
}
2023-08-27 15:39:49 +03:00
</div>
2023-07-15 17:46:19 +03:00
);
}
2023-07-25 20:27:29 +03:00
export default LoginPage;