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-11-27 12:11:39 +03:00
|
|
|
|
import SubmitButton from '../components/Common/SubmitButton';
|
|
|
|
|
import TextInput from '../components/Common/TextInput';
|
|
|
|
|
import TextURL from '../components/Common/TextURL';
|
2023-12-08 00:33:47 +03:00
|
|
|
|
import ExpectedAnonymous from '../components/ExpectedAnonymous';
|
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-11 20:31:54 +03:00
|
|
|
|
import { IUserLoginData } from '../models/library';
|
2023-12-08 00:33:47 +03:00
|
|
|
|
import { resources } from '../utils/constants';
|
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-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-12-08 00:33:47 +03:00
|
|
|
|
const { user, login, 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-12-08 00:33:47 +03:00
|
|
|
|
if (user) {
|
|
|
|
|
return (<ExpectedAnonymous />);
|
2023-09-18 15:25:25 +03:00
|
|
|
|
}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
return (
|
2023-12-08 00:33:47 +03:00
|
|
|
|
<form
|
|
|
|
|
className='pt-12 pb-6 px-6 flex flex-col gap-3 w-[24rem]'
|
2023-11-27 11:33:34 +03:00
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
>
|
|
|
|
|
<img alt='Концепт Портал'
|
2023-12-08 00:33:47 +03:00
|
|
|
|
src={resources.logo}
|
|
|
|
|
className='max-h-[2.5rem] min-w-[2.5rem] mb-3'
|
2023-11-27 11:33:34 +03:00
|
|
|
|
/>
|
|
|
|
|
<TextInput id='username' autoFocus required allowEnter
|
|
|
|
|
label='Имя пользователя'
|
|
|
|
|
value={username}
|
|
|
|
|
onChange={event => setUsername(event.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput id='password' type='password' required allowEnter
|
|
|
|
|
label='Пароль'
|
|
|
|
|
value={password}
|
|
|
|
|
onChange={event => setPassword(event.target.value)}
|
|
|
|
|
/>
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2023-11-27 11:33:34 +03:00
|
|
|
|
<div className='flex justify-center w-full py-2'>
|
|
|
|
|
<SubmitButton
|
|
|
|
|
text='Войти'
|
|
|
|
|
dimensions='w-[12rem]'
|
|
|
|
|
loading={loading}
|
2023-12-08 00:33:47 +03:00
|
|
|
|
disabled={!username || !password}
|
2023-11-27 11:33:34 +03:00
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className='flex flex-col text-sm'>
|
|
|
|
|
<TextURL text='Восстановить пароль...' href='/restore-password' />
|
|
|
|
|
<TextURL text='Нет аккаунта? Зарегистрируйтесь...' href='/signup' />
|
|
|
|
|
</div>
|
|
|
|
|
{error ? <ProcessError error={error} /> : null}
|
2023-12-08 00:33:47 +03:00
|
|
|
|
</form>);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 20:27:29 +03:00
|
|
|
|
export default LoginPage;
|