util functions for fetching archived games
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
import GameAuthWrapper from "@/components/game/GameAuthWrapper";
|
import GameAuthWrapper from "@/components/game/GameAuthWrapper";
|
||||||
import { getGame } from "@/lib/game";
|
import { getActiveGame } from "@/lib/game";
|
||||||
import { notFound } from "next/navigation";
|
import { notFound } from "next/navigation";
|
||||||
|
|
||||||
export async function generateMetadata({ params }: { params: { code: string } }) {
|
export async function generateMetadata({ params }: { params: { code: string } }) {
|
||||||
const game = await getGame(params.code);
|
const game = await getActiveGame(params.code);
|
||||||
if (!game) {
|
if (!game) {
|
||||||
return {
|
return {
|
||||||
description: "Game not found",
|
description: "Game not found",
|
||||||
@@ -35,7 +35,7 @@ export async function generateMetadata({ params }: { params: { code: string } })
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default async function Game({ params }: { params: { code: string } }) {
|
export default async function Game({ params }: { params: { code: string } }) {
|
||||||
const game = await getGame(params.code);
|
const game = await getActiveGame(params.code);
|
||||||
if (!game) {
|
if (!game) {
|
||||||
notFound();
|
notFound();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
import type { FormEvent } from "react";
|
import type { FormEvent } from "react";
|
||||||
import { useState, useContext } from "react";
|
import { useState, useContext } from "react";
|
||||||
import { SessionContext } from "@/context/session";
|
import { SessionContext } from "@/context/session";
|
||||||
import { getGame } from "@/lib/game";
|
import { getActiveGame } from "@/lib/game";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
|
|
||||||
export default function JoinGame() {
|
export default function JoinGame() {
|
||||||
@@ -31,7 +31,7 @@ export default function JoinGame() {
|
|||||||
code = new URL(code).pathname.split("/")[1];
|
code = new URL(code).pathname.split("/")[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
const game = await getGame(code);
|
const game = await getActiveGame(code);
|
||||||
|
|
||||||
if (game && game.code) {
|
if (game && game.code) {
|
||||||
router.push(`/${game.code}`);
|
router.push(`/${game.code}`);
|
||||||
|
|||||||
@@ -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 {
|
try {
|
||||||
const res = await fetch(`${API_URL}/v1/games/${code}`, { cache: "no-store" });
|
const res = await fetch(`${API_URL}/v1/games/${code}`, { cache: "no-store" });
|
||||||
|
|
||||||
@@ -47,3 +47,29 @@ export const getPublicGames = async () => {
|
|||||||
console.error(err);
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user