Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 669ffe38

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

Refactoring after code review

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, Input, Space, Table, Typography } from 'antd';
6
import { Button, 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';
......
12 12
import { DocumentListInfo, UserInfo } from '../../../api';
13 13
import { documentController, userController } from '../../../controllers';
14 14
import AssignDocumentModal from '../../../components/modals/AssignDocumentModal';
15
import { ShowToast } from '../../../utils/alerts';
15
import { ShowConfirmDelete, ShowToast } from '../../../utils/alerts';
16 16
import { TableDocInfo } from '../../../components/types/TableDocInfo';
17
import { SearchOutlined } from '@ant-design/icons';
18 17
import { UserFilter } from '../../../components/types/UserFilter';
18
import { getColumnSearchProps, getLocaleProps } from '../../../utils/tableUtils';
19
import { UserOutlined } from '@ant-design/icons';
20
import Swal from 'sweetalert2';
19 21

  
20 22
function AdminDocumentPage() {
21 23
    const redirecting = useUnauthRedirect('/login');
......
76 78
        setVisibleAssign(false);
77 79
    };
78 80

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

  
86
    const handleReset = (clearFilters: () => void) => {
87
        clearFilters();
81
    const removeUserDocument = (userId: string, documentId: string) => {
82
        Swal.fire({
83
            title: 'Opravdu si přejete uživateli odebrat dokument?',
84
            showCancelButton: true,
85
            confirmButtonText: 'Odebrat',
86
        }).then((result) => {
87
            // TODO call API
88
        });
88 89
    };
89 90

  
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

  
130 91
    const columns = [
131 92
        {
132 93
            title: 'Název dokumentu',
133 94
            dataIndex: 'name',
134 95
            key: 'name',
135 96
            ...getColumnSearchProps('name', 'název'),
97
            sorter: {
98
                // @ts-ignore
99
                compare: (a, b) => a.name.localeCompare(b.name),
100
                multiple: 2,
101
            },
136 102
        },
137 103
        {
138 104
            title: 'Délka',
......
143 109
                compare: (a, b) => a.length - b.length,
144 110
                multiple: 1,
145 111
            },
146
            sortDirections: ['descend', 'ascend'],
147 112
        },
148 113
        {
149 114
            title: 'Anotátoři',
......
152 117
            render: (columnData: UserInfo[], record: DocumentListInfo, index: number) => {
153 118
                return (
154 119
                    <div>
155
                        {columnData.map((e) => (
156
                            <span
157
                                key={e.username + '.' + record.id}
158
                                title={e.username ?? ''}
159
                            >
160
                                <FontAwesomeIcon
161
                                    icon={faUser}
120
                        <Space>
121
                            {columnData.map((e) => (
122
                                <span
123
                                    key={e.username + '.' + record.id}
162 124
                                    title={e.username ?? ''}
163
                                    className={'me-2'}
164
                                />
165
                            </span>
166
                        ))}
125
                                >
126
                                    <FontAwesomeIcon
127
                                        icon={faUser}
128
                                        size={'2x'}
129
                                        onClick={() =>
130
                                            // @ts-ignore
131
                                            removeUserDocument(e.id, record.id)
132
                                        }
133
                                        title={e.username ?? ''}
134
                                        className={'me-2'}
135
                                    />
136
                                </span>
137
                            ))}
138
                        </Space>
167 139
                    </div>
168 140
                );
169 141
            },
......
176 148
            sorter: {
177 149
                // @ts-ignore
178 150
                compare: (a, b) => a.annotatingUsers.length - b.annotatingUsers.length,
179
                multiple: 1,
151
                multiple: 3,
180 152
            },
181
            sortDirections: ['descend', 'ascend'],
182 153
        },
183 154
    ];
184 155

  
......
204 175
            )}
205 176

  
206 177
            <Table
207
                locale={{
208
                    filterSearchPlaceholder: 'Hledat uživatele',
209
                    triggerDesc: 'Seřadit sestupně',
210
                    triggerAsc: 'Seřadit vzestupně',
211
                    cancelSort: 'Vypnout řazení',
212
                }}
178
                locale={{ ...getLocaleProps() }}
213 179
                rowSelection={{
214 180
                    type: 'checkbox',
215 181
                    ...rowSelection,

Také k dispozici: Unified diff