-
Notifications
You must be signed in to change notification settings - Fork 1
Persist sort order returned by normalizr #18
Persist sort order returned by normalizr #18
Conversation
@mikestone14 @pachun can you take a look at this one? I can't request reviewers on this repo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks alright to me. @mikestone14 will have more context. The explanation by Dan Abramov which you liked to from the issue you created was really helpful. I'd like to keep that in the commit message. Otherwise, I think this is good to go pending Mike's approval.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for contributing @coffenbacher! A couple notes:
This seems to cover the case of the loadAll
action in which we would want to replace sortedIds
with payload.data.sortedIds
in the reducer. I think this is missing the cases for create
, load
, and destroy
.
Scenarios:
create
: The resultIds
(containing just the newly created entity id) should be added to a new copy of the existing resultIds
and replace resultIds
.
load
: If the loaded entity's id exists in resultIds
we should do nothing with that key. If the loaded entity's id does not exist in resultIds
we should add it to the end of the resultIds
array.
destroy
: We should remove the destroyed entity's id from from the resultIds
array.
package.json
Outdated
@@ -1,6 +1,6 @@ | |||
{ | |||
"name": "redux-entity-config", | |||
"version": "2.0.0", | |||
"version": "2.0.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be updated as part of the PR but can be done as part of a new tagged commit for version update along with an update to the changelog.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay cool, I guess I leave that to you then?
c5877a3
to
3f067e4
Compare
@mikestone14 @pachun I added a new version that covers and tests the Create/Load/Destroy scenarios |
I don't think it'd be unreasonable to expect the client to sort the records it retrieves. For example, storing each returned result by Not saying we should or shouldn't, just throwing the thought of "maybe the client should be responsible for order" out there. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good @coffenbacher just a couple more comments. I'm also curious why the existing tests are passing if we're changing the shape of the payload in the reducer. I'd be happy to pair on this later if that would be helpful.
src/config.js
Outdated
@@ -55,6 +59,7 @@ class ReduxEntityConfig extends BaseConfig { | |||
...state, | |||
loading: false, | |||
errors: {}, | |||
sortedIds: _without(state.sortedIds, payload.data), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be payload.data.sortedIds
? This also assumes that the destroyed entity is returned by the server which is not always (maybe even rarely) the case. There is another issue for handling deleting entities though so not in scope for this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I think it should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For posterity: no change to the sortedIds
array for a deletion, since there's ambiguity as to whether an object will be returned with the API response.
@@ -45,6 +48,7 @@ class ReduxEntityConfig extends BaseConfig { | |||
...state, | |||
loading: false, | |||
errors: {}, | |||
sortedIds: _uniq([...state.sortedIds, ...payload.data.sortedIds]), | |||
data: { | |||
...state.data, | |||
...payload.data[entityName], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be payload.data.entities[entityName]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on this documentation of normalizr, I think you're right. The code on L41 would need to be changed too.
But I would expect tests to fail if this code is wrong. Will check out the tests now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked out the test for this code, and it seems to reflect reality. I changed L54 to ...payload.data.entities[entityName]
and the test failed. Given those two things, I believe the code is correct as written. I'm not sure exactly where the entities
key is removed from payload.data
, possibly here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out, it's happening here when entities is deconstructed.
Looking into your question about existing tests, @mikestone14, the payload contents are only being tested in |
@darkmoves you're right but I think that means we're missing test coverage. |
@mikestone14 commit pushed with the changes that we discussed. This should be good to go. |
b8dd2e7
to
f9ec5c5
Compare
This PR's been rebased to include the DESTROY fix and the |
When data is returned from a REST API in an array It often has a useful sort order That should be available to client code. This data is returned from the normalizr.normalize Function as `result`, but currently only `entities` Are persisted. Addresses issue: TheGnarCo#17 Explanation of Normalizr sorting from Dan Abramov: https://github.com/paularmstrong/normalizr/issues/9
f9ec5c5
to
d1b963b
Compare
When data is returned from a REST API in an array
It often has a useful sort order
That should be available to client code.
This data is returned from the normalizr.normalize
Function as
result
, but currently onlyentities
Are persisted.
Addresses #17