All files / @gce/documents/store reducers.js

94.12% Statements 16/17
85.71% Branches 6/7
85.71% Functions 6/7
100% Lines 13/13

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    1x                           1x                 1x                 1x                 1x                 1x 1x   1x   1x     1x                                 6x 4x     2x    
import { SET_DOCUMENTS, SET_DOCUMENT, SET_FILTER_TYPE, FILTER_TYPES, SET_FILTER_UNREAD, SET_DOC_READ } from './constants';
 
export const initialState = {
  documents: [],
  document: null,
  filterType: FILTER_TYPES.ALL,
  filterUnread: false,
};
 
/**
 * Set the document iddms that the user is currently viewing
 *
 * @param {Object} state
 * @param {Object} action
 * @param {Array} action.payload.iddms
 */
const setDocument = (state, action) => ({ ...state, document: action.payload.iddms });
 
/**
 * Set fetched documents
 *
 * @param {Object} state
 * @param {Object} action
 * @param {Array} action.payload.documents
 */
const setDocuments = (state, action) => ({ ...state, documents: action.payload.documents });
 
/**
 * Set current document type filter
 *
 * @param {Object} state
 * @param {Object} action
 * @param {String} action.payload.filterType
 */
const setFilterType = (state, action) => ({ ...state, filterType: action.payload.filterType });
 
/**
 * Set current document unread filter
 *
 * @param {Object} state
 * @param {Object} action
 * @param {Boolean} action.payload.unread
 */
const setFilterUnread = (state, action) => ({ ...state, filterUnread: action.payload.unread });
 
/**
 * Set a document seen flag
 *
 * @param {Object} state
 * @param {Object} action
 * @param {String} action.payload.iddms
 */
const setDocRead = (state, action) => {
  const documents = [...state.documents].map(
    // eslint-disable-next-line
    d => d.iddms === action.payload.iddms ? { ...d, seen: true } : d,
  );
  return { ...state, documents };
};
 
const FUNCTION_BY_ACTION = {
  [SET_DOCUMENTS]: setDocuments,
  [SET_DOCUMENT]: setDocument,
  [SET_FILTER_TYPE]: setFilterType,
  [SET_FILTER_UNREAD]: setFilterUnread,
  [SET_DOC_READ]: setDocRead,
};
 
/**
 * 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;
};