Skip to content

Commit

Permalink
ref(KUI-1176): rename roundList to roundsBySemester
Browse files Browse the repository at this point in the history
  • Loading branch information
belanglos committed May 21, 2024
1 parent 9af196e commit cc095a8
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 49 deletions.
12 changes: 6 additions & 6 deletions public/js/app/components/DropdownRounds.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { useLanguage } from '../hooks/useLanguage'
const DROPDOWN_ID = 'roundsDropdown'
const EMPTY_OPTION = -1

const RoundOptions = ({ courseRoundList }) => {
const RoundOptions = ({ roundsForSelectedSemester }) => {
const { createRoundLabel } = useRoundUtils()

return courseRoundList.map((round, roundIndex) => {
return roundsForSelectedSemester.map((round, roundIndex) => {
const optionLabel = createRoundLabel(round)

const uniqueKey = `${optionLabel}${round.round_application_code}${round.round_start_date}`
Expand All @@ -21,7 +21,7 @@ const RoundOptions = ({ courseRoundList }) => {
})
}

const DropdownRounds = ({ courseRoundList, semesterRoundState }) => {
const DropdownRounds = ({ roundsForSelectedSemester, semesterRoundState }) => {
const { setSelectedRoundIndex, resetSelectedRoundIndex } = semesterRoundState

const { translation } = useLanguage()
Expand All @@ -31,7 +31,7 @@ const DropdownRounds = ({ courseRoundList, semesterRoundState }) => {

useEffect(() => {
setSelectedOptionIndex(EMPTY_OPTION)
}, [courseRoundList])
}, [roundsForSelectedSemester])

const handleDropdownSelect = React.useCallback(
({ target }) => {
Expand All @@ -54,7 +54,7 @@ const DropdownRounds = ({ courseRoundList, semesterRoundState }) => {
[setSelectedRoundIndex, resetSelectedRoundIndex, setSelectedOptionIndex]
)

if (!courseRoundList || courseRoundList.length < 2) {
if (!roundsForSelectedSemester || roundsForSelectedSemester.length < 2) {
return null
}

Expand All @@ -74,7 +74,7 @@ const DropdownRounds = ({ courseRoundList, semesterRoundState }) => {
>
(<option value={EMPTY_OPTION}>{label.placeholder}</option>
)
<RoundOptions courseRoundList={courseRoundList} />
<RoundOptions roundsForSelectedSemester={roundsForSelectedSemester} />
</select>
</div>
</div>
Expand Down
40 changes: 20 additions & 20 deletions public/js/app/hooks/__tests__/useSemesterRoundState.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const roundList = {
const roundsBySemester = {
20222: [
{
round_time_slots: '<i>No information inserted</i>',
Expand Down Expand Up @@ -222,7 +222,7 @@ describe('useSemesterRoundsLogic', () => {
useSemesterRoundState({
initiallySelectedRoundIndex: undefined,
initiallySelectedSemester: 20242,
roundList,
roundsBySemester,
syllabusList,
activeSemesters,
})
Expand All @@ -233,7 +233,7 @@ describe('useSemesterRoundsLogic', () => {
expect(result.current.selectedSemester).toBe(20242)
expect(result.current.showRoundData).toBe(false)
expect(result.current.activeSemesterOnlyHasOneRound).toBe(false)
expect(result.current.firstRoundInActiveSemester).toEqual(roundList[20242][0])
expect(result.current.firstRoundInActiveSemester).toEqual(roundsBySemester[20242][0])
expect(result.current.hasActiveSemesters).toBe(true)
expect(result.current.activeSyllabus).toBe(syllabusList[0])
expect(result.current.hasSyllabus).toBe(true)
Expand All @@ -244,7 +244,7 @@ describe('useSemesterRoundsLogic', () => {
useSemesterRoundState({
initiallySelectedRoundIndex: undefined,
initiallySelectedSemester: 20242,
roundList,
roundsBySemester,
syllabusList,
activeSemesters,
})
Expand All @@ -259,15 +259,15 @@ describe('useSemesterRoundsLogic', () => {

await waitFor(() => expect(result.current.selectedRoundIndex).toBe(0))
await waitFor(() => expect(result.current.showRoundData).toBe(true))
await waitFor(() => expect(result.current.activeRound).toEqual(roundList[20242][0]))
await waitFor(() => expect(result.current.activeRound).toEqual(roundsBySemester[20242][0]))
})

test('updates selectedSemester, firstRoundInActiveSemester, when setSelectedSemester is called', async () => {
const { result } = renderHook(() =>
useSemesterRoundState({
initiallySelectedRoundIndex: undefined,
initiallySelectedSemester: 20242,
roundList,
roundsBySemester,
syllabusList,
activeSemesters,
})
Expand All @@ -278,15 +278,15 @@ describe('useSemesterRoundsLogic', () => {
})

await waitFor(() => expect(result.current.selectedSemester).toBe(20232))
await waitFor(() => expect(result.current.firstRoundInActiveSemester).toBe(roundList[20232][0]))
await waitFor(() => expect(result.current.firstRoundInActiveSemester).toBe(roundsBySemester[20232][0]))
})

test('if selectedRoundIndex is set and setSelectedSemester is called, reset selectedRoundIndex, activeRound and showRoundData', async () => {
const { result } = renderHook(() =>
useSemesterRoundState({
initiallySelectedRoundIndex: undefined,
initiallySelectedSemester: 20242,
roundList,
roundsBySemester,
syllabusList,
activeSemesters,
})
Expand All @@ -301,7 +301,7 @@ describe('useSemesterRoundsLogic', () => {

await waitFor(() => expect(result.current.selectedRoundIndex).toBe(0))
await waitFor(() => expect(result.current.showRoundData).toBe(true))
await waitFor(() => expect(result.current.activeRound).toEqual(roundList[20242][0]))
await waitFor(() => expect(result.current.activeRound).toEqual(roundsBySemester[20242][0]))

act(() => {
result.current.setSelectedSemester(20232)
Expand All @@ -316,23 +316,23 @@ describe('useSemesterRoundsLogic', () => {
useSemesterRoundState({
initiallySelectedRoundIndex: undefined,
initiallySelectedSemester: 20222,
roundList,
roundsBySemester,
syllabusList,
activeSemesters,
})
)
expect(result.current.activeSemesterOnlyHasOneRound).toBe(true)
expect(result.current.showRoundData).toBe(true)

expect(result.current.activeRound).toEqual(roundList[20222][0])
expect(result.current.activeRound).toEqual(roundsBySemester[20222][0])
})

test('if initiallySelectedSemester is a string, converts it to number', async () => {
const { result } = renderHook(() =>
useSemesterRoundState({
initiallySelectedRoundIndex: undefined,
initiallySelectedSemester: '20222',
roundList,
roundsBySemester,
syllabusList,
activeSemesters,
})
Expand All @@ -342,13 +342,13 @@ describe('useSemesterRoundsLogic', () => {
})

test.each([[], undefined])(
'if roundList is empty or undefined, returns values accordingly',
async invalidRoundList => {
'if roundsBySemester is empty or undefined, returns values accordingly',
async invalidRoundsBySemester => {
const { result } = renderHook(() =>
useSemesterRoundState({
initiallySelectedRoundIndex: undefined,
initiallySelectedSemester: 20242,
roundList: invalidRoundList,
roundsBySemester: invalidRoundsBySemester,
syllabusList,
activeSemesters,
})
Expand All @@ -368,7 +368,7 @@ describe('useSemesterRoundsLogic', () => {
useSemesterRoundState({
initiallySelectedRoundIndex: undefined,
initiallySelectedSemester: 20182,
roundList,
roundsBySemester,
syllabusList,
activeSemesters,
})
Expand All @@ -383,7 +383,7 @@ describe('useSemesterRoundsLogic', () => {
useSemesterRoundState({
initiallySelectedRoundIndex: undefined,
initiallySelectedSemester: 20182,
roundList,
roundsBySemester,
syllabusList: [],
activeSemesters,
})
Expand All @@ -398,7 +398,7 @@ describe('useSemesterRoundsLogic', () => {
useSemesterRoundState({
initiallySelectedRoundIndex: undefined,
initiallySelectedSemester: 20182,
roundList,
roundsBySemester,
syllabusList: [{ course_valid_from: [], course_valid_to: [] }],
activeSemesters,
})
Expand All @@ -413,7 +413,7 @@ describe('useSemesterRoundsLogic', () => {
useSemesterRoundState({
initiallySelectedRoundIndex: undefined,
initiallySelectedSemester: 20242,
roundList,
roundsBySemester,
syllabusList,
activeSemesters,
})
Expand All @@ -429,7 +429,7 @@ describe('useSemesterRoundsLogic', () => {

await waitFor(() => expect(result.current.selectedRoundIndex).toBe(0))
await waitFor(() => expect(result.current.showRoundData).toBe(true))
await waitFor(() => expect(result.current.activeRound).toEqual(roundList[20242][0]))
await waitFor(() => expect(result.current.activeRound).toEqual(roundsBySemester[20242][0]))

act(() => {
result.current.resetSelectedRoundIndex()
Expand Down
16 changes: 8 additions & 8 deletions public/js/app/hooks/useSemesterRoundState.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const getElementOrEmpty = (arr, index) => {
const useSemesterRoundState = ({
initiallySelectedRoundIndex,
initiallySelectedSemester,
roundList,
roundsBySemester,
syllabusList,
activeSemesters,
}) => {
Expand All @@ -31,8 +31,8 @@ const useSemesterRoundState = ({
rounds[semester].length === 1

const activeSemesterOnlyHasOneRound = useMemo(
() => determineSemesterOnlyHasOneRound(roundList, selectedSemester),
[roundList, selectedSemester]
() => determineSemesterOnlyHasOneRound(roundsBySemester, selectedSemester),
[roundsBySemester, selectedSemester]
)

const showRoundData = useMemo(() => {
Expand All @@ -44,12 +44,12 @@ const useSemesterRoundState = ({
}, [isSetSelectedRoundIndex, activeSemesterOnlyHasOneRound])

const roundsForActiveSemester = useMemo(() => {
if (!roundList) {
if (!roundsBySemester) {
return []
}

return roundList[selectedSemester]
}, [roundList, selectedSemester])
return roundsBySemester[selectedSemester]
}, [roundsBySemester, selectedSemester])

const firstRoundInActiveSemester = useMemo(
() => getElementOrEmpty(roundsForActiveSemester, 0),
Expand Down Expand Up @@ -77,13 +77,13 @@ const useSemesterRoundState = ({
const setSelectedSemesterAsNumber = useCallback(
newActiveSemester => {
setSelectedSemester(() => Number(newActiveSemester))
if (determineSemesterOnlyHasOneRound(roundList, selectedSemester)) {
if (determineSemesterOnlyHasOneRound(roundsBySemester, selectedSemester)) {
setSelectedRoundIndex(0)
} else {
resetSelectedRoundIndex()
}
},
[resetSelectedRoundIndex, roundList, selectedSemester]
[resetSelectedRoundIndex, roundsBySemester, selectedSemester]
)

return {
Expand Down
7 changes: 4 additions & 3 deletions public/js/app/pages/CoursePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function CoursePage() {
const semesterRoundState = useSemesterRoundState({
initiallySelectedRoundIndex,
initiallySelectedSemester,
roundList: courseData.roundList,
roundsBySemester: courseData.roundsBySemester,
syllabusList: courseData.syllabusList,
activeSemesters,
})
Expand Down Expand Up @@ -178,9 +178,10 @@ function CoursePage() {
{hasActiveSemesters && (
<DropdownSemesters semesterList={activeSemesters} semesterRoundState={semesterRoundState} />
)}
{courseData.roundList[selectedSemester] && courseData.roundList[selectedSemester].length > 1 ? (
{courseData.roundsBySemester[selectedSemester] &&
courseData.roundsBySemester[selectedSemester].length > 1 ? (
<DropdownRounds
courseRoundList={courseData.roundList[selectedSemester]}
roundsForSelectedSemester={courseData.roundsBySemester[selectedSemester]}
semesterRoundState={semesterRoundState}
/>
) : (
Expand Down
2 changes: 1 addition & 1 deletion public/js/app/pages/__tests__/CoursePage.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions server/apiCalls/getFilteredData.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function _parseRounds({ roundInfos, courseCode, language, memoList }) {
responsibles: [],
}

const courseRoundList = {}
const roundsBySemester = {}
for (const roundInfo of roundInfos) {
const courseRound = _getRound(roundInfo, language)
const { round_course_term: yearAndTermArr, round_application_code: applicationCode } = courseRound
Expand All @@ -196,7 +196,7 @@ function _parseRounds({ roundInfos, courseCode, language, memoList }) {
if (yearAndTermArr && tempList.indexOf(semester) < 0) {
tempList.push(semester)
activeSemesterArray.push([...yearAndTermArr, semester])
courseRoundList[semester] = []
roundsBySemester[semester] = []
}

const hasMemoForThisRound = !!(memoList[semester] && memoList[semester][applicationCode])
Expand All @@ -210,7 +210,7 @@ function _parseRounds({ roundInfos, courseCode, language, memoList }) {
}
courseRound.has_round_published_memo = true
}
courseRoundList[semester].push(courseRound)
roundsBySemester[semester].push(courseRound)
// TODO: This will be removed. Because UG Rest Api is still using ladokRoundId. So once it get replaced by application code then this will be removed.
const { round = {} } = roundInfo
const { ladokRoundId } = round
Expand All @@ -228,7 +228,7 @@ function _parseRounds({ roundInfos, courseCode, language, memoList }) {
semester,
}))

return { courseRoundList, activeSemesters, employees }
return { roundsBySemester, activeSemesters, employees }
}

const getFilteredData = async ({ courseCode, language, memoList }) => {
Expand Down Expand Up @@ -260,11 +260,7 @@ const getFilteredData = async ({ courseCode, language, memoList }) => {
const { syllabusList } = createSyllabusList(courseDetails, language)

//* **** Get a list of rounds and a list of redis keys for using to get teachers and responsibles from UG Rest API *****//
const {
courseRoundList: roundList,
activeSemesters,
employees,
} = _parseRounds({
const { roundsBySemester, activeSemesters, employees } = _parseRounds({
roundInfos: courseDetails.roundInfos,
courseCode,
language,
Expand All @@ -274,7 +270,7 @@ const getFilteredData = async ({ courseCode, language, memoList }) => {
const courseData = {
syllabusList,
courseInfo,
roundList,
roundsBySemester,
courseTitleData,
language,
}
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/__tests__/courseCtrl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('Discontinued course to test', () => {
"course_title": "Kolhydratteknik inom glykovetenskap",
},
"language": "sv",
"roundList": {},
"roundsBySemester": {},
"syllabusList": [
{
"course_additional_regulations": "",
Expand Down

0 comments on commit cc095a8

Please sign in to comment.