Skip to content

Commit efe882e

Browse files
committed
Fix: Dispatch request to retrieve all user XP only triggers on export button click
1 parent e08c26c commit efe882e

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

src/pages/leaderboard/subcomponents/ContestLeaderboard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ const ContestLeaderboard: React.FC<Props> = ({ type, contestID }) => {
168168
<LeaderboardDropdown contests={contestDetails} />
169169

170170
{/* Export Button */}
171-
<LeaderboardExportButton type={type} contest={contestName} data={rankedLeaderboard} />
171+
<LeaderboardExportButton type={type} contest={contestName} contestID={contestID}/>
172172
</div>
173173

174174
{/* Leaderboard Table (Top 3) */}

src/pages/leaderboard/subcomponents/LeaderboardExportButton.tsx

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
11
import 'src/styles/Leaderboard.scss';
22

3-
import React from 'react';
3+
import React, { useEffect, useState } from 'react';
44
import { useTypedSelector } from 'src/commons/utils/Hooks';
55
import { ContestLeaderboardRow, LeaderboardRow } from 'src/features/leaderboard/LeaderboardTypes';
66

77
import { Role } from '../../../commons/application/ApplicationTypes';
8+
import { useDispatch } from 'react-redux';
9+
import LeaderboardActions from 'src/features/leaderboard/LeaderboardActions';
810

9-
type Props =
10-
| { type: string; contest: string | undefined; data: ContestLeaderboardRow[] | undefined }
11-
| { type: string; contest: string | undefined; data: LeaderboardRow[] };
11+
type Props = {
12+
type: string
13+
contest?: string
14+
contestID?: number
15+
}
16+
17+
const LeaderboardExportButton: React.FC<Props> = ({ type, contest, contestID }) => {
18+
19+
// Retrieve relevant leaderboard data
20+
const [ exportRequested, setExportRequest ] = useState(false);
21+
const dispatch = useDispatch();
22+
const data = (type == "overall")
23+
? useTypedSelector(store => store.leaderboard.userXp)
24+
: (type == "score")
25+
? useTypedSelector(store => store.leaderboard.contestScore)
26+
: useTypedSelector(store => store.leaderboard.contestPopularVote);
27+
28+
const onExportClick = () => {
29+
// Dispatch relevant request
30+
if (type == "overall") dispatch(LeaderboardActions.getAllUsersXp());
31+
else if (type == "score") dispatch(LeaderboardActions.getAllContestScores(contestID as number));
32+
else dispatch(LeaderboardActions.getAllContestPopularVotes(contestID as number));
33+
setExportRequest(true)
34+
}
35+
36+
// Return the CSV when requested and data is loaded
37+
useEffect(() => {
38+
if (exportRequested) {
39+
exportCSV();
40+
setExportRequest(false); // Clear request
41+
}
42+
}, [data])
1243

13-
const LeaderboardExportButton: React.FC<Props> = ({ type, contest, data }) => {
14-
// pls remove this
15-
if (!data) return;
1644
const role = useTypedSelector(store => store.session.role);
1745
const exportCSV = () => {
1846
const headers = [
@@ -48,7 +76,7 @@ const LeaderboardExportButton: React.FC<Props> = ({ type, contest, data }) => {
4876
};
4977

5078
return role === Role.Admin || role === Role.Staff ? (
51-
<button onClick={exportCSV} className="export-button">
79+
<button onClick={onExportClick} className="export-button">
5280
Export as .csv
5381
</button>
5482
) : (

src/pages/leaderboard/subcomponents/OverallLeaderboard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ const OverallLeaderboard: React.FC = () => {
158158
<LeaderboardDropdown contests={contestDetails} />
159159

160160
{/* Export Button */}
161-
<LeaderboardExportButton type="overall" contest={undefined} data={undefined} />
161+
<LeaderboardExportButton type="overall" />
162162
</div>
163163

164164
{/* Leaderboard Table (Replaced with ag-Grid) */}

0 commit comments

Comments
 (0)