From 70b721fd041a0788ddd24315ae71362ddb10aa31 Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Thu, 11 Apr 2024 16:47:23 -0700 Subject: [PATCH] feat: fetch lastUpdated date from db query --- components/search/Blurb.tsx | 15 ++++++++++----- components/search/Search.tsx | 7 +++++-- lib/utils/query-db.ts | 14 ++++++++++---- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/components/search/Blurb.tsx b/components/search/Blurb.tsx index 92ecd99..988f25d 100644 --- a/components/search/Blurb.tsx +++ b/components/search/Blurb.tsx @@ -1,17 +1,20 @@ -import React from "react"; import { CourseObject, FilterValues } from "./Search"; +import { format } from "date-fns"; interface BlurbProps { filterData: ( data: CourseObject[], filterValues: FilterValues, ) => CourseObject[]; - data: CourseObject[] | undefined; + courses: CourseObject[] | undefined; + lastUpdated: number | undefined; filterValues: FilterValues; } const Blurb = (props: BlurbProps) => { - const { filterData, data, filterValues } = props; + const { filterData, courses, lastUpdated, filterValues } = props; + + const date = lastUpdated ? format(new Date(lastUpdated), "M/d") : "x"; return ( <> @@ -20,7 +23,9 @@ const Blurb = (props: BlurbProps) => {
We found{" "} - {data ? filterData(data, filterValues).length : "x"}{" "} + {courses + ? filterData(courses, filterValues).length + : "x"}{" "} courses {" "} based on your search and filters. Please consult an @@ -28,7 +33,7 @@ const Blurb = (props: BlurbProps) => {
- {"GE-Z's"} data was last updated on 3/7 + {"GE-Z's"} data was last updated on {date}
diff --git a/components/search/Search.tsx b/components/search/Search.tsx index 554ae3b..619dd19 100644 --- a/components/search/Search.tsx +++ b/components/search/Search.tsx @@ -115,6 +115,7 @@ const Search = () => { const [sort, setSort] = useState("Default Sort"); const [courses, setCourses] = useState(); + const [lastUpdated, setLastUpdated] = useState(); const [filterValues, setFilterValues] = useState({ format: format, @@ -199,7 +200,8 @@ const Search = () => { const geParam = ge.includes("GE") ? ge.split(" ")[1] : ge; const courses = await queryDatabase(universityParam, geParam); - setCourses(courses); + setCourses(courses.data); + setLastUpdated(courses.lastUpdated); setLoading(false); setError(false); @@ -306,8 +308,9 @@ const Search = () => {
diff --git a/lib/utils/query-db.ts b/lib/utils/query-db.ts index c8cef58..257f5aa 100644 --- a/lib/utils/query-db.ts +++ b/lib/utils/query-db.ts @@ -1,11 +1,16 @@ import { CourseObject } from "../../components/search/Search"; -const cache: Record = {}; +type DatabaseReturn = { + data: CourseObject[]; + lastUpdated: number; +}; + +const cache: Record = {}; export async function queryDatabase( university: string, ge: string, -): Promise { +): Promise { const cacheKey = university + ge; if (cache[cacheKey] && cache[cacheKey][0]) { @@ -29,10 +34,11 @@ export async function queryDatabase( } const data = await response.json(); + const output = { data: data.data, lastUpdated: data.lastUpdated }; - cache[cacheKey] = [new Date(), data.data]; + cache[cacheKey] = [new Date(), output]; - return data.data; + return output; } catch (error) { console.error("Error:", error); throw error;