From 9fb3069845fddc1852086d5f35925c5d68c82201 Mon Sep 17 00:00:00 2001 From: Aponia Date: Thu, 19 Oct 2023 10:48:01 -0700 Subject: [PATCH 1/5] fix: refresh button refetches courses --- .../src/components/RightPane/CoursePane/CoursePaneRoot.tsx | 2 +- .../components/RightPane/CoursePane/CourseRenderPane.tsx | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/antalmanac/src/components/RightPane/CoursePane/CoursePaneRoot.tsx b/apps/antalmanac/src/components/RightPane/CoursePane/CoursePaneRoot.tsx index f953dda83..46f498fd0 100644 --- a/apps/antalmanac/src/components/RightPane/CoursePane/CoursePaneRoot.tsx +++ b/apps/antalmanac/src/components/RightPane/CoursePane/CoursePaneRoot.tsx @@ -66,7 +66,7 @@ function RightPane() { {RightPaneStore.getDoDisplaySearch() ? ( ) : ( - + )} ); diff --git a/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx b/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx index c812b3d18..293ff4443 100644 --- a/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx +++ b/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx @@ -40,7 +40,7 @@ function flattenSOCObject(SOCObject: WebsocAPIResponse): (WebsocSchool | WebsocD return accumulator; }, []); -}; +} const RecruitmentBanner = () => { const [bannerVisibility, setBannerVisibility] = React.useState(true); @@ -135,7 +135,7 @@ const SectionTableWrapped = ( return
{component}
; }; -export function CourseRenderPane() { +export function CourseRenderPane(props: { id?: number }) { const [loading, setLoading] = useState(true); const [error, setError] = useState(false); const [scheduleNames, setScheduleNames] = useState(AppStore.getScheduleNames()); @@ -203,7 +203,7 @@ export function CourseRenderPane() { useEffect(() => { loadCourses(); - }, []); + }, [props.id]); useEffect(() => { const updateScheduleNames = () => { From 0bca6bc8ac4a3b90d1196f5ffbc8c18d566582e3 Mon Sep 17 00:00:00 2001 From: Aponia Date: Thu, 19 Oct 2023 15:59:45 -0700 Subject: [PATCH 2/5] chore: add debugging statements --- .../src/components/RightPane/CoursePane/CourseRenderPane.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx b/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx index 293ff4443..4e2a0a579 100644 --- a/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx +++ b/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx @@ -202,9 +202,12 @@ export function CourseRenderPane(props: { id?: number }) { }, []); useEffect(() => { + console.log('loading courses'); loadCourses(); }, [props.id]); + console.log('id: ', props.id); + useEffect(() => { const updateScheduleNames = () => { setScheduleNames(AppStore.getScheduleNames()); From 960809466a2110e11cd1ea0f809f2a189cee4200 Mon Sep 17 00:00:00 2001 From: Aponia Date: Thu, 19 Oct 2023 16:16:11 -0700 Subject: [PATCH 3/5] bruh, why were there two separate websoc caches --- .../RightPane/CoursePane/CourseRenderPane.tsx | 3 ++- apps/antalmanac/src/lib/course-helpers.ts | 4 ++-- apps/antalmanac/src/lib/helpers.ts | 21 ++++--------------- 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx b/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx index 4e2a0a579..86b578b01 100644 --- a/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx +++ b/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx @@ -18,6 +18,7 @@ import AppStore from '$stores/AppStore'; import { isDarkMode, queryWebsoc, queryWebsocMultiple } from '$lib/helpers'; import Grades from '$lib/grades'; import analyticsEnum from '$lib/analytics'; +import { websocCache } from '$lib/course-helpers'; function flattenSOCObject(SOCObject: WebsocAPIResponse): (WebsocSchool | WebsocDepartment | AACourse)[] { const courseColors = AppStore.getAddedCourses().reduce((accumulator, { section }) => { @@ -202,7 +203,7 @@ export function CourseRenderPane(props: { id?: number }) { }, []); useEffect(() => { - console.log('loading courses'); + console.log('loading courses, cache: ', websocCache); loadCourses(); }, [props.id]); diff --git a/apps/antalmanac/src/lib/course-helpers.ts b/apps/antalmanac/src/lib/course-helpers.ts index c66c38d10..fbbc3327f 100644 --- a/apps/antalmanac/src/lib/course-helpers.ts +++ b/apps/antalmanac/src/lib/course-helpers.ts @@ -6,10 +6,10 @@ interface CacheEntry extends WebsocAPIResponse { timestamp: number; } -const websocCache: { [key: string]: CacheEntry } = {}; +export let websocCache: { [key: string]: CacheEntry } = {}; export function clearCache() { - Object.keys(websocCache).forEach((key) => delete websocCache[key]); //https://stackoverflow.com/a/19316873/14587004 + websocCache = {}; } export function getCourseInfo(SOCObject: WebsocAPIResponse) { diff --git a/apps/antalmanac/src/lib/helpers.ts b/apps/antalmanac/src/lib/helpers.ts index 279027c01..ce0c04ff1 100644 --- a/apps/antalmanac/src/lib/helpers.ts +++ b/apps/antalmanac/src/lib/helpers.ts @@ -1,8 +1,6 @@ -import React from 'react'; - import { WebsocSectionMeeting, WebsocSection, WebsocAPIResponse } from 'peterportal-api-next-types'; import { PETERPORTAL_GRAPHQL_ENDPOINT, PETERPORTAL_WEBSOC_ENDPOINT } from './api/endpoints'; -import Grades from './grades'; +import { websocCache } from './course-helpers'; import { addCourse, openSnackbar } from '$actions/AppStoreActions'; import AppStore from '$stores/AppStore'; import { RepeatingCustomEvent } from '$components/Calendar/Toolbar/CustomEventDialog/CustomEventDialog'; @@ -71,17 +69,6 @@ export async function queryZotCourse(schedule_name: string) { }; } -interface CacheEntry extends WebsocAPIResponse { - timestamp: number; -} - -const websocCache: { [key: string]: CacheEntry } = {}; - -export function clearCache() { - Object.keys(websocCache).forEach((key) => delete websocCache[key]); //https://stackoverflow.com/a/19316873/14587004 - Grades.clearCache(); -} - function cleanParams(record: Record) { if ('term' in record) { const termValue = record['term']; @@ -120,14 +107,14 @@ function cleanParams(record: Record) { // Construct a request to PeterPortal with the params as a query string export async function queryWebsoc(params: Record) { - // Construct a request to PeterPortal with the params as a query string const url = new URL(PETERPORTAL_WEBSOC_ENDPOINT); + const searchString = new URLSearchParams(cleanParams(params)).toString(); + if (websocCache[searchString]?.timestamp > Date.now() - 30 * 60 * 1000) { - //NOTE: Check out how caching works - //if cache hit and less than 30 minutes old return websocCache[searchString]; } + url.search = searchString; //The data from the API will duplicate a section if it has multiple locations. From 0badb1eff867a6fcd694a5aefe4cf5be0ab20049 Mon Sep 17 00:00:00 2001 From: Minh Nguyen <64875104+MinhxNguyen7@users.noreply.github.com> Date: Thu, 26 Oct 2023 00:12:34 -0700 Subject: [PATCH 4/5] Remove console.log Co-authored-by: Kevin Wu --- .../src/components/RightPane/CoursePane/CourseRenderPane.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx b/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx index 86b578b01..339a87b14 100644 --- a/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx +++ b/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx @@ -207,7 +207,6 @@ export function CourseRenderPane(props: { id?: number }) { loadCourses(); }, [props.id]); - console.log('id: ', props.id); useEffect(() => { const updateScheduleNames = () => { From 767b553bd76cb1c745823ae9e2cfa47b5ef8704a Mon Sep 17 00:00:00 2001 From: Eric Pedley Date: Thu, 26 Oct 2023 19:24:08 -0700 Subject: [PATCH 5/5] put loadCourses dependency back in useEffect --- .../src/components/RightPane/CoursePane/CourseRenderPane.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx b/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx index e6725651f..a543067e5 100644 --- a/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx +++ b/apps/antalmanac/src/components/RightPane/CoursePane/CourseRenderPane.tsx @@ -48,7 +48,7 @@ const flattenSOCObject = (SOCObject: WebsocAPIResponse): (WebsocSchool | WebsocD return accumulator; }, []); -} +}; const RecruitmentBanner = () => { const [bannerVisibility, setBannerVisibility] = useState(true); @@ -243,7 +243,7 @@ export default function CourseRenderPane(props: { id?: number }) { return () => { AppStore.off('scheduleNamesChange', updateScheduleNames); }; - }, [props.id]); + }, [loadCourses, props.id]); return ( <>