@tomino/dynamic-form-semantic-ui
Version:
Semantic UI form renderer based on dynamic form generation
118 lines • 4.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const editor_common_1 = require("../editor/editor_common");
const mobx_react_1 = require("mobx-react");
const apollo_query_editor_1 = require("./apollo_query.editor");
const apollo_mutation_view_1 = require("./apollo_mutation_view");
const graphql_tag_1 = tslib_1.__importDefault(require("graphql-tag"));
const test_utils_1 = require("react-apollo/test-utils");
const context_1 = require("../context");
const helpers_1 = require("../helpers");
const editor_context_1 = require("../editor/editor_context");
const ApolloMutationEditorComponent = mobx_react_1.observer((props) => {
const controlProps = props.formElement.props;
const context = react_1.default.useContext(context_1.Context);
const editorContext = react_1.default.useContext(editor_context_1.EditorContext);
const handler = helpers_1.getValue(props, context, 'clickHandler');
if (editorContext.projectHandlers.indexOf(handler) === -1) {
editorContext.projectHandlers.push(handler);
}
controlProps.mutation;
let fakeData = helpers_1.getValue(props, context, 'fakeData');
if (fakeData) {
let data;
try {
data = eval(`(${fakeData.trim()})`);
}
catch (ex) {
return react_1.default.createElement("div", null,
"Error parsing json: ",
ex.message);
}
const mocks = [
{
request: {
query: graphql_tag_1.default([controlProps.mutation])
},
result: {
data
}
}
];
return (react_1.default.createElement(test_utils_1.MockedProvider, { mocks: mocks, addTypename: false },
react_1.default.createElement(apollo_mutation_view_1.ApolloMutationView.Component, Object.assign({}, props))));
}
return react_1.default.createElement(apollo_mutation_view_1.ApolloMutationView.Component, Object.assign({}, props));
});
exports.ApolloMutationEditor = {
Component: ApolloMutationEditorComponent,
title: 'Apollo Mutation',
control: 'ApolloMutation',
thumbnail: apollo_query_editor_1.thumbnails,
provider: true,
group: 'Data',
handlers: {
newHandler({ owner, context, args: { current, previous } }) {
context.projectHandlers.remove(previous);
if (context.projectHandlers.some((s) => s === current)) {
owner.setError('clickHandler', `Handler "${current}" exists`);
return null;
}
else {
context.projectHandlers.push(current);
}
owner.setError('clickHandler', '');
owner.setValue('clickHandler', current);
return current;
}
},
props: {
...editor_common_1.propGroup('Mutation', {
mutation: editor_common_1.prop({
control: 'Code',
props: {
language: 'graphql'
},
type: 'string'
})
}),
...editor_common_1.propGroup('Options', {
target: editor_common_1.prop({
control: 'Select',
props: {
options: { handler: 'datasetSource' }
}
}),
clickHandler: editor_common_1.prop({
documentation: 'Name of the handler that submits this mutation. You can assign this handler to any other component.',
props: {
value: {
source: 'clickHandler',
parse: 'newHandler'
}
}
}),
successAlert: editor_common_1.prop({
documentation: 'Displays an alert after successful mutation. Requires "AlertProvider"'
}),
errorAlert: editor_common_1.prop({
documentation: 'Displays an alert after failed mutation. Requires "AlertProvider"'
})
}),
...apollo_query_editor_1.variables,
...editor_common_1.propGroup('Handlers', {
onSubmit: editor_common_1.handlerProp(),
onResult: editor_common_1.handlerProp(),
onError: editor_common_1.handlerProp()
}),
...editor_common_1.propGroup('Editor', {
fakeData: editor_common_1.prop({ control: 'Code', props: { language: 'javascript' } })
})
},
defaultProps: {
loadingText: 'Loading ...'
}
};
//# sourceMappingURL=apollo_mutation_editor.js.map