refactor(UserSchedule): index by a unique id rather than name (#166)

* refactor(UserSchedule): index by a unique id rather than name

* refactor: Update parameter names in schedule function jsdocs

* refactor: change more instances of .name to .id

* refactor: Fix typo in variable name and update references

* refactor: Remove console.log statement

* fix(chromatic): Update ScheduleListItem story

* refactor: remove unused eslint rule
This commit is contained in:
Razboy20
2024-03-14 23:09:04 -05:00
committed by GitHub
parent 5714ed16d7
commit 85769e9d2c
31 changed files with 182 additions and 304 deletions

View File

@@ -3,14 +3,14 @@ import type { Course } from '@shared/types/Course';
/**
* Adds a course to a user's schedule.
* @param scheduleName - The name of the schedule to add the course to.
* @param scheduleId - The id of the schedule to add the course to.
* @param course - The course to add.
* @returns A promise that resolves to void.
* @throws An error if the schedule is not found.
*/
export default async function addCourse(scheduleName: string, course: Course): Promise<void> {
export default async function addCourse(scheduleId: string, course: Course): Promise<void> {
const schedules = await UserScheduleStore.get('schedules');
const activeSchedule = schedules.find(s => s.name === scheduleName);
const activeSchedule = schedules.find(s => s.id === scheduleId);
if (!activeSchedule) {
throw new Error('Schedule not found');
}

View File

@@ -2,14 +2,14 @@ import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
/**
* Clears the courses for a given schedule.
* @param scheduleName - The name of the schedule.
* @param scheduleId - The id of the schedule.
* @throws Error if the schedule does not exist.
*/
export default async function clearCourses(scheduleName: string): Promise<void> {
export default async function clearCourses(scheduleId: string): Promise<void> {
const schedules = await UserScheduleStore.get('schedules');
const schedule = schedules.find(schedule => schedule.name === scheduleName);
const schedule = schedules.find(schedule => schedule.id === scheduleId);
if (!schedule) {
throw new Error(`Schedule ${scheduleName} does not exist`);
throw new Error(`Schedule ${scheduleId} does not exist`);
}
schedule.courses = [];
schedule.updatedAt = Date.now();

View File

@@ -1,4 +1,5 @@
import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
import { generateRandomId } from '@shared/util/random';
/**
* Creates a new schedule with the given name
@@ -7,11 +8,12 @@ import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
*/
export default async function createSchedule(scheduleName: string): Promise<string | undefined> {
const schedules = await UserScheduleStore.get('schedules');
if (schedules.find(schedule => schedule.name === scheduleName)) {
return `Schedule ${scheduleName} already exists`;
}
// if (schedules.find(schedule => schedule.name === scheduleName)) {
// return `Schedule ${scheduleName} already exists`;
// }
schedules.push({
id: generateRandomId(),
name: scheduleName,
courses: [],
hours: 0,

View File

@@ -3,18 +3,18 @@ import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
/**
* Deletes a schedule with the specified name.
*
* @param scheduleName - The name of the schedule to delete.
* @param scheduleId - The id of the schedule to delete.
* @returns A promise that resolves to a string if there is an error, or undefined if the schedule is deleted successfully.
*/
export default async function deleteSchedule(scheduleName: string): Promise<string | undefined> {
export default async function deleteSchedule(scheduleId: string): Promise<string | undefined> {
const [schedules, activeIndex] = await Promise.all([
UserScheduleStore.get('schedules'),
UserScheduleStore.get('activeIndex'),
]);
const scheduleIndex = schedules.findIndex(schedule => schedule.name === scheduleName);
const scheduleIndex = schedules.findIndex(schedule => schedule.id === scheduleId);
if (scheduleIndex === -1) {
return `Schedule ${scheduleName} does not exist`;
return `Schedule ${scheduleId} does not exist`;
}
if (scheduleIndex === activeIndex) {
return 'Cannot delete active schedule';

View File

@@ -4,9 +4,9 @@ import type { Course } from '@shared/types/Course';
/**
*
*/
export default async function removeCourse(scheduleName: string, course: Course): Promise<void> {
export default async function removeCourse(scheduleId: string, course: Course): Promise<void> {
const schedules = await UserScheduleStore.get('schedules');
const activeSchedule = schedules.find(s => s.name === scheduleName);
const activeSchedule = schedules.find(s => s.id === scheduleId);
if (!activeSchedule) {
throw new Error('Schedule not found');
}

View File

@@ -2,19 +2,19 @@ import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
/**
* Renames a schedule with the specified name to a new name.
* @param scheduleName - The name of the schedule to be renamed.
* @param scheduleId - The id of the schedule to be renamed.
* @param newName - The new name for the schedule.
* @returns A promise that resolves to a string if there is an error, or undefined if the schedule is renamed successfully.
*/
export default async function renameSchedule(scheduleName: string, newName: string): Promise<string | undefined> {
export default async function renameSchedule(scheduleId: string, newName: string): Promise<string | undefined> {
const schedules = await UserScheduleStore.get('schedules');
const scheduleIndex = schedules.findIndex(schedule => schedule.name === scheduleName);
const scheduleIndex = schedules.findIndex(schedule => schedule.id === scheduleId);
if (scheduleIndex === -1) {
return `Schedule ${scheduleName} does not exist`;
}
if (schedules.find(schedule => schedule.name === newName)) {
return `Schedule ${newName} already exists`;
return `Schedule ${scheduleId} does not exist`;
}
// if (schedules.find(schedule => schedule.name === newName)) {
// return `Schedule ${newName} already exists`;
// }
schedules[scheduleIndex].name = newName;
schedules[scheduleIndex].updatedAt = Date.now();

View File

@@ -3,15 +3,15 @@ import { UserScheduleStore } from '@shared/storage/UserScheduleStore';
/**
* Switches the active schedule to the specified schedule name.
* Throws an error if the schedule does not exist.
* @param scheduleName - The name of the schedule to switch to.
* @param scheduleId - The id of the schedule to switch to.
* @returns A Promise that resolves when the active schedule is successfully switched.
*/
export default async function switchSchedule(scheduleName: string): Promise<void> {
export default async function switchSchedule(scheduleId: string): Promise<void> {
const schedules = await UserScheduleStore.get('schedules');
const scheduleIndex = schedules.findIndex(schedule => schedule.name === scheduleName);
const scheduleIndex = schedules.findIndex(schedule => schedule.id === scheduleId);
if (scheduleIndex === -1) {
throw new Error(`Schedule ${scheduleName} does not exist`);
throw new Error(`Schedule ${scheduleId} does not exist`);
}
schedules[scheduleIndex].updatedAt = Date.now();