All files / @tech/pdf reducers.js

100% Statements 13/13
100% Branches 5/5
100% Functions 5/5
100% Lines 9/9

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    1x                         1x             1x                 1x             1x     1x                             6x 4x   2x    
import { SET_PDF, RESET_PDF, SET_ERROR, SET_LOADING } from './constants';
 
const initialState = {
  pdf: null,
  error: false,
  loading: false,
};
 
/**
 * Set fetched pdf local uri
 * @param {Object} state
 * @param {Object} action
 * @param {String} action.payload.pdf
 * @return {Object}
 */
const setPdf = (state, action) => ({ ...state, pdf: action.payload.pdf });
 
/**
 * Set the PDF path to null
 * @param {Object} state
 * @return {Object}
 */
const resetPdf = state => ({ ...state, pdf: null, error: false });
 
/**
 * Set a flag indicating an error with PDF fetch
 * @param {Object} state
 * @param {Object} action
 * @param {Boolean} action.payload.error
 * @return {Object}
 */
const setError = (state, action) => ({ ...state, error: action.payload.error });
 
/**
 * Set the PDF path to null
 * @param {Object} state
 * @return {Object}
 */
const setLoading = (state, action) => ({ ...state, loading: action.payload.loading });
 
// convenience object to map actions to functions
const FUNCTION_BY_ACTION = {
  [SET_PDF]: setPdf,
  [RESET_PDF]: resetPdf,
  [SET_ERROR]: setError,
  [SET_LOADING]: setLoading,
};
 
/**
 * 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;
};