-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDailyLeaderboard.tsx
167 lines (159 loc) · 4.8 KB
/
DailyLeaderboard.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
import { useEffect, useState } from 'react';
import { Table } from 'react-bootstrap';
import { useAppSelector } from '../../store/hooks';
import styles from './Leaderboard.module.css';
import { getAvatarByID } from '../Avatar/Avatar';
import {
DailyChallengeLeaderBoardResponse,
DailyChallengesApi,
} from '@codecharacter-2023/client';
import { apiConfig, ApiError } from '../../api/ApiConfig';
import Loader from '../Loader/Loader';
import Toast from 'react-hot-toast';
import { user } from '../../store/User/UserSlice';
function PaginatedItems() {
const [page, setPage] = useState(0);
const [items, setItems] = useState<DailyChallengeLeaderBoardResponse[]>([]);
const [nextItems, setNextItems] = useState<
DailyChallengeLeaderBoardResponse[]
>([]);
const [isLoaded, setIsLoaded] = useState(false);
const itemsPerPage = 8;
const currentUserName = useAppSelector(user).username;
useEffect(() => {
fetchLeaderboard(page);
}, [page]);
useEffect(() => {
checkEmpty();
}, [nextItems]);
function checkEmpty() {
let emptylistBool = false;
if (nextItems.length == 0) {
emptylistBool = true;
}
return emptylistBool;
}
const fetchLeaderboard = (pageNum: number) => {
setIsLoaded(false);
const leaderboardAPI = new DailyChallengesApi(apiConfig);
leaderboardAPI
.getDailyChallengeLeaderBoard(pageNum, itemsPerPage)
.then(response => {
setItems(response);
setIsLoaded(true);
})
.catch(error => {
if (error instanceof ApiError) Toast.error(error.message);
});
leaderboardAPI
.getDailyChallengeLeaderBoard(pageNum + 1, itemsPerPage)
.then(response => {
setNextItems(response);
})
.catch(error => {
if (error instanceof ApiError) Toast.error(error.message);
});
};
return (
<>
<>
{!isLoaded ? (
<Loader />
) : (
<>
<div className={styles.list}>
<Table hover className={styles.list} responsive>
<thead>
<tr className={styles.tableHeader}>
<th className={styles.tableHeader}>RANK</th>
<th className={styles.tableHeader}>USERNAME</th>
<th className={styles.tableHeader}>SCORE</th>
</tr>
</thead>
<tbody>
{items &&
items.map((row: DailyChallengeLeaderBoardResponse) => (
<tr
className={
styles.item +
' ' +
(currentUserName === row.userName
? styles.currentUserItem
: '')
}
key={row.userName}
>
<td className={styles.pos}>
{items.indexOf(row) + 1 + page * itemsPerPage}
</td>
<td className={styles.name}>
<div>
<img
className={styles.pic}
src={getAvatarByID(row.avatarId).url}
></img>
{' ' + row.userName.substring(0, 10)}
</div>
</td>
<td className={styles.score}>{row.score.toFixed(2)}</td>
</tr>
))}
</tbody>
</Table>
</div>
</>
)}
</>
<nav className={styles.paginationouter}>
<button
type="button"
className={styles.button}
onClick={() => {
if (page !== 0) {
setPage(prevPage => prevPage - 1);
}
}}
>
{'<'}
</button>
<button
type="button"
className={styles.button}
onClick={() => {
if (!checkEmpty()) {
setPage(prevPage => prevPage + 1);
} else {
Toast("You're at the last page");
}
}}
>
{'>'}
</button>
<button
type="button"
className={styles.button}
onClick={() => {
fetchLeaderboard(0);
setPage(0);
}}
>
Refresh
</button>
</nav>
</>
);
}
export default function DailyChallengeLeaderboard(): JSX.Element {
return (
<div className={styles.body}>
<div className={styles.header}>
<h1 className={styles.header__title}>
<span>Daily Challenge Leaderboard</span>
</h1>
</div>
<div className={styles.ranklist}>
<PaginatedItems />
</div>
</div>
);
}