All files / projects/organization-management/src/app/store/cost-centers cost-centers.reducer.ts

91.66% Statements 22/24
100% Branches 0/0
88.88% Functions 8/9
91.66% Lines 22/24

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 133 134 135 136 137 138 139 140 141 14249x 49x       49x   49x                                                       8x     49x                 49x         49x                                                                 5x   5x         11x   11x         2x   2x                       2x   2x         2x   2x         2x   2x         2x   2x          
import { EntityState, createEntityAdapter } from '@ngrx/entity';
import { createReducer, on } from '@ngrx/store';
 
import { CostCenter } from 'ish-core/models/cost-center/cost-center.model';
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { setErrorOn, setLoadingOn, unsetLoadingAndErrorOn } from 'ish-core/utils/ngrx-creators';
 
import {
  addCostCenter,
  addCostCenterBuyers,
  addCostCenterBuyersFail,
  addCostCenterBuyersSuccess,
  addCostCenterFail,
  addCostCenterSuccess,
  deleteCostCenter,
  deleteCostCenterBuyer,
  deleteCostCenterBuyerFail,
  deleteCostCenterBuyerSuccess,
  deleteCostCenterFail,
  deleteCostCenterSuccess,
  loadCostCenter,
  loadCostCenterFail,
  loadCostCenterSuccess,
  loadCostCenters,
  loadCostCentersFail,
  loadCostCentersSuccess,
  updateCostCenter,
  updateCostCenterBuyer,
  updateCostCenterBuyerFail,
  updateCostCenterBuyerSuccess,
  updateCostCenterFail,
  updateCostCenterSuccess,
} from './cost-centers.actions';
 
function sortByCostCenterId(a: CostCenter, b: CostCenter): number {
  return a.costCenterId.localeCompare(b.costCenterId);
}
 
export const costCentersAdapter = createEntityAdapter<CostCenter>({
  sortComparer: sortByCostCenterId,
});
 
export interface CostCentersState extends EntityState<CostCenter> {
  loading: boolean;
  error: HttpError;
}
 
const initialState: CostCentersState = costCentersAdapter.getInitialState({
  loading: false,
  error: undefined,
});
 
export const costCentersReducer = createReducer(
  initialState,
  setLoadingOn(
    loadCostCenters,
    loadCostCenter,
    addCostCenter,
    updateCostCenter,
    deleteCostCenter,
    addCostCenterBuyers,
    updateCostCenterBuyer,
    deleteCostCenterBuyer
  ),
  unsetLoadingAndErrorOn(
    loadCostCentersSuccess,
    loadCostCenterSuccess,
    addCostCenterSuccess,
    updateCostCenterSuccess,
    deleteCostCenterSuccess,
    addCostCenterBuyersSuccess,
    updateCostCenterBuyerSuccess,
    deleteCostCenterBuyerSuccess
  ),
  setErrorOn(
    loadCostCentersFail,
    loadCostCenterFail,
    addCostCenterFail,
    updateCostCenterFail,
    deleteCostCenterFail,
    addCostCenterBuyersFail,
    updateCostCenterBuyerFail,
    deleteCostCenterBuyerFail
  ),
  on(loadCostCentersSuccess, (state, action) => {
    const { costCenters } = action.payload;
 
    return {
      ...costCentersAdapter.upsertMany(costCenters, state),
    };
  }),
  on(loadCostCenterSuccess, (state, action) => {
    const { costCenter } = action.payload;
 
    return {
      ...costCentersAdapter.upsertOne(costCenter, state),
    };
  }),
  on(addCostCenterSuccess, (state, action) => {
    const { costCenter } = action.payload;
 
    return {
      ...costCentersAdapter.addOne(costCenter, state),
    };
  }),
  on(updateCostCenterSuccess, (state, action) => {
    const { costCenter } = action.payload;
 
    return {
      ...costCentersAdapter.upsertOne(costCenter, state),
    };
  }),
  on(deleteCostCenterSuccess, (state, action) => {
    const { id } = action.payload;
 
    return {
      ...costCentersAdapter.removeOne(id, state),
    };
  }),
  on(addCostCenterBuyersSuccess, (state, action) => {
    const { costCenter } = action.payload;
 
    return {
      ...costCentersAdapter.upsertOne(costCenter, state),
    };
  }),
  on(updateCostCenterBuyerSuccess, (state, action) => {
    const { costCenter } = action.payload;
 
    return {
      ...costCentersAdapter.upsertOne(costCenter, state),
    };
  }),
  on(deleteCostCenterBuyerSuccess, (state, action) => {
    const { costCenter } = action.payload;
 
    return {
      ...costCentersAdapter.upsertOne(costCenter, state),
    };
  })
);