Skip to content

Commit

Permalink
fix: fixed issues involving course meeting objects not being recogniz…
Browse files Browse the repository at this point in the history
…ed as course meeting objects (#132)

* fix: coursemeeting objects now created properly. course popup works on calendar

* refactor: removed duplicated getTimeString method in useFlattenedHook

* refactor: meeting constructor

---------

Co-authored-by: Razboy20 <[email protected]>
  • Loading branch information
knownotunknown and Razboy20 authored Mar 10, 2024
1 parent c51e688 commit 3406e9a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 75 deletions.
6 changes: 5 additions & 1 deletion src/shared/types/CourseSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import { CourseMeeting, DAY_MAP } from './CourseMeeting';
* This represents the schedule for a course, which includes all the meeting times for the course, as well as helper functions for parsing, serializing, and deserializing the schedule
*/
export class CourseSchedule {
meetings: CourseMeeting[] = [];
meetings: CourseMeeting[];

constructor(courseSchedule?: Serialized<CourseSchedule>) {
if (!courseSchedule) {
return;
}
Object.assign(this, courseSchedule);
this.meetings = [];
for (let meeting of courseSchedule.meetings) {
this.meetings.push(new CourseMeeting(meeting));
}
}

/**
Expand Down
22 changes: 6 additions & 16 deletions src/shared/types/UserSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,12 @@ export class UserSchedule {
name: string;
hours: number;

constructor(schedule: Serialized<UserSchedule>);
constructor(courses: Course[], name: string, hours: number);
constructor(coursesOrSchedule: Course[] | Serialized<UserSchedule>, name?: string, hours?: number) {
if (Array.isArray(coursesOrSchedule)) {
this.courses = coursesOrSchedule;
this.name = name || '';
this.hours = hours || 0;
} else {
this.courses = coursesOrSchedule?.courses.map(c => new Course(c)) || [];
this.name = coursesOrSchedule?.name || 'new schedule';
this.hours = 0;
if (this.courses && this.courses.length > 0) {
for (const course of this.courses) {
this.hours += course.creditHours;
}
}
constructor(schedule: Serialized<UserSchedule>) {
this.courses = schedule.courses.map(c => new Course(c));
this.name = schedule.name;
this.hours = 0;
for (const course of this.courses) {
this.hours += course.creditHours;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/views/components/calendar/Calendar/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function Calendar(): JSX.Element {
Check CalendarGrid.tsx and AccountForCourseConflicts for an example */}
{course ? (
<CourseCatalogInjectedPopup
course={ExampleCourse}
course={course}
activeSchedule={activeSchedule}
onClose={() => setCourse(null)}
/>
Expand Down
65 changes: 8 additions & 57 deletions src/views/hooks/useFlattenedCourseSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ export function useFlattenedCourseSchedule(): FlattenedCourseSchedule {
if (!activeSchedule) {
return {
courseCells: [] as CalendarGridCourse[],
activeSchedule: new UserSchedule([], 'Something may have went wrong', 0),
activeSchedule: new UserSchedule({
courses: [],
name: 'Something may have went wrong',
hours: 0,
}),
} satisfies FlattenedCourseSchedule;
}

Expand Down Expand Up @@ -137,13 +141,11 @@ function processAsyncCourses({
/**
* Function to process each in-person class into its distinct meeting objects for calendar grid
*/
function processInPersonMeetings(
{ days, startTime, endTime, location }: CourseMeeting,
{ courseDeptAndInstr, status, course }
) {
function processInPersonMeetings(meeting: CourseMeeting, { courseDeptAndInstr, status, course }) {
const { days, startTime, endTime, location } = meeting;
const midnightIndex = 1440;
const normalizingTimeFactor = 720;
const time = getTimeString({ separator: '-', capitalize: true }, startTime, endTime);
const time = meeting.getTimeString({ separator: '-', capitalize: true });
const timeAndLocation = `${time} - ${location ? location.building : 'WB'}`;
let normalizedStartTime = startTime >= midnightIndex ? startTime - normalizingTimeFactor : startTime;
let normalizedEndTime = endTime >= midnightIndex ? endTime - normalizingTimeFactor : endTime;
Expand Down Expand Up @@ -181,54 +183,3 @@ function sortCourses(a: CalendarGridCourse, b: CalendarGridCourse): number {
}
return endIndexA - endIndexB;
}

/**
* Utility function also present in CourseMeeting object. Wasn't being found at runtime, so I copied it over.
*/
function getTimeString(options: TimeStringOptions, startTime: number, endTime: number): string {
const startHour = Math.floor(startTime / 60);
const startMinute = startTime % 60;
const endHour = Math.floor(endTime / 60);
const endMinute = endTime % 60;

let startTimeString = '';
let endTimeString = '';

if (startHour === 0) {
startTimeString = '12';
} else if (startHour > 12) {
startTimeString = `${startHour - 12}`;
} else {
startTimeString = `${startHour}`;
}

startTimeString += startMinute === 0 ? ':00' : `:${startMinute}`;
startTimeString += startHour >= 12 ? 'pm' : 'am';

if (endHour === 0) {
endTimeString = '12';
} else if (endHour > 12) {
endTimeString = `${endHour - 12}`;
} else {
endTimeString = `${endHour}`;
}
endTimeString += endMinute === 0 ? ':00' : `:${endMinute}`;
endTimeString += endHour >= 12 ? 'pm' : 'am';

if (options.capitalize) {
startTimeString = startTimeString.toUpperCase();
endTimeString = endTimeString.toUpperCase();
}

return `${startTimeString} ${options.separator} ${endTimeString}`;
}

/**
* Options to control the format of the time string
*/
type TimeStringOptions = {
/** the separator between the start and end times */
separator: string;
/** capitalizes the AM/PM */
capitalize?: boolean;
};

0 comments on commit 3406e9a

Please sign in to comment.