Skip to content

Commit

Permalink
fix: schedule view
Browse files Browse the repository at this point in the history
  • Loading branch information
unewMe committed Sep 10, 2024
1 parent 4d745a3 commit 97854e7
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 106 deletions.
42 changes: 42 additions & 0 deletions src/app/api/data/[registrationId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable @eslint-community/eslint-comments/disable-enable-pair */

import { NextResponse } from "next/server";

import type { ApiResponse } from "@/lib/types";
import { createUsosService } from "@/lib/usos";

export const revalidate = 3600;

export async function GET(
_request: Request,
{ params }: { params: { facultyId: string } },
) {
const service = createUsosService();

try {
const registrations = await Promise.all(
(await service.getRegistrations(params.facultyId)).map(async (r) => ({
registration: r,
courses: await Promise.all(
r.related_courses.flatMap(async (c) => ({
course: await service.getCourse(c.course_id),
groups: await service.getGroups(c.course_id, c.term_id),
})),
),
})),
);

return NextResponse.json(
{ registrations },
{
headers: { "Cache-Control": "public, max-age=3600" },
},
);
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
return new Response("Internal Server Error", { status: 500 });
}
}

export type ApiProfileGet = ApiResponse<typeof GET>;
15 changes: 8 additions & 7 deletions src/components/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AccordionTrigger,
} from "@/components/ui/accordion";
import { Combobox } from "@/components/ui/combobox";
import type { MockRegistration } from "@/lib/types";
import type { MockRegistration, Registration } from "@/lib/types";

