peer-at-code-web/app/dashboard/puzzles/[id]/page.tsx
2023-04-11 11:23:50 +02:00

47 lines
1.2 KiB
TypeScript

import { cookies } from 'next/headers';
import { getPuzzle } from '@/lib/puzzles';
import Puzzle from '@/ui/Puzzle';
import type { Metadata } from 'next';
import { notFound } from 'next/navigation';
export async function generateMetadata({ params }: { params: { id: number } }): Promise<Metadata> {
const { id } = params;
const token = cookies().get('token')?.value;
if (!token) {
notFound();
}
const puzzle = await getPuzzle({ token, id });
return { title: `${puzzle.name} - Peer-at Code` };
}
export default async function Page({ params }: { params: { id: number } }) {
const { id } = params;
const token = cookies().get('token')?.value;
if (!token) {
notFound();
}
const puzzle = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/puzzle/${id}`, {
headers: {
Authorization: `Bearer ${token}`
}
}).then((res) => res.json());
if (!puzzle) {
notFound();
}
return <Puzzle puzzle={puzzle} />;
}
// export const dynamicParams = true;
// export async function generateStaticParams() {
// const { puzzles } = await getPuzzles();
// // every id is a number, but we need to return a string
// return puzzles.flatMap((puzzle) => ({ id: puzzle.id.toString() }));
// }