Skip to content

Commit

Permalink
fix(connectInfiniteHits): fix page state when adding or removing widg…
Browse files Browse the repository at this point in the history
…ets (#4104)
  • Loading branch information
Yannick Croissant authored and Haroenv committed Oct 23, 2019
1 parent 079db57 commit 1077340
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/connectors/infinite-hits/connectInfiniteHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ const connectInfiniteHits: InfiniteHitsConnector = (
const getShowMore = (helper: Helper): (() => void) => () => {
helper.setPage(lastReceivedPage + 1).search();
};
const filterEmptyRefinements = (refinements = {}) => {
return Object.keys(refinements)
.filter(key =>
Array.isArray(refinements[key])
? refinements[key].length
: Object.keys(refinements[key]).length
)
.reduce((obj, key) => {
obj[key] = refinements[key];
return obj;
}, {});
};

return {
$$type: 'ais.infiniteHits',
Expand Down Expand Up @@ -131,7 +143,22 @@ const connectInfiniteHits: InfiniteHitsConnector = (
// We're doing this to "reset" the widget if a refinement or the
// query changes between renders, but we want to keep it as is
// if we only change pages.
const { page = 0, ...currentState } = state;
const {
page = 0,
hierarchicalFacets,
disjunctiveFacets,
...currentState
} = state;

currentState.hierarchicalFacetsRefinements = filterEmptyRefinements(
currentState.hierarchicalFacetsRefinements
);
currentState.disjunctiveFacetsRefinements = filterEmptyRefinements(
currentState.disjunctiveFacetsRefinements
);
currentState.numericRefinements = filterEmptyRefinements(
currentState.numericRefinements
);

if (!isEqual(currentState, prevState)) {
hitsCache = [];
Expand Down Expand Up @@ -230,7 +257,11 @@ const connectInfiniteHits: InfiniteHitsConnector = (
);
}

if (hasShowPrevious && uiState.page) {
if (!hasShowPrevious) {
return widgetSearchParameters;
}

if (uiState.page) {
// The page in the search parameters is decremented by one
// to get to the actual parameter value from the UI state.
return widgetSearchParameters.setQueryParameter(
Expand Down

0 comments on commit 1077340

Please sign in to comment.