UNPKG

canner

Version:

Build CMS in few lines of code for different data sources

377 lines (318 loc) 9.12 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.generateAction = generateAction; var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray")); var _lodash = require("lodash"); var _isAction = require("./isAction"); function generateAction(arg) { if ((0, _isAction.isCreateArray)(arg)) { var _splitId = splitId(arg.id, arg.rootValue), key = _splitId.key; return { type: 'CREATE_ARRAY', payload: { key: key, id: arg.value.id, value: arg.value } }; } if ((0, _isAction.isCreateNestedArrayInArray)(arg)) { var _splitId2 = splitId(arg.id, arg.rootValue), _key = _splitId2.key, id = _splitId2.id, index = _splitId2.index, paths = _splitId2.paths; var item = (0, _lodash.get)(arg.rootValue, [_key, index]); (0, _lodash.update)(item, paths, function (arr) { return arr.concat(arg.value); }); return { type: 'UPDATE_ARRAY', payload: { key: _key, id: id, value: (0, _lodash.pick)(item, paths.slice(0, 1)) } }; } if ((0, _isAction.isCreateNestedArrayInObject)(arg)) { var _splitId3 = splitId(arg.id, arg.rootValue), _key2 = _splitId3.key, _id = _splitId3.id, _paths = _splitId3.paths; var _item = (0, _lodash.get)(arg.rootValue, [_key2]); (0, _lodash.update)(_item, _paths, function (arr) { return arr ? arr.concat(arg.value) : [arg.value]; }); return { type: 'UPDATE_OBJECT', payload: { key: _key2, id: _id, value: (0, _lodash.pick)(_item, _paths.slice(0, 1)) } }; } if ((0, _isAction.isCreateAndConnect)(arg)) { var _splitId4 = splitId(arg.id, arg.rootValue), _key3 = _splitId4.key, _id2 = _splitId4.id, path = _splitId4.path; return { type: 'CREATE_AND_CONNECT', payload: { key: _key3, id: _id2, path: path, value: arg.value, relation: arg.relation } }; } if ((0, _isAction.isDeleteArray)(arg)) { var _splitId5 = splitId(arg.id, arg.rootValue), _key4 = _splitId5.key, _id3 = _splitId5.id; return { type: 'DELETE_ARRAY', payload: { key: _key4, id: _id3, value: {} } }; } if ((0, _isAction.isDeleteNestedArrayInArray)(arg)) { var _splitId6 = splitId(arg.id, arg.rootValue), _key5 = _splitId6.key, _id4 = _splitId6.id, _index = _splitId6.index, _paths2 = _splitId6.paths; var prePaths = _paths2.slice(0, -1); var deleteIndex = _paths2.slice(-1)[0]; var _item2 = (0, _lodash.get)(arg.rootValue, [_key5, _index]); (0, _lodash.update)(_item2, prePaths, function (arr) { arr.splice(deleteIndex, 1); return arr; }); return { type: 'UPDATE_ARRAY', payload: { key: _key5, id: _id4, value: (0, _lodash.pick)(_item2, _paths2.slice(0, 1)) } }; } if ((0, _isAction.isDeleteNestedArrayInObject)(arg)) { var _splitId7 = splitId(arg.id, arg.rootValue), _key6 = _splitId7.key, _id5 = _splitId7.id, _paths3 = _splitId7.paths; var _prePaths = _paths3.slice(0, -1); var _deleteIndex = _paths3.slice(-1)[0]; var _item3 = (0, _lodash.get)(arg.rootValue, [_key6]); (0, _lodash.update)(_item3, _prePaths, function (arr) { arr.splice(_deleteIndex, 1); return arr; }); return { type: 'UPDATE_OBJECT', payload: { key: _key6, id: _id5, value: (0, _lodash.pick)(_item3, _paths3.slice(0, 1)) } }; } if ((0, _isAction.isDisconnectAndDelete)(arg)) { var _splitId8 = splitId(arg.id, arg.rootValue), _key7 = _splitId8.key, _id6 = _splitId8.id, _path = _splitId8.path; return { type: 'DISCONNECT_AND_DELETE', payload: { key: _key7, id: _id6, path: _path, value: arg.value, relation: arg.relation } }; } if ((0, _isAction.isUpdateArray)(arg)) { var _splitId9 = splitId(arg.id, arg.rootValue), _key8 = _splitId9.key, _id7 = _splitId9.id, _paths4 = _splitId9.paths, _index2 = _splitId9.index; var _item4 = (0, _lodash.get)(arg.rootValue, [_key8, _index2]); (0, _lodash.set)(_item4, _paths4, arg.value); return { type: 'UPDATE_ARRAY', payload: { key: _key8, id: _id7, value: (0, _lodash.pick)(_item4, _paths4.slice(0, 1)) } }; } if ((0, _isAction.isUpdateConnect)(arg)) { /** * unsupport action */ } if ((0, _isAction.isUpdateObject)(arg)) { var _splitId10 = splitId(arg.id, arg.rootValue), _key9 = _splitId10.key, _id8 = _splitId10.id, _paths5 = _splitId10.paths; var _item5 = (0, _lodash.get)(arg.rootValue, _key9); (0, _lodash.set)(_item5, _paths5, arg.value); return { type: 'UPDATE_OBJECT', payload: { key: _key9, id: _id8, value: (0, _lodash.pick)(_item5, _paths5.slice(0, 1)) } }; } if ((0, _isAction.isSwapRootArray)(arg)) { /** * Swap root array is not allowed, */ } if ((0, _isAction.isSwapArrayInArray)(arg)) { var _splitId11 = splitId(arg.id, arg.rootValue), _key10 = _splitId11.key, _id9 = _splitId11.id, _index3 = _splitId11.index, _paths6 = _splitId11.paths; // $FlowFixMe var _arg$id = arg.id, firstId = _arg$id.firstId, secondId = _arg$id.secondId; var nestedArrPath = _paths6.slice(0, -1); var _item6 = (0, _lodash.get)(arg.rootValue, [_key10, _index3]); var firstIndex = firstId.split('/').slice(-1)[0]; var secondIndex = secondId.split('/').slice(-1)[0]; (0, _lodash.update)(_item6, nestedArrPath, function (arr) { var newArr = arr.slice(); newArr[firstIndex] = arr[secondIndex]; newArr[secondIndex] = arr[firstIndex]; return newArr; }); return { type: 'UPDATE_ARRAY', payload: { key: _key10, id: _id9, value: (0, _lodash.pick)(_item6, _paths6.slice(0, 1)) } }; } if ((0, _isAction.isSwapArrayInObject)(arg)) { var _splitId12 = splitId(arg.id, arg.rootValue), _key11 = _splitId12.key, _id10 = _splitId12.id, _paths7 = _splitId12.paths; // $FlowFixMe var _arg$id2 = arg.id, _firstId = _arg$id2.firstId, _secondId = _arg$id2.secondId; var _nestedArrPath = _paths7.slice(0, -1); var _item7 = (0, _lodash.get)(arg.rootValue, [_key11]); var _firstIndex = _firstId.split('/').slice(-1)[0]; var _secondIndex = _secondId.split('/').slice(-1)[0]; (0, _lodash.update)(_item7, _nestedArrPath, function (arr) { var newArr = arr.slice(); newArr[_firstIndex] = arr[_secondIndex]; newArr[_secondIndex] = arr[_firstIndex]; return newArr; }); return { type: 'UPDATE_OBJECT', payload: { key: _key11, id: _id10, value: (0, _lodash.pick)(_item7, _paths7.slice(0, 1)) } }; } if ((0, _isAction.isConnect)(arg)) { var _splitId13 = splitId(arg.id, arg.rootValue), _key12 = _splitId13.key, _id11 = _splitId13.id, _path2 = _splitId13.path; return { type: 'CONNECT', payload: { key: _key12, id: _id11, path: _path2, value: arg.value, relation: arg.relation, transformGqlPayload: arg.transformGqlPayload } }; } if ((0, _isAction.isDisconnect)(arg)) { var _splitId14 = splitId(arg.id, arg.rootValue), _key13 = _splitId14.key, _id12 = _splitId14.id, _path3 = _splitId14.path; return { type: 'DISCONNECT', payload: { key: _key13, id: _id12, path: _path3, value: arg.value, relation: arg.relation } }; } return { type: 'NOOP', payload: { key: '', value: {} } }; } function splitId(id, rootValue) { if (typeof id === 'string') { if ((0, _isAction.isArrayAction)(rootValue, id)) { var _id$split = id.split('/'), _id$split2 = (0, _toArray2.default)(_id$split), key = _id$split2[0], index = _id$split2[1], path = _id$split2.slice(2); var item = rootValue[key][index]; return { key: key, id: item && item.id, index: index, paths: path, path: path.join('/') }; } else { var _id$split3 = id.split('/'), _id$split4 = (0, _toArray2.default)(_id$split3), _key14 = _id$split4[0], _path4 = _id$split4.slice(1); return { key: _key14, path: _path4.join('/'), paths: _path4, id: '', index: '' }; } } return splitId(id.firstId, rootValue); }