const departmentOptions = [
"Filia w Legnicy [FLG]",
Expand Down Expand Up @@ -38,8 +38,8 @@ export const GroupsAccordion = ({
}: {
registrationName: string;
index: number;
onDepartmentChange: (value: string) => Promise<MockRegistration[]>;
onRegistrationChange: () => void;
onDepartmentChange: (value: string) => Promise<Registration[]>;
onRegistrationChange: (value: string) => Promise<void>;
updateDepartmentSelection: (value: string) => void;
updateRegistrationSelection: (value: string) => void;
}) => {
Expand All @@ -58,15 +58,15 @@ export const GroupsAccordion = ({
const handleDepartmentChange = async (value: string) => {
setSelectedDepartment(value);
updateDepartmentSelection(value);
const plan = await onDepartmentChange(value);
const registrations = await onDepartmentChange(value);
setShowRegistrationSelect(true);
setRegistrationOptions(plan.map((p) => p.registration.name));
setRegistrationOptions(registrations.map((r) => r.id));
};

const handleRegistrationChange = (value: string) => {
const handleRegistrationChange = async (value: string) => {
setSelectedRegistration(value);
updateRegistrationSelection(value);
onRegistrationChange();
await onRegistrationChange(value);
};

return (
Expand Down Expand Up @@ -110,6 +110,7 @@ export const GroupsAccordion = ({
id={`registration-select-${index}`}
options={registrationOptions}
value={selectedRegistration ?? ""}
// eslint-disable-next-line @typescript-eslint/no-misused-promises
onChange={handleRegistrationChange}
/>
</div>
Expand Down
73 changes: 36 additions & 37 deletions src/components/ClassBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const ClassBlock = ({
endTime,
group,
courseName,
courseID,
lecturer,
week,
courseType,
Expand All @@ -41,55 +42,53 @@ const ClassBlock = ({
endTime: string;
group: string;
courseName: string;
courseID: string;
lecturer: string;
week: "" | "TN" | "TP";
courseType: "C" | "L" | "P" | "S" | "W";
courses: ExtendedCourse[];
groups: ExtendedGroup[];
onClick: (id: string) => void;
onClick: (id: string, courseType: string, group: string) => void;
}) => {
const position = calculatePosition(startTime, endTime);
const [startGrid, durationSpan] = position;
const isCourseChecked = courses.find((course) => course.name === courseName);
const checkedGroupFromCourse = groups.find(
(g) =>
g.courseType === courseType && courseName === g.courseName && g.isChecked,
g.courseType === courseType && courseID === g.courseID && g.isChecked,
);
const isThisGroupChecked = checkedGroupFromCourse?.group === group;

const isThisGroupChecked = checkedGroupFromCourse ? checkedGroupFromCourse.group === group : false;

return (
Boolean(isCourseChecked?.isChecked) && (
<button
disabled={
checkedGroupFromCourse?.isChecked === true
? !isThisGroupChecked
: false
}
onClick={() => {
onClick(group);
}}
style={{
gridColumnStart: startGrid,
gridColumnEnd: `span ${durationSpan}`,
}}
className={cn(
position,
typeClasses[courseType],
`relative flex flex-col justify-center truncate rounded-lg p-2 shadow-md`,
checkedGroupFromCourse?.isChecked === true
? isThisGroupChecked
? "cursor-pointer"
: "opacity-20"
: "cursor-pointer opacity-60",
)}
>
<div className="flex justify-between">
<p>{`${courseType} ${week === "" ? "" : `|${week}`}`}</p>
<p>{`Grupa ${group}`}</p>
</div>
<p className="truncate font-bold">{courseName}</p>
<p className="truncate font-semibold">{lecturer}</p>
</button>
)
<button
disabled={
checkedGroupFromCourse?.isChecked === true ? !isThisGroupChecked : false
}
onClick={() => {
onClick(courseID, courseType, group);
}}
style={{
gridColumnStart: startGrid,
gridColumnEnd: `span ${durationSpan}`,
}}
className={cn(
position,
typeClasses[courseType],
`relative flex flex-col justify-center truncate rounded-lg p-2 shadow-md`,
checkedGroupFromCourse?.isChecked === true
? isThisGroupChecked
? "cursor-pointer"
: "opacity-20"
: "cursor-pointer opacity-60",
)}
>
<div className="flex justify-between">
<p>{`${courseType} ${week === "" ? "" : `|${week}`}`}</p>
<p>{`Grupa ${group}`}</p>
</div>
<p className="truncate font-bold">{courseName}</p>
<p className="truncate font-semibold">{lecturer}</p>
</button>
);
};

Expand Down
1 change: 1 addition & 0 deletions src/components/ClassSchedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const ClassSchedule = ({
groups: ExtendedGroup[];
onClick: (id: string) => void;
}) => {
//console.log(schedule);
return (
<div className="flex h-full flex-col">
<div className="z-20 flex items-center justify-center bg-white text-2xl font-semibold">
Expand Down
1 change: 1 addition & 0 deletions src/components/Schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const ScheduleTest = ({
const filterScheduleByDay = (dayCode: string) => {
return schedule.filter((item) => item.day === dayCode);
};
console.log(schedule);

return (
<div className="flex max-h-[80vh] flex-col gap-2 overflow-auto p-1 scrollbar-thin scrollbar-track-sky-300 scrollbar-thumb-sky-900">
Expand Down
18 changes: 9 additions & 9 deletions src/components/SelectGroups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ import type { Dispatch, SetStateAction } from "react";
import React, { useState } from "react";

import { GroupsAccordion } from "@/components/Accordion";
import type { ClassBlockProps, MockRegistration } from "@/lib/types";
import type {
ClassBlockProps,
MockRegistration,
Registration,
} from "@/lib/types";
import type { ExtendedCourse } from "@/pages/createplan/[id]";

const registrations = [
{ name: "Rejestracja 1" },
{ name: "Rejestracja 2" },
{ name: "Rejestracja 3" },
];
const registrations = [{ name: "Rejestracja 1" }];

export const SelectGroups = ({
handleDepartmentChange,
handleRegistrationChange,
}: {
courses: ExtendedCourse[];
checkCourse: (id: string) => void;
handleDepartmentChange: (value: string) => Promise<MockRegistration[]>;
handleRegistrationChange: Dispatch<SetStateAction<ClassBlockProps[]>>;
handleDepartmentChange: (value: string) => Promise<Registration[]>;
handleRegistrationChange: (value: string) => Promise<void>;
}) => {
const [departmentSelections, setDepartmentSelections] = useState<{
[key: string]: string | null;
Expand Down Expand Up @@ -82,7 +82,7 @@ export const SelectGroups = ({
registrationName={registration.name}
onDepartmentChange={handleDepartmentChange}
// eslint-disable-next-line @typescript-eslint/no-misused-promises
onRegistrationChange={onRegistrationChange}
onRegistrationChange={handleRegistrationChange}
updateDepartmentSelection={(value) => {
updateDepartmentSelection(registration.name, value);
}}
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ClassBlockProps {
day: string;
group: string;
courseName: string;
courseID: string;
lecturer: string;
week: "" | "TN" | "TP";
courseType: "C" | "L" | "P" | "S" | "W";
Expand Down
Loading

0 comments on commit 97854e7

Please sign in to comment.