From 24c167b6a11d879147d67e5883054896e2c01b3c Mon Sep 17 00:00:00 2001 From: Brian Rahadi Date: Tue, 8 Oct 2024 15:20:24 -0700 Subject: [PATCH] Refetch after a week --- pages/courses.tsx | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/pages/courses.tsx b/pages/courses.tsx index baef66b..cfc8976 100644 --- a/pages/courses.tsx +++ b/pages/courses.tsx @@ -13,19 +13,30 @@ const Courses: React.FC = () => { const [courseShown, setCourseShown] = useState(null); const [requirements, setRequirements] = useState([]); + const ONE_WEEK = 7 * 24 * 60 * 60 * 1000; // One week in milliseconds + useEffect(() => { const fetchCourses = async () => { try { const cachedData = localStorage.getItem("courses"); + const lastFetchTime = localStorage.getItem("lastFetchTime"); + const currentTime = Date.now(); - const json = cachedData - ? JSON.parse(cachedData) - : await (await fetch(COURSES_JSON_URL)).json(); - - if (!cachedData) { + // Check if the cached data exists and if it's been less than a week since last fetch + if ( + cachedData && + lastFetchTime && + currentTime - new Date(lastFetchTime).getTime() < ONE_WEEK + ) { + const json = JSON.parse(cachedData); + setRequirements(z.array(RequirementSchema).parse(json)); + } else { + // Fetch new data if no cached data or if a week has passed + const json = await (await fetch(COURSES_JSON_URL)).json(); localStorage.setItem("courses", JSON.stringify(json)); + localStorage.setItem("lastFetchTime", currentTime.toString()); + setRequirements(z.array(RequirementSchema).parse(json)); } - setRequirements(z.array(RequirementSchema).parse(json)); } catch (error) { console.error(error); }