UNPKG

canner

Version:

Build CMS in few lines of code for different data sources

221 lines (175 loc) 6.89 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = withRequest; exports.createAction = createAction; exports.findSchemaByRefId = findSchemaByRefId; exports.loop = loop; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf3 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var React = _interopRequireWildcard(require("react")); var _action = require("../action"); var _cannerHelpers = require("canner-helpers"); var _cannerRefId = _interopRequireDefault(require("canner-ref-id")); var _lodash = require("lodash"); function withRequest(Com) { // this hoc will update data; return ( /*#__PURE__*/ function (_React$Component) { (0, _inherits2.default)(ComponentWithRequest, _React$Component); function ComponentWithRequest() { var _getPrototypeOf2; var _this; (0, _classCallCheck2.default)(this, ComponentWithRequest); for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) { _args[_key] = arguments[_key]; } _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(ComponentWithRequest)).call.apply(_getPrototypeOf2, [this].concat(_args))); (0, _defineProperty2.default)((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)), "onChange", function (refId, type, delta, config, transformGqlPayload) { var id; if ((0, _lodash.isArray)(refId)) { // changeQueue var _changeQueue = refId; // $FlowFixMe return Promise.all(_changeQueue.map(function (args) { var refId = args.refId, type = args.type, value = args.value, config = args.config, transformGqlPayload = args.transformGqlPayload; return _this.onChange(refId, type, value, config, transformGqlPayload); })); } else if (refId instanceof _cannerRefId.default) { id = refId.toString(); } else { id = { // $FlowFixMe: refId should be object here firstId: refId.firstRefId.toString(), // $FlowFixMe: refId should be object here secondId: refId.secondRefId.toString() }; } var _this$props = _this.props, schema = _this$props.schema, rootValue = _this$props.rootValue, request = _this$props.request; var itemSchema = findSchemaByRefId(schema, refId); var relation = itemSchema.relation, items = itemSchema.items, pattern = itemSchema.pattern; var action = createAction({ relation: relation, id: id, type: type, value: delta, config: config, rootValue: (0, _objectSpread2.default)({}, rootValue), items: items, pattern: pattern, transformGqlPayload: transformGqlPayload }); if (!action) { throw new Error('invalid change'); } if (action.type === 'NOOP') { return Promise.resolve(); } // $FlowFixMe return request(action); }); return _this; } (0, _createClass2.default)(ComponentWithRequest, [{ key: "render", value: function render() { return React.createElement(Com, (0, _extends2.default)({}, this.props, { onChange: this.onChange })); } }]); return ComponentWithRequest; }(React.Component) ); } function createAction(_ref) { var relation = _ref.relation, id = _ref.id, type = _ref.type, value = _ref.value, config = _ref.config, rootValue = _ref.rootValue, items = _ref.items, transformGqlPayload = _ref.transformGqlPayload; if (type === 'create') { if (!config) { var emptyData = (0, _cannerHelpers.createEmptyData)(items); if ((0, _lodash.isPlainObject)(emptyData)) { value = (0, _lodash.merge)(emptyData, value); } else { value = emptyData; } } if (typeof id === 'string' && id.split('/').length === 1) { (0, _lodash.update)(value, 'id', function (id) { return id || randomId(); }); } } return (0, _action.generateAction)({ id: id, updateType: type, value: value, rootValue: rootValue, relation: relation, transformGqlPayload: transformGqlPayload }); } function randomId() { return Math.random().toString(36).substr(2, 12); } function findSchemaByRefId(schema, refId) { var paths = []; if ((0, _lodash.isPlainObject)(refId)) { paths = refId.firstRefId.getPathArr(); } else { paths = refId.getPathArr(); } var pattern = ''; return loop(schema, paths, pattern); } function loop(schema, paths, pattern) { if (paths.length === 0) { return (0, _objectSpread2.default)({}, schema, { pattern: removeFirstSlash(pattern) }); } if (!schema.type) { // in items of object return loop(schema[paths[0]], paths.slice(1), "".concat(pattern, "/").concat(schema[paths[0]].type)); } if (schema.type === 'json') { return {}; } if (schema.type === 'array') { if (paths.length === 1) { // paths = [index], so just return return loop(schema, [], pattern); } else if (schema.items.type === 'object') { // path[0] is index, so we take the paths[1] return loop(schema.items.items[paths[1]], paths.slice(2), "".concat(pattern, "/").concat(schema.items.items[paths[1]].type)); } } return loop(schema.items[paths[0]], paths.slice(1), "".concat(pattern, "/").concat(schema.items[paths[0]].type)); } function removeFirstSlash(pattern) { return pattern.split('/').slice(1).join('/'); }