1

I need to update the isExpanded property in all entities. I tried to do this with reduce() but got nested objects with v key :/

function updateAllIsExpanded(state, isExpanded): any {
  return Object.entries(state.entities).reduce(
    (p, [k, v]) => ({ ...p, [k]: { v, ...{ isExpanded } } }),
    {}
  );
}

In ngrx documentation we can find something like updateMany... but the problem is that I have to create arrays of objects with id and change... so I guess that's not a good idea ...

JanuszFrontEnd'u
  • 451
  • 6
  • 14

2 Answers2

5

You're probably looking for map.

map: Update multiple entities in the collection by defining a map function, similar to Array.map.

adapter.map(
  (entity) => ({...entity, isExpanded }),
  state
);

Community
  • 1
  • 1
timdeschryver
  • 14,415
  • 1
  • 19
  • 32
1

You need ... in front of v:

UPDATE:

function updateAllIsExpanded(state, isExpanded): any {
  return Object.entries(state.entities).reduce(
    (p, [k, v]: [string, Object]) => ({ ...p, [k]: { ...v, ...{ isExpanded } } }),
    {}
  );
}
munleashed
  • 1,677
  • 1
  • 3
  • 9
  • but then I will get error: `Spread types may only be created from object types.` – JanuszFrontEnd'u Oct 29 '21 at 10:17
  • I updated it. Its a typescript check error. Is this working for you now? – munleashed Oct 29 '21 at 10:21
  • great, it works but why in the redux tool I can see as many actions as I have edited entities? It looks like ngrx is registering additional actions due to changes in the store? – JanuszFrontEnd'u Oct 29 '21 at 10:37
  • Its impossible to say what is happening in your case because we do not have a code :) It depends on few things, how your reducers, effects etc are organized, etc. – munleashed Oct 29 '21 at 10:41