redux-resource
Version:
Resource management for Redux.
151 lines (129 loc) • 7.08 kB
JavaScript
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 _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; };
import upsertResources from '../utils/upsert-resources';
import upsertMeta from '../utils/upsert-meta';
import initialResourceMetaState from '../utils/initial-resource-meta-state';
import requestStatuses from '../utils/request-statuses';
import warning from '../utils/warning';
export default (function (resourceType, _ref) {
var initialResourceMeta = _ref.initialResourceMeta;
return function (state, action) {
if (action.type !== 'UPDATE_RESOURCES' && action.type !== 'DELETE_RESOURCES') {
return state;
}
var naiveNewResources = action.resources && action.resources[resourceType];
var naiveNewMeta = action.meta && action.meta[resourceType];
var additionalLists = action.lists && action.lists[resourceType];
if (action.type === 'UPDATE_RESOURCES') {
if (process.env.NODE_ENV !== 'production') {
if (typeof action.mergeListIds !== 'undefined') {
warning('You passed the "mergeListId" properties to an UPDATE_RESOURCES action. ' + 'This property only works for the request action types (such as ' + 'READ_RESOURCES_PENDING). When using UPDATE_RESOURCES, you must modify the ' + 'list yourself, and then pass the new list to the action creator. ' + 'For more information, refer to the documentation on this action at: ' + 'https://redux-resource.js.org/docs/resources/modifying-resources.html', 'MERGE_LIST_ID_UPDATE_RESOURCES');
}
if (!action.resources && !action.meta && !action.lists) {
warning('You dispatched an UPDATE_RESOURCES action without any resources, meta, ' + 'or lists, so the store will not be updated. ' + 'For more information, refer to the documentation on this action at: ' + 'https://redux-resource.js.org/docs/resources/modifying-resources.html', 'UPDATE_RESOURCES_NO_OP');
}
}
var mergeResources = void 0;
if (typeof action.mergeResources === 'boolean') {
mergeResources = action.mergeResources;
} else if (_typeof(action.mergeResources) === 'object') {
mergeResources = action.mergeResources[resourceType];
} else {
mergeResources = true;
}
var mergeMeta = void 0;
if (typeof action.mergeMeta === 'boolean') {
mergeMeta = action.mergeMeta;
} else if (_typeof(action.mergeMeta) === 'object') {
mergeMeta = action.mergeMeta[resourceType];
} else {
mergeMeta = true;
}
var newResources = upsertResources(state.resources, naiveNewResources, mergeResources);
var newMeta = void 0;
if (!Array.isArray(naiveNewMeta)) {
newMeta = upsertMeta(state.meta, naiveNewMeta, mergeMeta);
} else {
newMeta = state.meta;
}
var newLists = state.lists;
if (additionalLists) {
newLists = _extends({}, state.lists, additionalLists);
}
return _extends({}, state, action.resourceSliceAttributes, {
resources: newResources,
meta: newMeta,
lists: newLists
});
} else {
var _ret = function () {
if (process.env.NODE_ENV !== 'production') {
if (!action.resources && !action.meta) {
warning('You dispatched a DELETE_RESOURCES action without any resources ' + 'or meta, so the store will not be updated. ' + 'For more information, refer to the documentation on this action at: ' + 'https://redux-resource.js.org/docs/resources/modifying-resources.html', 'DELETE_RESOURCES_NO_OP');
}
}
var idList = void 0;
if (naiveNewResources && naiveNewResources.map) {
idList = naiveNewResources.map(function (r) {
if ((typeof r === 'undefined' ? 'undefined' : _typeof(r)) === 'object') {
if (process.env.NODE_ENV !== 'production') {
if (!r.id && r.id !== 0 || typeof r.id !== 'string' && typeof r.id !== 'number') {
warning('A resource without an ID was passed to an action with type ' + (action.type + '. Every resource must have an ID that is either ') + 'a number of a string. You should check your action creators to ' + 'make sure that an ID is always included in your resources. ' + 'For more information, refer to the documentation on resource objects at: ' + 'https://redux-resource.js.org/docs/resources/resource-objects.html', 'NO_RESOURCE_ID');
}
}
return r.id;
} else {
if (process.env.NODE_ENV !== 'production') {
if (typeof r !== 'string' && typeof r !== 'number') {
warning('A resource without an ID was passed to an action with type ' + (action.type + '. Every resource must have an ID that is either ') + 'a number of a string. You should check your action creators to ' + 'make sure that an ID is always included in your resources. ' + 'For more information, refer to the documentation on resource objects at: ' + 'https://redux-resource.js.org/docs/resources/resource-objects.html', 'NO_RESOURCE_ID');
}
}
return r;
}
});
} else {
idList = Object.keys(naiveNewResources || {});
}
var hasIds = idList && idList.length;
if (!hasIds) {
return {
v: state
};
}
var newMeta = void 0;
var newLists = {};
var meta = state.meta;
var lists = state.lists;
for (var resourceList in lists) {
var existingList = lists[resourceList];
newLists[resourceList] = existingList.filter(function (r) {
return !idList.includes(r);
});
}
var nullMeta = idList.reduce(function (memo, id) {
var newMeta = (naiveNewMeta || {})[id];
memo[id] = _extends({}, initialResourceMetaState, initialResourceMeta, newMeta, {
deleteStatus: requestStatuses.SUCCEEDED
});
return memo;
}, {});
newMeta = _extends({}, meta, nullMeta);
// Shallow clone the existing resource object, nulling any deleted resources
var newResources = Object.assign({}, state.resources);
if (hasIds) {
idList.forEach(function (id) {
delete newResources[id];
});
}
return {
v: _extends({}, state, {
meta: newMeta,
lists: newLists,
resources: newResources
})
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
};
});