UNPKG

redux-resource-plugins

Version:

Official plugins for Redux Resource

97 lines (80 loc) 3.27 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; }; 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 _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } var actionTypes = { SELECT_RESOURCES: 'SELECT_RESOURCES', DESELECT_RESOURCES: 'DESELECT_RESOURCES', CLEAR_SELECTED_RESOURCES: 'CLEAR_SELECTED_RESOURCES' }; var initialState = { selectedIds: [] }; function selectResources(resourceType, resources) { return { type: actionTypes.SELECT_RESOURCES, resourceType: resourceType, resources: resources }; } function deselectResources(resourceType, resources) { return { type: actionTypes.DESELECT_RESOURCES, resourceType: resourceType, resources: resources }; } function clearSelectedResources(resourceType) { return { type: actionTypes.CLEAR_SELECTED_RESOURCES, resourceType: resourceType }; } function selection(resourceType) { return function (state, action) { // Ignore actions that were dispatched for another resource type var typeToUse = action.resourceType || action.resourceName; if (typeToUse !== resourceType) { return state; } var selectedIds = void 0, resourceIds = void 0; var resources = action.resources || []; if (action.type === actionTypes.SELECT_RESOURCES) { selectedIds = [].concat(_toConsumableArray(state.selectedIds)) || []; resourceIds = resources.map(function (r) { return (typeof r === 'undefined' ? 'undefined' : _typeof(r)) === 'object' ? r.id : r; }); var selectedIdsSet = new Set(selectedIds); resourceIds.forEach(function (id) { if (!selectedIdsSet.has(id)) { selectedIds.push(id); } }); return _extends({}, state, { selectedIds: selectedIds }); } else if (action.type === actionTypes.DESELECT_RESOURCES) { selectedIds = [].concat(_toConsumableArray(state.selectedIds)) || []; resourceIds = resources.map(function (r) { return (typeof r === 'undefined' ? 'undefined' : _typeof(r)) === 'object' ? r.id : r; }); var resourceIdsSet = new Set(resourceIds); return _extends({}, state, { selectedIds: selectedIds.filter(function (id) { return !resourceIdsSet.has(id); }) }); } else if (action.type === actionTypes.CLEAR_SELECTED_RESOURCES) { return _extends({}, state, { selectedIds: [] }); } return state; }; } selection.actionTypes = actionTypes; selection.selectResources = selectResources; selection.deselectResources = deselectResources; selection.clearSelectedResources = clearSelectedResources; selection.initialState = initialState; export default selection;