All files / src ActionFactory.js

100% Statements 8/8
100% Branches 3/3
100% Functions 4/4
100% Lines 6/6
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                                  6x       5x 1x     4x 4x   4x            
import Horseman from "./Horseman";
import * as types from "./constants/ActionTypes";
import fetchResource from "./fetchResource";
 
/**
 * Fetch a singular endpoint from an api and dispatches actions depending on
 * the result.
 *
 * @param successAction {string} The action to dispatch if a resource comes
 * back successfully.
 *
 * @param bypass {func} Function that will be fired when checking if we should
 * bypass the fetch.
 *
 * @return {Promise} Callback accepting an endpoint and dispatching the
 * actions
 */
export default (successAction, bypass = () => false) => endpoint => (
  dispatch,
  getState,
) => {
  if (bypass(getState())) {
    return Promise.resolve("Bypass");
  }
 
  Horseman.addResource({ endpoint, action: successAction });
  dispatch({ type: types.RESOURCE_REQUEST, meta: { endpoint } });
 
  return fetchResource({
    endpoint,
    dispatch,
    successAction,
  });
};