All files / @nova/finance/store reducers.js

86.36% Statements 19/22
72.73% Branches 8/11
57.14% Functions 4/7
94.74% Lines 18/19

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                1x                                 1x 1x 8x 1x   7x   1x 1x       1x 1x   1x               1x                             1x                       1x           1x                               4x 2x     2x    
import {
  InvoicesStatus,
  SET_INVOICES,
  SET_FILTERS_SELECTED,
  SET_POST_JOKER_STATUS,
  SET_ARRANGEMENT_ACTIVE,
} from './constants';
 
const initialState = {
  invoices: [],
  filter: InvoicesStatus.ALL,
  postJokerStatus: {
    status: '',
    statementNumber: null,
  },
};
 
/**
 * Set post joker status and update joker props in associated invoice
 *
 * @param {Object} state
 * @param {Object} action
 * @param {String?|Number?} action.payload.status
 * @param {String?} action.payload.statementNumber
 */
const setPostJokerStatus = (state, action) => {
  const updateJoker = (i) => {
    if (i.statementNumber === action.payload.statementNumber) {
      return { ...i, jokerActive: true, jokerEligible: false };
    }
    return i;
  };
  const nextState = { ...state };
  nextState.postJokerStatus = {
    status: action.payload.status,
    statementNumber: action.payload.statementNumber,
  };
  Eif (action.payload.status === 201) {
    nextState.invoices = nextState.invoices.map(updateJoker);
  }
  return nextState;
};
 
/**
 * Set arrangement active for the given statementNumber
 * @param {*} state
 * @param {*} action
 */
const setArrangementActive = (state, action) => ({
  ...state,
  invoices: state.invoices.map(i => (i.statementNumber !== action.payload.statementNumber
    ? i
    : { ...i, arrangementActive: true }
  )),
});
 
/**
 * Set fetched invoices for a given insured
 * @param {Object} state
 * @param {Object} action
 * @param {Array} action.payload.invoices
 * @return {Object}
 */
const setInvoices = (state, action) => ({
  ...state,
  invoices: action.payload.invoices,
});
 
/**
 * Set filter selected
 *
 * @param {Object} state
 * @param {Object} action
 * @param {String} action.payload.filter
 */
const setFiltersSelected = (state, action) => ({
  ...state,
  filter: action.payload.filter,
});
 
// convenience object to map actions to functions
const FUNCTION_BY_ACTION = {
  [SET_INVOICES]: setInvoices,
  [SET_FILTERS_SELECTED]: setFiltersSelected,
  [SET_POST_JOKER_STATUS]: setPostJokerStatus,
  [SET_ARRANGEMENT_ACTIVE]: setArrangementActive,
};
 
/**
 * Reducer function
 *
 * @param {Object} state
 * @param {Object=} action
 *
 * @returns {Object}
 */
export default (state = initialState, action) => {
  if (action && action.type in FUNCTION_BY_ACTION) {
    return FUNCTION_BY_ACTION[action.type](state, action);
  }
 
  return state;
};