Skip to content

Commit ca0c8c6

Browse files
committed
[Frontend] Fix curriculumPeriod filter
1 parent f5b81d7 commit ca0c8c6

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

services/frontend/src/components/FilterView/filters/curriculumPeriod.tsx

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import { orderBy } from 'lodash'
2-
import { Form, Dropdown } from 'semantic-ui-react'
1+
import { Form, Dropdown, type DropdownItemProps } from 'semantic-ui-react'
32

43
import { createFilter } from './createFilter'
54

65
const CurriculumPeriodFilterCard = ({ options, onOptionsChange, students }) => {
76
const { selected } = options
87

9-
const dropdownOptions = students.reduce((curriculumPeriods, { curriculumPeriod }) => {
10-
if (curriculumPeriods.every(option => option.value !== curriculumPeriod)) {
11-
const count = students.filter(({ curriculumVersion }) => curriculumVersion === curriculumPeriod).length
12-
13-
curriculumPeriods.push({
14-
key: curriculumPeriod,
15-
value: curriculumPeriod,
16-
text: `${curriculumPeriod ?? 'No period selected'} (${count})`,
17-
count,
18-
})
19-
}
20-
21-
return curriculumPeriods
22-
}, [])
23-
24-
const sortedDropdownOptions = orderBy(dropdownOptions, ['text', 'count'], ['asc', 'desc'])
8+
const dropdownOptions = Array.from(
9+
students
10+
.map(({ curriculumVersion }) => curriculumVersion)
11+
.filter(Boolean)
12+
.sort()
13+
.reverse()
14+
.reduce((versions: Map<string, DropdownItemProps>, curriculumVersion: string) => {
15+
versions.set(curriculumVersion, {
16+
key: curriculumVersion,
17+
text: curriculumVersion,
18+
value: curriculumVersion,
19+
})
20+
21+
return versions
22+
}, new Map())
23+
.values()
24+
)
2525

2626
return (
2727
<div className="card-content">
@@ -33,7 +33,7 @@ const CurriculumPeriodFilterCard = ({ options, onOptionsChange, students }) => {
3333
data-cy="curriculumPeriodFilter-dropdown"
3434
fluid
3535
onChange={(_, { value: inputValue }) => onOptionsChange({ selected: inputValue })}
36-
options={sortedDropdownOptions}
36+
options={dropdownOptions}
3737
placeholder="Choose curriculum period"
3838
selectOnBlur={false}
3939
selection
@@ -47,9 +47,7 @@ const CurriculumPeriodFilterCard = ({ options, onOptionsChange, students }) => {
4747
export const curriculumPeriodFilter = createFilter({
4848
key: 'Curriculum period',
4949

50-
defaultOptions: {
51-
selected: '',
52-
},
50+
defaultOptions: { selected: null },
5351

5452
isActive: ({ selected }) => !!selected,
5553

0 commit comments

Comments
 (0)