UNPKG

react-redux-quest

Version:

API and generic utilities for react and redux eco-system

60 lines (45 loc) 1.77 kB
'use strict'; exports.__esModule = true; exports.createQuestReducer = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _utils = require('../utils'); var initialState = {}; function requestStatusHandler() { var _extends2; var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var action = arguments[1]; if (!action.meta) { return state; } var _action$meta = action.meta, operation = _action$meta.operation, slicedStateKey = _action$meta.slicedStateKey; if (!operation || !slicedStateKey) { return state; } var operationOnstateSlice = operation.toLowerCase() + '.' + slicedStateKey; var nextState = _extends({}, state, (_extends2 = {}, _extends2[operationOnstateSlice] = action.isRequest, _extends2)); return Object.defineProperty(nextState, 'statusOf', { get: function get() { return function (s) { return nextState[s]; }; } }); } // Any action that ends with REQUEST/SUCCESS/FAIL go throw `handlerFn` reducer var createQuestReducer = exports.createQuestReducer = function createQuestReducer(handlerFn) { return (0, _utils.createRegExReducer)(initialState)({ REQUEST: function REQUEST(state, action) { return handlerFn(state, action); }, SUCCESS: function SUCCESS(state, action) { return handlerFn(state, action); }, FAIL: function FAIL(state, action) { return handlerFn(state, action); } }); }; var questReducer = createQuestReducer(requestStatusHandler); exports.default = questReducer;