diff --git a/services/frontend/src/components/material/PopulationLink.tsx b/services/frontend/src/components/material/PopulationLink.tsx index 12ac82397..59f3be7c2 100644 --- a/services/frontend/src/components/material/PopulationLink.tsx +++ b/services/frontend/src/components/material/PopulationLink.tsx @@ -1,5 +1,3 @@ -// TODO: This file contains copy-paste, consider refactoring - import { NorthEast as NorthEastIcon } from '@mui/icons-material' import { IconButton } from '@mui/material' import moment from 'moment' @@ -14,9 +12,92 @@ const getMonths = (year: number) => { return Math.round(moment.duration(moment(lastDayOfMonth).diff(moment(start))).asMonths()) } -const PopulationLinkButton = ({ cypress, title, to }: { cypress?: string; title: string; to: string }) => { +const getUrl = (params: { + months: number + studyRights: string + tag?: string + year: string | number + years?: string +}) => { + const baseUrl = '/populations' + const urlParts = [ + `months=${params.months}`, + 'semesters=FALL', + 'semesters=SPRING', + `studyRights=${params.studyRights}`, + ] + + if (params.years) { + urlParts.push('year=All', `years=${params.years}`) + } else { + urlParts.push(`year=${params.year}`) + } + + if (params.tag) { + urlParts.push(`tag=${params.tag}`) + } + + const url = `${baseUrl}?${urlParts.join('&')}` + return url +} + +const getStudyRights = (studyProgramme: string, combinedProgramme?: string, studyTrack?: string) => { + const studyRights: Record = { programme: studyProgramme } + if (studyTrack) { + studyRights.studyTrack = studyTrack + } + if (combinedProgramme) { + studyRights.combinedProgramme = combinedProgramme + } + return encodeURIComponent(JSON.stringify(studyRights)) +} + +const getTitle = (selectedYear: string | number, year: string, tag?: Tag) => { + if (tag) { + return `Population statistics of class ${selectedYear} with tag ${tag.name}` + } + if (year === 'Total') { + return 'Population statistics of all years' + } + return `Population statistics of class ${selectedYear}` +} + +export const PopulationLink = ({ + combinedProgramme, + cypress, + studyProgramme, + studyTrack, + tag, + year, + years, +}: { + combinedProgramme?: string + cypress?: string + studyProgramme: string + studyTrack?: string + tag?: Tag + year: string + years?: number[] +}) => { + const selectedYear = tag?.year ?? (year === 'Total' ? Math.min(...(years ?? [])) : Number(year.slice(0, 4))) + const months = + year === 'Total' + ? getMonths(Math.min(...(years ?? []))) + : Math.ceil(moment.duration(moment().diff(`${selectedYear}-08-01`)).asMonths()) + + const title = getTitle(selectedYear, year, tag) + + const studyRights = getStudyRights(studyProgramme, combinedProgramme, studyTrack) + const url = getUrl({ + months, + studyRights, + year: year === 'Total' ? Math.min(...(years ?? [])) : selectedYear, + years: year === 'Total' ? (years ?? []).join('&years=') : undefined, + tag: tag?.id, + }) + return ( - + ) } - -const getTotalPopulationLink = ( - combinedProgramme: string | undefined, - months: number, - studyProgramme: string, - studyTrack: string | undefined, - years: string -) => { - if (studyTrack) { - return ( - `/populations?months=${months}&semesters=FALL&semesters=` + - `SPRING&studyRights=%7B"programme"%3A"${studyProgramme}"%2C"studyTrack"%3A"${studyTrack}"%7D&year=All&years=${years}` - ) - } - if (combinedProgramme) { - return ( - `/populations?months=${months}&semesters=FALL&semesters=` + - `SPRING&studyRights=%7B"programme"%3A"${studyProgramme}"%2C"combinedProgramme"%3A"${combinedProgramme}"%7D&year=All&years=${years}` - ) - } - return ( - `/populations?months=${months}&semesters=FALL&semesters=` + - `SPRING&studyRights=%7B"programme"%3A"${studyProgramme}"%7D&year=All&years=${years}` - ) -} - -const getPopulationLink = ( - combinedProgramme: string | undefined, - months: number, - startYear: number, - studyProgramme: string, - studyTrack: string | undefined -) => { - if (studyTrack) { - return ( - `/populations?months=${months}&semesters=FALL&semesters=` + - `SPRING&studyRights=%7B"programme"%3A"${studyProgramme}"%2C"studyTrack"%3A"${studyTrack}"%7D&year=${startYear}` - ) - } - if (combinedProgramme) { - return ( - `/populations?months=${months}&semesters=FALL&semesters=` + - `SPRING&studyRights=%7B"programme"%3A"${studyProgramme}"%2C"combinedProgramme"%3A"${combinedProgramme}"%7D&year=${startYear}` - ) - } - return ( - `/populations?months=${months}&semesters=FALL&semesters=` + - `SPRING&studyRights=%7B"programme"%3A"${studyProgramme}"%7D&year=${startYear}` - ) -} - -export const PopulationLink = ({ - combinedProgramme, - cypress, - studyProgramme, - studyTrack, - year, - years, -}: { - combinedProgramme?: string - cypress?: string - studyProgramme: string - studyTrack?: string - year: string - years: number[] -}) => { - if (year === 'Total') { - const yearsString = years.join('&years=') - const months = getMonths(Math.min(...years.map(year => Number(year)))) - const href = getTotalPopulationLink(combinedProgramme, months, studyProgramme, studyTrack, yearsString) - return - } - - const startYear = Number(year.slice(0, 4)) - const months = Math.ceil(moment.duration(moment().diff(`${startYear}-08-01`)).asMonths()) - const href = getPopulationLink(combinedProgramme, months, startYear, studyProgramme, studyTrack) - return -} - -export const PopulationLinkWithTag = ({ - combinedProgramme, - studyProgramme, - tag, -}: { - combinedProgramme: string - studyProgramme: string - tag: Tag -}) => { - const year = tag.year ?? new Date().getFullYear() - const months = Math.ceil(moment.duration(moment().diff(`${year}-08-01`)).asMonths()) - const href = combinedProgramme - ? `/populations?months=${months}&semesters=FALL&semesters=` + - `SPRING&studyRights=%7B"programme"%3A"${studyProgramme}"%2C"combinedProgramme"%3A"${combinedProgramme}"%7D&year=${year}&tag=${tag.id}` - : `/populations?months=${months}&semesters=FALL&semesters=` + - `SPRING&studyRights=%7B"programme"%3A"${studyProgramme}"%7D&year=${year}&tag=${tag.id}` - - return -} diff --git a/services/frontend/src/pages/StudyProgramme/TagsTab/TagsSection/TagItem/index.tsx b/services/frontend/src/pages/StudyProgramme/TagsTab/TagsSection/TagItem/index.tsx index 9f19e0154..d9721fa91 100644 --- a/services/frontend/src/pages/StudyProgramme/TagsTab/TagsSection/TagItem/index.tsx +++ b/services/frontend/src/pages/StudyProgramme/TagsTab/TagsSection/TagItem/index.tsx @@ -7,7 +7,7 @@ import { import { IconButton, ListItem, ListItemIcon, ListItemText, Stack, Tooltip } from '@mui/material' import { useState } from 'react' -import { PopulationLinkWithTag } from '@/components/material/PopulationLink' +import { PopulationLink } from '@/components/material/PopulationLink' import { Tag } from '@/shared/types' import { AddStudentsDialog } from './AddStudentsDialog' import { DeleteTagDialog } from './DeleteTagDialog' @@ -52,10 +52,11 @@ export const TagItem = ({ {tag.name} {tag.year && ( - )} {tag.personalUserId && (