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
63 changes: 44 additions & 19 deletions app/[locale]/academics/curricula/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ export default async function Curricula({
}) {
const text = (await getTranslations(locale)).Curricula;

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

const pagesCount = await db.select({count:count()}).from(courses);
const totalPages = Math.ceil(pagesCount[0].count/10);
// console.log(totalPages);

return (
<>
Expand Down Expand Up @@ -64,29 +67,51 @@ export default async function Curricula({
</Suspense>
<PaginationWithLogic
currentPage={page}
query={db.select({ count: count() }).from(courses)}
totalPages={totalPages}
/>
</main>
</>
);
}

const Courses = async ({ page }: { page: number }) => {
const courses = await db.query.courses.findMany({
columns: { code: true, title: true },
with: {
coursesToMajors: {
columns: {
lectureCredits: true,
practicalCredits: true,
tutorialCredits: true,
},
with: { major: { columns: { name: true } } },
},
},
limit: 10,
offset: (page - 1) * 10,
});
// fetch call for original db
// const courses = await db.query.courses.findMany({
// columns: { code: true, title: true },
// with: {
// coursesToMajors: {
// columns: {
// lectureCredits: true,
// practicalCredits: true,
// tutorialCredits: true,
// },
// with: { major: { columns: { name: true } } },
// },
// },
// limit: 10,
// offset: (page - 1) * 10,
// });

// just for my setup db -- To be reversed later
interface Course {
code: string;
title: string;
coursesToMajors: {
lectureCredits: number;
practicalCredits: number;
tutorialCredits: number;
major: {
name: string;
};
}[];
}

// call for my setup db
const courses: Course[] = await db.query.courses.findMany({
limit:10,
offset:(page-1)*10,
})


return courses.map(({ code, coursesToMajors, title }) =>
coursesToMajors.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export default async function Committee({
? 1
: Math.max(Number(searchParams.meetingPage ?? '1'), 1);

const pagesCount = await db.select({count:count()}).from(committeeMeetings).where(sql`committee_type = ${type}`);
const totalPages = Math.ceil(pagesCount[0].count/10);

return (
<section className="container">
{type !== 'senate' && (
Expand Down Expand Up @@ -83,10 +86,7 @@ export default async function Committee({
</Suspense>
<PaginationWithLogic
currentPage={meetingPage}
query={db
.select({ count: count() })
.from(committeeMeetings)
.where(sql`${committeeMeetings.committeeType} = ${type}`)}
totalPages={totalPages}
/>
</section>
);
Expand Down
55 changes: 26 additions & 29 deletions components/pagination/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,57 @@ import {
export const PaginationWithLogic = async ({
className,
currentPage,
query,
...props
totalPages,
}: React.ComponentProps<'nav'> & {
currentPage: number;
query: Promise<{ count: number }[]>;
totalPages : number;
}) => {
const rows = await query;
const noOfPages = Math.ceil(Number(rows[0].count) / 10);

return (
<Pagination className={cn('mt-4 md:mt-5 xl:mt-6', className)} {...props}>
<Pagination className={cn('mt-4 md:mt-5 xl:mt-6', className)}>
<PaginationContent>
<PaginationItem>
<PaginationPrevious
disabled={currentPage <= 1}
href={{ query: { meetingPage: currentPage - 1 } }}
href={{ query: { page: currentPage - 1 } }}
/>
</PaginationItem>

<PaginationItem>
<PaginationLink
href={{ query: { meetingPage: 1 } }}
href={{ query: { page: 1 } }}
isActive={currentPage == 1}
>
1
</PaginationLink>
</PaginationItem>
{noOfPages > 1 && (currentPage < 4 || noOfPages < 6) && (
{totalPages > 1 && (currentPage < 4 || totalPages < 6) && (
<PaginationItem>
<PaginationLink
href={{ query: { meetingPage: 2 } }}
href={{ query: { page: 2 } }}
isActive={currentPage == 2}
>
2
</PaginationLink>
</PaginationItem>
)}
{noOfPages > 2 && (currentPage < 4 || noOfPages < 6) && (
{totalPages > 2 && (currentPage < 4 || totalPages < 6) && (
<PaginationItem>
<PaginationLink
href={{ query: { meetingPage: 3 } }}
href={{ query: { page: 3 } }}
isActive={currentPage == 3}
>
3
</PaginationLink>
</PaginationItem>
)}

{noOfPages > 5 && <PaginationEllipsis />}
{currentPage > 3 && currentPage < noOfPages - 2 && (
{totalPages > 5 && <PaginationEllipsis />}
{currentPage > 3 && currentPage < totalPages - 2 && (
<>
<PaginationItem>
<PaginationLink
href={{ query: { meetingPage: currentPage } }}
href={{ query: { page: currentPage } }}
isActive={currentPage == currentPage}
>
{currentPage}
Expand All @@ -76,41 +73,41 @@ export const PaginationWithLogic = async ({
</>
)}

{noOfPages > 5 && currentPage > noOfPages - 3 && (
{totalPages > 5 && currentPage > totalPages - 3 && (
<PaginationItem>
<PaginationLink
href={{ query: { meetingPage: noOfPages - 2 } }}
isActive={currentPage == noOfPages - 2}
href={{ query: { page: totalPages - 2 } }}
isActive={currentPage == totalPages - 2}
>
{noOfPages - 2}
{totalPages - 2}
</PaginationLink>
</PaginationItem>
)}
{noOfPages > 4 && (currentPage > noOfPages - 3 || noOfPages < 6) && (
{totalPages > 4 && (currentPage > totalPages - 3 || totalPages < 6) && (
<PaginationItem>
<PaginationLink
href={{ query: { meetingPage: noOfPages - 1 } }}
isActive={currentPage == noOfPages - 1}
href={{ query: { page: totalPages - 1 } }}
isActive={currentPage == totalPages - 1}
>
{noOfPages - 1}
{totalPages - 1}
</PaginationLink>
</PaginationItem>
)}
{noOfPages > 3 && (
{totalPages > 3 && (
<PaginationItem>
<PaginationLink
href={{ query: { meetingPage: noOfPages } }}
isActive={currentPage == noOfPages}
href={{ query: { page: totalPages } }}
isActive={currentPage == totalPages}
>
{noOfPages}
{totalPages}
</PaginationLink>
</PaginationItem>
)}

<PaginationItem>
<PaginationNext
disabled={currentPage >= noOfPages}
href={{ query: { meetingPage: currentPage + 1 } }}
disabled={currentPage >= totalPages}
href={{ query: { page: currentPage + 1 } }}
/>
</PaginationItem>
</PaginationContent>
Expand Down
133 changes: 78 additions & 55 deletions server/db/schema/courses.schema.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,86 @@
import { relations, sql } from 'drizzle-orm';
// import { relations, sql } from 'drizzle-orm';
// correct all these commented codes after the review
import {
char,
integer,
// char,
// integer,
pgTable,
smallint,
smallserial,
text,
// smallint,
// smallserial,
// text,
varchar,
serial,
jsonb
} from 'drizzle-orm/pg-core';

import { courseLogs, coursesToMajors, departments, faculty } from '.';
// import { courseLogs, coursesToMajors, departments, faculty } from '.';

export const courses = pgTable('courses', {
id: smallserial('id').primaryKey(),
code: varchar('code', { length: 7 }).unique().notNull(),
title: varchar('title', { length: 128 }).notNull(),
coordinatorId: integer('coordinator_id')
.references(() => faculty.id)
.notNull(),
departmentId: smallint('department_id')
.references(() => departments.id)
.notNull(),
prerequisites: varchar('prerequisites', { length: 7 })
.array()
.default(sql`'{}'`)
.notNull(),
nature: char('nature', { length: 3 }).notNull(),
objectives: text('objectives')
.array()
.default(sql`'{}'`)
.notNull(),
content: text('content').notNull(),
outcomes: text('outcomes')
.array()
.default(sql`'{}'`)
.notNull(),
essentialReading: text('essential_reading')
.array()
.default(sql`'{}'`)
.notNull(),
supplementaryReading: text('supplementary_reading')
.array()
.default(sql`'{}'`)
.notNull(),
similarCourses: varchar('similar_courses', { length: 7 })
.array()
.default(sql`'{}'`)
.notNull(),
// Define the Courses table with embedded coursesToMajors array
export const courses = pgTable("courses", {
id: serial("id").primaryKey(),
code: varchar("code", { length: 255 }).notNull().unique(),
title: varchar("title", { length: 255 }).notNull(),
coursesToMajors: jsonb("courses_to_majors")
.notNull()
.$type<{
courseId: number;
majorId: number;
semester: number;
lectureCredits: number;
tutorialCredits: number;
practicalCredits: number;
major: { id: number; name: string };
}[]>(), // Storing as an array of JSON
});

export const coursesRelations = relations(courses, ({ many, one }) => ({
coordinator: one(faculty, {
fields: [courses.coordinatorId],
references: [faculty.id],
}),
courseLogs: many(courseLogs),
coursesToMajors: many(coursesToMajors),
department: one(departments, {
fields: [courses.departmentId],
references: [departments.id],
}),
}));
// Will use this old schema and relations after review

// export const courses = pgTable('courses', {
// id: smallserial('id').primaryKey(),
// code: varchar('code', { length: 7 }).unique().notNull(),
// title: varchar('title', { length: 128 }).notNull(),
// coordinatorId: integer('coordinator_id')
// .references(() => faculty.id)
// .notNull(),
// departmentId: smallint('department_id')
// .references(() => departments.id)
// .notNull(),
// prerequisites: varchar('prerequisites', { length: 7 })
// .array()
// .default(sql`'{}'`)
// .notNull(),
// nature: char('nature', { length: 3 }).notNull(),
// objectives: text('objectives')
// .array()
// .default(sql`'{}'`)
// .notNull(),
// content: text('content').notNull(),
// outcomes: text('outcomes')
// .array()
// .default(sql`'{}'`)
// .notNull(),
// essentialReading: text('essential_reading')
// .array()
// .default(sql`'{}'`)
// .notNull(),
// supplementaryReading: text('supplementary_reading')
// .array()
// .default(sql`'{}'`)
// .notNull(),
// similarCourses: varchar('similar_courses', { length: 7 })
// .array()
// .default(sql`'{}'`)
// .notNull(),
// });

// export const coursesRelations = relations(courses, ({ many, one }) => ({
// coordinator: one(faculty, {
// fields: [courses.coordinatorId],
// references: [faculty.id],
// }),
// courseLogs: many(courseLogs),
// coursesToMajors: many(coursesToMajors),
// department: one(departments, {
// fields: [courses.departmentId],
// references: [departments.id],
// }),
// }));