2023-09-03 18:26:50 +03:00
|
|
|
|
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
|
|
|
|
|
2023-09-03 18:26:50 +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';
|
2023-09-03 18:26:50 +03:00
|
|
|
|
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
|
|
|
|
|
2023-09-03 18:26:50 +03:00
|
|
|
|
function ProcessError({error}: {error: ErrorInfo}): React.ReactElement {
|
|
|
|
|
if (axios.isAxiosError(error) && error.response && error.response.status === 400) {
|
|
|
|
|
return (
|
2023-09-15 23:29:52 +03:00
|
|
|
|
<div className='text-sm select-text text-warning'>
|
2023-09-03 18:26:50 +03:00
|
|
|
|
На Портале отсутствует такое сочетание имени пользователя и пароля
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return ( <BackendError error={error} />);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
|
function LoginPage() {
|
2023-09-03 18:26:50 +03:00
|
|
|
|
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 ?? '');
|
2023-07-15 17:57:25 +03:00
|
|
|
|
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) {
|
2023-07-26 23:11:00 +03:00
|
|
|
|
const data: IUserLoginData = {
|
|
|
|
|
username: username,
|
|
|
|
|
password: password
|
|
|
|
|
};
|
2023-09-02 01:11:27 +03:00
|
|
|
|
login(data, () => {
|
2023-09-03 18:26:50 +03:00
|
|
|
|
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-09-04 16:40:40 +03:00
|
|
|
|
<div className='flex items-start justify-center w-full pt-4 select-none' style={{minHeight: mainHeight}}>
|
2023-09-03 18:26:50 +03:00
|
|
|
|
{ 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>
|
2023-09-03 18:26:50 +03:00
|
|
|
|
</div>}
|
|
|
|
|
{ !user &&
|
|
|
|
|
<Form
|
|
|
|
|
title='Вход в Портал'
|
|
|
|
|
onSubmit={handleSubmit}
|
2023-09-15 23:29:52 +03:00
|
|
|
|
dimensions='w-[24rem]'
|
2023-09-03 18:26:50 +03:00
|
|
|
|
>
|
2023-11-05 16:31:49 +03:00
|
|
|
|
<TextInput id='username' type='text'
|
2023-09-03 18:26:50 +03:00
|
|
|
|
label='Имя пользователя'
|
|
|
|
|
required
|
2023-11-05 16:31:49 +03:00
|
|
|
|
allowEnter
|
2023-09-03 18:26:50 +03:00
|
|
|
|
value={username}
|
|
|
|
|
autoFocus
|
|
|
|
|
onChange={event => setUsername(event.target.value)}
|
|
|
|
|
/>
|
2023-11-05 16:31:49 +03:00
|
|
|
|
<TextInput id='password' type='password'
|
2023-09-03 18:26:50 +03:00
|
|
|
|
label='Пароль'
|
|
|
|
|
required
|
2023-11-05 16:31:49 +03:00
|
|
|
|
allowEnter
|
2023-09-03 18:26:50 +03:00
|
|
|
|
value={password}
|
|
|
|
|
onChange={event => setPassword(event.target.value)}
|
|
|
|
|
/>
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2023-09-15 23:29:52 +03:00
|
|
|
|
<div className='flex justify-center w-full gap-2 py-2'>
|
2023-09-03 18:26:50 +03:00
|
|
|
|
<SubmitButton
|
2023-09-29 16:22:49 +03:00
|
|
|
|
text='Войти'
|
2023-09-15 23:29:52 +03:00
|
|
|
|
dimensions='w-[12rem]'
|
2023-09-03 18:26:50 +03:00
|
|
|
|
loading={loading}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2023-09-15 23:29:52 +03:00
|
|
|
|
<div className='flex flex-col text-sm'>
|
2023-09-03 18:26:50 +03:00
|
|
|
|
<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;
|