Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(connectInfiniteHits): fix page state when adding or removing widgets #4104

Merged
merged 3 commits into from
Sep 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/connectors/infinite-hits/connectInfiniteHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,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 @@ -186,7 +198,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(
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
currentState.disjunctiveFacetsRefinements
);
currentState.numericRefinements = filterEmptyRefinements(
currentState.numericRefinements
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we forgot to handle facets & facetsRefinements.


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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have the expected behavior anymore. The widget has to set a default value otherwise it gets controlled by its parent. Here is an example of a structure that wouldn't behave like expected:

search.addWidgets([
  hits(),
  pagination(),

  index().addWidgets([
    infiniteHits(),
  ]),
]);

Here is the live example (CodeSandbox doesn't work with the ES version at the moment). You can select a page inside the pagination it shouldn't impact the infiniteHits but it does. The widget doesn't have a default value anymore. We have to keep the page of 0 no matter what when the widget is mounted without refinement.


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