All files / src/app/core/store/customer/orders orders.reducer.ts

94.11% Statements 16/17
50% Branches 1/2
100% Functions 5/5
94.11% Lines 16/17

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 82158x 158x         158x   158x                           158x 22x                     158x               158x             5x             3x   3x           8x 8x 8x 8x               1x            
import { EntityState, createEntityAdapter } from '@ngrx/entity';
import { createReducer, on } from '@ngrx/store';
 
import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { OrderListQuery } from 'ish-core/models/order-list-query/order-list-query.model';
import { Order } from 'ish-core/models/order/order.model';
import { setErrorOn, setLoadingOn, unsetLoadingAndErrorOn } from 'ish-core/utils/ngrx-creators';
 
import {
  createOrder,
  createOrderFail,
  createOrderSuccess,
  loadOrder,
  loadOrderFail,
  loadOrderSuccess,
  loadOrders,
  loadOrdersFail,
  loadOrdersSuccess,
  resetOrderErrors,
  selectOrder,
} from './orders.actions';
 
export const orderAdapter = createEntityAdapter<Order>({
  selectId: order => order.id,
});
 
export interface OrdersState extends EntityState<Order> {
  loading: boolean;
  selected: string;
  query: OrderListQuery;
  moreAvailable: boolean;
  error: HttpError;
}
 
const initialState: OrdersState = orderAdapter.getInitialState({
  loading: false,
  selected: undefined,
  query: undefined,
  moreAvailable: true,
  error: undefined,
});
 
export const ordersReducer = createReducer(
  initialState,
  setLoadingOn(createOrder, loadOrder, loadOrders),
  unsetLoadingAndErrorOn(createOrderSuccess, loadOrderSuccess, loadOrdersSuccess),
  setErrorOn(loadOrdersFail, loadOrderFail, createOrderFail),
  on(
    selectOrder,
    (state, action): OrdersState => ({
      ...state,
      selected: action.payload.orderId,
    })
  ),
 
  on(createOrderSuccess, loadOrderSuccess, (state, action) => {
    const { order } = action.payload;
 
    return {
      ...orderAdapter.upsertOne(order, state),
      selected: order.id,
    };
  }),
  on(loadOrdersSuccess, (state, action) => {
    const { orders, query, allRetrieved } = action.payload;
    const newState = { ...state, query, moreAvailable: !allRetrieved };
    if (!query.offset) {
      return orderAdapter.setAll(orders, newState);
    } else E{
      return orderAdapter.addMany(orders, newState);
    }
  }),
 
  on(
    resetOrderErrors,
    (state): OrdersState => ({
      ...state,
      error: undefined,
    })
  )
);