Projekt

Obecné

Profil

Stáhnout (3.2 KB) Statistiky
| Větev: | Tag: | Revize:
1
import 'antd/dist/antd.css';
2
import { Form, Input, Button } from 'antd';
3
import { UserOutlined, LockOutlined } 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
import env from '@beam-australia/react-env';
12

    
13
/**
14
 * Creates a login screen.
15
 * @returns Html structure of the login screen.
16
 */
17
function Login() {
18
    const { login, role } = useContext(LoggedUserContext);
19
    const router = useRouter();
20

    
21
    /**
22
     * Handles submission a form when its fields were successfully validated.
23
     * @param values Fields of the login form.
24
     */
25
    const onFinish = async (values: any) => {
26
        const loginRes = await login(values.username, values.password);
27

    
28
        if (!loginRes) {
29
            ShowToast(
30
                'Chybně zadané heslo nebo uživatelské jméno',
31
                'error',
32
                3000,
33
                'top-end'
34
            );
35
        } else {
36
            let data: LoginResponse | null = await getTokenData();
37
            ShowToast('Přihlášení proběhlo úspěšně', 'success', 3000, 'top-end');
38
            if (data?.role === 'ADMINISTRATOR') {
39
                await router.push('/documents/admin');
40
            } else if (data?.role === 'ANNOTATOR') {
41
                await router.push('/documents/annotator');
42
            }
43
        }
44
    };
45

    
46
    /**
47
     * Handles submission a form when its validation failed.
48
     * @param errorInfo Validation errors.
49
     */
50
    const onFinishFailed = (errorInfo: any) => {
51
        ShowToast('Zadané údaje nejsou validní', 'error', 3000, 'top-end');
52
    };
53

    
54
    return (
55
        <LoginLayout>
56
            <Form
57
                name="login"
58
                onFinish={onFinish}
59
                onFinishFailed={onFinishFailed}
60
                autoComplete="off"
61
            >
62
                <Form.Item
63
                    name="username"
64
                    rules={[
65
                        {
66
                            required: true,
67
                            message: 'Prosím zadejte své uživatelské jméno',
68
                        },
69
                    ]}
70
                >
71
                    <Input
72
                        prefix={<UserOutlined className="site-form-item-icon" />}
73
                        placeholder="Uživatelské jméno"
74
                    />
75
                </Form.Item>
76

    
77
                <Form.Item
78
                    name="password"
79
                    rules={[{ required: true, message: 'Prosím zadejte své heslo' }]}
80
                >
81
                    <Input.Password
82
                        prefix={<LockOutlined className="site-form-item-icon" />}
83
                        placeholder="Heslo"
84
                    />
85
                </Form.Item>
86
                <Form.Item>
87
                    <Button type="primary" htmlType="submit" className="w-100">
88
                        Přihlásit
89
                    </Button>
90
                </Form.Item>
91
            </Form>
92
        </LoginLayout>
93
    );
94
}
95

    
96
export default Login;
    (1-1/1)