Skip to content

Commit

Permalink
feat: fetch lastUpdated date from db query
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 committed Apr 11, 2024
1 parent eb1eb37 commit 70b721f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
15 changes: 10 additions & 5 deletions components/search/Blurb.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
Expand All @@ -20,15 +23,17 @@ const Blurb = (props: BlurbProps) => {
<div>
We found{" "}
<b className="text-black">
{data ? filterData(data, filterValues).length : "x"}{" "}
{courses
? filterData(courses, filterValues).length
: "x"}{" "}
courses
</b>{" "}
based on your search and filters. Please consult an
academic advisor for further information.
</div>

<div className="flex text-sm font-light text-gray md:justify-end md:text-base">
{"GE-Z's"} data was last updated on 3/7
{"GE-Z's"} data was last updated on {date}
</div>
</div>

Expand Down
7 changes: 5 additions & 2 deletions components/search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const Search = () => {
const [sort, setSort] = useState("Default Sort");

const [courses, setCourses] = useState<CourseObject[]>();
const [lastUpdated, setLastUpdated] = useState<number>();

const [filterValues, setFilterValues] = useState<FilterValues>({
format: format,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -306,8 +308,9 @@ const Search = () => {

<Blurb
filterData={filterData}
data={courses}
courses={courses}
filterValues={filterValues}
lastUpdated={lastUpdated}
/>

<div className="mt-4 flex flex-row gap-4 md:mt-8 md:gap-8">
Expand Down
14 changes: 10 additions & 4 deletions lib/utils/query-db.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { CourseObject } from "../../components/search/Search";

const cache: Record<string, [Date, CourseObject[]]> = {};
type DatabaseReturn = {
data: CourseObject[];
lastUpdated: number;
};

const cache: Record<string, [Date, DatabaseReturn]> = {};

export async function queryDatabase(
university: string,
ge: string,
): Promise<CourseObject[]> {
): Promise<DatabaseReturn> {
const cacheKey = university + ge;

if (cache[cacheKey] && cache[cacheKey][0]) {
Expand All @@ -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;
Expand Down

0 comments on commit 70b721f

Please sign in to comment.