Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a subset of entities with ngrx-entity?

I'm updating a set of entities using a HTTP Patch request to a remote backend. The response from the backend includes the updated entities only (i.e., not all entities).

I set up my reducer with an entity state adapter and use updateMany to update my entities:

case settings.SettingsActionTypes.UpdateSettingsSuccess: {
   return {
     ...state,
     ...adapter.updateMany(action.payload.map((category) => Object.assign({}, {id: category.name, changes: category})), state),
     loaded: true,
     loading: false,
   }
 }

While this updates the entities that received an update, it deletes all others that are not returned by the backend.

Is there a way to tell ngrx to only update entities that are included in the action.payload?

like image 589
tilo Avatar asked Jan 12 '18 08:01

tilo


Video Answer


1 Answers

You shouldn't spread so many times.

Update many takes the state as a parameter you can use your spread fu in there.

return adapter.updateMany( 
   action.payload.map((category) => Object.assign({}, {id: category.name, changes: category})), 
   { ...state, loaded: true, loading: false }
);
like image 182
MTJ Avatar answered Sep 24 '22 13:09

MTJ