Skip to content

Commit

Permalink
fix(ui): SearchParams - trigger filter change correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Feb 27, 2022
1 parent b99c083 commit 9009424
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/ui/src/hooks/search-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const getSearchPattern = (search) => {
};

const getSearchReducer =
({ defaultFilters, allEntriesFilters, setParentState }) => (state, action) => {
({ defaultFilters, allEntriesFilters, setParentState }) =>
(state, action) => {
const { type, payload } = action;

switch (type) {
Expand Down Expand Up @@ -116,29 +117,35 @@ export const useSearchParams = ({
// Update state when the custom filters/search are changing - initial load or route updates
useLayoutEffect(() => {
// Run a deep comparison to prevent circular setState triggering
if (parentSearch === search || isEqual(parentFilters, filters)) {
if (parentSearch === search && isEqual(initialFilters, filters)) {
return;
}

dispatch({ type: ACTION_SET, payload: generateState(initialFilters, parentSearch) });
}, [search, filters, parentFilters, parentSearch]);
}, [dispatch, search, filters, parentSearch, initialFilters]);

/** Callbacks */
const handleUpdateSearch = useCallback((newValue) => {
dispatch({ type: ACTION_SET_SEARCH, payload: newValue });
}, []);
const handleUpdateSearch = useCallback(
(newValue) => {
dispatch({ type: ACTION_SET_SEARCH, payload: newValue });
},
[dispatch],
);

const handleUpdateFilters = useCallback((newFilters) => {
dispatch({ type: ACTION_SET_FILTERS, payload: newFilters });
}, []);
const handleUpdateFilters = useCallback(
(newFilters) => {
dispatch({ type: ACTION_SET_FILTERS, payload: newFilters });
},
[dispatch],
);

const handleResetFilters = useCallback(() => {
dispatch({ type: ACTION_RESET_DEFAULT });
}, []);
}, [dispatch]);

const handleResetAllFilters = useCallback(() => {
dispatch({ type: ACTION_RESET_ALL });
}, []);
}, [dispatch]);

return {
search,
Expand Down

0 comments on commit 9009424

Please sign in to comment.