All files / src/lib/core/manifest manifest.reducers.ts

92.5% Statements 37/40
100% Branches 6/6
88.46% Functions 23/26
92.11% Lines 35/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                          35x     1x       1x         1x   2x 1x 1x 1x 1x 1x 1x     1x 1x   1x 1x     1x               2x       4x 1x       3x                   2x       4x 1x     3x                   2x               2x   4x             32x                 1x     1x     1x     1x     1x   1x   1x  
import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity';
import { Action, createReducer, on } from '@ngrx/store';
import { Manifest } from './manifest';
import { ManifestEntryRequest } from './manifest-entry-request';
import * as ManifestActions from './manifest.actions';
 
export interface State extends EntityState<Manifest> {
  pendingRequests: Array<ManifestEntryRequest>;
  currentRequest: ManifestEntryRequest;
}
 
// tslint:disable-next-line:only-arrow-functions
export function selectManifestByName(manifest: Manifest): string {
  return manifest.name;
}
 
export const adapter: EntityAdapter<Manifest> = createEntityAdapter<Manifest>({
  selectId: selectManifestByName
});
 
export const initialState: State = adapter.getInitialState({
  pendingRequests: [],
  currentRequest: undefined
});
 
const manifestReducer = createReducer(
  initialState,
  on(ManifestActions.addManifest, (state, { manifest }) => adapter.addOne(manifest, state)),
  on(ManifestActions.setManifest, (state, { manifest }) => adapter.setOne(manifest, state)),
  on(ManifestActions.upsertManifest, (state, { manifest }) => adapter.upsertOne(manifest, state)),
  on(ManifestActions.addManifests, (state, { manifests }) => adapter.addMany(manifests, state)),
  on(ManifestActions.upsertManifests, (state, { manifests }) => adapter.upsertMany(manifests, state)),
  on(ManifestActions.updateManifest, (state, { update }) => adapter.updateOne(update, state)),
  on(ManifestActions.updateManifests, (state, { updates }) => adapter.updateMany(updates, state)),
  on(ManifestActions.mapManifest, (state, { entityMap }) => adapter.mapOne(entityMap, state)),
  on(ManifestActions.mapManifests, (state, { entityMap }) => adapter.map(entityMap, state)),
  on(ManifestActions.deleteManifest, (state, { id }) => adapter.removeOne(id, state)),
  on(ManifestActions.deleteManifests, (state, { ids }) => adapter.removeMany(ids, state)),
  on(ManifestActions.deleteManifestsByPredicate, (state, { predicate }) => adapter.removeMany(predicate, state)),
  on(ManifestActions.loadManifests, (state, { manifests }) => adapter.setAll(manifests, state)),
  on(ManifestActions.clearManifests, state => adapter.removeAll({ ...state, selectedManifestId: undefined })),
  // tslint:disable-next-line:arrow-return-shorthand
  on(ManifestActions.submitRequest, (state, { request }) => {
    return {
      ...state,
      currentRequest: request
    // tslint:disable-next-line:semicolon
    }
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(ManifestActions.submitRequestSuccess, (state, { request, response, manifest }) => {
    return adapter.updateOne({
      id: manifest.name,
      changes: {
        entries: state.entities[manifest.name].entries.map(entry => {
          if (entry.name === request.entryName) {
            return { ...entry, request, response };
          }
 
          // tslint:disable-next-line:arrow-return-shorthand
          return entry;
        })
      }
    }, {
      ...state,
      currentRequest: undefined
    });
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(ManifestActions.submitRequestFailure, (state, { request, response, manifest }) => {
    return adapter.updateOne({
      id: manifest.name,
      changes: {
        entries: state.entities[manifest.name].entries.map(entry => {
          if (entry.name === request.entryName) {
            return { ...entry, request, response };
          }
 
          return entry;
        })
      }
    }, {
      ...state,
      currentRequest: undefined
    });
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(ManifestActions.queueRequest, (state, { request }) => {
    return {
      ...state,
      pendingRequests: state.pendingRequests.concat([{ ...request }]),
      currentRequest: undefined
    };
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(ManifestActions.dequeueRequest, (state, { request }) => {
    return {
      ...state,
      pendingRequests: state.pendingRequests.filter(r => r.manifestName !== request.manifestName || r.entryName !== request.entryName)
    };
  })
);
 
// tslint:disable-next-line:typedef
export function reducer(state: State | undefined, action: Action) {
  return manifestReducer(state, action);
}
 
// get the selectors
const {
  selectIds,
  selectEntities,
  selectAll,
  selectTotal
} = adapter.getSelectors();
 
// select the array of manifest names
export const selectManifestNames = selectIds;
 
// select the dictionary of manifest entities
export const selectManifestEntities = selectEntities;
 
// select the array of manifests
export const selectAllManifests = selectAll;
 
// select the total manifest count
export const selectManifestTotal = selectTotal;
 
export const selectCurrentRequest = (state: State) => state.currentRequest;
 
export const selectPendingRequests = (state: State) => state.pendingRequests;