UNPKG

redux-shelf

Version:

Helper reducers and actions to take away your boilerplate code from both Data and Communication state types

336 lines (251 loc) 7.3 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var methods = { REMOVE: 'REMOVE', SET: 'SET', UPDATE: 'UPDATE' }; function ids() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var action = arguments[1]; switch (action.meta.method) { case methods.REMOVE: if (state.includes(action.meta.selector)) { return state.filter(function (id) { return id !== action.meta.selector; }); } return state; case methods.UPDATE: return action.payload.ids.reduce(function (acc, id) { if (acc.includes(id)) { return acc; } return [].concat(acc, [id]); }, state); case methods.SET: return action.payload.ids; default: return state; } } 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 objectWithoutProperties = function (obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }; function content() { var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var action = arguments[1]; switch (action.meta.method) { case methods.REMOVE: { if (!state[action.meta.selector]) { return state; } var omit = state['' + action.meta.selector], newState = objectWithoutProperties(state, ['' + action.meta.selector]); return newState; } case methods.SET: return action.payload.content; case methods.UPDATE: return _extends({}, state, action.payload.content); default: return state; } } function of(type, selector) { if (selector) { return this.contentOf(type, selector); } return this.idsOf(type); } function idsOf(type) { var entity = this[type]; if (entity && entity.ids) { return entity.ids; } return []; } function contentOf(type, selector) { var entity = this[type]; if (entity && entity.content) { return entity.content[selector]; } return undefined; } function generateActionType(type, method) { return 'entities/' + type + '/' + method; } function generateActionByMethod(method) { return function (type, payload) { return { type: generateActionType(type, method), meta: { method: method, type: type }, payload: payload }; }; } function remove(type, selector) { return { type: generateActionType(type, methods.REMOVE), meta: { method: methods.REMOVE, selector: selector, type: type } }; } var set$1 = generateActionByMethod(methods.SET); var update = generateActionByMethod(methods.UPDATE); function reducer() { var _babelHelpers$extends; var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var action = arguments[1]; if (!action.type.startsWith('entities') || !(action.meta && action.meta.type)) { return state; } var entity = state[action.meta.type] || {}; var newIds = ids(entity.ids, action); var newContent = content(entity.content, action); if (entity.content === newContent) { return state; } return _extends({}, state, (_babelHelpers$extends = {}, _babelHelpers$extends[action.meta.type] = { ids: newIds, content: newContent }, _babelHelpers$extends)); } function entities(state, action) { var newState = reducer(state, action); newState.of = of; newState.idsOf = idsOf; newState.contentOf = contentOf; return newState; } entities.set = set$1; entities.update = update; entities.remove = remove; function generateActionByStatus(status) { return function (type, selector) { var action = { type: 'communication/' + type + '/' + status, meta: { type: type, status: status } }; if (selector) { action.meta.selector = selector; } return action; }; } var cancel = generateActionByStatus('CANCEL'); var done = generateActionByStatus('DONE'); function fail(type) { var selector = void 0; var error = void 0; for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } if (args.length > 1) { selector = args[0]; error = args[1]; } else { error = args[0]; } var action = generateActionByStatus('FAIL')(type, selector); if (error) { action.payload = error; action.error = true; } return action; } var starting = generateActionByStatus('STARTING'); function of$1(type, selector) { var identifier = [type]; if (selector) { identifier.push(selector); } var state = this[identifier.join(':')] || {}; return { loading: state.status === 'STARTING', error: state.error }; } function reducer$1() { var _babelHelpers$extends, _babelHelpers$extends2; var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var action = arguments[1]; if (!action.type || !action.type.startsWith('communication') || !(action.meta && action.meta.status)) { return state; } var identifier = [action.meta.type]; if (action.meta.selector) { identifier.push(action.meta.selector); } switch (action.meta.status) { case 'STARTING': return _extends({}, state, (_babelHelpers$extends = {}, _babelHelpers$extends[identifier.join(':')] = { status: action.meta.status }, _babelHelpers$extends)); case 'DONE': case 'CANCEL': { var omit = state[identifier.join(':')], newState = objectWithoutProperties(state, [identifier.join(':')]); return newState; } case 'FAIL': return _extends({}, state, (_babelHelpers$extends2 = {}, _babelHelpers$extends2[identifier.join(':')] = { status: action.meta.status, error: action.payload }, _babelHelpers$extends2)); default: return state; } } function communication(state, action) { var newState = reducer$1(state, action); newState.of = of$1; return newState; } communication.cancel = cancel; communication.done = done; communication.fail = fail; communication.starting = starting; function guaranteeArray() { var param = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; return Array.isArray(param) ? param : [param]; } function normalize(payload) { var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'id'; return guaranteeArray(payload).reduce(function (obj, element) { var _babelHelpers$extends; var id = element && element[key]; if (!id) { return obj; } return { ids: [].concat(obj.ids, [id]), content: _extends({}, obj.content, (_babelHelpers$extends = {}, _babelHelpers$extends[id] = element, _babelHelpers$extends)) }; }, { ids: [], content: {} }); } exports.communication = communication; exports.entities = entities; exports.normalize = normalize;