All files / src makeFetchAction.js

80% Statements 16/20
83.33% Branches 5/6
60% Functions 3/5
88.24% Lines 15/17
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                1x               3x 1x   2x       2x       1x             1x               3x               1x 1x 1x 1x 1x 1x   1x                        
import get from './utils/get';
import {
  CALL_API,
  REDUCER_PATH,
  ACTION_UPDATE_LOCAL,
  ACTION_RESET_LOCAL,
} from './constants';
 
const normalizeResetData = (data = [
  'lastRequest',
  'isFetching',
  'isInvalidated',
  'lastResponse',
  'data',
  'error',
]) => {
  if (typeof data === 'string') {
    return [data];
  }
  Iif (!Array.isArray(data)) {
    console.warn('You are using resetter wrong, the params should be string, array or undefined');
    return [];
  }
  return data;
}
 
export default (apiName, apiConfigFn, selectorDescriptor = {}) => {
  const actionCreator = (...params) => ({
    [CALL_API]: {
      ...apiConfigFn(...params),
      name: apiName,
    },
  });
 
  const updater = data => ({
    type: ACTION_UPDATE_LOCAL,
    payload: {
      name: apiName,
      data,
    },
  });
 
  const resetter = data => ({
    type: ACTION_RESET_LOCAL,
    payload: {
      name: apiName,
      data: normalizeResetData(data)
    },
  });
 
  const isFetchingSelector = get([REDUCER_PATH, apiName, 'isFetching'], false);
  const isInvalidatedSelector = get([REDUCER_PATH, apiName, 'isInvalidated'], false);
  const dataSelector = get([REDUCER_PATH, apiName, 'data'], null);
  const headersSelector = get([REDUCER_PATH, apiName, 'headers'], null);
  const errorSelector = get([REDUCER_PATH, apiName, 'error'], null);
  const lastResponseSelector = get([REDUCER_PATH, apiName, 'lastResponse'], null);
 
  return {
    actionCreator,
    updater,
    isFetchingSelector,
    isInvalidatedSelector,
    dataSelector,
    headersSelector,
    errorSelector,
    lastResponseSelector,
    resetter,
  };
};