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

120 lines
3.8 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';
2023-07-15 17:46:19 +03:00
import Form from '../components/Common/Form';
import SubmitButton from '../components/Common/SubmitButton';
2023-07-25 20:27:29 +03:00
import TextInput from '../components/Common/TextInput';
2023-07-15 17:46:19 +03:00
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';
import { IUserLoginData } from '../utils/models';
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='mt-2 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-07-31 23:47:18 +03:00
const { user, login, loading, error, setError } = useAuth();
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
return (
2023-09-04 16:40:40 +03:00
<div className='flex items-start justify-center w-full pt-4 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>
<TextURL text='Выйти' href='/logout'/>
</p>
</div>}
{ !user &&
<Form
title='Вход в Портал'
onSubmit={handleSubmit}
widthClass='w-[24rem]'
>
<TextInput id='username'
label='Имя пользователя'
required
type='text'
value={username}
autoFocus
onChange={event => setUsername(event.target.value)}
/>
<TextInput id='password'
label='Пароль'
required
type='password'
value={password}
onChange={event => setPassword(event.target.value)}
/>
2023-07-15 17:46:19 +03:00
<div className='flex justify-center w-full gap-2 py-2 mt-4'>
<SubmitButton
text='Вход'
widthClass='w-[12rem]'
loading={loading}
/>
</div>
<div className='flex flex-col mt-2 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;