inveniosoftware / react-searchkit

React component library for interacting with a REST API.
https://inveniosoftware.github.io/react-searchkit/
MIT License
78 stars 40 forks source link

fix url not updating state #157

Closed FlorianCassayre closed 3 years ago

FlorianCassayre commented 3 years ago

Tested on zenodo demo and on ILS. Also tested on CDS videos demo which has disabled support for url handling.

It may not be the best way to do it, but it works. I'm preventing the url from being updated twice: this is a workaround (I hope temporary) because the url is replaced twice, and even though the state is equivalent in both cases the order of the attributes is not.

Open to suggestions.

FlorianCassayre commented 3 years ago

Because of this change, nothing prevents the user from making concurrent requests using the history (ie. clicking on the "back" button several times). Therefore we need to handle these cases to avoid race conditions: see 15457b6. I used the following widespread pattern for requests:

Imagine that the user previously visited B, A and C in this order. Now the user clicks on "back" twice. The following scenarios can occur:

FlorianCassayre commented 3 years ago

PR was revised:

FlorianCassayre commented 3 years ago

Changes:

    case SET_QUERY_STATE:
      return {
        ...state,
        ...INITIAL_QUERY_STATE,
-       ...action.payload,
+       ...updateQueryState(INITIAL_QUERY_STATE, action.payload, INITIAL_QUERY_STATE_KEYS),
      };

INITIAL_QUERY_STATE is required for the filters to be correctly reset.