From ab3cbffe4b85610c9e79e3334e350297a5d9cca7 Mon Sep 17 00:00:00 2001
From: Tyler Hill
Date: Tue, 29 Oct 2024 14:02:13 -0500
Subject: [PATCH] move gpaToLetterGrade func to calculateGrades
---
src/components/CourseOverview.tsx | 17 +----------------
src/components/SearchResultsTable.tsx | 19 ++-----------------
src/pages/index.tsx | 18 ++++++++++++++++++
3 files changed, 21 insertions(+), 33 deletions(-)
diff --git a/src/components/CourseOverview.tsx b/src/components/CourseOverview.tsx
index d2dd030..34b99aa 100644
--- a/src/components/CourseOverview.tsx
+++ b/src/components/CourseOverview.tsx
@@ -4,21 +4,6 @@ import { TRENDS_URL } from '~data/config';
import type { GenericFetchedData, GradesType } from '~pages';
import { type SearchQuery, searchQueryLabel } from '~utils/SearchQuery';
-const gpaToLetterGrade = (gpa: number): string => {
- if (gpa >= 4.0) return 'A';
- if (gpa >= 3.67) return 'A-';
- if (gpa >= 3.33) return 'B+';
- if (gpa >= 3.0) return 'B';
- if (gpa >= 2.67) return 'B-';
- if (gpa >= 2.33) return 'C+';
- if (gpa >= 2.0) return 'C';
- if (gpa >= 1.67) return 'C-';
- if (gpa >= 1.33) return 'D+';
- if (gpa >= 1.0) return 'D';
- if (gpa >= 0.67) return 'D-';
- return 'F';
-};
-
type CourseOverviewProps = {
course: SearchQuery;
grades: GenericFetchedData;
@@ -32,7 +17,7 @@ const CourseOverview = ({ course, grades }: CourseOverviewProps) => {
{grades.state === 'done' && (
- {'Overall grade: ' + gpaToLetterGrade(grades.data.gpa)}
+ {'Overall grade: ' + grades.data.letter_grade}
)}
{
- if (gpa >= 4.0) return 'A';
- if (gpa >= 3.67) return 'A-';
- if (gpa >= 3.33) return 'B+';
- if (gpa >= 3.0) return 'B';
- if (gpa >= 2.67) return 'B-';
- if (gpa >= 2.33) return 'C+';
- if (gpa >= 2.0) return 'C';
- if (gpa >= 1.67) return 'C-';
- if (gpa >= 1.33) return 'D+';
- if (gpa >= 1.0) return 'D';
- if (gpa >= 0.67) return 'D-';
- return 'F';
-};
-
type RowProps = {
course: SearchQuery;
grades: GenericFetchedData;
@@ -164,7 +149,7 @@ function Row({ course, grades, backupGrades, rmp, setPage }: RowProps) {
backgroundColor: gpaToColor(backupGrades.data.gpa),
}}
>
- {gpaToLetterGrade(backupGrades.data.gpa)}
+ {backupGrades.data.letter_grade}
@@ -186,7 +171,7 @@ function Row({ course, grades, backupGrades, rmp, setPage }: RowProps) {
className="text-base text-black rounded-full px-5 py-2 inline"
sx={{ backgroundColor: gpaToColor(grades.data.gpa) }}
>
- {gpaToLetterGrade(grades.data.gpa)}
+ {grades.data.letter_grade}
)) ||
diff --git a/src/pages/index.tsx b/src/pages/index.tsx
index 6cef1b4..d4d2764 100644
--- a/src/pages/index.tsx
+++ b/src/pages/index.tsx
@@ -36,6 +36,21 @@ export type GenericFetchedData =
| GenericFetchedDataLoading
| GenericFetchedDataDone;
+const gpaToLetterGrade = (gpa: number): string => {
+ if (gpa >= 4.0) return 'A';
+ if (gpa >= 3.67) return 'A-';
+ if (gpa >= 3.33) return 'B+';
+ if (gpa >= 3.0) return 'B';
+ if (gpa >= 2.67) return 'B-';
+ if (gpa >= 2.33) return 'C+';
+ if (gpa >= 2.0) return 'C';
+ if (gpa >= 1.67) return 'C-';
+ if (gpa >= 1.33) return 'D+';
+ if (gpa >= 1.0) return 'D';
+ if (gpa >= 0.67) return 'D-';
+ return 'F';
+};
+
//Find GPA, total, and grade_distribution based on including some set of semesters
function calculateGrades(grades: GradesData, academicSessions?: string[]) {
let grade_distribution = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
@@ -68,11 +83,13 @@ function calculateGrades(grades: GradesData, academicSessions?: string[]) {
) /
(total - grade_distribution[grade_distribution.length - 1]);
}
+ const letter_grade = gpaToLetterGrade(gpa);
return {
gpa: gpa,
total: total,
grade_distribution: grade_distribution,
+ letter_grade: letter_grade,
};
}
type GradesData = {
@@ -83,6 +100,7 @@ export type GradesType = {
gpa: number;
total: number;
grade_distribution: number[];
+ letter_grade: string;
grades: GradesData;
};
//Fetch grades by academic session from nebula api