canner
Version:
Build CMS in few lines of code for different data sources
257 lines (213 loc) • 7.84 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = mutate;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
var _immer = _interopRequireDefault(require("immer"));
var _lodash = require("lodash");
function mutate(originValue, action) {
var _action$payload = action.payload,
key = _action$payload.key,
id = _action$payload.id,
value = _action$payload.value,
path = _action$payload.path,
relation = _action$payload.relation; // $FlowFixMe
return (0, _immer.default)(originValue, function (draft) {
switch (action.type) {
case 'CREATE_ARRAY':
{
if (draft[key].edges) {
// array connection
draft[key].edges.push({
__typename: null,
cursor: value.id,
node: value
});
} else {
draft[key].push(value);
}
break;
}
case 'UPDATE_ARRAY':
{
if (draft[key].edges) {
draft[key].edges = draft[key].edges.map(function (item) {
return item.cursor === id ? {
__typename: item.__typename,
cursor: id,
node: (0, _objectSpread2.default)({}, item.node, value)
} : item;
});
} else {
draft[key] = draft[key].map(function (item) {
return item.id === id ? (0, _objectSpread2.default)({}, item, value) : item;
});
}
break;
}
case 'DELETE_ARRAY':
{
if (draft[key].edges) {
draft[key].edges = draft[key].edges.filter(function (item) {
return item.cursor !== id;
});
} else {
draft[key] = draft[key].filter(function (item) {
return item.id !== id;
});
}
break;
}
case 'UPDATE_OBJECT':
{
draft[key] = (0, _objectSpread2.default)({}, draft[key], value);
break;
}
case 'CONNECT':
{
if (id) {
// array connect
var index = (0, _lodash.findIndex)(draft[key].edges || [], function (item) {
return item.cursor === id;
}); // $FlowFixMe
var relationValue = (0, _lodash.get)(draft, [key, 'edges', index, 'node'].concat(path.split('/')), []);
if (relation && relation.type === 'toOne') {
relationValue = (0, _objectSpread2.default)({}, value, {
__typename: null
});
} else {
var _index = relationValue.findIndex(function (v) {
return v.id === value.id;
});
if (_index === -1) {
relationValue.push((0, _objectSpread2.default)({}, value, {
__typename: null
}));
} else {
relationValue[_index] = (0, _objectSpread2.default)({}, value, {
__typename: null
});
}
} // $FlowFixMe
(0, _lodash.set)(draft, [key, 'edges', index, 'node'].concat(path.split('/')), relationValue);
} else {
if (relation && relation.type === 'toOne') {
// $FlowFixMe
(0, _lodash.set)(draft, [key].concat(path.split('/')), (0, _objectSpread2.default)({}, value, {
__typename: null
}));
} else {
// $FlowFixMe
var _relationValue = (0, _lodash.get)(draft, [key].concat(path.split('/')), []);
_relationValue.push((0, _objectSpread2.default)({}, value, {
__typename: null
})); // $FlowFixMe
(0, _lodash.set)(draft, [key].concat(path.split('/')), _relationValue);
}
}
break;
}
case 'DISCONNECT':
{
if (id) {
var _index2 = (0, _lodash.findIndex)(draft[key].edges || [], function (item) {
return item.cursor === id;
}); // $FlowFixMe
var paths = [key, 'edges', _index2, 'node'].concat(path.split('/'));
if (relation && relation.type === 'toOne') {
(0, _lodash.set)(draft, paths, null);
} else {
var _relationValue2 = (0, _lodash.get)(draft, paths, []);
_relationValue2 = _relationValue2.filter(function (item) {
return item.id !== value.id;
});
(0, _lodash.set)(draft, paths, _relationValue2);
}
} else {
// $FlowFixMe
var _paths = [key].concat(path.split('/'));
if (relation && relation.type === 'toOne') {
(0, _lodash.set)(draft, _paths, null);
} else {
var _relationValue3 = (0, _lodash.get)(draft, _paths, []);
_relationValue3 = _relationValue3.filter(function (item) {
return item.id !== value.id;
});
(0, _lodash.set)(draft, _paths, _relationValue3);
}
}
break;
}
case 'CREATE_AND_CONNECT':
{
if (id) {
var _index3 = (0, _lodash.findIndex)(draft[key].edges || [], function (item) {
return item.cursor === id;
});
if (_index3 === -1) {
throw new Error("Can't find the id in rootValue");
}
var _relationValue4 = draft[key].edges[_index3].node[path] || [];
if (relation && relation.type === 'toOne') {
_relationValue4 = (0, _objectSpread2.default)({}, value, {
__typename: null
});
} else {
if (!_relationValue4.find(function (v) {
return v.id === value.id;
})) {
_relationValue4.push((0, _objectSpread2.default)({}, value, {
__typename: null
}));
}
}
draft[key].edges[_index3].node[path] = _relationValue4;
} else {
if (relation && relation.type === 'toOne') {
draft[key][path] = (0, _objectSpread2.default)({}, value, {
__typename: null
});
} else {
draft[key][path] = draft[key][path] || [];
draft[key][path].push((0, _objectSpread2.default)({}, value, {
__typename: null
}));
}
}
break;
}
case 'DISCONNECT_AND_DELETE':
{
if (id) {
var _index4 = (0, _lodash.findIndex)(draft[key].edges || [], function (item) {
return item.cursor === id;
});
if (_index4 === -1) {
throw new Error("Can't find the id in rootValue");
}
var _relationValue5 = draft[key].edges[_index4].node[path];
if ((0, _lodash.isArray)(_relationValue5)) {
(0, _lodash.remove)(draft[key].edges[_index4].node[path], function (item) {
return item.id === value.id;
});
}
} else {
var _relationValue6 = draft[key][path];
if (!_relationValue6) {
draft[key][path] = [value];
} else if ((0, _lodash.isArray)(_relationValue6)) {
(0, _lodash.remove)(draft[key][path], function (item) {
return item.id === value.id;
});
}
}
break;
}
case 'NOOP':
default:
break;
}
});
}