fetch added directly to tournament page

This commit is contained in:
Owen Rees
2023-06-13 08:25:58 +02:00
parent d8e12c48af
commit bd60205fb0
2 changed files with 14 additions and 6 deletions
+13 -5
View File
@@ -1,11 +1,9 @@
import { Tournament } from "@/types";
import dynamic from "next/dynamic"; import dynamic from "next/dynamic";
import Layout from "@/components/Layout"; import Layout from "@/components/Layout";
import TournamentTable from "@/components/TournamentTable"; import TournamentTable from "@/components/TournamentTable";
import getTournaments from "@/utils/getTournamentData";
// TODO can these functions be put into a custom hook? const server = process.env.SERVER;
/** /**
* Imports the tournament map component, ensuring CSR only. * Imports the tournament map component, ensuring CSR only.
* @remarks SSR is not supported by react-leaflet * @remarks SSR is not supported by react-leaflet
@@ -20,7 +18,17 @@ const TournamentMap = dynamic(() => import("@/components/TournamentMap"), {
}); });
export default async function Tournaments() { export default async function Tournaments() {
const tournamentData: Tournament[] = await getTournaments("france"); /**
* Retrieves tournament data from /api/tournaments/:country
* @remarks The result is cached for the revalidation period in seconds
*/
const res = await fetch(`${server}/api/tournaments/france`, {
next: { revalidate: 300 },
});
if (res.status !== 200) {
throw new Error("Failed to fetch tournament data");
}
const tournamentData = await res.json();
return ( return (
<Layout> <Layout>
+1 -1
View File
@@ -14,5 +14,5 @@ export default async function getTournaments(country: string) {
if (res.status !== 200) { if (res.status !== 200) {
throw new Error("Failed to fetch tournament data"); throw new Error("Failed to fetch tournament data");
} }
return res.json(); return await res.json();
} }