Skip to content

Commit 71c1c0c

Browse files
committed
Commit utils file
1 parent 6873ca2 commit 71c1c0c

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { LoaderFunction, redirect, replace } from 'react-router';
2+
import { store } from 'src/pages/createStore';
3+
4+
export const leaderboardLoader: LoaderFunction = ({ request, params }) => {
5+
const baseUrl = `/courses/${params.courseId}`;
6+
const { enableOverallLeaderboard, enableContestLeaderboard } = store.getState().session;
7+
if (!enableOverallLeaderboard && !enableContestLeaderboard) {
8+
return redirect(baseUrl + '/not_found');
9+
}
10+
// If if it a sub-path, pass through transparently
11+
const path = new URL(request.url).pathname;
12+
const rootRegex = new RegExp(`^${baseUrl}/leaderboard/?$`);
13+
if (!rootRegex.test(path)) {
14+
// Don't try to redirect subpaths, pass through and proceed
15+
return null;
16+
}
17+
// Prefer showing overall
18+
return enableOverallLeaderboard
19+
? replace(baseUrl + '/leaderboard/overall')
20+
: replace(baseUrl + '/leaderboard/contests');
21+
};
22+
23+
export const contestLeaderboardLoader: LoaderFunction = ({ request, params }) => {
24+
const baseUrl = `/courses/${params.courseId}`;
25+
const { enableContestLeaderboard } = store.getState().session;
26+
if (!enableContestLeaderboard) {
27+
return redirect(baseUrl + '/not_found');
28+
}
29+
const contests = store.getState().leaderboard.contests;
30+
const fallback = contests.find(c => c.published);
31+
if (!fallback) {
32+
// No contests are ready to show scores yet
33+
return redirect(baseUrl + '/not_found');
34+
}
35+
if (!params.contestId) {
36+
// Fallback to default contest ID
37+
// Prefer score leaderboard
38+
return replace(`${baseUrl}/leaderboard/contests/${fallback.contest_id}/score`);
39+
}
40+
const leaderboardType = params.type!;
41+
if (!['score', 'popularvote'].includes(leaderboardType)) {
42+
return redirect(baseUrl + '/not_found');
43+
}
44+
// Pass through and proceed
45+
return null;
46+
};

0 commit comments

Comments
 (0)