Newer
Older
import React, { useState } from 'react'
import { useQuery } from '@apollo/react-hooks'
import { Heading, Action } from '@pubsweet/ui'
import { Container, Table, Header } from './style'
import Spinner from '../../Spinner'
query Users(
$sort: UsersSort
$filter: UsersFilter
$offset: Int
$limit: Int
) {
users(sort: $sort, filter: $filter, offset: $offset, limit: $limit) {
admin
email
profilePicture
online
created
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
const UsersManager = () => {
const SortHeader = ({ thisSortName, children }) => {
const changeSort = () => {
if (sortName !== thisSortName) {
setSortName(thisSortName)
setSortDirection('ASC')
} else if (sortDirection === 'ASC') {
setSortDirection('DESC')
} else if (sortDirection === 'DESC') {
setSortName()
setSortDirection()
}
}
const UpDown = () => {
if (thisSortName === sortName) {
return sortDirection
}
}
return (
<th onClick={changeSort}>
{children} {UpDown()}
</th>
)
}
const [sortName, setSortName] = useState('created')
const [sortDirection, setSortDirection] = useState('DESC')
const [page, setPage] = useState(1)
const limit = 15
const sort = sortName && sortDirection && `${sortName}_${sortDirection}`
const { loading, error, data } = useQuery(GET_USERS, {
variables: {
sort,
offset: (page - 1) * limit,
limit
},
})
if (loading) return <Spinner/>
if (error) return `Error! ${error.message}`
return (
<Container>
<Heading level={1}>List of users</Heading>
<Table>
<Header>
<tr>
<SortHeader thisSortName="username">Name</SortHeader>
<SortHeader thisSortName="created">Created</SortHeader>
<SortHeader thisSortName="admin">Admin</SortHeader>
<th />
</tr>
</Header>
<tbody>
{data.users.map((user, key) => (
<User key={user.id} number={key + 1} user={user} />
))}
</tbody>
</Table>
{ page > 1 && <><Action onClick={() => setPage(page - 1)}>Previous</Action> </> }
{ data.users.length === limit && <Action onClick={() => setPage(page + 1)}>Next</Action> }
</Container>
)
}