Skip to content
This repository has been archived by the owner on Aug 16, 2019. It is now read-only.

Persist sort order returned by normalizr #18

Merged

Conversation

coffenbacher
Copy link
Contributor

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 #17

@coffenbacher
Copy link
Contributor Author

@mikestone14 @pachun can you take a look at this one? I can't request reviewers on this repo

@pachun pachun requested review from mikestone14 and pachun April 18, 2018 18:19
Copy link

@pachun pachun left a 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.

Copy link
Member

@mikestone14 mikestone14 left a 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",
Copy link
Member

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.

Copy link
Contributor Author

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?

@coffenbacher coffenbacher force-pushed the add_normalizr_result_to_success branch 2 times, most recently from c5877a3 to 3f067e4 Compare April 18, 2018 19:13
@coffenbacher
Copy link
Contributor Author

@mikestone14 @pachun I added a new version that covers and tests the Create/Load/Destroy scenarios

@BlakeWilliams
Copy link

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 id in an object (for access/performance reasons when working with individual results) you'd want to do something like: Object.values(records) which I don't believe guarantees order.

Not saying we should or shouldn't, just throwing the thought of "maybe the client should be responsible for order" out there.

Copy link
Member

@mikestone14 mikestone14 left a 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),
Copy link
Member

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.

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.

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],
Copy link
Member

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]?

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.

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.

Copy link

@darkmoves darkmoves May 31, 2018

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.

@darkmoves
Copy link

Looking into your question about existing tests, @mikestone14, the payload contents are only being tested in config.tests.js via method calls that don't hit the reducer. I wouldn't expect them to fail with this change.

@mikestone14
Copy link
Member

@darkmoves you're right but I think that means we're missing test coverage.

@darkmoves
Copy link

@mikestone14 commit pushed with the changes that we discussed. This should be good to go.

@darkmoves darkmoves force-pushed the add_normalizr_result_to_success branch 3 times, most recently from b8dd2e7 to f9ec5c5 Compare June 1, 2018 17:40
@darkmoves
Copy link

This PR's been rebased to include the DESTROY fix and the sortedIds array has been added for that action. Ready for another round.

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
@darkmoves darkmoves force-pushed the add_normalizr_result_to_success branch from f9ec5c5 to d1b963b Compare June 1, 2018 18:38
@mikestone14 mikestone14 merged commit ba37025 into TheGnarCo:master Jun 1, 2018
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.

5 participants