import React from 'react'; import PropTypes from 'prop-types'; import { Badge } from 'react-bootstrap'; import AceEditor from 'react-ace'; import 'brace/mode/sql'; import 'brace/mode/json'; import 'brace/mode/html'; import 'brace/mode/markdown'; import 'brace/theme/textmate'; import ModalTrigger from '../../components/ModalTrigger'; import InfoTooltipWithTrigger from '../../components/InfoTooltipWithTrigger'; import Button from '../../components/Button'; import { t } from '../../locales'; const propTypes = { onChange: PropTypes.func, code: PropTypes.string, language: PropTypes.oneOf(['yaml', 'json']), }; const defaultProps = { label: null, description: null, onChange: () => {}, code: '{}', }; export default class TemplateParamsEditor extends React.Component { constructor(props) { super(props); const codeText = props.code || '{}'; this.state = { codeText, parsedJSON: null, isValid: true, }; this.onChange = this.onChange.bind(this); } componentDidMount() { this.onChange(this.state.codeText); } onChange(value) { const codeText = value; let isValid; let parsedJSON = {}; try { parsedJSON = JSON.parse(value); isValid = true; } catch (e) { isValid = false; } this.setState({ parsedJSON, isValid, codeText }); if (isValid) { this.props.onChange(codeText); } else { this.props.onChange('{}'); } } renderDoc() { return (

Assign a set of parameters as JSON below (example: {'{"my_table": "foo"}'}), and they become available in your SQL (example: SELECT * FROM {'{{ my_table }}'} ) by using Jinja templating syntax.

); } renderModalBody() { return (
{this.renderDoc()}
); } render() { const paramCount = this.state.parsedJSON ? Object.keys(this.state.parsedJSON).length : 0; return ( {`${t('parameters')} `} {paramCount > 0 && {paramCount} } {!this.state.isValid && } } modalBody={this.renderModalBody(true)} /> ); } } TemplateParamsEditor.propTypes = propTypes; TemplateParamsEditor.defaultProps = defaultProps;