1
|
import 'antd/dist/antd.css';
|
2
|
import { Button, Form, Input } from 'antd';
|
3
|
import { LockOutlined, UserOutlined } from '@ant-design/icons';
|
4
|
import { LoggedUserContext } from '../../contexts/LoggedUserContext';
|
5
|
import { LoginLayout } from '../../layouts/LoginLayout';
|
6
|
import { useContext } from 'react';
|
7
|
import { useRouter } from 'next/router';
|
8
|
import { ShowToast } from '../../utils/alerts';
|
9
|
import { getTokenData } from '../../utils/login';
|
10
|
import { LoginResponse } from '../../api';
|
11
|
|
12
|
/**
|
13
|
* Creates a login screen.
|
14
|
* @returns Html structure of the login screen.
|
15
|
*/
|
16
|
function Login() {
|
17
|
const { login, role } = useContext(LoggedUserContext);
|
18
|
const router = useRouter();
|
19
|
|
20
|
/**
|
21
|
* Handles submission a form when its fields were successfully validated.
|
22
|
* @param values Fields of the login form.
|
23
|
*/
|
24
|
const onFinish = async (values: any) => {
|
25
|
const loginRes = await login(values.username, values.password);
|
26
|
|
27
|
if (!loginRes) {
|
28
|
ShowToast(
|
29
|
'Chybně zadané heslo nebo uživatelské jméno',
|
30
|
'error',
|
31
|
3000,
|
32
|
'top-end'
|
33
|
);
|
34
|
} else {
|
35
|
let data: LoginResponse | null = await getTokenData();
|
36
|
ShowToast('Přihlášení proběhlo úspěšně', 'success', 3000, 'top-end');
|
37
|
if (data?.role === 'ADMINISTRATOR') {
|
38
|
await router.push('/documents/admin');
|
39
|
} else if (data?.role === 'ANNOTATOR') {
|
40
|
await router.push('/documents/annotator');
|
41
|
}
|
42
|
}
|
43
|
};
|
44
|
|
45
|
/**
|
46
|
* Handles submission a form when its validation failed.
|
47
|
* @param errorInfo Validation errors.
|
48
|
*/
|
49
|
const onFinishFailed = (errorInfo: any) => {
|
50
|
ShowToast('Zadané údaje nejsou validní', 'error', 3000, 'top-end');
|
51
|
};
|
52
|
|
53
|
return (
|
54
|
<LoginLayout>
|
55
|
<Form
|
56
|
name="login"
|
57
|
onFinish={onFinish}
|
58
|
onFinishFailed={onFinishFailed}
|
59
|
autoComplete="off"
|
60
|
>
|
61
|
<Form.Item
|
62
|
name="username"
|
63
|
rules={[
|
64
|
{
|
65
|
required: true,
|
66
|
message: 'Prosím zadejte své uživatelské jméno',
|
67
|
},
|
68
|
]}
|
69
|
>
|
70
|
<Input
|
71
|
prefix={<UserOutlined className="site-form-item-icon" />}
|
72
|
placeholder="Uživatelské jméno"
|
73
|
/>
|
74
|
</Form.Item>
|
75
|
|
76
|
<Form.Item
|
77
|
name="password"
|
78
|
rules={[{ required: true, message: 'Prosím zadejte své heslo' }]}
|
79
|
>
|
80
|
<Input.Password
|
81
|
prefix={<LockOutlined className="site-form-item-icon" />}
|
82
|
placeholder="Heslo"
|
83
|
/>
|
84
|
</Form.Item>
|
85
|
<Form.Item>
|
86
|
<Button type="primary" htmlType="submit" className="w-100">
|
87
|
Přihlásit
|
88
|
</Button>
|
89
|
</Form.Item>
|
90
|
</Form>
|
91
|
</LoginLayout>
|
92
|
);
|
93
|
}
|
94
|
|
95
|
export default Login;
|