1
|
import { Button, Form, Input, Modal, Radio } from 'antd';
|
2
|
import 'antd/dist/antd.css';
|
3
|
import React from 'react';
|
4
|
import { ChangeUserInfoRequest, ERole, UserInfo } from '../../api';
|
5
|
import { userController } from '../../controllers';
|
6
|
import { ShowToast } from '../../utils/alerts';
|
7
|
|
8
|
interface ModalProps {
|
9
|
onCancel: () => void;
|
10
|
userInfo: UserInfo;
|
11
|
}
|
12
|
|
13
|
/**
|
14
|
* Creates a modal window that loads documents to the app.
|
15
|
* @returns The modal window.
|
16
|
*/
|
17
|
const EditUserModal: React.FC<ModalProps> = ({ onCancel, userInfo }: ModalProps) => {
|
18
|
/**
|
19
|
* Handles successful closing of the modal window.
|
20
|
*/
|
21
|
const handleOk = () => {
|
22
|
onCancel();
|
23
|
};
|
24
|
|
25
|
/**
|
26
|
* Handles cancelling of the model window.
|
27
|
*/
|
28
|
const handleCancel = () => {
|
29
|
onCancel();
|
30
|
};
|
31
|
|
32
|
const onFinish = async (values: any) => {
|
33
|
const req: ChangeUserInfoRequest = {
|
34
|
username: values.username,
|
35
|
password: values.password,
|
36
|
name: values.name,
|
37
|
surname: values.surname,
|
38
|
role: values.role,
|
39
|
};
|
40
|
if (userInfo.id) {
|
41
|
await userController.userUserIdPut(userInfo.id, req);
|
42
|
}
|
43
|
onCancel();
|
44
|
};
|
45
|
|
46
|
const onFinishFailed = (errorInfo: any) => {
|
47
|
ShowToast('Zadané údaje nejsou validní', 'error', 3000, 'top-end');
|
48
|
};
|
49
|
|
50
|
return (
|
51
|
<Modal
|
52
|
title="Upravit uživatele"
|
53
|
onOk={handleOk}
|
54
|
visible={true}
|
55
|
onCancel={handleCancel}
|
56
|
footer={null}
|
57
|
>
|
58
|
<Form
|
59
|
onFinish={onFinish}
|
60
|
onFinishFailed={onFinishFailed}
|
61
|
autoComplete="off"
|
62
|
labelCol={{ span: 4 }}
|
63
|
layout="horizontal"
|
64
|
>
|
65
|
<Form.Item
|
66
|
label=""
|
67
|
name="role"
|
68
|
rules={[
|
69
|
{
|
70
|
required: true,
|
71
|
message: 'Prosím vyberte roli',
|
72
|
},
|
73
|
]}
|
74
|
>
|
75
|
<Radio.Group>
|
76
|
<Radio.Button value={ERole.Annotator}>Anotátor</Radio.Button>
|
77
|
<Radio.Button value={ERole.Administrator}>Admin</Radio.Button>
|
78
|
</Radio.Group>
|
79
|
</Form.Item>
|
80
|
<Form.Item
|
81
|
name="username"
|
82
|
initialValue={userInfo.username}
|
83
|
rules={[
|
84
|
{
|
85
|
required: true,
|
86
|
message: 'Prosím zadejte uživatelské jméno',
|
87
|
},
|
88
|
]}
|
89
|
>
|
90
|
<Input placeholder="Uživatelské jméno" />
|
91
|
</Form.Item>
|
92
|
<Form.Item
|
93
|
name="password"
|
94
|
rules={[{ required: true, message: 'Prosím zadejte heslo' }]}
|
95
|
>
|
96
|
<Input.Password placeholder="Heslo" />
|
97
|
</Form.Item>
|
98
|
|
99
|
<Form.Item
|
100
|
name="name"
|
101
|
initialValue={userInfo.name}
|
102
|
rules={[
|
103
|
{
|
104
|
required: true,
|
105
|
message: 'Prosím zadejte jméno',
|
106
|
},
|
107
|
]}
|
108
|
>
|
109
|
<Input placeholder="Jméno" />
|
110
|
</Form.Item>
|
111
|
|
112
|
<Form.Item
|
113
|
name="surname"
|
114
|
initialValue={userInfo.surname}
|
115
|
rules={[
|
116
|
{
|
117
|
required: true,
|
118
|
message: 'Prosím zadejte příjmení',
|
119
|
},
|
120
|
]}
|
121
|
>
|
122
|
<Input placeholder="Příjmení" />
|
123
|
</Form.Item>
|
124
|
<Form.Item>
|
125
|
<Button type="primary" htmlType="submit" className="w-100">
|
126
|
Upravit
|
127
|
</Button>
|
128
|
</Form.Item>
|
129
|
</Form>
|
130
|
</Modal>
|
131
|
);
|
132
|
};
|
133
|
|
134
|
export default EditUserModal;
|