UNPKG

@tomino/dynamic-form-semantic-ui

Version:

Semantic UI form renderer based on dynamic form generation

115 lines 4.09 kB
import React from 'react'; import { prop, propGroup, handlerProp } from '../editor/editor_common'; import { observer } from 'mobx-react'; import { thumbnails, variables } from './apollo_query.editor'; import { ApolloMutationView } from './apollo_mutation_view'; import gql from 'graphql-tag'; import { MockedProvider } from 'react-apollo/test-utils'; import { Context } from '../context'; import { getValue } from '../helpers'; import { EditorContext } from '../editor/editor_context'; const ApolloMutationEditorComponent = observer((props) => { const controlProps = props.formElement.props; const context = React.useContext(Context); const editorContext = React.useContext(EditorContext); const handler = getValue(props, context, 'clickHandler'); if (editorContext.projectHandlers.indexOf(handler) === -1) { editorContext.projectHandlers.push(handler); } controlProps.mutation; let fakeData = getValue(props, context, 'fakeData'); if (fakeData) { let data; try { data = eval(`(${fakeData.trim()})`); } catch (ex) { return React.createElement("div", null, "Error parsing json: ", ex.message); } const mocks = [ { request: { query: gql([controlProps.mutation]) }, result: { data } } ]; return (React.createElement(MockedProvider, { mocks: mocks, addTypename: false }, React.createElement(ApolloMutationView.Component, Object.assign({}, props)))); } return React.createElement(ApolloMutationView.Component, Object.assign({}, props)); }); export const ApolloMutationEditor = { Component: ApolloMutationEditorComponent, title: 'Apollo Mutation', control: 'ApolloMutation', thumbnail: 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: { ...propGroup('Mutation', { mutation: prop({ control: 'Code', props: { language: 'graphql' }, type: 'string' }) }), ...propGroup('Options', { target: prop({ control: 'Select', props: { options: { handler: 'datasetSource' } } }), clickHandler: 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: prop({ documentation: 'Displays an alert after successful mutation. Requires "AlertProvider"' }), errorAlert: prop({ documentation: 'Displays an alert after failed mutation. Requires "AlertProvider"' }) }), ...variables, ...propGroup('Handlers', { onSubmit: handlerProp(), onResult: handlerProp(), onError: handlerProp() }), ...propGroup('Editor', { fakeData: prop({ control: 'Code', props: { language: 'javascript' } }) }) }, defaultProps: { loadingText: 'Loading ...' } }; //# sourceMappingURL=apollo_mutation_editor.js.map