Skip to content

Commit

Permalink
[Study programmes] Use curriculum periods from db instead of hardcodi…
Browse files Browse the repository at this point in the history
…ng the period format
  • Loading branch information
valtterikantanen committed Aug 14, 2024
1 parent 3e9fa50 commit 95fbe7d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
2 changes: 2 additions & 0 deletions services/backend/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const closeToGraduation = require('./routes/closeToGraduation').default
const completedCoursesSearch = require('./routes/completedCoursesSearch').default
const courseExclusions = require('./routes/courseExclusions').default
const courses = require('./routes/courses').default
const curriculumPeriods = require('./routes/curriculumPeriods').default
const customOpenUniSearch = require('./routes/customOpenUniSearch').default
const customPopulationSearch = require('./routes/customPopulationSearch').default
const faculties = require('./routes/faculties').default
Expand Down Expand Up @@ -80,6 +81,7 @@ module.exports = (app, url) => {
app.use(`${url}/studyguidancegroups`, auth.roles(['studyGuidanceGroups']), studyGuidanceGroups)
app.use(`${url}/close-to-graduation`, auth.roles(['fullSisuAccess', 'studyGuidanceGroups']), closeToGraduation)
app.use(`${url}/study-programme-pins`, studyProgrammePins)
app.use(`${url}/curriculum-periods`, curriculumPeriods)
app.get('*', async (_, res) => {
const results = { error: 'unknown endpoint' }
res.status(404).json(results)
Expand Down
12 changes: 12 additions & 0 deletions services/backend/src/routes/curriculumPeriods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Request, Response, Router } from 'express'

import { CurriculumPeriod } from '../models'

const router = Router()

router.get('/', async (_req: Request, res: Response) => {
const result = await CurriculumPeriod.findAll()
res.json(result)
})

export default router
9 changes: 9 additions & 0 deletions services/frontend/src/common/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import qs from 'query-string'
import { useCallback, useEffect, useRef, useState } from 'react'

import { SEARCH_HISTORY_VERSION } from '@/constants'
import { useGetCurriculumPeriodsQuery } from '@/redux/curriculumPeriods'
import { useGetSemestersQuery } from '@/redux/semesters'

export const useTabChangeAnalytics = () => {
Expand Down Expand Up @@ -283,3 +284,11 @@ export const useCurrentSemester = () => {
semester => new Date(semester.startdate) <= new Date() && new Date(semester.enddate) >= new Date()
)
}

export const useCurrentCurriculumPeriod = () => {
const { data: curriculumPeriods = [] } = useGetCurriculumPeriodsQuery()
return curriculumPeriods.find(
curriculumPeriod =>
new Date(curriculumPeriod.startDate) <= new Date() && new Date(curriculumPeriod.endDate) >= new Date()
)
}
2 changes: 0 additions & 2 deletions services/frontend/src/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,5 +484,3 @@ export const getEnrollmentTypeTextForExcel = (type, statutoryAbsence) => {
export const isDefaultServiceProvider = () => {
return serviceProvider && serviceProvider === 'toska'
}

export const yearCodeToCurriculumPeriodId = yearCode => (yearCode ? `hy-lv-${yearCode}` : null)
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import {
createLocaleComparator,
createPinnedFirstComparator,
getUnifiedProgrammeName,
yearCodeToCurriculumPeriodId,
isDefaultServiceProvider,
} from '@/common'
import { useCurrentSemester } from '@/common/hooks'
import { useCurrentCurriculumPeriod } from '@/common/hooks'
import { FilterOldProgrammesToggle } from '@/components/common/FilterOldProgrammesToggle'
import { useLanguage } from '@/components/LanguagePicker/useLanguage'
import { useGetProgrammesQuery } from '@/redux/populations'
Expand All @@ -27,8 +26,7 @@ export const StudyProgrammeSelector = () => {
const { getTextIn } = useLanguage()
const { data: programmes, isLoading } = useGetProgrammesQuery()
const studyProgrammes = Object.values(programmes || {})
const currentSemester = useCurrentSemester()
const currentCurriculumPeriodId = yearCodeToCurriculumPeriodId(currentSemester?.yearcode)
const currentCurriculumPeriod = useCurrentCurriculumPeriod()
const [filter, setFilter] = useState('')
const [otherProgrammesVisible, setOtherProgrammesVisible] = useState(false)
const handleFilterChange = debounce(value => {
Expand All @@ -46,7 +44,7 @@ export const StudyProgrammeSelector = () => {
const pinnedProgrammes = studyProgrammePins?.studyProgrammes || []
const pinnedFirstComparator = createPinnedFirstComparator(pinnedProgrammes)

if (isLoading || !currentCurriculumPeriodId) return <Loader active>Loading</Loader>
if (isLoading || !currentCurriculumPeriod) return <Loader active>Loading</Loader>

const isPinned = programmeCode => pinnedProgrammes.includes(programmeCode)

Expand Down Expand Up @@ -126,7 +124,7 @@ export const StudyProgrammeSelector = () => {
if (isDefaultServiceProvider() && programme.code.startsWith('2_')) {
continue
}
if (!programme.curriculum_period_ids.includes(currentCurriculumPeriodId)) {
if (!programme.curriculum_period_ids.includes(currentCurriculumPeriod.id)) {
otherProgrammes.push(programme)
} else if (programme.degreeProgrammeType === 'urn:code:degree-program-type:bachelors-degree') {
bachelorProgrammes.push(programme)
Expand Down
12 changes: 12 additions & 0 deletions services/frontend/src/redux/curriculumPeriods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { RTKApi } from '@/apiConnection'

const curriculumPeriodsApi = RTKApi.injectEndpoints({
endpoints: builder => ({
getCurriculumPeriods: builder.query({
query: () => '/curriculum-periods',
}),
}),
overrideExisting: false,
})

export const { useGetCurriculumPeriodsQuery } = curriculumPeriodsApi

0 comments on commit 95fbe7d

Please sign in to comment.