Files
echecsfrance/app/[lang]/tournois/page.tsx
T
Timothy Armes 1715b1646b Mutual Highlighting (#41) (#55)
* Mutual Highlighting (#41)

* Tidy up

* Delete unused API route
2023-07-05 13:55:56 +02:00

54 lines
1.3 KiB
TypeScript

import clientPromise from "@/lib/mongodb";
import { errorLog } from "@/utils/logger";
import { Tournament } from "@/types";
import TournamentsDisplay from "./TournamentsDisplay";
export const revalidate = 3600; // Revalidate cache every 6 hours
const getTournaments = async () => {
try {
const client = await clientPromise;
const db = client.db("tournamentsFranceDB");
const data = await db
.collection("tournaments")
.aggregate<Tournament>([
{
$addFields: {
dateParts: {
$dateFromString: {
dateString: "$date",
format: "%d/%m/%Y",
},
},
},
},
{
$sort: {
dateParts: 1,
},
},
{
$unset: "dateParts",
},
])
.toArray();
// We map the ObjectId to a string so that it can be serialized and sent to a client component
return data.map((t) => ({
...t,
_id: t._id.toString(),
}));
} catch (error) {
errorLog(error);
throw new Error("Error fetching tournament data");
}
};
export default async function Tournaments() {
const tournaments = await getTournaments();
return <TournamentsDisplay tournaments={tournaments} />;
}