Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refresh button refetches courses #750

Merged
merged 7 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function RightPane() {
{RightPaneStore.getDoDisplaySearch() ? (
<SearchForm toggleSearch={toggleSearch} />
) : (
<CourseRenderPane key={key} />
<CourseRenderPane key={key} id={key} />
)}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand All @@ -40,7 +41,7 @@ function flattenSOCObject(SOCObject: WebsocAPIResponse): (WebsocSchool | WebsocD

return accumulator;
}, []);
};
}
const RecruitmentBanner = () => {
const [bannerVisibility, setBannerVisibility] = React.useState<boolean>(true);

Expand Down Expand Up @@ -135,7 +136,7 @@ const SectionTableWrapped = (
return <div>{component}</div>;
};

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());
Expand Down Expand Up @@ -202,8 +203,10 @@ export function CourseRenderPane() {
}, []);

useEffect(() => {
console.log('loading courses, cache: ', websocCache);
loadCourses();
}, []);
}, [props.id]);


useEffect(() => {
const updateScheduleNames = () => {
Expand Down
4 changes: 2 additions & 2 deletions apps/antalmanac/src/lib/course-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 4 additions & 17 deletions apps/antalmanac/src/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<string, string>) {
if ('term' in record) {
const termValue = record['term'];
Expand Down Expand Up @@ -120,14 +107,14 @@ function cleanParams(record: Record<string, string>) {

// Construct a request to PeterPortal with the params as a query string
export async function queryWebsoc(params: Record<string, string>) {
// 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.
Expand Down