Skip to content

Commit

Permalink
Automatic last updated courses
Browse files Browse the repository at this point in the history
  • Loading branch information
brianrahadi committed Dec 12, 2024
1 parent 24c167b commit ca023bf
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
34 changes: 33 additions & 1 deletion pages/courses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import { useEffect, useState } from "react";
import { Course, Requirement, RequirementSchema } from "types/course";
import { z } from "zod";
import { SidebarCourse } from "components/SidebarCourse";
import { formatDate } from "utils";

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

const COURSES_API_COMMITS_URL =
"https://api.github.com/repos/ssss-sfu/course-explorer-script/commits?per_page=1";

const Courses: React.FC = () => {
// Parse the JSON data using Zod schemas
const [courseShown, setCourseShown] = useState<Course | null>(null);
const [requirements, setRequirements] = useState<Requirement[]>([]);
const [lastUpdatedDate, setLastUpdatedDate] = useState<string | null>(null);

const ONE_WEEK = 7 * 24 * 60 * 60 * 1000; // One week in milliseconds

Expand Down Expand Up @@ -42,7 +47,34 @@ const Courses: React.FC = () => {
}
};

const fetchLastCommitDate = async () => {
try {
const response = await fetch(COURSES_API_COMMITS_URL, {
headers: {
Accept: "application/vnd.github.v3+json",
},
});

if (!response.ok) {
throw new Error(`Error fetching data: ${response.statusText}`);
}

const commits = await response.json();

if (commits.length === 0) {
throw new Error("No commits found.");
}

const lastCommitDate = commits[0].commit.author.date;
setLastUpdatedDate(formatDate(lastCommitDate));
} catch (error: any) {
console.error("Error:", error.message);
throw error;
}
};

fetchCourses();
fetchLastCommitDate();
}, []);

return (
Expand Down Expand Up @@ -83,7 +115,7 @@ const Courses: React.FC = () => {
</a>{" "}
for official resources.
</p>
<p>Last updated: Oct 8, 2024</p>
<p>Last updated: {lastUpdatedDate}</p>
</section>
<section className="requirements-section">
<div
Expand Down
2 changes: 1 addition & 1 deletion scripts/course-explorer-script
7 changes: 0 additions & 7 deletions utils/index.js

This file was deleted.

40 changes: 40 additions & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function formatDate(date: any) {
return new Date(date).toLocaleDateString("en-US", {
month: "long",
day: "numeric",
year: "numeric",
});
}

async function getLastCommitDate(
repoOwner: string,
repoName: string
): Promise<string> {
const url = `https://api.github.com/repos/${repoOwner}/${repoName}/commits?per_page=1`;

try {
const response = await fetch(url, {
headers: {
Accept: "application/vnd.github.v3+json",
},
});

if (!response.ok) {
throw new Error(`Error fetching data: ${response.statusText}`);
}

const commits = await response.json();

if (commits.length === 0) {
throw new Error("No commits found.");
}

const lastCommitDate = commits[0].commit.author.date;
return lastCommitDate;
} catch (error: any) {
console.error("Error:", error.message);
throw error;
}
}

export { formatDate, getLastCommitDate };

0 comments on commit ca023bf

Please sign in to comment.