Skip to content

Commit 4a1f454

Browse files
fix: searching "all courses" from studio home wasn't working (#2083)
1 parent 4adf2ff commit 4a1f454

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/studio-home/data/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export async function getStudioHomeCourses(search) {
2424
/**
2525
* Get's studio home courses.
2626
* @param {string} search - Query string parameters for filtering the courses.
27+
* TODO: this should be an object with a list of allowed keys and values; not a string.
2728
* @param {object} customParams - Additional custom parameters for the API request.
2829
* @returns {Promise<Object>} - A Promise that resolves to the response data containing the studio home courses.
2930
* Note: We are changing /api/contentstore/v1 to /api/contentstore/v2 due to upcoming breaking changes.

src/studio-home/data/thunks.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import {
1414
fetchCourseDataSuccessV2,
1515
} from './slice';
1616

17+
/**
18+
* Load both the "Studio Home" data and the course list. Store it in the Redux state.
19+
*
20+
* TODO: this should be replaced with two separate React Query hooks - one that calls
21+
* useQuery() to load the "studio home" data, and another that calls useQuery() to
22+
* load the course list.
23+
*/
1724
function fetchStudioHomeData(
1825
search,
1926
hasHomeData,

src/studio-home/hooks.jsx renamed to src/studio-home/hooks.tsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from 'react';
2-
import { useLocation } from 'react-router-dom';
2+
import { useSearchParams } from 'react-router-dom';
33
import { useDispatch, useSelector } from 'react-redux';
44

55
import { RequestStatus } from '../data/constants';
@@ -15,8 +15,8 @@ import {
1515
import { updateSavingStatuses } from './data/slice';
1616

1717
const useStudioHome = () => {
18-
const location = useLocation();
1918
const dispatch = useDispatch();
19+
const [searchParams] = useSearchParams(); // The query string (location.search)
2020
const studioHomeData = useSelector(getStudioHomeData);
2121
const studioHomeCoursesParams = useSelector(getStudioHomeCoursesParams);
2222
const { isFiltered } = studioHomeCoursesParams;
@@ -31,14 +31,31 @@ const useStudioHome = () => {
3131
const isLoadingPage = studioHomeLoadingStatus === RequestStatus.IN_PROGRESS;
3232
const isFailedLoadingPage = studioHomeLoadingStatus === RequestStatus.FAILED;
3333

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()}` : '';
3451
useEffect(() => {
35-
dispatch(fetchStudioHomeData(location.search ?? ''));
52+
dispatch(fetchStudioHomeData(courseListQueryString));
3653
setShowNewCourseContainer(false);
37-
}, [location.search]);
54+
}, [courseListQueryString]);
3855

3956
useEffect(() => {
4057
const firstPage = 1;
41-
dispatch(fetchStudioHomeData(location.search ?? '', false, { page: firstPage, order: 'display_name' }));
58+
dispatch(fetchStudioHomeData(courseListQueryString, false, { page: firstPage, order: 'display_name' }));
4259
}, []);
4360

4461
useEffect(() => {

0 commit comments

Comments
 (0)