From 819e043b2e5a791785e21fe8b432fb3a716cfe8e Mon Sep 17 00:00:00 2001 From: Pakapool Date: Fri, 26 Jul 2024 01:49:01 +0700 Subject: [PATCH] classCode now based on user's language --- .../courses/ClasssRefinementItem.tsx | 4 +- .../CourseDetails/CourseDetailsContainer.tsx | 4 +- src/components/Courses/RefineControls.tsx | 2 +- src/const/class_code.ts | 1010 +++++++++++++++++ src/helpers/courses.ts | 52 +- 5 files changed, 1052 insertions(+), 20 deletions(-) create mode 100644 src/const/class_code.ts diff --git a/src/app/[lang]/(mods-pages)/courses/ClasssRefinementItem.tsx b/src/app/[lang]/(mods-pages)/courses/ClasssRefinementItem.tsx index 92c07e0e..afcd2d68 100644 --- a/src/app/[lang]/(mods-pages)/courses/ClasssRefinementItem.tsx +++ b/src/app/[lang]/(mods-pages)/courses/ClasssRefinementItem.tsx @@ -95,7 +95,7 @@ const ClassRefinementItem = ({
{selected.map(i => - {getFormattedClassCode(i, selectedSemester)} + {getFormattedClassCode(i, selectedSemester, 'zh')} )}
@@ -139,7 +139,7 @@ const ClassRefinementItem = ({ - {getFormattedClassCode(item.label, selectedSemester)} + {getFormattedClassCode(item.label, selectedSemester, 'zh')} {item.count} diff --git a/src/components/CourseDetails/CourseDetailsContainer.tsx b/src/components/CourseDetails/CourseDetailsContainer.tsx index acf086ed..c5f05d8a 100644 --- a/src/components/CourseDetails/CourseDetailsContainer.tsx +++ b/src/components/CourseDetails/CourseDetailsContainer.tsx @@ -266,7 +266,7 @@ const CourseDetailContainer = async ({ lang, courseId, bottomAware = false, moda

{dict.course.details.compulsory}

{course.compulsory_for?.map((m, index) => - {getFormattedClassCode(m, course.semester)} + {getFormattedClassCode(m, course.semester, lang)} )}
} @@ -274,7 +274,7 @@ const CourseDetailContainer = async ({ lang, courseId, bottomAware = false, moda

{dict.course.details.elective}

{course.elective_for?.map((m, index) => - {getFormattedClassCode(m, course.semester)} + {getFormattedClassCode(m, course.semester, lang)} )}
} diff --git a/src/components/Courses/RefineControls.tsx b/src/components/Courses/RefineControls.tsx index eb85174a..ee8d3d93 100644 --- a/src/components/Courses/RefineControls.tsx +++ b/src/components/Courses/RefineControls.tsx @@ -224,7 +224,7 @@ const RefineControls: FC<{ form: UseFormReturn }> = ({ f name="className" placeholder={dict.course.refine.class} loading={load3} - options={classList.map(classname => ({ value: classname, label: getFormattedClassCode(classname, semester) }))} + options={classList.map(classname => ({ value: classname, label: getFormattedClassCode(classname, semester, 'zh') }))} label={dict.course.refine.compulsory_elective} /> { //核心通識Core GE courses 1, 核心通識Core GE courses 2 <- return this number if(ge_type.includes('核心通識Core GE courses')){ @@ -43,8 +45,16 @@ export const getScoreType = (score: string) => { } // Define mappings for degree types and class letters -const degreeTypes: {[x: string]: string } = { 'B': '', 'M': '碩士班', 'D': '博士班', 'P': '專班' }; -const classLetters: {[x: string]: string } = { 'A': '清班', 'B': '華班', 'C': '梅班', 'D': '班' }; // Assuming 'D' stands for a general class +const degreeTypes: { [key: string]: any } = { + 'zh': { 'B': '', 'M': '碩士班', 'D': '博士班', 'P': '專班' }, + 'en': { 'B': '', 'M': 'Master', 'D': 'Doctoral', 'P': 'Special' } +}; + +const classLetters : { [key: string]: any } = { + 'zh': { 'A': '清班', 'B': '華班', 'C': '梅班', 'D': '班' }, + 'en': { 'A': 'Qing', 'B': 'Hua', 'C': 'Mei', 'D': '' } +}; + export const checkValidClassCode = (class_code: string, semester: string) => { // Parse the input string const match = class_code.toUpperCase().match(/(^[^\d]+)(\d+)([BMD])([A-D]?)/); @@ -63,31 +73,43 @@ export const checkValidClassCode = (class_code: string, semester: string) => { } return true; } - - -export const getFormattedClassCode = (class_code: string, semester: string) =>{ - + +export const getFormattedClassCode = (class_code: string, semester: string, lang: string) =>{ // Parse the input string - const match = class_code.toUpperCase().match(/(^[^\d]+)(\d+)([BMD])([A-D]?)/); + const match = class_code.toUpperCase().match(/(^[^\d]+)(\d+)([BMDP])([A-D]?)/); if (!match) { return class_code; } - + const sem = parseInt(semester.slice(0, 3)); // Extract components const [, deptName, year, degreeType, classLetter] = match; // Translate components - const readableYear = (sem - parseInt(year) + 1) + '年級'; - const readableDegreeType = degreeTypes[degreeType] || ''; - const readableClassLetter = classLetters[classLetter] || ''; + const yearNumber = sem - parseInt(year) + 1; + const readableYear = lang === 'zh' ? `${yearNumber}年級` : ` Y${yearNumber} `; + + + const readableDegreeType = degreeTypes[lang][degreeType] || ''; + const readableClassLetter = classLetters[lang][classLetter] || ''; + + const department = classCode.find(dept => dept.code_zh === deptName); + + let deptCode; + + if (department?.code_zh == 'NA') { + deptCode = deptName; + } else { + // Select the department name based on the language + deptCode = lang === 'zh' ? department?.code_zh : department?.code; + } // exception for EECS-GS - if(deptName == '電資院學士班' && degreeType == 'B' && classLetter == 'A'){ - return `電資院學士班${readableYear}國際學程`; + if (deptName == '電資院學士班' && degreeType == 'B' && classLetter == 'A'){ + return lang === 'zh' ? `電資院學士班${readableYear}國際學程` : `EECS-GS ${readableYear}`; } // Construct the output - return `${deptName}${readableYear}${readableDegreeType}${readableClassLetter}`; + return `${deptCode}${readableYear} ${readableDegreeType} ${readableClassLetter}`; } \ No newline at end of file