Projekt

Obecné

Profil

Stáhnout (3.31 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Button, Checkbox, List, Modal } from 'antd';
2
import 'antd/dist/antd.css';
3
import React, { useContext, useEffect, useState } from 'react';
4
import { UserInfo } from '../../api';
5
import { annotationController, userController } from '../../controllers';
6
import { useUnauthRedirect } from '../../hooks';
7
import { LoggedUserContext } from '../../contexts/LoggedUserContext';
8
import { ShowToast } from '../../utils/alerts';
9

    
10
interface ModalProps {
11
    onCancel: () => void;
12
    documentsIds: (string | undefined)[];
13
}
14

    
15
function AssignDocumentModal({ onCancel, documentsIds }: ModalProps) {
16
    const redirecting = useUnauthRedirect('/login');
17
    const { role } = useContext(LoggedUserContext);
18
    const [users, setUsers] = useState<UserInfo[]>([]);
19
    const [selectedUsers, setSelectedUsers] = useState<string[]>([]);
20

    
21
    const handleOk = () => {
22
        onCancel();
23
    };
24

    
25
    const handleCancel = () => {
26
        onCancel();
27
    };
28

    
29
    useEffect(() => {
30
        console.log(documentsIds);
31
        if (!redirecting && role === 'ADMINISTRATOR') {
32
            userController.usersGet().then(({ data: { users } }) => {
33
                setUsers(users || []);
34
            });
35
        }
36
    }, [documentsIds, redirecting, role]);
37

    
38
    const handleUpload = async () => {
39
        // console.log(documentsIds, selectedUsers);
40
        if (documentsIds && documentsIds.length !== 0) {
41
            await annotationController
42
                .annotationsPost({
43
                    // @ts-ignore
44
                    documentIdList: documentsIds,
45
                    userIdList: selectedUsers,
46
                })
47
                .then((r) => {
48
                    ShowToast(
49
                        'Přiřazení dokumentů proběhlo úspěšně',
50
                        'success',
51
                        3000,
52
                        'top-end'
53
                    );
54
                    onCancel();
55
                });
56
        }
57
    };
58

    
59
    function onChange(event: any) {
60
        if (event.target.checked) {
61
            selectedUsers.push(event.target.value);
62
        } else {
63
            setSelectedUsers(selectedUsers.filter((usr) => usr !== event.target.value));
64
        }
65
    }
66

    
67
    return (
68
        <Modal
69
            title="Přiřadit dokumenty"
70
            onOk={handleOk}
71
            visible={true}
72
            onCancel={handleCancel}
73
            footer={[
74
                <Button key="back" onClick={handleCancel}>
75
                    Storno
76
                </Button>,
77
                <Button key="submit" type="primary" onClick={handleUpload}>
78
                    Přiřadit
79
                </Button>,
80
            ]}
81
        >
82
            <List
83
                dataSource={users}
84
                size={'small'}
85
                grid={{ gutter: 10, column: 1 }}
86
                style={{ maxHeight: 400, overflowY: 'auto', overflowX: 'hidden' }}
87
                renderItem={(item) => (
88
                    <List.Item key={item.id}>
89
                        <List.Item.Meta
90
                            title={
91
                                <Checkbox onChange={onChange} value={item.id}>
92
                                    {item.name + ' ' + item.surname}
93
                                </Checkbox>
94
                            }
95
                        />
96
                    </List.Item>
97
                )}
98
            />
99
        </Modal>
100
    );
101
}
102

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