util functions for fetching archived games

This commit is contained in:
Nathaniel Tampus
2023-04-02 12:36:48 +08:00
parent 75eb21c337
commit c7f3abf29c
3 changed files with 32 additions and 6 deletions

View File

@@ -22,7 +22,7 @@ export const createGame = async (side: string, unlisted: boolean) => {
}
};
export const getGame = async (code: string) => {
export const getActiveGame = async (code: string) => {
try {
const res = await fetch(`${API_URL}/v1/games/${code}`, { cache: "no-store" });
@@ -47,3 +47,29 @@ export const getPublicGames = async () => {
console.error(err);
}
};
export const getArchivedGame = async ({ id, userid }: { id?: number; userid?: number }) => {
let url = `${API_URL}/v1/games?`;
if (id) {
url += `id=${id}`;
} else {
url += `userid=${userid}`;
}
try {
const res = await fetch(url, {
next: { revalidate: 20 }
});
if (res && res.status === 200) {
if (userid) {
const games: Game[] = await res.json();
return games;
} else {
const game: Game = await res.json();
return game;
}
}
} catch (err) {
console.error(err);
}
};