@@ -34,30 +34,23 @@ export function getCourseTags(course: CourseGQLData) {
34
34
}
35
35
36
36
// 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
+ }
54
47
}
55
48
56
49
// 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 ,
59
52
names : string [ ] ,
60
- ) : Promise < BatchCourseData | BatchProfessorData > {
53
+ ) : Promise < T extends 'courses' ? BatchCourseData : BatchProfessorData > {
61
54
const data =
62
55
index === 'courses'
63
56
? await trpc . courses . batch . mutate ( { courses : names } )
@@ -77,7 +70,7 @@ export async function searchAPIResults(
77
70
transformed [ key ] = transformGQLData ( index , data [ id ] ) ;
78
71
}
79
72
}
80
- return transformed ;
73
+ return transformed as T extends 'courses' ? BatchCourseData : BatchProfessorData ;
81
74
}
82
75
83
76
export const hourMinuteTo12HourString = ( { hour, minute } : { hour : number ; minute : number } ) =>
0 commit comments