Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/web/app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ export async function GET(req: NextRequest) {
if (fromQuery != process.env.MY_SECRET_TOKEN) {
return NextResponse.json({ message: "Invalid token" }, { status: 401 });
}
if (!path) {
return NextResponse.json({ message: "Missing path parameter" }, { status: 400 });
}
try {
if (path) {
revalidatePath(path);
revalidatePath(path, "page");
return NextResponse.json({ revalidated: true, now: Date.now() });
}
} catch (e) {
Expand Down
96 changes: 49 additions & 47 deletions apps/web/components/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { MCQQuestion, Prisma, Problem, Track, TrackProblems } from "@prisma/client";
import db from "@repo/db/client";
import { cache } from "../../../packages/db/Cache";
import { revalidateTag, unstable_cache } from "next/cache";

interface Tracks extends Track {
problems: { problem: Problem }[];
Expand Down Expand Up @@ -155,54 +156,45 @@ export async function getTrack(trackId: string) {
return null;
}
}

export async function getAllTracks() {
const value = await cache.get("getAllTracks", []);
if (value) {
const data: AllTracks[] = value.map((track: Tracks) => ({
...track,
problems: track.problems.map((problem: { problem: Problem }) => ({ ...problem.problem })),
}));
return data;
}
try {
const tracks = await db.track.findMany({
where: {
hidden: false,
},
include: {
problems: {
select: {
problemId: true,
problem: true,
const getCachedTracks = unstable_cache(
async () => {
return await db.track.findMany({
where: { hidden: false },
include: {
problems: { select: { problemId: true, problem: true }, orderBy: [{ sortingOrder: "desc" }] },
categories: { select: { category: true } },
},
orderBy: [
{
sortingOrder: "desc",
},
],
},
categories: {
select: {
category: true,
},
},
},
orderBy: {
createdAt: "asc",
orderBy: { createdAt: "asc" },
});
},
});
await cache.set("getAllTracks", [], tracks);
return tracks.map((track: any) => ({
["tracks"],
{ revalidate: 3600, tags: ["tracks"] }
);

const tracks = await getCachedTracks();

if (!Array.isArray(tracks)) {
// console.error("Error: getAllTracks did not return an array!", tracks);
return [];
}

return tracks.map((track) => ({
...track,
problems: track.problems.map((problem: any) => ({ ...problem.problem })),
problems: track.problems.map((problem) => ({ ...problem.problem })),
}));
} catch (e) {
console.error(e);
console.error("Error fetching tracks:", e);
return [];
}
}

export async function revalidateTracks() {
revalidateTag("tracks");
revalidateTag("categories");
}

export async function createTrack(data: {
id: string;
title: string;
Expand Down Expand Up @@ -251,6 +243,9 @@ export async function createTrack(data: {
});
});
}

await revalidateTracks();

return track;
} catch (e) {
return new Error("Failed to create track");
Expand Down Expand Up @@ -315,21 +310,28 @@ export async function updateTrack(
}

export async function getAllCategories() {
const value = await cache.get("getAllCategories", []);
if (value) {
return value;
}
try {
const categories = await db.categories.findMany({
select: {
id: true,
category: true,
const getCachedCategories = unstable_cache(
async () => {
return await db.categories.findMany({
select: {
id: true,
category: true,
},
distinct: ["category"],
});
},
distinct: ["category"],
});
["categories"],
{ tags: ["categories"] }
);

const categories = await getCachedCategories();

await cache.set("getAllCategories", [], categories);

return categories;
} catch (e) {
console.error("Error fetching categories:", e);
return [];
}
}
Expand Down
Loading