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