eeg_certs/frontend/src/utils/allCourses.ts
2023-08-31 12:11:48 -05:00

34 lines
976 B
TypeScript

import { Accessor, createMemo, createSignal } from "solid-js";
import { Course } from "../types/Course";
type CourseMap = {[k: number]: Course};
export const [allCourses, setAllCourses] = createSignal<Array<Course>>([]);
export const [courseMap, setCourseMap] = createSignal<CourseMap>({});
(() => {
// Get all courses from the API
fetch(`${import.meta.env.VITE_BACKEND_URL}/api/course`)
.then((res) => res.json())
.then((data: Array<Course>) => {
setAllCourses(data);
const map: CourseMap = {};
for (const course of data) {
map[course.course_id] = course;
}
setCourseMap(map);
});
})();
export function getCourseMemo(name: string): Accessor<Course | null> {
return createMemo(() => {
const course = allCourses().find((course) => course.course_name === name);
if (course) {
return course;
}
return null;
});
}