Projekt

Obecné

Profil

Stáhnout (6.37 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Badge, Button, Checkbox, Dropdown, List, Modal, Space, Table } from 'antd';
2
import 'antd/dist/antd.css';
3
import React, { useContext, useEffect, useState } from 'react';
4
import { DocumentListInfo, UserInfo } from '../../api';
5
import {
6
    annotationController,
7
    documentController,
8
    userController,
9
} from '../../controllers';
10
import { useUnauthRedirect } from '../../hooks';
11
import { LoggedUserContext } from '../../contexts/LoggedUserContext';
12
import { ShowToast } from '../../utils/alerts';
13
import { getColumnSearchProps, getLocaleProps } from '../../utils/tableUtils';
14
import { TableUserInfo } from '../types/TableUserInfo';
15
import { TableDocInfo } from '../types/TableDocInfo';
16
import { ABadge, BadgeStyle } from '../common/ABadge';
17
import { ok } from 'assert';
18

    
19
interface ModalProps {
20
    onCancel: (ok: boolean) => void;
21
    documentsIds: (string | undefined)[];
22
}
23

    
24
function AssignDocumentModal({ onCancel, documentsIds }: ModalProps) {
25
    const redirecting = useUnauthRedirect('/login');
26
    const { role } = useContext(LoggedUserContext);
27
    const [users, setUsers] = useState<TableUserInfo[]>([]);
28
    const [selectedUsers, setSelectedUsers] = useState<string[]>([]);
29

    
30
    const handleOk = () => {
31
        onCancel(true);
32
    };
33

    
34
    const handleCancel = () => {
35
        onCancel(false);
36
    };
37

    
38
    async function fetchData() {
39
        if (!redirecting && role === 'ADMINISTRATOR') {
40
            const usrs = (await userController.usersGet()).data.users;
41
            // @ts-ignore
42
            const tableUsers: TableUserInfo[] = usrs?.map((usr, index) => {
43
                return { key: index, ...usr };
44
            });
45
            setUsers(tableUsers || []);
46
        }
47
    }
48

    
49
    useEffect(() => {
50
        fetchData();
51
    }, [documentsIds, redirecting, role]);
52

    
53
    const handleUpload = async () => {
54
        // console.log(documentsIds, selectedUsers);
55
        if (documentsIds && documentsIds.length !== 0) {
56
            await annotationController
57
                .annotationsPost({
58
                    // @ts-ignore
59
                    documentIdList: documentsIds,
60
                    userIdList: selectedUsers,
61
                })
62
                .then((r) => {
63
                    ShowToast(
64
                        'Přiřazení dokumentů proběhlo úspěšně',
65
                        'success',
66
                        3000,
67
                        'top-end'
68
                    );
69
                    onCancel(true);
70
                });
71
        }
72
    };
73

    
74
    const rowSelection = {
75
        onChange: (selectedRowKeys: React.Key[], selectedRows: UserInfo[]) => {
76
            // @ts-ignore
77
            setSelectedUsers(selectedRows.map((row) => row.id));
78
        },
79
    };
80

    
81
    const columns = [
82
        {
83
            title: 'Jméno',
84
            dataIndex: 'name',
85
            key: 'name',
86
            ...getColumnSearchProps('name', 'jméno'),
87
            // @ts-ignore
88
            sorter: (a, b) => a.name.localeCompare(b.name),
89
        },
90
        {
91
            title: 'Příjmení',
92
            dataIndex: 'surname',
93
            key: 'surname',
94
            ...getColumnSearchProps('surname', 'příjmení'),
95
            // @ts-ignore
96
            sorter: (a, b) => a.surname.localeCompare(b.surname),
97
        },
98
        {
99
            title: 'Uživatelské jméno',
100
            dataIndex: 'username',
101
            key: 'username',
102
            ...getColumnSearchProps('username', 'uživatelské jméno'),
103
            // @ts-ignore
104
            sorter: (a, b) => a.username.localeCompare(b.username),
105
        },
106
        {
107
            title: 'Role',
108
            dataIndex: 'role',
109
            key: 'role',
110
            filters: [
111
                {
112
                    text: 'Administrátor',
113
                    value: 'ADMINISTRATOR',
114
                },
115
                {
116
                    text: 'Anotátor',
117
                    value: 'ANNOTATOR',
118
                },
119
            ],
120
            // @ts-ignore
121
            onFilter: (value, record) => record.role.indexOf(value) === 0,
122
            // @ts-ignore
123
            sorter: (a, b) => a.username.localeCompare(b.username),
124
            render: (role: string) => {
125
                let label = '';
126
                if (role === 'ANNOTATOR') {
127
                    label = 'Anotátor';
128
                }
129
                if (role === 'ADMINISTRATOR') {
130
                    label = 'Administrátor';
131
                }
132
                return label;
133
            },
134
        },
135
        {
136
            title: 'Aktivní | Hotovo | Celkem',
137
            key: 'docCounts',
138
            align: 'center' as 'center',
139
            dataIndex: 'assignedDocumentsCount',
140
            // @ts-ignore
141
            render: (text, row) => (
142
                <div>
143
                    <ABadge style={BadgeStyle.WARNING}>
144
                        {row.newDocumentsCount + row.inProgressDocumentsCount}
145
                    </ABadge>
146
                    {' | '}
147
                    <ABadge style={BadgeStyle.SUCCESS}>
148
                        {row.finalizedDocumentsCount}
149
                    </ABadge>
150
                    {' | '}
151
                    <ABadge style={BadgeStyle.GENERAL}>
152
                        {row.finalizedDocumentsCount +
153
                            row.newDocumentsCount +
154
                            row.inProgressDocumentsCount}
155
                    </ABadge>
156
                </div>
157
            ),
158
            // @ts-ignore
159
            sorter: (a, b) =>
160
                a.newDocumentsCount +
161
                a.inProgressDocumentsCount -
162
                (b.newDocumentsCount + b.inProgressDocumentsCount),
163
        },
164
    ];
165

    
166
    return (
167
        <Modal
168
            title="Přiřadit dokumenty"
169
            onOk={handleOk}
170
            visible={true}
171
            onCancel={handleCancel}
172
            width={1100}
173
            footer={[
174
                <Button key="back" onClick={handleCancel}>
175
                    Storno
176
                </Button>,
177
                <Button key="submit" type="primary" onClick={handleUpload}>
178
                    Přiřadit
179
                </Button>,
180
            ]}
181
        >
182
            <Table
183
                locale={{ ...getLocaleProps() }}
184
                rowSelection={{
185
                    type: 'checkbox',
186
                    ...rowSelection,
187
                }}
188
                // @ts-ignore
189
                columns={columns}
190
                dataSource={users}
191
                pagination={false}
192
                scroll={{ y: 400 }}
193
            />
194
        </Modal>
195
    );
196
}
197

    
198
export default AssignDocumentModal;
(3-3/11)