eeg_certs/frontend/src/utils/allCourses.ts

34 lines
976 B
TypeScript
Raw Normal View History

2023-08-31 17:11:48 +00:00
import { Accessor, createMemo, createSignal } from "solid-js";
2023-08-25 22:54:30 +00:00
import { Course } from "../types/Course";
2023-08-29 16:14:33 +00:00
type CourseMap = {[k: number]: Course};
2023-08-25 22:54:30 +00:00
export const [allCourses, setAllCourses] = createSignal<Array<Course>>([]);
2023-08-29 16:14:33 +00:00
export const [courseMap, setCourseMap] = createSignal<CourseMap>({});
2023-08-25 22:54:30 +00:00
(() => {
// Get all courses from the API
fetch(`${import.meta.env.VITE_BACKEND_URL}/api/course`)
.then((res) => res.json())
2023-08-29 16:14:33 +00:00
.then((data: Array<Course>) => {
setAllCourses(data);
const map: CourseMap = {};
for (const course of data) {
map[course.course_id] = course;
}
setCourseMap(map);
});
2023-08-25 22:54:30 +00:00
})();
2023-08-31 17:11:48 +00:00
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;
});
}