mirror of
https://github.com/UniversityOfHelsinkiCS/oodikone.git
synced 2026-10-12 19:03:39 +03:00
[Courses tab] Fix export using incorrect column keys
This commit is contained in:
+27
@@ -0,0 +1,27 @@
|
||||
type ExportModule = {
|
||||
courses: { code: string; groupId: string }[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Student rows are keyed by course groupId, which is not readable.
|
||||
* Maps each course column's groupId to its course code for use as a key (and header) in the Excel export.
|
||||
*/
|
||||
export const getGroupIdToExportKey = (modules: Map<string, ExportModule>): Record<string, string> => {
|
||||
const groupIdToExportKey: Record<string, string> = {}
|
||||
const usedKeys = new Set<string>()
|
||||
|
||||
for (const { courses } of modules.values()) {
|
||||
for (const { code, groupId } of courses) {
|
||||
if (groupId in groupIdToExportKey) continue
|
||||
|
||||
const key = usedKeys.has(code) ? `${code} (${groupId})` : code
|
||||
groupIdToExportKey[groupId] = key
|
||||
usedKeys.add(key)
|
||||
}
|
||||
}
|
||||
|
||||
return groupIdToExportKey
|
||||
}
|
||||
|
||||
export const renameKeys = <T extends object>(row: T, keyMap: Record<string, string>): Record<string, unknown> =>
|
||||
Object.fromEntries(Object.entries(row).map(([key, value]) => [keyMap[key] ?? key, value]))
|
||||
+15
-3
@@ -5,6 +5,10 @@ import { OodiTable } from '@/components/OodiTable'
|
||||
import { OodiTableExcelExport } from '@/components/OodiTable/excelExport'
|
||||
import { IncludeSubstitutionsToggle } from '@/components/PopulationComponents/Students/IncludeSubstitutionsToggle'
|
||||
import { useGetColumnDefinitions } from '@/components/PopulationComponents/Students/Table/CoursesTab/columnDefinitions'
|
||||
import {
|
||||
getGroupIdToExportKey,
|
||||
renameKeys,
|
||||
} from '@/components/PopulationComponents/Students/Table/CoursesTab/excelExport'
|
||||
import { useToggle } from '@/hooks/toggle'
|
||||
import { useCurriculum } from '@/hooks/useCurriculum'
|
||||
import { FilteredCourse } from '@/util/coursesOfPopulation'
|
||||
@@ -360,16 +364,24 @@ export const CoursesTabContainer = ({ students, courses, idToGroupIdMap, program
|
||||
},
|
||||
}
|
||||
|
||||
// Export courses by their code instead of the groupId
|
||||
const groupIdToExportKey = useMemo(() => getGroupIdToExportKey(coursesByParentModule), [coursesByParentModule])
|
||||
|
||||
const keysForExport: string[] = useMemo(() => {
|
||||
const squashGroups = column => {
|
||||
if (column.columns) return column.columns.flatMap(squashGroups)
|
||||
return [column.accessorKey ?? column.id?.split(';')?.[1]]
|
||||
}
|
||||
|
||||
const keys = columns.flatMap(squashGroups)
|
||||
const keys = columns.flatMap(squashGroups).map(key => groupIdToExportKey[key] ?? key)
|
||||
// Remove duplicates preserving order of initial occurences
|
||||
return keys.filter((key, idx) => keys.indexOf(key) === idx)
|
||||
}, [columns])
|
||||
}, [columns, groupIdToExportKey])
|
||||
|
||||
const exportData = useMemo(
|
||||
() => formattedStudents.map(student => renameKeys(student, groupIdToExportKey)),
|
||||
[formattedStudents, groupIdToExportKey]
|
||||
)
|
||||
|
||||
return (
|
||||
<OodiTable
|
||||
@@ -379,7 +391,7 @@ export const CoursesTabContainer = ({ students, courses, idToGroupIdMap, program
|
||||
options={tableOptions}
|
||||
toolbarContent={
|
||||
<>
|
||||
<OodiTableExcelExport data={formattedStudents} exportColumnKeys={keysForExport} />
|
||||
<OodiTableExcelExport data={exportData} exportColumnKeys={keysForExport} />
|
||||
<FilterDegreeCoursesModal degreeProgramme={programme} />
|
||||
<StudentNameVisibilityToggle />
|
||||
<IncludeSubstitutionsToggle
|
||||
|
||||
Reference in New Issue
Block a user