Search code examples
angularngrxngrx-entity

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?


Solution

  • 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 }
    );