UNPKG

redux-resource

Version:
122 lines (101 loc) 5.27 kB
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; 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; }; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import setResourceMeta from './set-resource-meta'; import upsertResources from './upsert-resources'; import requestStatuses from './request-statuses'; import warning from './warning'; // This reducer helper handles the "CRU" in "CRUD". export default function (state, action, _ref, updatedMeta) { var initialResourceMeta = _ref.initialResourceMeta; var resources = action.resources; var resourcesIsUndefined = typeof resources === 'undefined'; var hasResources = resources && resources.length; var requestKey = void 0, requestName = void 0; if (action.request && typeof action.request === 'string') { requestKey = requestName = action.request; } if (action.requestKey && typeof action.requestKey === 'string') { requestKey = action.requestKey; } if (action.requestName && typeof action.requestName === 'string') { requestName = action.requestName; } var list = void 0; if (action.list && typeof action.list === 'string') { list = action.list; } if (process.env.NODE_ENV !== 'production') { if (!resources) { warning('A \'resources\' array was not included in a Redux Resource ' + ('"success" action with type "' + action.type + '. Without a \'resources\' ') + 'Array, Redux Resource will not be able to track which resources ' + 'were affected by this CRUD operation. You should check your Action ' + 'Creators to make sure that they always include a \'resources\' array. ' + 'For more information, refer to the request action documentation at: ' + 'https://redux-resource.js.org/docs/requests/request-actions.html', 'SUCCESS_NO_RESOURCES'); } else if (!Array.isArray(resources)) { warning('A non-array \'resources\' value was passed to a Redux Resource ' + ('"success" action with type "' + action.type + '". \'resources\' must be an ') + 'array. If your backend returned a single object, be sure to wrap it ' + 'inside of an array. If you\'re using the Redux Resource XHR ' + 'library, you can do this using the "transformData" option. ' + 'For more information, refer to the request action documentation at: ' + 'https://redux-resource.js.org/docs/requests/request-actions.html', 'NON_ARRAY_RESOURCES'); } } // Without resources, a list, or a request key, there is nothing to update if (!hasResources && !requestKey && !list) { return state; } var newResources = upsertResources(state.resources, resources, action.mergeResources); var newMeta = setResourceMeta({ resources: resources, meta: state.meta, newMeta: updatedMeta, mergeMeta: action.mergeMeta, initialResourceMeta: initialResourceMeta }); var newRequests = void 0; if (requestKey) { var existingRequest = state.requests[requestKey] || {}; var newRequest = _extends({}, existingRequest, action.requestProperties, { resourceType: action.resourceType || action.resourceName, requestKey: requestKey, status: requestStatuses.SUCCEEDED }); if (requestName) { newRequest.requestName = requestName; } var newRequestIds = void 0; if (hasResources) { newRequestIds = resources.map(function (resource) { return (typeof resource === 'undefined' ? 'undefined' : _typeof(resource)) === 'object' ? resource.id : resource; }); } newRequest.ids = newRequestIds || []; newRequests = _extends({}, state.requests, _defineProperty({}, requestKey, newRequest)); } else { newRequests = state.requests; } var newLists = void 0; if (list) { var currentList = state.lists[list] || []; var newList = void 0; if (action.mergeListIds === false) { if (hasResources) { newList = resources.map(function (resource) { return (typeof resource === 'undefined' ? 'undefined' : _typeof(resource)) === 'object' ? resource.id : resource; }); } else if (!resourcesIsUndefined) { newList = []; } } else if (hasResources) { newList = Array.prototype.slice.call(currentList); resources.forEach(function (resource) { var id = (typeof resource === 'undefined' ? 'undefined' : _typeof(resource)) === 'object' ? resource.id : resource; if (!newList.includes(id)) { newList.push(id); } }); } newLists = _extends({}, state.lists, _defineProperty({}, list, newList || currentList)); } else { newLists = state.lists; } return _extends({}, state, { resources: newResources, meta: newMeta, requests: newRequests, lists: newLists }); }