-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSearchResultsTable.tsx
374 lines (361 loc) · 11.9 KB
/
SearchResultsTable.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import KeyboardArrowIcon from '@mui/icons-material/KeyboardArrowRight';
import PersonIcon from '@mui/icons-material/Person';
import {
Badge,
Collapse,
IconButton,
Paper,
Skeleton,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
Tooltip,
Typography,
} from '@mui/material';
import React, { useState } from 'react';
import Rating from '~components/Rating';
import SingleGradesInfo from '~components/SingleGradesInfo';
import SingleProfInfo from '~components/SingleProfInfo';
import TableSortLabel from '~components/TableSortLabel';
import type { RMPInterface } from '~data/fetchFromRmp';
import type { GenericFetchedData, GradesType } from '~pages';
import { useRainbowColors } from '~utils/colors';
import {
convertToCourseOnly,
convertToProfOnly,
type SearchQuery,
searchQueryLabel,
} from '~utils/SearchQuery';
type RowProps = {
course: SearchQuery;
grades: GenericFetchedData<GradesType>;
backupGrades: GenericFetchedData<GradesType>;
rmp: GenericFetchedData<RMPInterface>;
setPage: (arg0: SearchQuery) => void;
};
function Row({ course, grades, backupGrades, rmp, setPage }: RowProps) {
const [open, setOpen] = useState(false);
const canOpen =
!(typeof grades === 'undefined' || grades.state === 'error') ||
!(typeof rmp === 'undefined' || rmp.state === 'error');
const rainbowColors = useRainbowColors();
const gpaToColor = (gpa: number): string => {
if (gpa >= 4.0) return rainbowColors[1];
if (gpa >= 3.67) return rainbowColors[2];
if (gpa >= 3.33) return rainbowColors[3];
if (gpa >= 3.0) return rainbowColors[4];
if (gpa >= 2.67) return rainbowColors[5];
if (gpa >= 2.33) return rainbowColors[6];
if (gpa >= 2.0) return rainbowColors[7];
if (gpa >= 1.67) return rainbowColors[8];
if (gpa >= 1.33) return rainbowColors[9];
if (gpa >= 1.0) return rainbowColors[10];
if (gpa >= 0.67) return rainbowColors[11];
return rainbowColors[12];
};
return (
<>
<TableRow>
<TableCell className="border-b-0 pb-0" colSpan={6}>
<Typography className="leading-tight text-lg text-gray-600 dark:text-gray-200">
{searchQueryLabel(convertToProfOnly(course))}
</Typography>
</TableCell>
</TableRow>
<TableRow
onClick={() => {
if (canOpen) {
setOpen(!open);
}
}} // opens/closes the card by clicking anywhere on the row
>
<TableCell className="border-b-0 pr-0">
<Tooltip
title={open ? 'Minimize Result' : 'Expand Result'}
placement="top"
>
<IconButton
aria-label="expand row"
className={'transition-transform' + (open ? ' rotate-90' : '')}
disabled={!canOpen}
>
<KeyboardArrowIcon />
</IconButton>
</Tooltip>
</TableCell>
<TableCell
className="border-b-0"
onClick={
(e) => e.stopPropagation() // prevents opening/closing the card when clicking on the compare checkbox
}
>
<Tooltip title="Open professor profile" placement="top">
<IconButton
aria-label="open professor profile"
onClick={() => setPage(convertToProfOnly(course))}
>
<PersonIcon />
</IconButton>
</Tooltip>
</TableCell>
<TableCell align="right" className="border-b-0">
{((typeof grades === 'undefined' || grades.state === 'error') &&
(((typeof backupGrades === 'undefined' ||
backupGrades.state === 'error') && <></>) ||
(backupGrades.state === 'loading' && (
<Skeleton
variant="rounded"
className="rounded-full px-5 py-2 ml-auto"
>
<Typography className="text-base">A</Typography>
</Skeleton>
)) ||
(backupGrades.state === 'done' && (
<Tooltip
title={'GPA: ' + backupGrades.data.gpa.toFixed(2)}
placement="top"
>
<Badge
color="primary"
sx={{
'& .MuiBadge-badge': {
padding: 0,
},
}}
badgeContent={
<Tooltip
title={
'Grade from professor overall, not just ' +
searchQueryLabel(convertToCourseOnly(course))
}
>
<div className="w-full h-full flex justify-center items-center">
*
</div>
</Tooltip>
}
>
<Typography
className="text-base text-black rounded-full px-5 py-2 inline"
sx={{
backgroundColor: gpaToColor(backupGrades.data.gpa),
}}
>
{backupGrades.data.letter_grade}
</Typography>
</Badge>
</Tooltip>
)))) ||
(grades.state === 'loading' && (
<Skeleton
variant="rounded"
className="rounded-full px-5 py-2 ml-auto"
>
<Typography className="text-base">A</Typography>
</Skeleton>
)) ||
(grades.state === 'done' && (
<Tooltip
title={'GPA: ' + grades.data.gpa.toFixed(2)}
placement="top"
>
<Typography
className="text-base text-black rounded-full px-5 py-2 inline"
sx={{ backgroundColor: gpaToColor(grades.data.gpa) }}
>
{grades.data.letter_grade}
</Typography>
</Tooltip>
)) ||
null}
</TableCell>
<TableCell align="right" className="border-b-0">
{((typeof rmp === 'undefined' || rmp.state === 'error') && <></>) ||
(rmp.state === 'loading' && (
<Skeleton variant="rounded" className="rounded-full ml-auto">
<Rating sx={{ fontSize: 25 }} readOnly />
</Skeleton>
)) ||
(rmp.state === 'done' && (
<Tooltip
title={'Professor rating: ' + rmp.data.avgRating}
placement="top"
>
<div>
<Rating
defaultValue={rmp.data.avgRating}
precision={0.1}
sx={{ fontSize: 25 }}
readOnly
/>
</div>
</Tooltip>
)) ||
null}
</TableCell>
</TableRow>
<TableRow>
<TableCell className="p-0" colSpan={6}>
<Collapse in={open} timeout="auto" unmountOnExit>
<div className="p-2 md:p-4 flex flex-col gap-2">
<SingleGradesInfo course={course} grades={grades} />
<SingleProfInfo rmp={rmp} />
</div>
</Collapse>
</TableCell>
</TableRow>
</>
);
}
type SearchResultsTableProps = {
results: SearchQuery[];
grades: { [key: string]: GenericFetchedData<GradesType> };
rmp: { [key: string]: GenericFetchedData<RMPInterface> };
setPage: (arg0: SearchQuery) => void;
};
const SearchResultsTable = ({
results,
grades,
rmp,
setPage,
}: SearchResultsTableProps) => {
//Table sorting category
const [orderBy, setOrderBy] = useState<'none' | 'gpa' | 'rating'>('none');
//Table sorting direction
const [order, setOrder] = useState<'asc' | 'desc'>('asc');
//Cycle through sorting
function handleClick(col: 'none' | 'gpa' | 'rating') {
if (orderBy !== col) {
setOrderBy(col);
setOrder('desc'); //default number behavior goes from high to low for our metrics
} else {
if (order === 'desc') {
setOrder('asc');
} else {
setOrderBy('none');
}
}
}
//Sort
let sortedResults = results;
if (orderBy !== 'none') {
sortedResults = [...results].sort((a, b) => {
if (orderBy === 'gpa') {
let aGrades = grades[searchQueryLabel(a)];
let bGrades = grades[searchQueryLabel(b)];
if (!aGrades || aGrades.state !== 'done') {
aGrades = grades[searchQueryLabel(convertToProfOnly(a))];
}
if (!bGrades || bGrades.state !== 'done') {
bGrades = grades[searchQueryLabel(convertToProfOnly(b))];
}
if (
(!aGrades || aGrades.state !== 'done') &&
(!bGrades || bGrades.state !== 'done')
) {
return 0;
}
if (!aGrades || aGrades.state !== 'done') {
return 9999;
}
if (!bGrades || bGrades.state !== 'done') {
return -9999;
}
if (order === 'asc') {
return aGrades.data.gpa - bGrades.data.gpa;
}
return bGrades.data.gpa - aGrades.data.gpa;
}
if (orderBy === 'rating') {
const aRmp = rmp[searchQueryLabel(convertToProfOnly(a))];
const bRmp = rmp[searchQueryLabel(convertToProfOnly(b))];
//drop loading/error rows to bottom
if (
(!aRmp || aRmp.state !== 'done') &&
(!bRmp || bRmp.state !== 'done')
) {
// If both aRmp and bRmp are not done, treat them as equal and return 0
return 0;
}
if (!aRmp || aRmp.state !== 'done') {
return 9999;
}
if (!bRmp || bRmp.state !== 'done') {
return -9999;
}
const aRating = aRmp?.data?.avgRating ?? 0; // Fallback to 0 if undefined
const bRating = bRmp?.data?.avgRating ?? 0; // Fallback to 0 if undefined
if (order === 'asc') {
return aRating - bRating;
}
return bRating - aRating;
}
return 0;
});
}
return (
//TODO: sticky header
<TableContainer component={Paper}>
<Table stickyHeader aria-label="collapsible table">
<TableHead>
<TableRow>
<TableCell />
<TableCell>Profile</TableCell>
<TableCell>
<Tooltip
title="Average GPA Across Course Sections"
placement="top"
>
<div>
<TableSortLabel
active={orderBy === 'gpa'}
direction={orderBy === 'gpa' ? order : 'desc'}
onClick={() => {
handleClick('gpa');
}}
>
Grades
</TableSortLabel>
</div>
</Tooltip>
</TableCell>
<TableCell>
<Tooltip
title="Average Professor Rating from Rate My Professors"
placement="top"
>
<div>
<TableSortLabel
active={orderBy === 'rating'}
direction={orderBy === 'rating' ? order : 'desc'}
onClick={() => {
handleClick('rating');
}}
>
Rating
</TableSortLabel>
</div>
</Tooltip>
</TableCell>
</TableRow>
</TableHead>
<TableBody>
{sortedResults.map((result) => (
<Row
key={searchQueryLabel(result)}
course={result}
grades={grades[searchQueryLabel(result)]}
backupGrades={grades[searchQueryLabel(convertToProfOnly(result))]}
rmp={rmp[searchQueryLabel(convertToProfOnly(result))]}
setPage={setPage}
/>
))}
</TableBody>
</Table>
</TableContainer>
);
};
export default SearchResultsTable;