Skip to content

Commit

Permalink
Merge pull request #540 from DmytroAlipov/fix-discussion-search
Browse files Browse the repository at this point in the history
Fix bug with a repeated search query
  • Loading branch information
edx-requirements-bot authored Jul 13, 2023
2 parents a16bd78 + 4a2b324 commit 445caca
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/components/Search.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {
useCallback, useContext, useEffect, useState,
useCallback, useContext, useEffect, useRef, useState,
} from 'react';

import camelCase from 'lodash/camelCase';
Expand All @@ -25,6 +25,7 @@ const Search = () => {
const isPostSearch = ['posts', 'my-posts'].includes(page);
const isTopicSearch = 'topics'.includes(page);
const [searchValue, setSearchValue] = useState('');
const previousSearchValueRef = useRef('');
let currentValue = '';

if (isPostSearch) {
Expand All @@ -39,14 +40,15 @@ const Search = () => {
dispatch(setSearchQuery(''));
dispatch(setTopicFilter(''));
dispatch(setUsernameSearch(''));
}, []);
previousSearchValueRef.current = '';
}, [previousSearchValueRef]);

const onChange = useCallback((query) => {
setSearchValue(query);
}, []);

const onSubmit = useCallback((query) => {
if (query === '') {
if (query === '' || query === previousSearchValueRef.current) {
return;
}

Expand All @@ -57,7 +59,8 @@ const Search = () => {
} else if (page === 'learners') {
dispatch(setUsernameSearch(query));
}
}, [page, searchValue]);
previousSearchValueRef.current = query;
}, [page, searchValue, previousSearchValueRef]);

const handleIconClick = useCallback((e) => {
e.preventDefault();
Expand Down

0 comments on commit 445caca

Please sign in to comment.