Skip to content

Commit

Permalink
feat: attempt 1 to hook settings.tsx to importSchedule
Browse files Browse the repository at this point in the history
  • Loading branch information
DereC4 committed Nov 17, 2024
1 parent 6df59bd commit 6df2c51
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/pages/background/lib/importSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ import switchSchedule from './switchSchedule';
* Imports a user schedule from portable file, creating a new schedule for it
* @param scheduleData - Data to be parsed back into a course schedule
*/
export default async function importSchedule(scheduleData: string) {
const parsedData = JSON.parse(scheduleData);
const newScheduleId = await createSchedule(parsedData.name);
await switchSchedule(newScheduleId);
const activeSchedule = getActiveSchedule();
export default async function importSchedule(scheduleData: string | null) {
if (scheduleData) {
const parsedData = JSON.parse(scheduleData);
const newScheduleId = await createSchedule(parsedData.name);
await switchSchedule(newScheduleId);
const activeSchedule = getActiveSchedule();

for (const course of parsedData.courses) {
if (course.url) {
// eslint-disable-next-line no-await-in-loop
await addCourseByURL(activeSchedule, course.url);
for (const course of parsedData.courses) {
if (course.url) {
// eslint-disable-next-line no-await-in-loop
await addCourseByURL(activeSchedule, course.url);
}
}
} else {
console.error('No schedule data provided for import');
}
}
2 changes: 2 additions & 0 deletions src/views/components/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { addCourseByURL } from '@pages/background/lib/addCourseByURL';
import { deleteAllSchedules } from '@pages/background/lib/deleteSchedule';
import exportSchedule from '@pages/background/lib/exportSchedule';
import importSchedule from '@pages/background/lib/importSchedule';
import { background } from '@shared/messages';
import { initSettings, OptionsStore } from '@shared/storage/OptionsStore';
import { downloadBlob } from '@shared/util/downloadBlob';
Expand Down Expand Up @@ -229,6 +230,7 @@ export default function Settings(): JSX.Element {
setImportedSchedule(JSON.stringify(jsonObject, null, 2));
console.log('Course schedule successfully parsed!');
console.log(jsonObject);
importSchedule(_importedSchedule);
} catch (error) {
console.error('invalid import file');
}
Expand Down

0 comments on commit 6df2c51

Please sign in to comment.