-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeaderboardView.tsx
121 lines (112 loc) · 4.07 KB
/
LeaderboardView.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
import { useReactiveVar } from '@apollo/client'
import {
Avatar,
Badge,
Box,
Center,
Group,
MediaQuery,
Pagination,
ScrollArea,
Select,
Table,
Text
} from '@mantine/core'
import { IconSortAscending, IconSortDescending } from '@tabler/icons'
import { useState } from 'react'
import CustomError from 'src/components/common/CustomError'
import CustomLoading from 'src/components/common/CustomLoading'
import MobilePagination from 'src/components/pagination/MobilePagination'
import { Order } from 'src/graphql/generated/generated'
import useGetUsers from 'src/hooks/users/useGetUsers'
import { orderByVar } from 'src/state/leaderboard'
import capitalize from 'src/utils/capitalize'
import { formatRatingStats, getTimeSince, userHasRatings } from 'src/utils/views/LeaderboardView'
import View from './View'
const LeaderboardView = () => {
const [page, setPage] = useState(1)
const orderBy = useReactiveVar(orderByVar)
const { data, loading, error, handleOnChange } = useGetUsers(page)
const numberOfPages = data?.users.info.pages || 0
const availableOrders = Object.keys(Order)
const users = data?.users.results
return (
<View>
<Box>
{loading ? (
<CustomLoading />
) : error ? (
<CustomError overlay />
) : (
<Box>
<MediaQuery largerThan="xs" styles={{ display: 'flex', justifyContent: 'flex-start' }}>
<Box>
<Select
style={{ padding: 12, zIndex: 2 }}
data={availableOrders}
icon={orderBy === Order.Asc ? <IconSortAscending /> : <IconSortDescending />}
placeholder={capitalize(Order.Desc)}
label="Order By"
onChange={handleOnChange}
/>
</Box>
</MediaQuery>
<ScrollArea>
<Table verticalSpacing="sm">
<thead>
<tr>
<th>Users</th>
<MediaQuery smallerThan="sm" styles={{ display: 'none' }}>
<th>Created</th>
</MediaQuery>
<th>Status</th>
</tr>
</thead>
<tbody>
{users?.map((user) => (
<tr key={user.id}>
<td>
<Group>
<Avatar size={40} radius={40} />
<>
<Text size="sm" weight={500}>
{user.username}
</Text>
<Text size="xs" color="dimmed">
{user.email}
</Text>
</>
</Group>
</td>
<MediaQuery smallerThan="sm" styles={{ display: 'none' }}>
<td>{getTimeSince(user.createdAt)}</td>
</MediaQuery>
<td style={{ textAlign: 'center' }}>
{user.ratings && userHasRatings(user.ratings) ? (
<Badge size="lg">{formatRatingStats(user.ratings)}</Badge>
) : (
<Badge color="gray" size="lg">
No ratings yet
</Badge>
)}
</td>
</tr>
))}
</tbody>
</Table>
</ScrollArea>
<Center style={{ marginTop: 24, marginBottom: 24 }}>
<MediaQuery smallerThan="sm" styles={{ display: 'none' }}>
<Pagination page={page} onChange={setPage} total={numberOfPages} size="xl" />
</MediaQuery>
<MediaQuery largerThan="sm" styles={{ display: 'none' }}>
<MobilePagination page={page} onChange={setPage} total={numberOfPages} />
</MediaQuery>
</Center>
</Box>
)}
</Box>
</View>
)
}
export default LeaderboardView