peer-at-code-web/lib/leaderboard.ts
2023-04-11 11:22:21 +02:00

77 lines
1.3 KiB
TypeScript

import fetcher from './fetcher';
import { type Group } from './groups';
export const getScores = async ({ token }: { token: string }): Promise<Score[]> => {
const { data, status } = await fetcher.get(`/leaderboard`, {
headers: {
Authorization: `Bearer ${token}`
}
});
const scores = data;
if (status !== 200) {
throw new Error('Failed to fetch scores');
}
if (!scores) {
return [] as Score[];
}
return scores as Score[];
};
export const getScoresEvent = async ({
token,
id
}: {
token: string;
id: number;
}): Promise<ScoreEvent> => {
const { data, status } = await fetcher.get(`/leaderboard/${id}`, {
headers: {
Authorization: `Bearer ${token}`
}
});
const scores = data;
if (status !== 200) {
throw new Error('Failed to fetch scores');
}
if (!scores) {
return {} as ScoreEvent;
}
return scores as ScoreEvent;
};
export type Score = {
score: number;
tries: number;
completions: number;
pseudo: string;
groups: Group[];
avatar: string;
rank: number;
};
export type ScoreEvent = {
start_date: string;
end_date: string;
groups: [
{
name: string;
rank: number;
players: [
{
pseudo: string;
tries: number;
completions: number;
score: number;
}
];
}
];
};