Skip to content

Commit

Permalink
feat: reworked how schedule is passed and check for file being schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
DereC4 committed Nov 18, 2024
1 parent fd0a291 commit c4cf138
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
20 changes: 14 additions & 6 deletions src/pages/background/lib/importSchedule.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import { Course } from '@shared/types/Course';
import type { UserSchedule } from '@shared/types/UserSchedule';
import type { Serialized } from 'chrome-extension-toolkit';

import addCourse from './addCourse';
import createSchedule from './createSchedule';
import switchSchedule from './switchSchedule';

function isValidSchedule(data: unknown): data is Serialized<UserSchedule> {
if (typeof data !== 'object' || data === null) return false;
const schedule = data as Record<string, unknown>;
return typeof schedule.id === 'string' && typeof schedule.name === 'string' && Array.isArray(schedule.courses);
}

/**
* 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 | null): Promise<void> {
if (scheduleData) {
const parsedData = JSON.parse(scheduleData);
const newScheduleId = await createSchedule(parsedData.name);
export default async function importSchedule(scheduleData: unknown): Promise<void> {
if (isValidSchedule(scheduleData)) {
const newScheduleId = await createSchedule(scheduleData.name);
await switchSchedule(newScheduleId);

for (const c of parsedData.courses) {
for (const c of scheduleData.courses) {
const course = new Course(c);
// eslint-disable-next-line no-await-in-loop
await addCourse(newScheduleId, course);
}
console.log('Course schedule successfully parsed!');
} else {
console.error('No schedule data provided for import');
}
Expand Down
12 changes: 2 additions & 10 deletions src/views/components/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export default function Settings(): JSX.Element {
const [highlightConflicts, setHighlightConflicts] = useState<boolean>(false);
const [loadAllCourses, setLoadAllCourses] = useState<boolean>(false);
const [_enableDataRefreshing, setEnableDataRefreshing] = useState<boolean>(false);
const [_importedSchedule, setImportedSchedule] = useState<string | null>(null);

const showMigrationDialog = useMigrationDialog();

Expand Down Expand Up @@ -227,11 +226,11 @@ export default function Settings(): JSX.Element {
const file = event.target.files?.[0];
if (file) {
const reader = new FileReader();
reader.onload = e => {
reader.onload = async e => {
try {
const result = e.target?.result as string;
const jsonObject = JSON.parse(result);
setImportedSchedule(JSON.stringify(jsonObject, null, 2));
await importSchedule(jsonObject);
} catch (error) {
console.error('Invalid import file!');
}
Expand All @@ -240,13 +239,6 @@ export default function Settings(): JSX.Element {
}
};

useEffect(() => {
if (_importedSchedule) {
console.log('Course schedule successfully parsed!');
importSchedule(_importedSchedule);
}
}, [_importedSchedule]);

// const handleAddCourseByLink = async () => {
// // todo: Use a proper modal instead of a prompt
// const link: string | null = prompt('Enter course link');
Expand Down

0 comments on commit c4cf138

Please sign in to comment.