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

[Qeustion] Is it possible to set multiple values at once with the setFilter action? #601

Open
dbruvers opened this issue Jan 4, 2022 · 5 comments

Comments

@dbruvers
Copy link

dbruvers commented Jan 4, 2022

Please correct me if I am wrong but it seems like it is not possible to set multiple values at once with the setFilter action. Is that correct?

The value is being wrapped in an array in this line making it impossible to pass an array as the value since that would be wrapped in an additional array.

The expected behavior would be that one can do either of these actions:

  setFilter('my_field', 'value1');
  setFilter('my_field', ['value1', 'value2']);

The later one ends up with a filter state of

{ 
  field: 'my_field',
  type: 'all',
  values: [
    ['value1', 'value2'],
  ],
 }

which should be rather

{ 
  field: 'my_field',
  type: 'all',
  values: ['value1', 'value2'],
 }
@JasonStoltz
Copy link
Member

I think you may be right, you'd need to call setFilter twice. It's not terribly inefficient since they'll ultimately get collapsed into a single state update, but it's not the cleanest.

@dbruvers
Copy link
Author

dbruvers commented Jan 5, 2022

Thank you for the suggestion @JasonStoltz. I am working with an Elasticsearch based backend and working on a multi-value Select Facet view.

Setting setFilter multiple times did not work well: only one value gets set at a time. This is probably related to the fact that calling it causes a re-render and it's not possible to call it again while the re-render is in progress. In contrast the MultiCheckboxFacet view sets one value per render.

What I ended up was comparing new values to old values and then applying addFilter (onSelect) and removeFilter (onRemove) accordingly. As you wrote, this is not the cleanest solution but works since addFilter accepts an array of values.

One more note though: it seems that removeFilter has the same issue with accepting a single value only. This works when using the React Select component because it removes either one value at a time or all values at once. I can see though how this could cause issues in other circumstances.

@wentaoxu415
Copy link

Thanks for the insightful tips so far, @dbruvers and @JasonStoltz!

To expand on the idea of making improvements to enable setting multiple filter values at once, is it possible to add another action that can set the entire request state? (perhaps called something like setRequestState?)

That new action will be helpful for batching multiple value changes at once even when they span across different dimensions (For example, changing multiple filters and searchTerm at once).

@JasonStoltz, what is your thought on that idea?

@joemcelroy
Copy link
Member

Hey all!

Another way we could look at it is having a request builder pattern for when you need to make more than one change to search. For the above example it could be

 request()
   .resetFilters()
   .addFilter("states", "Alaska")
   .addFilter("states", "Texas")
   .setPagination(1)
   .search()

The advantages is that any further additions to the API can be exposed via new methods and also typed really nicely. Behind the scenes the existing API like setFilters will use this.

@JasonStoltz
Copy link
Member

Nice, I like that a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants