Skip to content

Commit f5b81d7

Browse files
committed
[Frontend] Fix CourseStatistics resetState on empty URL params
1 parent 4544d1c commit f5b81d7

File tree

2 files changed

+20
-49
lines changed

2 files changed

+20
-49
lines changed

services/frontend/src/pages/CourseStatistics/SearchForm/index.tsx

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import TextField from '@mui/material/TextField'
1212

1313
import { omit, sortBy } from 'lodash'
1414
import { useEffect, useState } from 'react'
15-
import { useLocation, useNavigate } from 'react-router'
15+
import { useNavigate } from 'react-router'
1616

1717
import { validateInputLength } from '@/common'
1818
import { useLanguage } from '@/components/LanguagePicker/useLanguage'
@@ -23,10 +23,8 @@ import { useDebouncedState } from '@/hooks/debouncedState'
2323
import { useSearchHistory } from '@/hooks/searchHistory'
2424
import { useToggle } from '@/hooks/toggle'
2525
import { useGetCourseSearchResultQuery } from '@/redux/courseSearch'
26-
import { getCourseStats, clearCourseStats } from '@/redux/courseStats'
27-
import { useAppDispatch, useAppSelector } from '@/redux/hooks'
2826
import { SearchHistoryItem } from '@/types/searchHistory'
29-
import { parseQueryParams, queryParamsToString } from '@/util/queryparams'
27+
import { queryParamsToString } from '@/util/queryparams'
3028
import { MemoizedCourseTable as CourseTable } from './CourseTable'
3129
import { FetchStatisticsButton } from './FetchStatisticsButton'
3230
import { MultipleCoursesAlert } from './MultipleCoursesAlert'
@@ -35,12 +33,9 @@ import { MultipleCoursesAlert } from './MultipleCoursesAlert'
3533
// fail if the courses have small populations (this used to be limited to 40)
3634
const MAX_SELECTED_COURSES = 99999
3735

38-
export const SearchForm = ({ onProgress, progress }) => {
36+
export const SearchForm = ({ progress, isPending }) => {
3937
const { getTextIn } = useLanguage()
40-
const dispatch = useAppDispatch()
41-
const location = useLocation()
4238
const navigate = useNavigate()
43-
const isLoadingCourseStats = useAppSelector(state => state.courseStats.pending)
4439
const [combineSubstitutions, toggleCombineSubstitutions] = useToggle(true)
4540
const [selectMultipleCourses, toggleSelectMultipleCourses] = useToggle(false)
4641
const [courseName, setCourseName] = useState('')
@@ -71,33 +66,6 @@ export const SearchForm = ({ onProgress, progress }) => {
7166
setDebouncedCourseCode(event.target.value)
7267
}
7368

74-
const parseQueryFromUrl = () => {
75-
const search = parseQueryParams(location.search)
76-
const query = {
77-
courseCodes: JSON.parse(search.courseCodes as string),
78-
separate: JSON.parse(search.separate as string),
79-
combineSubstitutions: search.combineSubstitutionsFrom ?? JSON.parse(combineSubstitutions.toString()),
80-
}
81-
return query
82-
}
83-
84-
const fetchStatisticsFromUrlParams = () => {
85-
const query = parseQueryFromUrl()
86-
dispatch(getCourseStats(query, onProgress))
87-
}
88-
89-
useEffect(() => {
90-
if (!location.search) {
91-
dispatch(clearCourseStats())
92-
}
93-
}, [])
94-
95-
useEffect(() => {
96-
if (location.search && location.search !== '?tab=0') {
97-
fetchStatisticsFromUrlParams()
98-
}
99-
}, [location.search])
100-
10169
const onSelectCourse = course => {
10270
const isSelected = !!selectedCourses[course.code]
10371

@@ -166,8 +134,8 @@ export const SearchForm = ({ onProgress, progress }) => {
166134
const courses = matchingCourses.filter(course => !selectedCourses[course.code])
167135

168136
const selected = Object.values(selectedCourses)
169-
const noSelectedCourses = selected.length === 0
170-
const disabled = isLoadingCourseStats || noSelectedCourses || selected.length > MAX_SELECTED_COURSES
137+
const noSelectedCourses = !selected.length
138+
const disabled = isPending ?? noSelectedCourses ?? selected.length > MAX_SELECTED_COURSES
171139

172140
const addAllCourses = () => {
173141
const newSelectedCourses = courses.reduce((newSelected, course) => {
@@ -183,7 +151,7 @@ export const SearchForm = ({ onProgress, progress }) => {
183151

184152
return (
185153
<Stack gap={2}>
186-
<Backdrop open={isLoadingCourseStats} sx={{ color: '#fff', zIndex: theme => theme.zIndex.drawer + 1 }}>
154+
<Backdrop open={isPending} sx={{ color: '#fff', zIndex: theme => theme.zIndex.drawer + 1 }}>
187155
<CircularProgress color="inherit" value={progress} variant="determinate" />
188156
</Backdrop>
189157
<Section title="Search for courses">
@@ -279,10 +247,7 @@ export const SearchForm = ({ onProgress, progress }) => {
279247
)}
280248
<CourseTable
281249
courses={courses}
282-
hidden={
283-
isLoadingCourseStats ||
284-
(Object.keys(courses).length === 0 && Object.keys(selectedCourses).length > 0)
285-
}
250+
hidden={isPending ?? (!Object.keys(courses).length && !Object.keys(selectedCourses).length)}
286251
onSelectCourse={onSelectCourse}
287252
title="Searched courses"
288253
/>

services/frontend/src/pages/CourseStatistics/index.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { useProgress } from '@/hooks/progress'
1212
import { useTabs } from '@/hooks/tabs'
1313
import { useTitle } from '@/hooks/title'
1414
import { useGetAuthorizedUserQuery } from '@/redux/auth'
15-
import { getCourseStats } from '@/redux/courseStats'
15+
import { clearCourseStats, getCourseStats } from '@/redux/courseStats'
1616
import { useAppDispatch, useAppSelector } from '@/redux/hooks'
1717
import { checkUserAccess, getFullStudyProgrammeRights, hasAccessToAllCourseStats } from '@/util/access'
1818
import { parseQueryParams } from '@/util/queryparams'
@@ -44,13 +44,19 @@ export const CourseStatistics = () => {
4444
useEffect(() => {
4545
const { courseCodes, ...params } = parseQueryParams(location.search)
4646
if (!courseCodes) {
47+
if (Object.keys(courseStatsData).length) dispatch(clearCourseStats())
4748
return
4849
}
49-
const query = {
50-
...params,
51-
courseCodes: JSON.parse(courseCodes as string),
52-
}
53-
dispatch(getCourseStats(query, onProgress))
50+
51+
dispatch(
52+
getCourseStats(
53+
{
54+
...params,
55+
courseCodes: JSON.parse(courseCodes as string),
56+
},
57+
onProgress
58+
)
59+
)
5460
}, [location.search])
5561

5662
useEffect(() => {
@@ -76,7 +82,7 @@ export const CourseStatistics = () => {
7682
<Container maxWidth="lg">
7783
<PageTitle title="Course statistics" />
7884
{statsIsEmpty || location.search === '' ? (
79-
<SearchForm onProgress={onProgress} progress={progress} />
85+
<SearchForm isPending={loading} progress={progress} />
8086
) : (
8187
<>
8288
<Box sx={{ alignItems: 'center', display: 'flex', justifyContent: 'space-between', marginBottom: 2 }}>

0 commit comments

Comments
 (0)