Projekt

Obecné

Profil

Stáhnout (6.08 KB) Statistiky
| Větev: | Tag: | Revize:
1
import 'antd/dist/antd.css';
2
import React, { useContext, useEffect, useState } from 'react';
3

    
4
import { useUnauthRedirect } from '../../../hooks';
5
import { useRouter } from 'next/router';
6
import { Button, Input, Space, Table, Tag, Typography } from 'antd';
7
import { faFileLines } from '@fortawesome/free-solid-svg-icons';
8
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
9
import { LoggedUserContext } from '../../../contexts/LoggedUserContext';
10
import { MainLayout } from '../../../layouts/MainLayout';
11
import { userController } from '../../../controllers';
12
import { AnnotationListInfo } from '../../../api';
13
import {
14
    CheckCircleOutlined,
15
    ClockCircleOutlined,
16
    SearchOutlined,
17
    SyncOutlined,
18
} from '@ant-design/icons';
19

    
20
function UserDocumentPage() {
21
    const redirecting = useUnauthRedirect('/login');
22
    const { logout, role } = useContext(LoggedUserContext);
23
    const [documents, setDocuments] = useState<AnnotationListInfo[]>([]);
24
    const router = useRouter();
25

    
26
    useEffect(() => {
27
        async function fetchData() {
28
            let docs = (await userController.userAnnotationsGet()).data.annotations;
29
            if (!docs) {
30
                setDocuments([]);
31
            } else {
32
                setDocuments(docs);
33
            }
34
        }
35

    
36
        if (!redirecting /* && role === 'ANNOTATOR'*/) {
37
            // admin can also fetch data if he likes so
38
            fetchData();
39
        }
40
    }, [logout, redirecting, role, router]);
41

    
42
    const handleSearch = (
43
        selectedKeys: React.SetStateAction<string>[],
44
        confirm: () => void
45
    ) => {
46
        confirm();
47
    };
48

    
49
    const handleReset = (clearFilters: () => void) => {
50
        clearFilters();
51
    };
52

    
53
    const getColumnSearchProps = (dataIndex: string, searchLabel: string) => ({
54
        // @ts-ignore
55
        filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
56
            <div style={{ padding: 8 }}>
57
                <Input
58
                    placeholder={`Vyhledat ${searchLabel}`}
59
                    value={selectedKeys[0]}
60
                    onChange={(e) =>
61
                        setSelectedKeys(e.target.value ? [e.target.value] : [])
62
                    }
63
                    onPressEnter={() => handleSearch(selectedKeys, confirm)}
64
                    style={{ marginBottom: 8, display: 'block' }}
65
                />
66
                <Space>
67
                    <Button
68
                        type="primary"
69
                        onClick={() => handleSearch(selectedKeys, confirm)}
70
                        icon={<SearchOutlined />}
71
                        style={{ width: 90 }}
72
                    >
73
                        Hledat
74
                    </Button>
75
                    <Button
76
                        onClick={() => handleReset(clearFilters)}
77
                        style={{ width: 90 }}
78
                    >
79
                        Smazat
80
                    </Button>
81
                </Space>
82
            </div>
83
        ),
84
        filterIcon: (filtered: any) => (
85
            <SearchOutlined style={{ color: filtered ? '#1890ff' : undefined }} />
86
        ),
87
        onFilter: (value: string, record: { [x: string]: { toString: () => string } }) =>
88
            record[dataIndex]
89
                ? record[dataIndex].toString().toLowerCase().includes(value.toLowerCase())
90
                : '',
91
    });
92

    
93
    const columns = [
94
        {
95
            title: 'Název dokumentu',
96
            dataIndex: 'documentName',
97
            key: 'documentName',
98
            ...getColumnSearchProps('documentName', 'název'),
99
        },
100
        {
101
            title: 'Stav anotace',
102
            key: 'state',
103
            dataIndex: 'state',
104
            render: (state: string) => {
105
                let color = 'green';
106
                let label = 'Hotovo';
107
                let icon = <CheckCircleOutlined />;
108
                if (state === 'NEW') {
109
                    color = 'volcano';
110
                    label = 'Nový';
111
                    icon = <ClockCircleOutlined />;
112
                }
113
                if (state === 'IN_PROGRESS') {
114
                    color = 'orange';
115
                    label = 'Rozpracováno';
116
                    icon = <SyncOutlined spin />;
117
                }
118
                return (
119
                    <Tag icon={icon} color={color} key={label}>
120
                        {label.toUpperCase()}
121
                    </Tag>
122
                );
123
            },
124
            filters: [
125
                {
126
                    text: 'Nový',
127
                    value: 'NEW',
128
                },
129
                {
130
                    text: 'Rozpracováno',
131
                    value: 'IN_PROGRESS',
132
                },
133
                {
134
                    text: 'Hotovo',
135
                    value: 'DONE',
136
                },
137
            ],
138
            // @ts-ignore
139
            onFilter: (value, record) => record.state.indexOf(value) === 0,
140
            // @ts-ignore
141
            sorter: (a, b) => a.state.length - b.state.length,
142
            sortDirections: ['descend', 'ascend'],
143
        },
144
        {
145
            title: '',
146
            key: 'action',
147
            dataIndex: 'annotationId',
148
            render: (anotationId: number) => (
149
                <Button
150
                    key={anotationId}
151
                    type={'primary'}
152
                    onClick={() => router.push('/annotation/' + anotationId)}
153
                >
154
                    Anotovat
155
                </Button>
156
            ),
157
        },
158
    ];
159

    
160
    return redirecting /*|| role !== 'ANNOTATOR' */ ? null : (
161
        <MainLayout>
162
            <Typography.Title level={2}>
163
                <FontAwesomeIcon icon={faFileLines} /> Dokumenty
164
            </Typography.Title>
165
            <Table
166
                locale={{
167
                    triggerDesc: 'Seřadit sestupně',
168
                    triggerAsc: 'Seřadit vzestupně',
169
                    cancelSort: 'Vypnout řazení',
170
                }}
171
                // @ts-ignore
172
                columns={columns}
173
                // @ts-ignore
174
                dataSource={documents}
175
                size="small"
176
                scroll={{ y: 500 }}
177
            />
178
        </MainLayout>
179
    );
180
}
181

    
182
export default UserDocumentPage;
    (1-1/1)