UNPKG

redux-resource

Version:
33 lines (25 loc) 1.65 kB
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; }; import actionReducersMap from './action-reducers-map'; import initialResourceMetaState from '../utils/initial-resource-meta-state'; import warning from '../utils/warning'; export default function requestStatusesPlugin(resourceType) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var customInitialMeta = options.initialResourceMeta || {}; var optionsToSend = { initialResourceMeta: _extends({}, initialResourceMetaState, customInitialMeta) }; return function (state, action) { var reducer = actionReducersMap[action.type]; if (process.env.NODE_ENV !== 'production') { if (reducer && !action.resourceName && !action.resourceType) { warning('A resourceType was not included in an action with type ' + ('"' + action.type + '". Without a resourceType, Redux Resource will ') + 'not be able to update a slice of your store. For more information, refer to ' + 'the guide on Request Actions: ' + 'https://redux-resource.js.org/docs/requests/request-actions.html', 'MISSING_RESOURCE_TYPE'); } } var actionResourceType = action.resourceType || action.resourceName; if (actionResourceType !== resourceType) { return state; } var callActionReducer = reducer && actionResourceType === resourceType; return callActionReducer ? reducer(state, action, optionsToSend) : state; }; }