I'd really appreicate any input on how to fix the issue presented below! I already found the cause of the issue but I am not too sure how to fix it.
Use Case
I created my own custom range connector that takes in 2 attributes (a minAttribute and a maxAttribute) instead of 1. The way it works is as follows:
- when a user enters a user_min value, the custom range connector will refine to show objects with a maxAttribute > user_min
- when a user enters a user_max, the custom range connector will refine to show objects with a minAttribute < user_max.
- If the user enters both a user_min and user_max then it just finds the intersection of the previous scenarios.
Issue
Currently when a user inputs a number outside the bounds, the search is not refining to show "0 results" and instead just shows all the data in my index.
Example
I have 3 objects in my index -> {minPrice: 10, maxPrice: 20}, {minPrice: 30, maxPrice: 40}, {minPrice: 50, maxPrice: 60}
Now if the user inputs a max value of 5, instead of showing "0 results", it shows all the data in my index ("3 results" in this case)
Cause of Issue
Note: I am using this file but with my own custom logic (https://github.com/algolia/instantsearch/blob/master/packages/instantsearch.js/src/connectors/range/connectRange.ts)
After looking at the connectRange function, I see that the issue stems in the _refine function
When I do
const refinedState = getRefinedState(
helper,
currentRange,
nextMin,
nextMax
);
The logs of the refinedState correctly shows the applied numericRefinement
refinedState: {
disjunctiveFacets: [...]
facets: []
...
...
numericRefinements : {
maximumPrice : {}
minimumPrice : {<= : [5] }
}
}
but then in the next step in the _refine function, it sets a new state for the helper instance and then does a .search()
if (refinedState) {
helper.setState(refinedState).search();
}
};
After applying the search for the new state, when I console log the helper instance and I look at the state object, the numericRefinement in the refinedState was removed.
helper: {
client: {}
lastResults: {}
...
...
state: {
disjunctiveFacets: [...]
facets: []
...
...
numericRefinements : {
maximumPrice : {}
minimumPrice : {}
}
}
}
As you can see the helper instance removed the minimumPrice refinement from its state on its own. Why does the .search() function remove my refinement?