Skip to content

Commit

Permalink
feat: use state params to keep query and filter while searching
Browse files Browse the repository at this point in the history
  • Loading branch information
germanolleunlp committed Dec 5, 2023
1 parent d7cffcd commit 467c225
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/course-home/courseware-search/CoursewareResultsFilter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import { Tabs, Tab } from '@edx/paragon';

import { useParams } from 'react-router';
import { useSearchParams } from 'react-router-dom';
import CoursewareSearchResults from './CoursewareSearchResults';
import messages from './messages';
import { useModel } from '../../generic/model-store';
Expand All @@ -17,6 +18,8 @@ export const filteredResultsBySelection = ({ key = noFilterKey, results = [] })
export const CoursewareSearchResultsFilter = ({ intl }) => {
const { courseId } = useParams();
const lastSearch = useModel('contentSearchResults', courseId);
const [searchParams, setSearchParams] = useSearchParams();
const filterKeyword = searchParams.get('f');

if (!lastSearch || !lastSearch?.results?.length) { return null; }

Expand All @@ -31,18 +34,26 @@ export const CoursewareSearchResultsFilter = ({ intl }) => {
...lastSearch.filters,
];

const activeKey = filters.find(({ key }) => key === filterKeyword)?.key || noFilterKey;

const getFilterTitle = (key, fallback) => {
const msg = messages[`filter:${key}`];
if (!msg) { return fallback; }
return intl.formatMessage(msg);
};

const handleSelect = (key) => {
setSearchParams((params) => ({ q: params.get('q'), f: key }));
};

return (
<Tabs
id="courseware-search-results-tabs"
data-testid="courseware-search-results-tabs"
variant="tabs"
activeKey={activeKey}
defaultActiveKey={noFilterKey}
onSelect={handleSelect}
>
{filters.map(({ key, label }) => (
<Tab key={key} eventKey={key} title={getFilterTitle(key, label)}>
Expand Down
13 changes: 10 additions & 3 deletions src/course-home/courseware-search/CoursewareSearch.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import React, { useEffect } from 'react';
import { useParams } from 'react-router';
import { useSearchParams } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { sendTrackingLogEvent } from '@edx/frontend-platform/analytics';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
Expand All @@ -20,6 +21,7 @@ import { searchCourseContent } from '../data/thunks';

const CoursewareSearch = ({ intl, ...sectionProps }) => {
const { courseId } = useParams();
const [searchParams, setSearchParams] = useSearchParams();
const dispatch = useDispatch();
const { org } = useModel('courseHomeMeta', courseId);
const {
Expand All @@ -28,13 +30,14 @@ const CoursewareSearch = ({ intl, ...sectionProps }) => {
errors,
total,
} = useModel('contentSearchResults', courseId);
const [searchKeyword, setSearchKeyword] = useState(lastSearchKeyword);

useLockScroll();

const info = useElementBoundingBox('courseTabsNavigation');
const top = info ? `${Math.floor(info.top)}px` : 0;

const searchKeyword = searchParams.get('q');

const clearSearch = () => {
dispatch(updateModel({
modelType: 'contentSearchResults',
Expand Down Expand Up @@ -68,10 +71,14 @@ const CoursewareSearch = ({ intl, ...sectionProps }) => {
dispatch(searchCourseContent(courseId, searchKeyword));
};

useEffect(() => {
handleSubmit();
}, []);

const handleOnChange = (value) => {
if (value === searchKeyword) { return; }

setSearchKeyword(value);
setSearchParams({ q: value });

if (!value) {
clearSearch();
Expand Down
7 changes: 5 additions & 2 deletions src/course-home/courseware-search/hooks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useEffect, useLayoutEffect } from 'react';
import { useParams } from 'react-router-dom';
import { useParams, useSearchParams } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { debounce } from 'lodash';
import { fetchCoursewareSearchSettings } from '../data/thunks';
Expand All @@ -19,9 +19,12 @@ export function useCoursewareSearchFeatureFlag() {

export function useCoursewareSearchState() {
const enabled = useCoursewareSearchFeatureFlag();
const [searchParams] = useSearchParams();

const q = searchParams.get('q');
const show = useSelector(state => state.courseHome.showSearch);

return { show: enabled && show };
return { show: enabled && (show || !!q) };
}

export function useElementBoundingBox(elementId) {
Expand Down
2 changes: 1 addition & 1 deletion src/course-tabs/CourseTabsNavigation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const CourseTabsNavigation = ({
))}
</Tabs>
</div>
{show && <CoursewareSearch />}
{show && <CoursewareSearch show={show} />}
</div>
);
};
Expand Down

0 comments on commit 467c225

Please sign in to comment.