Skip to content

Commit

Permalink
cache values
Browse files Browse the repository at this point in the history
  • Loading branch information
TeaByte committed Jan 8, 2024
1 parent cdd7c29 commit 5b6f685
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion components/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function NavBar() {
</a>
</div>
</div>
<Drawer courses={cache.merged} />
<Drawer courses={cache.courses} />
<ThemeToggle />
</nav>
);
Expand Down
4 changes: 2 additions & 2 deletions routes/group/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export const handler: Handlers<CourseGroup> = {
: parseInt(ctx.params.slug);

if (typeof toFind === "number") {
foundCourseGroup = cache.merged.find((c) => {
foundCourseGroup = cache.courses.find((c) => {
return "courses" in c && c.courses.length > 0 &&
c.order === toFind;
});
} else {
foundCourseGroup = cache.merged.find((c) => {
foundCourseGroup = cache.courses.find((c) => {
return "courses" in c && c.courses.length > 0 &&
c.label === toFind;
});
Expand Down
8 changes: 4 additions & 4 deletions routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import IconChevronDown from "https://deno.land/x/[email protected]/tsx/chev

populateCache();

export const handler: Handlers<{ merged: (Course | CourseGroup)[] }> = {
export const handler: Handlers<{ courses: (Course | CourseGroup)[] }> = {
async GET(_req, ctx) {
const courses = await getCourses(cache);
return ctx.render(courses);
},
};

export default function BlogIndexPage(
props: PageProps<{ merged: (Course | CourseGroup)[] }>,
props: PageProps<{ courses: (Course | CourseGroup)[] }>,
) {
const { merged } = props.data;
const { courses } = props.data;
return (
<>
<Head>
Expand All @@ -42,7 +42,7 @@ export default function BlogIndexPage(
<main class="max-w-screen-md px-4 pt-12 mx-auto mb-6">
<h1 class="text-5xl font-bold z-10">المحتوى</h1>
<section class="flex flex-col">
{merged.map((course, index) => {
{courses.map((course, index) => {
// Group of courses
if ("courses" in course) {
return (
Expand Down
2 changes: 1 addition & 1 deletion utils/course-cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Course, CourseGroup } from "./types.ts";
import { getCourses } from "./course.ts";
export const cache: { merged: (Course | CourseGroup)[] } = { merged: [] };
export const cache: { courses: (Course | CourseGroup)[] } = { courses: [] };

export async function populateCache(): Promise<void> {
await getCourses(cache);
Expand Down
21 changes: 12 additions & 9 deletions utils/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@ export async function getJson(
}

export async function getCourses(
cache: { merged: (Course | CourseGroup)[] } = { merged: [] },
cache: { courses: (Course | CourseGroup)[] } = { courses: [] },
): Promise<
{ merged: (Course | CourseGroup)[] }
{ courses: (Course | CourseGroup)[] }
> {
if (cache.merged.length > 0) {
if (cache.courses.length > 0) {
return cache;
}

console.log("Fetching courses...");
const files = Deno.readDir("./courses");
const groups: CourseGroup[] = [];
const nonGroups: Course[] = [];

for await (const file of files) {
if (file.isDirectory) {
const groupSlug = file.name;
Expand All @@ -71,11 +72,13 @@ export async function getCourses(
}
}
const groupOrder = await getGroupOrder(join("./courses", groupSlug));
groups.push({
courses: groupPromises,
order: groupOrder?.order,
label: groupOrder?.label,
});
if (groupOrder) {
groups.push({
courses: groupPromises,
order: groupOrder.order,
label: groupOrder.label,
});
}
} else if (file.name.endsWith(".md")) {
const slug = file.name.replace(".md", "");
const course = await getCourse(slug);
Expand All @@ -87,6 +90,6 @@ export async function getCourses(

const merged: (Course | CourseGroup)[] = [...nonGroups, ...groups];
merged.sort((a, b) => (a.order || 999) - (b.order || 999));
cache.merged = merged;
cache.courses = merged;
return cache;
}
10 changes: 5 additions & 5 deletions utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export interface Course {

export interface CourseGroup {
courses: Course[];
order?: number;
label?: string;
order: number;
label: string;
}

export interface CourseAttributes {
title?: string;
snippet?: string;
order?: number;
title: string;
snippet: string;
order: number;
}

0 comments on commit 5b6f685

Please sign in to comment.