Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Schedule Switching Bugs #138

Merged
merged 5 commits into from
Mar 10, 2024
Merged
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
10 changes: 6 additions & 4 deletions src/shared/types/UserSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ export class UserSchedule {
this.name = name || '';
this.hours = hours || 0;
} else {
this.courses = coursesOrSchedule.courses.map(c => new Course(c));
this.name = coursesOrSchedule.name;
this.courses = coursesOrSchedule?.courses.map(c => new Course(c)) || [];
this.name = coursesOrSchedule?.name || 'new schedule';
this.hours = 0;
for (const course of this.courses) {
this.hours += course.creditHours;
if (this.courses && this.courses.length > 0) {
for (const course of this.courses) {
this.hours += course.creditHours;
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/views/components/PopupMain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function PopupMain(): JSX.Element {

useEffect(() => {
function handleClickOutside(event) {
if (!popupRef.current.contains(event.target) && !toggleRef.current.contains(event.target)) {
if (!popupRef.current?.contains(event.target) && !toggleRef.current?.contains(event.target)) {
setIsPopupVisible(false);
}
}
Expand Down Expand Up @@ -114,7 +114,7 @@ export default function PopupMain(): JSX.Element {
{nonActiveSchedules.map(schedule => (
<div
key={schedule.name}
className='my-2 cursor-pointer border border-gray-300 rounded-md border-solid bg-white py-4 shadow-sm hover:bg-gray-100'
className='my-2 cursor-pointer border border-gray-300 rounded-md border-solid bg-white p-2 shadow-sm hover:bg-gray-100'
onClick={() => selectSchedule(schedule)}
>
<Text as='div' variant='h1-course' className='color-ut-burntorange'>
Expand Down
38 changes: 21 additions & 17 deletions src/views/hooks/useSchedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { useEffect, useState } from 'react';
*/
export default function useSchedules(): [active: UserSchedule | null, schedules: UserSchedule[]] {
const [schedules, setSchedules] = useState<UserSchedule[]>([]);
const [activeIndex, setActiveIndex] = useState<number>(0);
const [activeSchedule, setActiveSchedule] = useState<UserSchedule | null>(null);

useEffect(() => {
Expand All @@ -18,31 +17,36 @@ export default function useSchedules(): [active: UserSchedule | null, schedules:
UserScheduleStore.get('activeIndex'),
]);
setSchedules(storedSchedules.map(s => new UserSchedule(s)));
setActiveIndex(storedActiveIndex);
setActiveSchedule(new UserSchedule(storedSchedules[storedActiveIndex]));
};

const initializable = UserScheduleStore.initialize();
fetchData();

if (initializable) {
const l1 = UserScheduleStore.listen('schedules', ({ newValue }) => {
setSchedules(newValue.map(s => new UserSchedule(s)));
setActiveSchedule(new UserSchedule(newValue[activeIndex]));
const setupListeners = () => {
const l1 = UserScheduleStore.listen('schedules', ({ newValue }) => {
setSchedules(newValue.map(s => new UserSchedule(s)));
setActiveSchedule(currentActive => {
const newActiveIndex = newValue.findIndex(s => s.name === currentActive?.name);
return new UserSchedule(newValue[newActiveIndex]);
});
});

const l2 = UserScheduleStore.listen('activeIndex', ({ newValue }) => {
setActiveIndex(newValue);
setActiveSchedule(new UserSchedule(schedules[newValue]));
const l2 = UserScheduleStore.listen('activeIndex', ({ newValue }) => {
setSchedules(currentSchedules => {
setActiveSchedule(new UserSchedule(currentSchedules[newValue]));
return currentSchedules;
});
});

return () => {
UserScheduleStore.removeListener(l1);
UserScheduleStore.removeListener(l2);
};
}
return () => {
UserScheduleStore.removeListener(l1);
UserScheduleStore.removeListener(l2);
};
};

fetchData();
}, [activeIndex, schedules]);
const init = UserScheduleStore.initialize();
init.then(() => setupListeners()).catch(console.error);
}, []);

return [activeSchedule, schedules];
}
Expand Down
Loading