Skip to content

Commit 57d29e2

Browse files
committed
Add table header sorting
1 parent d3428d4 commit 57d29e2

File tree

4 files changed

+83
-11
lines changed

4 files changed

+83
-11
lines changed

src/Administration/Users/UsersList/UsersList.js

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import Grid from '@material-ui/core/Grid'
55
import {
66
makeStyles,
77
Paper,
8-
TableContainer,
98
Table,
109
TableCell,
1110
TableRow,
1211
TableHead,
1312
TableBody,
1413
TableFooter,
14+
TableContainer,
15+
TableSortLabel,
1516
} from '@material-ui/core'
1617

1718
import { Alert, AlertTitle } from '@material-ui/lab'
@@ -32,6 +33,46 @@ const UsersList = ({ match }) => {
3233
const [page, setPage] = React.useState(0)
3334
const [usersData, setUsersData] = useState({ users: [], count: 0 })
3435
const [rowsPerPage, setRowsPerPage] = React.useState(10)
36+
const [order, setOrder] = React.useState({
37+
order: 'desc',
38+
orderBy: 'createdAt',
39+
})
40+
41+
const { users, count } = usersData
42+
const rowsExpected = count ? Math.max(count - rowsPerPage * page, 0) : rowsPerPage
43+
44+
const tableColumns = [
45+
{
46+
id: 'avatarUrl',
47+
label: '',
48+
isSortable: false,
49+
},
50+
{
51+
id: 'firstName',
52+
label: 'First Name',
53+
isSortable: true,
54+
},
55+
{
56+
id: 'lastName',
57+
label: 'Last Name',
58+
isSortable: true,
59+
},
60+
{
61+
id: 'username',
62+
label: 'Username',
63+
isSortable: true,
64+
},
65+
{
66+
id: 'email',
67+
label: 'Email',
68+
isSortable: true,
69+
},
70+
{
71+
id: 'createdAt',
72+
label: 'Created',
73+
isSortable: true,
74+
},
75+
]
3576

3677
// Request users
3778
useEffect(() => {
@@ -71,8 +112,12 @@ const UsersList = ({ match }) => {
71112
setPage(0)
72113
}
73114

74-
const { users, count } = usersData
75-
const rowsExpected = count ? Math.max(count - rowsPerPage * page, 0) : rowsPerPage
115+
const handelChangeOrder = (event, columnId) => {
116+
setOrder({
117+
order: order.order === 'asc' ? 'desc' : 'asc',
118+
orderBy: columnId,
119+
})
120+
}
76121

77122
return (
78123
<BasePageContainer>
@@ -91,11 +136,17 @@ const UsersList = ({ match }) => {
91136
<Table className={classes.table} aria-label="custom pagination table">
92137
<TableHead>
93138
<TableRow>
94-
<TableCell>Avatar</TableCell>
95-
<TableCell>First Name</TableCell>
96-
<TableCell>Last Name</TableCell>
97-
<TableCell>Username</TableCell>
98-
<TableCell>Email</TableCell>
139+
{tableColumns.map(column => (
140+
<TableCell key={column.id}>
141+
<TableSortLabel
142+
active={order.orderBy === column.id}
143+
direction={order.orderBy === column.id ? order.order : 'asc'}
144+
onClick={event => handelChangeOrder(event, column.id)}
145+
>
146+
{column.label}
147+
</TableSortLabel>
148+
</TableCell>
149+
))}
99150
<TableCell>Actions</TableCell>
100151
</TableRow>
101152
</TableHead>
@@ -112,6 +163,7 @@ const UsersList = ({ match }) => {
112163
page={page}
113164
rowsPerPage={rowsPerPage}
114165
count={count}
166+
order={order}
115167
onChangePage={handleChangePage}
116168
onChangeRowsPerPage={handleChangeRowsPerPage}
117169
/>

src/Administration/Users/UsersList/UsersListTableItems.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import PropTypes from 'prop-types'
3+
import { FormattedDate } from 'react-intl'
34

45
import { makeStyles, TableBody, TableCell, TableRow, Avatar } from '@material-ui/core'
56
import { Skeleton } from '@material-ui/lab'
@@ -39,6 +40,9 @@ const UsersListTableItems = ({ users, rowsPerPage = 10, rowsExpected = 10 }) =>
3940
<TableCell>
4041
<Skeleton variant="text" />
4142
</TableCell>
43+
<TableCell>
44+
<Skeleton variant="text" />
45+
</TableCell>
4246
</TableRow>
4347
))}
4448
{users.map(row => (
@@ -52,6 +56,14 @@ const UsersListTableItems = ({ users, rowsPerPage = 10, rowsExpected = 10 }) =>
5256
<TableCell>{row.lastName}</TableCell>
5357
<TableCell>{row.username}</TableCell>
5458
<TableCell>{row.email}</TableCell>
59+
<TableCell>
60+
<FormattedDate
61+
value={new Date(row.createdAt)}
62+
year="numeric"
63+
month="long"
64+
day="2-digit"
65+
/>
66+
</TableCell>
5567
<TableCell>
5668
<EditIcon />
5769
<DeleteIcon />

src/_api/_data/usersData.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import _keyBy from 'lodash/keyBy'
2+
import moment from 'moment'
23
import User from '../_types/User'
34
import organizationsToUsersData from './organizationsToUsersData'
45

@@ -150,7 +151,14 @@ const list: User[] = [
150151
'https://images-na.ssl-images-amazon.com/images/M/MV5BMTgxMTc1MTYzM15BMl5BanBnXkFtZTgwNzI5NjMwOTE@._V1_UY256_CR16,0,172,256_AL_.jpg',
151152
userToOrganizations: organizationsToUsersData.byUserId[5],
152153
},
153-
]
154+
].map(item => {
155+
return {
156+
...item,
157+
createdAt: moment()
158+
.subtract(item.id, 'days')
159+
.format(),
160+
}
161+
})
154162

155163
const byId: { [key: number]: User } = _keyBy(list, 'id')
156164

src/_api/_types/User.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type UserId = EntityId
77

88
// global user role across the system (useful for SAAS or if organizations arn't used)
99
// Each user can have only one global role
10-
export type UserGlobalRole = 'admin' | 'support' | 'member'
10+
// export type UserGlobalRole = 'admin' | 'support' | 'member'
1111

1212
export interface UserSubmissionData {
1313
firstName?: string
@@ -17,7 +17,7 @@ export interface UserSubmissionData {
1717
email: string
1818
password?: string
1919
avatarUrl?: string
20-
globalRole?: UserGlobalRole
20+
globalRole?: string
2121
}
2222

2323
export default interface User extends UserSubmissionData, Entity {

0 commit comments

Comments
 (0)