Skip to content

Commit 7ff6ed0

Browse files
committed
refactor type of searchAPIResult helper, fix course page console error
1 parent 49592ac commit 7ff6ed0

File tree

5 files changed

+20
-29
lines changed

5 files changed

+20
-29
lines changed

site/src/helpers/planner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const expandPlanner = async (savedPlanner: SavedPlannerYearData[]): Promi
9090
let courseLookup: BatchCourseData = {};
9191
// only send request if there are courses
9292
if (courses.length > 0) {
93-
courseLookup = (await searchAPIResults('courses', courses)) as BatchCourseData;
93+
courseLookup = await searchAPIResults('courses', courses);
9494
}
9595

9696
return new Promise((resolve) => {

site/src/helpers/util.tsx

+14-21
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,23 @@ export function getCourseTags(course: CourseGQLData) {
3434
}
3535

3636
// helper function to search 1 result from course/professor page
37-
export function searchAPIResult(type: SearchType, name: string) {
38-
return new Promise<CourseGQLData | ProfessorGQLData | undefined>((res) => {
39-
let index: SearchIndex;
40-
if (type === 'course') {
41-
index = 'courses';
42-
} else {
43-
index = 'professors';
44-
}
45-
searchAPIResults(index, [name]).then((results) => {
46-
if (Object.keys(results).length > 0) {
47-
const key = Object.keys(results)[0];
48-
res(results[key]);
49-
} else {
50-
res(undefined);
51-
}
52-
});
53-
});
37+
export async function searchAPIResult<T extends SearchType>(
38+
type: T,
39+
name: string,
40+
): Promise<(T extends 'course' ? CourseGQLData : ProfessorGQLData) | undefined> {
41+
const results = await searchAPIResults(`${type}s`, [name]);
42+
if (Object.keys(results).length > 0) {
43+
return Object.values(results)[0];
44+
} else {
45+
return undefined;
46+
}
5447
}
5548

5649
// helper function to query from API and transform to data used in redux
57-
export async function searchAPIResults(
58-
index: SearchIndex,
50+
export async function searchAPIResults<T extends SearchIndex>(
51+
index: T,
5952
names: string[],
60-
): Promise<BatchCourseData | BatchProfessorData> {
53+
): Promise<T extends 'courses' ? BatchCourseData : BatchProfessorData> {
6154
const data =
6255
index === 'courses'
6356
? await trpc.courses.batch.mutate({ courses: names })
@@ -77,7 +70,7 @@ export async function searchAPIResults(
7770
transformed[key] = transformGQLData(index, data[id]);
7871
}
7972
}
80-
return transformed;
73+
return transformed as T extends 'courses' ? BatchCourseData : BatchProfessorData;
8174
}
8275

8376
export const hourMinuteTo12HourString = ({ hour, minute }: { hour: number; minute: number }) =>

site/src/pages/CoursePage/index.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import Error from '../../component/Error/Error';
1313

1414
import { useAppSelector, useAppDispatch } from '../../store/hooks';
1515
import { setCourse } from '../../store/slices/popupSlice';
16-
import { CourseGQLData } from '../../types/types';
1716
import { getCourseTags, searchAPIResult, sortTerms } from '../../helpers/util';
1817
import './CoursePage.scss';
1918

@@ -27,9 +26,9 @@ const CoursePage: FC = () => {
2726
if (id !== undefined) {
2827
searchAPIResult('course', id).then((course) => {
2928
if (course) {
30-
dispatch(setCourse(course as CourseGQLData));
29+
dispatch(setCourse(course));
3130
setError('');
32-
document.title = `${courseGQLData.department + ' ' + courseGQLData.courseNumber} | PeterPortal`;
31+
document.title = `${course.department + ' ' + course.courseNumber} | PeterPortal`;
3332
} else {
3433
setError(`Course ${id} does not exist!`);
3534
}

site/src/pages/ProfessorPage/index.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import Error from '../../component/Error/Error';
1212

1313
import { setProfessor } from '../../store/slices/popupSlice';
1414
import { useAppSelector, useAppDispatch } from '../../store/hooks';
15-
import { ProfessorGQLData } from '../../types/types';
1615
import { searchAPIResult, unionTerms } from '../../helpers/util';
1716

1817
const ProfessorPage: FC = () => {
@@ -25,9 +24,9 @@ const ProfessorPage: FC = () => {
2524
if (id !== undefined) {
2625
searchAPIResult('professor', id).then((professor) => {
2726
if (professor) {
28-
dispatch(setProfessor(professor as ProfessorGQLData));
27+
dispatch(setProfessor(professor));
2928
setError('');
30-
document.title = `${(professor as ProfessorGQLData).name} | PeterPortal`;
29+
document.title = `${professor.name} | PeterPortal`;
3130
} else {
3231
setError(`Professor ${id} does not exist!`);
3332
}

site/src/pages/RoadmapPage/ImportTranscriptPopup.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async function htmlFromFile(file: Blob): Promise<HTMLElement> {
7878

7979
async function transcriptCourseDetails(quarters: TranscriptQuarter[]): Promise<BatchCourseData> {
8080
const courseIDs = quarters.flatMap((q) => q.courses).map(toCourseID);
81-
const results = (await searchAPIResults('courses', courseIDs)) as BatchCourseData;
81+
const results = await searchAPIResults('courses', courseIDs);
8282
return results;
8383
}
8484

0 commit comments

Comments
 (0)