Skip to content

Commit

Permalink
Reset back to page one on category change
Browse files Browse the repository at this point in the history
  • Loading branch information
supernova-at committed Dec 20, 2019
1 parent fd01eaa commit 19d17c6
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/peregrine/lib/talons/SearchPage/useSearchPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export const useSearchPage = props => {
const inputText = getSearchParam('query', location);
const categoryId = getSearchParam('category', location);

// Keep track of the search term / query so we can tell when it changes.
const [searchTerm, setSearchTerm] = useState(inputText);
// Keep track of the search terms so we can tell when they change.
const [previousInputText, setPreviousInputText] = useState(inputText);
const [previousCategoryId, setPreviousCategoryId] = useState(categoryId);

const openDrawer = useCallback(() => {
toggleDrawer('filter');
Expand Down Expand Up @@ -87,15 +88,26 @@ export const useSearchPage = props => {
};
}, [data, setTotalPages]);

// Reset the current page back to one (1) when the query changes.
// Reset the current page back to one (1) when the query or category changes.
useEffect(() => {
if (searchTerm !== inputText) {
// The query changed.
if (
previousInputText !== inputText ||
previousCategoryId !== categoryId
) {
// The search term changed.
setCurrentPage(1);
}

setSearchTerm(inputText);
}, [inputText, searchTerm, setCurrentPage]);
// And update the state.
setPreviousCategoryId(categoryId);
setPreviousInputText(inputText);
}, [
categoryId,
inputText,
previousCategoryId,
previousInputText,
setCurrentPage
]);

return {
loading,
Expand Down

0 comments on commit 19d17c6

Please # to comment.