Skip to content

Commit

Permalink
External courses.json
Browse files Browse the repository at this point in the history
  • Loading branch information
brianrahadi committed Oct 8, 2024
1 parent e5215c1 commit 079f76f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
23 changes: 10 additions & 13 deletions pages/courses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { z } from "zod";
import { SidebarCourse } from "components/SidebarCourse";

const COURSES_JSON_URL =
"https://raw.githubusercontent.com/ssss-sfu/course-explorer-script/main/result/courses.json/";
"https://raw.githubusercontent.com/ssss-sfu/course-explorer-script/main/result/courses.json";

const Courses: React.FC = () => {
// Parse the JSON data using Zod schemas
Expand All @@ -17,19 +17,16 @@ const Courses: React.FC = () => {
useEffect(() => {
const fetchCourses = async () => {
try {
const response = await fetch(COURSES_JSON_URL, {
mode: "no-cors",
headers: {
"Content-Type": "application/json",
},
});
const cachedData = localStorage.getItem("courses");

const json = cachedData
? JSON.parse(cachedData)
: await (await fetch(COURSES_JSON_URL)).json();

if (!cachedData) {
localStorage.setItem("courses", JSON.stringify(json));
}

// if (!response.ok) {
// throw new Error("Failed to fetch courses data");
// }
const json = await response.json();
// console.log(json);
// const coursesJson = await response.json();
setRequirements(z.array(RequirementSchema).parse(json));
} catch (error) {
console.error(error);
Expand Down
2 changes: 1 addition & 1 deletion scripts/course-explorer-script

0 comments on commit 079f76f

Please sign in to comment.