1
1
import { useEffect , useState } from 'react' ;
2
- import { useLocation } from 'react-router-dom' ;
2
+ import { useSearchParams } from 'react-router-dom' ;
3
3
import { useDispatch , useSelector } from 'react-redux' ;
4
4
5
5
import { RequestStatus } from '../data/constants' ;
@@ -15,8 +15,8 @@ import {
15
15
import { updateSavingStatuses } from './data/slice' ;
16
16
17
17
const useStudioHome = ( ) => {
18
- const location = useLocation ( ) ;
19
18
const dispatch = useDispatch ( ) ;
19
+ const [ searchParams ] = useSearchParams ( ) ; // The query string (location.search)
20
20
const studioHomeData = useSelector ( getStudioHomeData ) ;
21
21
const studioHomeCoursesParams = useSelector ( getStudioHomeCoursesParams ) ;
22
22
const { isFiltered } = studioHomeCoursesParams ;
@@ -31,14 +31,31 @@ const useStudioHome = () => {
31
31
const isLoadingPage = studioHomeLoadingStatus === RequestStatus . IN_PROGRESS ;
32
32
const isFailedLoadingPage = studioHomeLoadingStatus === RequestStatus . FAILED ;
33
33
34
+ // FIXME: data should be loaded with React Query, not useEffect().
35
+ // To avoid a bug where changes in the "search all courses" query would trigger a reload,
36
+ // we need to remove the search 'q' from 'searchParams' and just limit this to the search
37
+ // parameters like 'active_only' that affect the course list. But really we need to replace
38
+ // fetchStudioHomeData() with separate React Query hooks - see docstring on that method.
39
+ // TODO: this whole thing is a bit weird; we sort of read the params from the search query,
40
+ // so if you enter the URL /home?archived_only=true it only shows archived courses, but the
41
+ // UI filters won't match it, and when you change the filters it doesn't update the search query.
42
+ // We should either use the search query as the only state / source of truth or ignore it entirely.
43
+ const courseListQuery = new URLSearchParams ( ) ;
44
+ for ( const key of [ 'org' , 'search' , 'order' , 'active_only' , 'archived_only' , 'page' ] ) {
45
+ // istanbul ignore if: this functionality is only partially implemented - see above
46
+ if ( searchParams . has ( key ) ) {
47
+ courseListQuery . set ( key , searchParams . get ( key ) ! ) ;
48
+ }
49
+ }
50
+ const courseListQueryString = courseListQuery . size ? `?${ courseListQuery . toString ( ) } ` : '' ;
34
51
useEffect ( ( ) => {
35
- dispatch ( fetchStudioHomeData ( location . search ?? '' ) ) ;
52
+ dispatch ( fetchStudioHomeData ( courseListQueryString ) ) ;
36
53
setShowNewCourseContainer ( false ) ;
37
- } , [ location . search ] ) ;
54
+ } , [ courseListQueryString ] ) ;
38
55
39
56
useEffect ( ( ) => {
40
57
const firstPage = 1 ;
41
- dispatch ( fetchStudioHomeData ( location . search ?? '' , false , { page : firstPage , order : 'display_name' } ) ) ;
58
+ dispatch ( fetchStudioHomeData ( courseListQueryString , false , { page : firstPage , order : 'display_name' } ) ) ;
42
59
} , [ ] ) ;
43
60
44
61
useEffect ( ( ) => {
0 commit comments