Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 0db53e25

Přidáno uživatelem Jaroslav Hrubý před asi 2 roky(ů)

Document filtering implemented

Zobrazit rozdíly:

webapp/pages/documents/admin/index.tsx
3 3

  
4 4
import { useUnauthRedirect } from '../../../hooks';
5 5
import { useRouter } from 'next/router';
6
import { Button, Table, Tag, Typography } from 'antd';
6
import { Button, Input, Space, Table, Typography } from 'antd';
7 7
import { faFileLines, faUser } from '@fortawesome/free-solid-svg-icons';
8 8
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
9 9
import { LoggedUserContext } from '../../../contexts/LoggedUserContext';
10 10
import { MainLayout } from '../../../layouts/MainLayout';
11 11
import AddDocumentModal from '../../../components/modals/AddDocumentModal';
12
import { AnnotationListInfo, DocumentListInfo, UserInfo } from '../../../api';
12
import { DocumentListInfo, UserInfo } from '../../../api';
13 13
import { documentController, userController } from '../../../controllers';
14 14
import AssignDocumentModal from '../../../components/modals/AssignDocumentModal';
15 15
import { ShowToast } from '../../../utils/alerts';
16 16
import { TableDocInfo } from '../../../components/types/TableDocInfo';
17
import { SearchOutlined } from '@ant-design/icons';
18
import { UserFilter } from '../../../components/types/UserFilter';
17 19

  
18 20
function AdminDocumentPage() {
19 21
    const redirecting = useUnauthRedirect('/login');
......
23 25
    const router = useRouter();
24 26

  
25 27
    const [documents, setDocuments] = useState<TableDocInfo[]>([]);
28
    const [userFilters, setUserFilters] = useState<UserFilter[]>([]);
26 29
    const [selectedDocs, setSelectedDocs] = useState<string[] | undefined[]>([]);
27 30

  
28 31
    async function fetchData() {
29 32
        const docs = (await documentController.documentsGet(0, 1000)).data.documents;
30

  
31 33
        // @ts-ignore
32 34
        const tableDocs: TableDocInfo[] = docs?.map((doc, index) => {
33 35
            return { key: index, ...doc };
34 36
        });
35 37

  
38
        const users = (await userController.usersGet()).data.users;
39
        // @ts-ignore
40
        const filters: UserFilter[] = users?.map((user) => {
41
            return {
42
                text: user.name + ' ' + user.surname,
43
                value: user.username,
44
            };
45
        });
46
        setUserFilters(filters);
47

  
36 48
        if (!docs) {
37 49
            setDocuments([]);
38 50
        } else {
......
64 76
        setVisibleAssign(false);
65 77
    };
66 78

  
79
    const handleSearch = (
80
        selectedKeys: React.SetStateAction<string>[],
81
        confirm: () => void
82
    ) => {
83
        confirm();
84
    };
85

  
86
    const handleReset = (clearFilters: () => void) => {
87
        clearFilters();
88
    };
89

  
90
    const getColumnSearchProps = (dataIndex: string, searchLabel: string) => ({
91
        // @ts-ignore
92
        filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
93
            <div style={{ padding: 8 }}>
94
                <Input
95
                    placeholder={`Vyhledat ${searchLabel}`}
96
                    value={selectedKeys[0]}
97
                    onChange={(e) =>
98
                        setSelectedKeys(e.target.value ? [e.target.value] : [])
99
                    }
100
                    onPressEnter={() => handleSearch(selectedKeys, confirm)}
101
                    style={{ marginBottom: 8, display: 'block' }}
102
                />
103
                <Space>
104
                    <Button
105
                        type="primary"
106
                        onClick={() => handleSearch(selectedKeys, confirm)}
107
                        icon={<SearchOutlined />}
108
                        style={{ width: 90 }}
109
                    >
110
                        Hledat
111
                    </Button>
112
                    <Button
113
                        onClick={() => handleReset(clearFilters)}
114
                        style={{ width: 90 }}
115
                    >
116
                        Smazat
117
                    </Button>
118
                </Space>
119
            </div>
120
        ),
121
        filterIcon: (filtered: any) => (
122
            <SearchOutlined style={{ color: filtered ? '#1890ff' : undefined }} />
123
        ),
124
        onFilter: (value: string, record: { [x: string]: { toString: () => string } }) =>
125
            record[dataIndex]
126
                ? record[dataIndex].toString().toLowerCase().includes(value.toLowerCase())
127
                : '',
128
    });
129

  
67 130
    const columns = [
68 131
        {
69 132
            title: 'Název dokumentu',
70 133
            dataIndex: 'name',
71 134
            key: 'name',
135
            ...getColumnSearchProps('name', 'název'),
72 136
        },
73 137
        {
74 138
            title: 'Délka',
75 139
            dataIndex: 'length',
76 140
            key: 'length',
141
            sorter: {
142
                // @ts-ignore
143
                compare: (a, b) => a.length - b.length,
144
                multiple: 1,
145
            },
146
            sortDirections: ['descend', 'ascend'],
77 147
        },
78 148
        {
79 149
            title: 'Anotátoři',
......
97 167
                    </div>
98 168
                );
99 169
            },
170
            filters: userFilters,
171
            filterSearch: true,
172
            // @ts-ignore
173
            onFilter: (value, record) =>
174
                // @ts-ignore
175
                record.annotatingUsers.find((user) => user['username'] === value),
176
            sorter: {
177
                // @ts-ignore
178
                compare: (a, b) => a.annotatingUsers.length - b.annotatingUsers.length,
179
                multiple: 1,
180
            },
181
            sortDirections: ['descend', 'ascend'],
100 182
        },
101 183
    ];
102 184

  
......
122 204
            )}
123 205

  
124 206
            <Table
207
                locale={{
208
                    filterSearchPlaceholder: 'Hledat uživatele',
209
                    triggerDesc: 'Seřadit sestupně',
210
                    triggerAsc: 'Seřadit vzestupně',
211
                    cancelSort: 'Vypnout řazení',
212
                }}
125 213
                rowSelection={{
126 214
                    type: 'checkbox',
127 215
                    ...rowSelection,
128 216
                }}
217
                // @ts-ignore
129 218
                columns={columns}
130 219
                dataSource={documents}
131 220
                size="middle"

Také k dispozici: Unified diff