Skip to content

Commit

Permalink
feat: pagination in courses
Browse files Browse the repository at this point in the history
  • Loading branch information
GetPsyched committed May 7, 2024
1 parent 866f4fd commit 1f9bf39
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions app/[locale]/academics/curricula/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { count } from 'drizzle-orm';
import Link from 'next/link';
import { Suspense } from 'react';
import { FaExternalLinkAlt } from 'react-icons/fa';

import Heading from '~/components/heading';
import Loading from '~/components/loading';
import { PaginationWithLogic } from '~/components/pagination';
import {
Button,
Table,
Expand All @@ -14,15 +16,21 @@ import {
TableRow,
} from '~/components/ui';
import { getTranslations } from '~/i18n/translations';
import { db } from '~/server/db';
import { courses, db } from '~/server/db';

export default async function Curricula({
params: { locale },
searchParams,
}: {
params: { locale: string };
searchParams: { page?: string };
}) {
const text = (await getTranslations(locale)).Curricula;

const page = isNaN(Number(searchParams.page ?? '1'))
? 1
: Math.max(Number(searchParams.page ?? '1'), 1);

return (
<>
<Heading
Expand Down Expand Up @@ -50,16 +58,20 @@ export default async function Curricula({
</TableRow>
</TableHeader>
<TableBody>
<Courses />
<Courses page={page} />
</TableBody>
</Table>
</Suspense>
<PaginationWithLogic
currentPage={page}
query={db.select({ count: count() }).from(courses)}
/>
</main>
</>
);
}

const Courses = async () => {
const Courses = async ({ page }: { page: number }) => {
const courses = await db.query.courses.findMany({
columns: { code: true, title: true },
with: {
Expand All @@ -72,6 +84,8 @@ const Courses = async () => {
with: { major: { columns: { name: true } } },
},
},
limit: 10,
offset: (page - 1) * 10,
});

return courses.map(({ code, coursesToMajors, title }, index) =>
Expand Down

0 comments on commit 1f9bf39

Please sign in to comment.