ToolJet/frontend/src/Editor/QueryManager/QueryEditors/Elasticsearch.jsx

156 lines
4.9 KiB
React
Raw Normal View History

2021-04-28 08:06:14 +00:00
import React from 'react';
import CodeMirror from '@uiw/react-codemirror';
import 'codemirror/theme/duotone-light.css';
import SelectSearch, { fuzzySearch } from 'react-select-search';
2021-05-09 03:17:19 +00:00
import { CodeHinter } from '../../CodeBuilder/CodeHinter';
2021-04-28 08:06:14 +00:00
class Elasticsearch extends React.Component {
2021-04-30 06:31:32 +00:00
constructor(props) {
super(props);
2021-04-28 08:06:14 +00:00
2021-04-30 06:31:32 +00:00
this.state = {
options: this.props.options
2021-04-30 06:31:32 +00:00
};
}
2021-04-28 08:06:14 +00:00
2021-04-30 06:31:32 +00:00
componentDidMount() {
this.setState({
options: this.props.options
2021-04-30 06:31:32 +00:00
});
}
2021-04-28 08:06:14 +00:00
changeOption = (option, value) => {
2021-04-30 06:31:32 +00:00
this.setState(
{
options: {
...this.state.options,
[option]: value
}
2021-04-30 06:31:32 +00:00
},
() => {
this.props.optionsChanged(this.state.options);
}
);
}
2021-04-28 08:06:14 +00:00
2021-04-30 06:31:32 +00:00
changeOperation = (operation) => {
this.setState(
{
options: {
...this.state.options,
operation
}
2021-04-30 06:31:32 +00:00
},
() => {
this.props.optionsChanged(this.state.options);
}
);
};
2021-04-28 08:06:14 +00:00
2021-04-30 06:31:32 +00:00
render() {
const { options } = this.state;
2021-04-28 08:06:14 +00:00
2021-04-30 06:31:32 +00:00
return (
<div>
{options && (
<div>
<div className="mb-3 mt-2">
2021-04-30 06:31:32 +00:00
<label className="form-label">Operation</label>
<SelectSearch
options={[
2021-05-14 12:59:33 +00:00
{ value: 'search', name: 'Search' },
{ value: 'index_document', name: 'Index a document'},
2021-05-14 13:16:50 +00:00
{ value: 'get', name: 'Get a document'},
2021-04-30 06:31:32 +00:00
// { value: 'update', name: 'Update'},
]}
value={this.state.options.operation}
search={false}
onChange={(value) => {
this.changeOperation(value);
}}
filterOptions={fuzzySearch}
placeholder="Select.."
/>
</div>
2021-05-14 12:59:33 +00:00
2021-05-14 13:16:50 +00:00
{options.operation === 'get' && (
<div>
<div className="mb-3 mt-2">
<label className="form-label text-muted">Index</label>
<CodeHinter
currentState={this.props.currentState}
initialValue={this.state.options.index}
onChange={(value) => this.changeOption('index', value)}
/>
</div>
<div className="mb-3 mt-2">
<label className="form-label text-muted">Id</label>
<CodeHinter
currentState={this.props.currentState}
initialValue={this.state.options.id}
onChange={(value) => this.changeOption('id', value)}
/>
</div>
</div>
)}
2021-05-14 12:59:33 +00:00
{options.operation === 'index_document' && (
<div>
<div className="mb-3 mt-2">
<label className="form-label text-muted">Index</label>
<CodeHinter
currentState={this.props.currentState}
initialValue={this.state.options.index}
onChange={(value) => this.changeOption('index', value)}
/>
</div>
<div className="mb-3 mt-2">
<label className="form-label text-reset">Body</label>
<CodeHinter
currentState={this.props.currentState}
initialValue={options.body}
mode="javascript"
placeholder={'{ "name": "The Hitchhikers Guide to the Galaxy" }'}
theme="duotone-light"
lineNumbers={true}
className="query-hinter"
onChange={(value) => this.changeOption('body', value)}
/>
</div>
</div>
)}
2021-04-30 06:31:32 +00:00
{options.operation === 'search' && (
<div>
<div className="mb-3 mt-2">
2021-04-30 06:31:32 +00:00
<label className="form-label text-muted">Index</label>
2021-05-09 03:17:19 +00:00
<CodeHinter
currentState={this.props.currentState}
initialValue={this.state.options.index}
onChange={(value) => this.changeOption('index', value)}
2021-04-30 06:31:32 +00:00
/>
2021-05-09 03:17:19 +00:00
2021-04-30 06:31:32 +00:00
</div>
<div className="mb-3 mt-2">
2021-04-30 06:31:32 +00:00
<label className="form-label text-reset">Query</label>
2021-05-09 04:25:17 +00:00
<CodeHinter
currentState={this.props.currentState}
initialValue={options.query}
mode="sql"
placeholder={'{ "name": "" }'}
2021-05-09 04:25:17 +00:00
theme="duotone-light"
lineNumbers={true}
className="query-hinter"
onChange={(value) => this.changeOption('query', value)}
2021-04-30 06:31:32 +00:00
/>
</div>
</div>
)}
</div>
)}
</div>
);
}
2021-04-28 08:06:14 +00:00
}
export { Elasticsearch };