@kineticdata/react
Version:
A React library for the Kinetic Platform
210 lines (209 loc) • 7.95 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.BridgeModelForm = void 0;
var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/regeneratorRuntime"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/asyncToGenerator"));
var _Form = require("../../form/Form");
var _immutable = require("immutable");
var _apis = require("../../../apis");
var getMapping = function getMapping(model) {
return model && model.get('mappings') && model.get('mappings').find(function (mapping) {
return mapping.get('name') === model.get('activeMappingName');
}) || {};
};
var dataSources = function dataSources(_ref) {
var modelName = _ref.modelName;
return {
model: {
fn: _apis.fetchBridgeModel,
params: modelName && [{
modelName: modelName,
include: 'details'
}],
transform: function transform(result) {
return result.bridgeModel;
}
},
modelMapping: {
fn: getMapping,
params: function params(_ref2) {
var model = _ref2.model;
return modelName && model && [model];
}
},
agents: {
fn: _apis.fetchAgentComponents,
params: [],
transform: function transform(result) {
return result.agents;
}
},
bridges: {
fn: _apis.fetchBridges,
params: function params(_ref3) {
var values = _ref3.values;
return values && [{
agentSlug: values.get('agentSlug', 'system')
}];
},
transform: function transform(result) {
return result.bridges;
}
}
};
};
var handleSubmit = function handleSubmit(_ref4) {
var modelName = _ref4.modelName;
return /*#__PURE__*/function () {
var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/(0, _regeneratorRuntime2["default"])().mark(function _callee(values, _ref5) {
var modelMapping, _values$toJS, name, status, bridgeSlug, agentSlug, structure, modelMethod, mappingName, mappingMethod, _yield$modelMethod, bridgeModel, modelError, _yield$mappingMethod, mappingError;
return (0, _regeneratorRuntime2["default"])().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
modelMapping = _ref5.modelMapping;
// destructure the values of the fields because they will be used in different
// payloads below
_values$toJS = values.toJS(), name = _values$toJS.name, status = _values$toJS.status, bridgeSlug = _values$toJS.bridgeSlug, agentSlug = _values$toJS.agentSlug, structure = _values$toJS.structure; // determine if we are creating / updating a model
modelMethod = modelName ? _apis.updateBridgeModel : _apis.createBridgeModel; // determine if we are creating / updating a model mapping, due to invalid
// states its possible that we can be updating a model but creating a mapping
mappingName = modelMapping && modelMapping.get('name');
mappingMethod = mappingName ? _apis.updateBridgeModelMapping : _apis.createBridgeModelMapping;
_context.next = 7;
return modelMethod({
modelName: modelName,
bridgeModel: {
name: name,
status: status,
activeMappingName: name
}
});
case 7:
_yield$modelMethod = _context.sent;
bridgeModel = _yield$modelMethod.bridgeModel;
modelError = _yield$modelMethod.error;
if (!modelError) {
_context.next = 12;
break;
}
throw modelError.statusCode === 400 && modelError.message || 'There was an error saving the model';
case 12:
_context.next = 14;
return mappingMethod({
modelName: name,
mappingName: mappingName,
bridgeModelMapping: {
name: name,
bridgeSlug: bridgeSlug,
agentSlug: agentSlug,
structure: structure
}
});
case 14:
_yield$mappingMethod = _context.sent;
mappingError = _yield$mappingMethod.error;
if (!mappingError) {
_context.next = 18;
break;
}
throw mappingError.statusCode === 400 && mappingError.message || 'There was an error saving the model mapping';
case 18:
return _context.abrupt("return", bridgeModel);
case 19:
case "end":
return _context.stop();
}
}, _callee);
}));
return function (_x, _x2) {
return _ref6.apply(this, arguments);
};
}();
};
var fields = function fields(_ref7) {
var modelName = _ref7.modelName;
return function (_ref8) {
var model = _ref8.model,
modelMapping = _ref8.modelMapping,
agents = _ref8.agents;
return (!modelName || model && modelMapping && agents) && [{
name: 'name',
label: 'Name',
type: 'text',
required: true,
initialValue: model ? model.get('name') : '',
helpText: 'User friendly name for the bridge model.'
}, {
name: 'status',
label: 'Status',
type: 'radio',
initialValue: model ? model.get('status') : 'Active',
options: [{
label: 'Active',
value: 'Active'
}, {
label: 'Inactive',
value: 'Inactive'
}]
}, {
name: 'bridgeSlug',
label: 'Bridge Slug',
type: 'select',
required: true,
initialValue: modelMapping ? modelMapping.get('bridgeSlug') : '',
helpText: 'Unique name of the bridge to connect with this model. Bridges are configured under Plugins',
options: function options(_ref9) {
var bridges = _ref9.bridges;
return bridges ? bridges.map(function (bridge) {
return (0, _immutable.Map)({
value: bridge.get('slug'),
label: bridge.get('slug')
});
}) : (0, _immutable.List)();
}
}, {
name: 'agentSlug',
label: 'Agent Slug',
type: 'select',
required: true,
// We want to show the agents field if agents are defined within the space OR if
// the agentSlug that a modelMapping is defined with no longer exists
visible: function visible(_ref10) {
var agents = _ref10.agents;
return agents && (agents.size > 0 || modelMapping && modelMapping.get('agentSlug') !== 'system' && agents.filter(function (a) {
return a.get('slug') === modelMapping.get('agentSlug');
}).size === 0);
},
initialValue: modelMapping ? modelMapping.get('agentSlug', 'system') : 'system',
helpText: 'Unique name of the agent this bridge model is connected to. Bridges are configured under Plugins',
options: function options(_ref11) {
var agents = _ref11.agents;
return agents ? agents.map(function (agent) {
return (0, _immutable.Map)({
value: agent.get('slug'),
label: agent.get('slug')
});
}).unshift((0, _immutable.Map)({
value: 'system',
label: 'system'
})) : (0, _immutable.List)();
}
}, {
name: 'structure',
label: 'Structure',
type: 'text',
required: true,
initialValue: modelMapping ? modelMapping.get('structure') : '',
helpText: "Structures vary depending on the system. They can be table names, forms, domains or other elements."
}];
};
};
var BridgeModelForm = exports.BridgeModelForm = (0, _Form.generateForm)({
formOptions: ['modelName'],
dataSources: dataSources,
fields: fields,
handleSubmit: handleSubmit
});
BridgeModelForm.displayName = 'BridgeModelForm';