redux-resource
Version:
Resource management for Redux.
142 lines (119 loc) • 6.7 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; };
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 reducerGenerator from '../utils/reducer-generator';
import initialResourceMetaState from '../utils/initial-resource-meta-state';
import requestStatuses from '../utils/request-statuses';
import warning from '../utils/warning';
var del = reducerGenerator('delete', requestStatuses.PENDING);
var delFail = reducerGenerator('delete', requestStatuses.FAILED);
var delIdle = reducerGenerator('delete', requestStatuses.IDLE);
function delSucceed(state, action, _ref) {
var initialResourceMeta = _ref.initialResourceMeta;
var resources = action.resources;
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;
}
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');
}
if (action.list) {
warning('You included a "list" in a delete action. You don\'t need to do this, ' + 'because successful deletes remove the deleted resources from all lists. ' + 'For more information, refer to the documentation on deleting resources at: ' + 'https://redux-resource.js.org/docs/requests/deleting-resources.html', 'DELETE_LISTS');
}
}
// Find the list of IDs affected by this action
var idList = void 0;
if (resources && resources.map) {
idList = resources.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;
}
});
}
var hasIds = idList && idList.length;
// If we have no IDs nor requestKey, then there is nothing to update
if (!hasIds && !requestKey) {
return state;
}
var newMeta = void 0;
var newLists = {};
var newRequests = {};
var meta = state.meta;
var requests = state.requests;
var lists = state.lists;
if (requestKey) {
var existingRequest = requests[requestKey] || {};
var newRequest = _extends({}, existingRequest, action.requestProperties, {
requestKey: requestKey,
resourceType: action.resourceType || action.resourceName,
status: requestStatuses.SUCCEEDED,
ids: idList || []
});
if (requestName) {
newRequest.requestName = requestName;
}
newRequests = _extends({}, requests, _defineProperty({}, requestKey, newRequest));
} else {
newRequests = requests;
}
for (var resourceList in lists) {
var existingList = lists[resourceList];
var newList = void 0;
if (hasIds && existingList) {
newList = existingList.filter(function (r) {
return !idList.includes(r);
});
} else if (existingList) {
newList = existingList;
}
newLists[resourceList] = newList;
}
if (hasIds) {
var nullMeta = idList.reduce(function (memo, id) {
memo[id] = _extends({}, initialResourceMetaState, initialResourceMeta, {
deleteStatus: requestStatuses.SUCCEEDED
});
return memo;
}, {});
newMeta = _extends({}, meta, nullMeta);
} else {
newMeta = meta;
}
// 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 _extends({}, state, {
meta: newMeta,
lists: newLists,
requests: newRequests,
resources: newResources
});
}
export { del, delFail, delIdle, delSucceed };