-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
user-management.tsx
197 lines (182 loc) · 7.68 KB
/
user-management.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import React, { useState, useEffect } from 'react';
import { connect } from 'react-redux';
import { Link, RouteComponentProps } from 'react-router-dom';
import { Button, Table, Row, Badge } from 'reactstrap';
import { Translate, TextFormat, JhiPagination, JhiItemCount, getSortState } from 'react-jhipster';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { APP_DATE_FORMAT } from 'app/config/constants';
import { ITEMS_PER_PAGE } from 'app/shared/util/pagination.constants';
import { getUsers, updateUser } from './user-management.reducer';
import { IRootState } from 'app/shared/reducers';
export interface IUserManagementProps extends StateProps, DispatchProps, RouteComponentProps<{}> {}
export const UserManagement = (props: IUserManagementProps) => {
const [pagination, setPagination] = useState(getSortState(props.location, ITEMS_PER_PAGE));
const getAllUsers = () => props.getUsers(pagination.activePage - 1, pagination.itemsPerPage, `${pagination.sort},${pagination.order}`);
useEffect(() => {
getAllUsers();
}, []);
const sortUsers = () => getAllUsers();
useEffect(() => {
sortUsers();
}, [pagination.activePage, pagination.order, pagination.sort]);
const sort = p => () => {
setPagination({
...pagination,
order: pagination.order === 'asc' ? 'desc' : 'asc',
sort: p
});
props.history.push(`${props.location.pathname}?page=${pagination.activePage}&sort=${pagination.sort},${pagination.order}`);
};
const handlePagination = currentPage =>
setPagination({
...pagination,
activePage: currentPage
});
const toggleActive = user => () =>
props.updateUser({
...user,
activated: !user.activated
});
const { users, account, match, totalItems } = props;
return (
<div>
<h2 id="user-management-page-heading">
<Translate contentKey="userManagement.home.title">Users</Translate>
<Link to={`${match.url}/new`} className="btn btn-primary float-right jh-create-entity">
<FontAwesomeIcon icon="plus" /> <Translate contentKey="userManagement.home.createLabel">Create a new user</Translate>
</Link>
</h2>
<Table responsive striped>
<thead>
<tr>
<th className="hand" onClick={sort('id')}>
<Translate contentKey="global.field.id">ID</Translate>
<FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={sort('login')}>
<Translate contentKey="userManagement.login">Login</Translate>
<FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={sort('email')}>
<Translate contentKey="userManagement.email">Email</Translate>
<FontAwesomeIcon icon="sort" />
</th>
<th />
<th className="hand" onClick={sort('langKey')}>
<Translate contentKey="userManagement.langKey">Lang Key</Translate>
<FontAwesomeIcon icon="sort" />
</th>
<th>
<Translate contentKey="userManagement.profiles">Profiles</Translate>
</th>
<th className="hand" onClick={sort('createdDate')}>
<Translate contentKey="userManagement.createdDate">Created Date</Translate>
<FontAwesomeIcon icon="sort" />
</th>
<th className="hand" onClick={sort('lastModifiedBy')}>
<Translate contentKey="userManagement.lastModifiedBy">Last Modified By</Translate>
<FontAwesomeIcon icon="sort" />
</th>
<th id="modified-date-sort" className="hand" onClick={sort('lastModifiedDate')}>
<Translate contentKey="userManagement.lastModifiedDate">Last Modified Date</Translate>
<FontAwesomeIcon icon="sort" />
</th>
<th />
</tr>
</thead>
<tbody>
{users.map((user, i) => (
<tr id={user.login} key={`user-${i}`}>
<td>
<Button tag={Link} to={`${match.url}/${user.login}`} color="link" size="sm">
{user.id}
</Button>
</td>
<td>{user.login}</td>
<td>{user.email}</td>
<td>
{user.activated ? (
<Button color="success" onClick={toggleActive(user)}>
Activated
</Button>
) : (
<Button color="danger" onClick={toggleActive(user)}>
Deactivated
</Button>
)}
</td>
<td>{user.langKey}</td>
<td>
{user.authorities
? user.authorities.map((authority, j) => (
<div key={`user-auth-${i}-${j}`}>
<Badge color="info">{authority}</Badge>
</div>
))
: null}
</td>
<td>
<TextFormat value={user.createdDate} type="date" format={APP_DATE_FORMAT} blankOnInvalid />
</td>
<td>{user.lastModifiedBy}</td>
<td>
<TextFormat value={user.lastModifiedDate} type="date" format={APP_DATE_FORMAT} blankOnInvalid />
</td>
<td className="text-right">
<div className="btn-group flex-btn-group-container">
<Button tag={Link} to={`${match.url}/${user.login}`} color="info" size="sm">
<FontAwesomeIcon icon="eye" />{' '}
<span className="d-none d-md-inline">
<Translate contentKey="entity.action.view">View</Translate>
</span>
</Button>
<Button tag={Link} to={`${match.url}/${user.login}/edit`} color="primary" size="sm">
<FontAwesomeIcon icon="pencil-alt" />{' '}
<span className="d-none d-md-inline">
<Translate contentKey="entity.action.edit">Edit</Translate>
</span>
</Button>
<Button
tag={Link}
to={`${match.url}/${user.login}/delete`}
color="danger"
size="sm"
disabled={account.login === user.login}
>
<FontAwesomeIcon icon="trash" />{' '}
<span className="d-none d-md-inline">
<Translate contentKey="entity.action.delete">Delete</Translate>
</span>
</Button>
</div>
</td>
</tr>
))}
</tbody>
</Table>
<div className={users && users.length > 0 ? '' : 'd-none'}>
<Row className="justify-content-center">
<JhiItemCount page={pagination.activePage} total={totalItems} itemsPerPage={pagination.itemsPerPage} i18nEnabled />
</Row>
<Row className="justify-content-center">
<JhiPagination
activePage={pagination.activePage}
onSelect={handlePagination}
maxButtons={5}
itemsPerPage={pagination.itemsPerPage}
totalItems={props.totalItems}
/>
</Row>
</div>
</div>
);
};
const mapStateToProps = (storeState: IRootState) => ({
users: storeState.userManagement.users,
totalItems: storeState.userManagement.totalItems,
account: storeState.authentication.account
});
const mapDispatchToProps = { getUsers, updateUser };
type StateProps = ReturnType<typeof mapStateToProps>;
type DispatchProps = typeof mapDispatchToProps;
export default connect(mapStateToProps, mapDispatchToProps)(UserManagement);