Revize 669ffe38
Přidáno uživatelem Jaroslav Hrubý před asi 2 roky(ů)
webapp/pages/users/index.tsx | ||
---|---|---|
3 | 3 |
|
4 | 4 |
import { useUnauthRedirect } from '../../hooks'; |
5 | 5 |
import { useRouter } from 'next/router'; |
6 |
import { Badge, Button, Input, Space, Table, Typography } from 'antd';
|
|
6 |
import { Badge, Button, Dropdown, Menu, Space, Table, Typography } from 'antd';
|
|
7 | 7 |
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
8 | 8 |
import { faUsers } from '@fortawesome/free-solid-svg-icons'; |
9 | 9 |
import { LoggedUserContext } from '../../contexts/LoggedUserContext'; |
... | ... | |
12 | 12 |
import { userController } from '../../controllers'; |
13 | 13 |
import AddUserModal from '../../components/modals/AddUserModal'; |
14 | 14 |
import UserDocumentsModal from '../../components/modals/UserDocumentsModal'; |
15 |
import { ShowConfirmDelete } from '../../utils/alerts'; |
|
15 |
import { ShowConfirmDelete, ShowToast } from '../../utils/alerts';
|
|
16 | 16 |
import EditUserModal from '../../components/modals/EditUserModal'; |
17 |
import { SearchOutlined } from '@ant-design/icons'; |
|
17 |
import { DeleteOutlined, EditOutlined, LockOutlined } from '@ant-design/icons'; |
|
18 |
import { getColumnSearchProps, getLocaleProps } from '../../utils/tableUtils'; |
|
19 |
import EditUserPasswordModal from '../../components/modals/EditUserPasswordModal'; |
|
18 | 20 |
|
19 | 21 |
function UsersPage() { |
20 | 22 |
const redirecting = useUnauthRedirect('/login'); |
... | ... | |
23 | 25 |
|
24 | 26 |
const [addVisible, setAddVisible] = React.useState(false); |
25 | 27 |
const [editVisible, setEditVisible] = React.useState(false); |
28 |
const [passwordEditVisible, setPasswordEditVisible] = React.useState(false); |
|
26 | 29 |
const [docsVisible, setDocsVisible] = React.useState(false); |
27 | 30 |
const [users, setUsers] = useState<UserInfo[]>([]); |
28 | 31 |
|
... | ... | |
52 | 55 |
|
53 | 56 |
const deleteUser = (userId: string) => { |
54 | 57 |
ShowConfirmDelete(() => { |
55 |
userController.userUserIdDelete(userId).then(() => fetchData()); |
|
58 |
userController.userUserIdDelete(userId).then(() => { |
|
59 |
ShowToast('Uživatel úspěšně odstraněn', 'success', 3000, 'top-end'); |
|
60 |
fetchData(); |
|
61 |
}); |
|
56 | 62 |
}, 'uživatele'); |
57 | 63 |
}; |
58 | 64 |
|
... | ... | |
63 | 69 |
setUserInfo(userId); |
64 | 70 |
setEditVisible(true); |
65 | 71 |
}; |
72 |
const showEditPasswordModal = (userId: string) => { |
|
73 |
setUserInfo(userId); |
|
74 |
setPasswordEditVisible(true); |
|
75 |
}; |
|
66 | 76 |
const showDocsModal = (userId: string) => { |
67 | 77 |
setUserInfo(userId); |
68 | 78 |
setDocsVisible(true); |
... | ... | |
73 | 83 |
setAddVisible(false); |
74 | 84 |
setDocsVisible(false); |
75 | 85 |
setEditVisible(false); |
86 |
setPasswordEditVisible(false); |
|
76 | 87 |
}; |
77 | 88 |
|
78 |
const handleSearch = ( |
|
79 |
selectedKeys: React.SetStateAction<string>[], |
|
80 |
confirm: () => void |
|
81 |
) => { |
|
82 |
confirm(); |
|
83 |
}; |
|
84 |
|
|
85 |
const handleReset = (clearFilters: () => void) => { |
|
86 |
clearFilters(); |
|
87 |
}; |
|
88 |
|
|
89 |
const getColumnSearchProps = (dataIndex: string, searchLabel: string) => ({ |
|
90 |
// @ts-ignore |
|
91 |
filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => ( |
|
92 |
<div style={{ padding: 8 }}> |
|
93 |
<Input |
|
94 |
placeholder={`Vyhledat ${searchLabel}`} |
|
95 |
value={selectedKeys[0]} |
|
96 |
onChange={(e) => |
|
97 |
setSelectedKeys(e.target.value ? [e.target.value] : []) |
|
98 |
} |
|
99 |
onPressEnter={() => handleSearch(selectedKeys, confirm)} |
|
100 |
style={{ marginBottom: 8, display: 'block' }} |
|
101 |
/> |
|
102 |
<Space> |
|
103 |
<Button |
|
104 |
type="primary" |
|
105 |
onClick={() => handleSearch(selectedKeys, confirm)} |
|
106 |
icon={<SearchOutlined />} |
|
107 |
style={{ width: 90 }} |
|
108 |
> |
|
109 |
Hledat |
|
110 |
</Button> |
|
111 |
<Button |
|
112 |
onClick={() => handleReset(clearFilters)} |
|
113 |
style={{ width: 90 }} |
|
114 |
> |
|
115 |
Smazat |
|
116 |
</Button> |
|
117 |
</Space> |
|
118 |
</div> |
|
119 |
), |
|
120 |
filterIcon: (filtered: any) => ( |
|
121 |
<SearchOutlined style={{ color: filtered ? '#1890ff' : undefined }} /> |
|
122 |
), |
|
123 |
onFilter: (value: string, record: { [x: string]: { toString: () => string } }) => |
|
124 |
record[dataIndex] |
|
125 |
? record[dataIndex].toString().toLowerCase().includes(value.toLowerCase()) |
|
126 |
: '', |
|
127 |
}); |
|
89 |
const menu = (id: string) => ( |
|
90 |
<Menu> |
|
91 |
<Menu.Item |
|
92 |
icon={<EditOutlined />} |
|
93 |
key={'edit'} |
|
94 |
onClick={() => showEditModal(id)} |
|
95 |
> |
|
96 |
Upravit |
|
97 |
</Menu.Item> |
|
98 |
<Menu.Item icon={<LockOutlined />} onClick={() => showEditPasswordModal(id)}> |
|
99 |
Změnit Heslo |
|
100 |
</Menu.Item> |
|
101 |
<Menu.Item icon={<DeleteOutlined />} onClick={() => deleteUser(id)}> |
|
102 |
Smazat |
|
103 |
</Menu.Item> |
|
104 |
</Menu> |
|
105 |
); |
|
128 | 106 |
|
129 | 107 |
const columns = [ |
130 | 108 |
{ |
... | ... | |
132 | 110 |
dataIndex: 'name', |
133 | 111 |
key: 'name', |
134 | 112 |
...getColumnSearchProps('name', 'jméno'), |
113 |
// @ts-ignore |
|
114 |
sorter: (a, b) => a.name.localeCompare(b.name), |
|
135 | 115 |
}, |
136 | 116 |
{ |
137 | 117 |
title: 'Příjmení', |
138 | 118 |
dataIndex: 'surname', |
139 | 119 |
key: 'surname', |
140 | 120 |
...getColumnSearchProps('surname', 'příjmení'), |
121 |
// @ts-ignore |
|
122 |
sorter: (a, b) => a.surname.localeCompare(b.surname), |
|
141 | 123 |
}, |
142 | 124 |
{ |
143 | 125 |
title: 'Uživatelské jméno', |
144 | 126 |
dataIndex: 'username', |
145 | 127 |
key: 'username', |
146 | 128 |
...getColumnSearchProps('username', 'uživatelské jméno'), |
129 |
// @ts-ignore |
|
130 |
sorter: (a, b) => a.username.localeCompare(b.username), |
|
131 |
}, |
|
132 |
{ |
|
133 |
title: 'Role', |
|
134 |
dataIndex: 'role', |
|
135 |
key: 'role', |
|
136 |
filters: [ |
|
137 |
{ |
|
138 |
text: 'Administrátor', |
|
139 |
value: 'ADMINISTRATOR', |
|
140 |
}, |
|
141 |
{ |
|
142 |
text: 'Anotátor', |
|
143 |
value: 'ANNOTATOR', |
|
144 |
}, |
|
145 |
], |
|
146 |
// @ts-ignore |
|
147 |
onFilter: (value, record) => record.role.indexOf(value) === 0, |
|
148 |
// @ts-ignore |
|
149 |
sorter: (a, b) => a.username.localeCompare(b.username), |
|
150 |
render: (role: string) => { |
|
151 |
let label = ''; |
|
152 |
if (role === 'ANNOTATOR') { |
|
153 |
label = 'Anotátor'; |
|
154 |
} |
|
155 |
if (role === 'ADMINISTRATOR') { |
|
156 |
label = 'Administrátor'; |
|
157 |
} |
|
158 |
return label; |
|
159 |
}, |
|
147 | 160 |
}, |
148 | 161 |
{ |
149 | 162 |
title: 'Přiřazeno dokumentů', |
... | ... | |
169 | 182 |
title: '', |
170 | 183 |
key: 'operations', |
171 | 184 |
dataIndex: 'id', |
172 |
align: 'center' as 'center', |
|
185 |
align: 'right' as 'right', |
|
186 |
width: '150px', |
|
173 | 187 |
// @ts-ignore |
174 | 188 |
render: (id) => ( |
175 | 189 |
<div> |
176 | 190 |
<Space size={'middle'}> |
177 |
<Button onClick={() => showEditModal(id)}>Upravit</Button>
|
|
178 |
<Button onClick={() => deleteUser(id)}>Smazat</Button>
|
|
191 |
{/*{menu(id)}*/}
|
|
192 |
{<Dropdown.Button overlay={menu(id)} placement="bottom" />}
|
|
179 | 193 |
</Space> |
180 | 194 |
</div> |
181 | 195 |
), |
... | ... | |
193 | 207 |
</Button> |
194 | 208 |
{addVisible && <AddUserModal onCancel={hideModals} />} |
195 | 209 |
{editVisible && <EditUserModal userInfo={user} onCancel={hideModals} />} |
210 |
{passwordEditVisible && ( |
|
211 |
<EditUserPasswordModal userInfo={user} onCancel={hideModals} /> |
|
212 |
)} |
|
196 | 213 |
{docsVisible && <UserDocumentsModal userId={user.id} onCancel={hideModals} />} |
197 | 214 |
|
198 | 215 |
<Table |
199 |
locale={{ |
|
200 |
triggerDesc: 'Seřadit sestupně', |
|
201 |
triggerAsc: 'Seřadit vzestupně', |
|
202 |
cancelSort: 'Vypnout řazení', |
|
203 |
}} |
|
216 |
locale={{ ...getLocaleProps() }} |
|
204 | 217 |
// @ts-ignore |
205 | 218 |
columns={columns} |
206 | 219 |
dataSource={users} |
207 |
scroll={{ y: 500 }}
|
|
220 |
scroll={{ y: 800 }}
|
|
208 | 221 |
/> |
209 | 222 |
</MainLayout> |
210 | 223 |
); |
Také k dispozici: Unified diff
Refactoring after code review