Skip to content

Commit

Permalink
fix: fixed bug involving saving courses with draggable list
Browse files Browse the repository at this point in the history
  • Loading branch information
knownotunknown committed Apr 11, 2024
1 parent bae1da4 commit 303c065
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/pages/background/lib/addCourse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
import type { Course } from '@shared/types/Course';
import { getUnusedColor } from '@shared/util/colors';
import { generateRandomId } from '@shared/util/random';

/**
* Adds a course to a user's schedule.
Expand All @@ -19,6 +20,7 @@ export default async function addCourse(scheduleId: string, course: Course): Pro
course.colors = getUnusedColor(activeSchedule, course);
activeSchedule.courses.push(course);
activeSchedule.updatedAt = Date.now();
activeSchedule.id = generateRandomId();

await UserScheduleStore.set('schedules', schedules);
}
10 changes: 6 additions & 4 deletions src/pages/background/lib/clearCourses.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
import { generateRandomId } from '@shared/util/random';

/**
* Clears the courses for a given schedule.
Expand All @@ -7,12 +8,13 @@ import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
*/
export default async function clearCourses(scheduleId: string): Promise<void> {
const schedules = await UserScheduleStore.get('schedules');
const schedule = schedules.find(schedule => schedule.id === scheduleId);
if (!schedule) {
const activeSchedule = schedules.find(schedule => schedule.id === scheduleId);
if (!activeSchedule) {
throw new Error(`Schedule ${scheduleId} does not exist`);
}
schedule.courses = [];
schedule.updatedAt = Date.now();
activeSchedule.courses = [];
activeSchedule.updatedAt = Date.now();
activeSchedule.id = generateRandomId();

await UserScheduleStore.set('schedules', schedules);
}
2 changes: 2 additions & 0 deletions src/pages/background/lib/removeCourse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
import type { Course } from '@shared/types/Course';
import { generateRandomId } from '@shared/util/random';

/**
*
Expand All @@ -13,6 +14,7 @@ export default async function removeCourse(scheduleId: string, course: Course):

activeSchedule.courses = activeSchedule.courses.filter(c => c.uniqueId !== course.uniqueId);
activeSchedule.updatedAt = Date.now();
activeSchedule.id = generateRandomId();

await UserScheduleStore.set('schedules', schedules);
}
12 changes: 7 additions & 5 deletions src/pages/background/lib/renameSchedule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
import { generateRandomId } from '@shared/util/random';

/**
* Renames a schedule with the specified name to a new name.
Expand All @@ -8,13 +9,14 @@ import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
*/
export default async function renameSchedule(scheduleId: string, newName: string): Promise<string | undefined> {
const schedules = await UserScheduleStore.get('schedules');
const scheduleIndex = schedules.findIndex(schedule => schedule.id === scheduleId);
if (scheduleIndex === -1) {
return `Schedule ${scheduleId} does not exist`;
const activeSchedule = schedules.find(schedule => schedule.id === scheduleId);
if (!activeSchedule) {
throw new Error(`Schedule ${scheduleId} does not exist`);
}

schedules[scheduleIndex]!.name = newName;
// schedules[scheduleIndex].updatedAt = Date.now();
activeSchedule.name = newName;
activeSchedule.updatedAt = Date.now();
activeSchedule.id = generateRandomId();

await UserScheduleStore.set('schedules', schedules);
return undefined;
Expand Down
12 changes: 7 additions & 5 deletions src/pages/background/lib/switchSchedule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
import { generateRandomId } from '@shared/util/random';

/**
* Switches the active schedule to the specified schedule name.
Expand All @@ -8,13 +9,14 @@ import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
*/
export default async function switchSchedule(scheduleId: string): Promise<void> {
const schedules = await UserScheduleStore.get('schedules');

const scheduleIndex = schedules.findIndex(schedule => schedule.id === scheduleId);
if (scheduleIndex === -1) {
const activeIndex = schedules.findIndex(schedule => schedule.id === scheduleId)
const activeSchedule = schedules.find(schedule => schedule.id === scheduleId);
if (!activeSchedule) {
throw new Error(`Schedule ${scheduleId} does not exist`);
}

schedules[scheduleIndex]!.updatedAt = Date.now();
activeSchedule.updatedAt = Date.now();
activeSchedule.id = generateRandomId();

await UserScheduleStore.set('activeIndex', scheduleIndex);
await UserScheduleStore.set('activeIndex', activeIndex);
}
2 changes: 1 addition & 1 deletion src/shared/types/UserSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class UserSchedule {

constructor(schedule: Serialized<UserSchedule>) {
this.courses = schedule.courses.map(c => new Course(c));
this.id = schedule.id ?? generateRandomId();
this.id = generateRandomId();
this.name = schedule.name;
this.hours = this.courses.reduce((acc, c) => acc + c.creditHours, 0);
this.updatedAt = schedule.updatedAt ?? 0;
Expand Down
1 change: 1 addition & 0 deletions src/views/components/calendar/CalendarSchedules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function CalendarSchedules() {
onReordered={reordered => {
const activeSchedule = getActiveSchedule();
const activeIndex = reordered.findIndex(s => s.id === activeSchedule.id);
reordered[activeIndex] = activeSchedule;

// don't care about the promise
UserScheduleStore.set('schedules', reordered);
Expand Down
2 changes: 0 additions & 2 deletions src/views/hooks/useSchedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export async function replaceSchedule(oldSchedule: UserSchedule, newSchedule: Us
oldIndex = oldIndex !== -1 ? oldIndex : 0;
schedules[oldIndex] = newSchedule;
await UserScheduleStore.set('schedules', schedules);
console.log('schedule replaced');
}

/**
Expand All @@ -98,7 +97,6 @@ export async function replaceSchedule(oldSchedule: UserSchedule, newSchedule: Us
* @returns A promise that resolves when the active schedule has been switched.
*/
export async function switchSchedule(id: string): Promise<void> {
console.log('Switching schedule...');
const schedules = await UserScheduleStore.get('schedules');
const activeIndex = schedules.findIndex(s => s.id === id);
await UserScheduleStore.set('activeIndex', activeIndex);
Expand Down

0 comments on commit 303c065

Please sign in to comment.