0

I have the following action

export const updateSuccess = createAction('Success', props<{ someId: string }>());

And I have an adapter in the reducer like this

export const adapter: EntityAdapter<Model> = createEntityAdapter<Model>({
  selectId: (item: Model) => item.someId,
  sortComparer: false
});

Now when I update an item, the backend is sending that someId. So I do not have the full Model data at that point. So I was thinking I need to filter the entities I have in the state and upsertOne with that filtered item.

So something like this

on(actions.updateSuccess, (state, payload) =>
 adapter.upsertOne(state.entities.filter(entitiy => entity.someId === payload.someId), { ...state, someOtherProperties: 'values' })
),

But this looks incorrect and gives errors. How can I do this?

AngularDebutant
  • 1,436
  • 5
  • 19
  • 41

1 Answers1

0

You can use upsertOne

updateOne: Update one entity in the collection. Supports partial updates.

https://ngrx.io/guide/entity/adapter#adapter-collection-methods

on(actions.updateSuccess, (state, payload) =>
 adapter.upsertOne({ id: 1, foo: 'bar' }, state)
),
timdeschryver
  • 14,415
  • 1
  • 19
  • 32