Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

fix(sffv): clamp maxFacetHits to the allowed range #1696

Merged
merged 3 commits into from
Oct 29, 2018
Merged

Conversation

samouss
Copy link
Collaborator

@samouss samouss commented Oct 26, 2018

Summary

With the current implementation the value used for maxFacetHits is the default limit of the widget or the user provided value. It's fine for most cases, but it happens that the value provided by the user is higher than 100. This leads to an issue because the Algolia engine only accepts values between 1 - 100. When the value is not in that range the API throws an error.

I chose to fix the issue inside InstantSearch rather than in the helper because it feels odd (to me) to change the value on behalf of the user. The helper can be directly manipulate by users so it makes sense to throw an error if the value is invalid. With InstantSearch it's a bit different we use the helper, they don't have any knowledge of it. Since we allow any value for the limit it makes sense to send the correct a value to the API.

The fix clamp the provided value in the range 1, 100. I've also create a default value of 10 for the maxFacetHits in case no value is provided. It avoid to gets a NaN value from Math.{min,max}. It doesn't breaks anything because 10 is the default value on the engine.

Before

before

After

after

@samouss samouss requested a review from a team October 26, 2018 23:35
@algobot
Copy link
Contributor

algobot commented Oct 26, 2018

Deploy preview for react-instantsearch ready!

Built with commit ed44141

https://deploy-preview-1696--react-instantsearch.netlify.com

Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

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

Max bundle size will need to be made bigger

function onSearchForFacetValues({ facetName, query, maxFacetHits = 10 }) {
// The values 1, 100 are the min / max values that the engine accepts.
// see: https://www.algolia.com/doc/api-reference/api-parameters/maxFacetHits
const maxFacetHitsWithinRange = Math.max(1, Math.min(maxFacetHits, 100));
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have this issue as well in InstantSearch.js?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We have kinda the same issue, but it's less visible because we don't use the showMore option to conditionally pick the value for maxFacetHits.

Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

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

LGTM with build size change

@samouss samouss merged commit 83ce245 into master Oct 29, 2018
@samouss samouss deleted the fix/sffv-max-hits branch October 29, 2018 23:08
samouss added a commit that referenced this pull request Oct 30, 2018
<a name="5.3.2"></a>
## [5.3.2](v5.3.1...v5.3.2) (2018-10-30)

### Bug Fixes

* **sffv:** clamp maxFacetHits to the allowed range ([#1696](#1696)) ([83ce245](83ce245))
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants