2023-07-15 17:46:19 +03:00
|
|
|
import { useEffect, useState } from 'react';
|
2023-07-25 20:27:29 +03:00
|
|
|
import { useLocation, useNavigate } from 'react-router-dom';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
2023-07-25 20:27:29 +03:00
|
|
|
import BackendError 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-07-26 23:11:00 +03:00
|
|
|
import { IUserLoginData } from '../utils/models';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
|
|
function LoginPage() {
|
2023-09-02 01:11:27 +03:00
|
|
|
const location = useLocation();
|
2023-07-15 17:46:19 +03:00
|
|
|
const navigate = useNavigate();
|
|
|
|
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 ?? '');
|
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, () => {
|
|
|
|
if (location.key !== "default") {
|
|
|
|
navigate(-1);
|
|
|
|
} else {
|
|
|
|
navigate('/library');
|
|
|
|
}
|
|
|
|
});
|
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-08-27 15:39:49 +03:00
|
|
|
<div className='flex justify-center w-full'>
|
|
|
|
<div className='py-2'> { user
|
2023-08-10 14:30:20 +03:00
|
|
|
? <b>{`Вы вошли в систему как ${user.username}`}</b>
|
2023-08-27 15:39:49 +03:00
|
|
|
:
|
|
|
|
<Form
|
2023-09-02 01:11:27 +03:00
|
|
|
title='Вход в Портал'
|
2023-08-27 15:39:49 +03:00
|
|
|
onSubmit={handleSubmit}
|
|
|
|
widthClass='w-[24rem]'
|
|
|
|
>
|
2023-07-15 17:46:19 +03:00
|
|
|
<TextInput id='username'
|
|
|
|
label='Имя пользователя'
|
|
|
|
required
|
|
|
|
type='text'
|
|
|
|
value={username}
|
2023-07-23 15:23:01 +03:00
|
|
|
autoFocus
|
2023-08-27 16:35:17 +03:00
|
|
|
onChange={event => setUsername(event.target.value)}
|
2023-07-15 17:46:19 +03:00
|
|
|
/>
|
|
|
|
<TextInput id='password'
|
|
|
|
label='Пароль'
|
|
|
|
required
|
|
|
|
type='password'
|
2023-07-15 17:57:25 +03:00
|
|
|
value={password}
|
2023-08-27 16:35:17 +03:00
|
|
|
onChange={event => setPassword(event.target.value)}
|
2023-07-15 17:46:19 +03:00
|
|
|
/>
|
|
|
|
|
2023-09-02 01:11:27 +03:00
|
|
|
<div className='flex justify-center w-full gap-2 py-2 mt-4'>
|
2023-08-22 17:52:59 +03:00
|
|
|
<SubmitButton
|
|
|
|
text='Вход'
|
2023-09-02 01:11:27 +03:00
|
|
|
widthClass='w-[12rem]'
|
2023-08-22 17:52:59 +03:00
|
|
|
loading={loading}
|
|
|
|
/>
|
2023-07-15 17:46:19 +03:00
|
|
|
</div>
|
2023-08-22 17:52:59 +03:00
|
|
|
<div className='flex flex-col mt-2 text-sm'>
|
|
|
|
<TextURL text='Восстановить пароль...' href='/restore-password' />
|
2023-07-15 18:25:31 +03:00
|
|
|
<TextURL text='Нет аккаунта? Зарегистрируйтесь...' href='/signup' />
|
|
|
|
</div>
|
2023-07-15 17:46:19 +03:00
|
|
|
{ error && <BackendError error={error} />}
|
|
|
|
</Form>
|
|
|
|
}</div>
|
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;
|