1

I need to remove selected "archivados" inside my reducer. Is there any way to filter the current state? I fount the following solution, but I think there should be a better way to do this.

export const adapter: EntityAdapter<Archivado> = createEntityAdapter<Archivado>(
  {
    sortComparer: (e1: Archivado, e2: Archivado) => e1.id - e2.id
  }
);

export const archivadosInitialState: ArchivadosState = adapter.getInitialState();

export function archivadosReducer(
  state: ArchivadosState = archivadosInitialState,
  action: ArchivadosAction
): any {
  switch (action.type) {
    case ArchivadosActionTypes.DELETE_SELECTED: {
      const ids: number[] = [];
      const entities = { ...state.entities };
      for (const key in Object.keys(entities)) {
        if (entities.hasOwnProperty(key) && entities[key].selected) {
          ids.push(+key);
        }
      }
      return adapter.removeMany(ids, state);
    }
    default: {
      return state;
    }
  }
}
Sergio Tx
  • 3,706
  • 1
  • 16
  • 29

0 Answers0