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

140 lines
4.5 KiB
React
Raw Normal View History

2021-04-04 17:07:03 +00:00
import React from 'react';
import { CodeHinter } from '../../CodeBuilder/CodeHinter';
import 'codemirror/theme/duotone-light.css';
2021-05-03 12:19:19 +00:00
import SelectSearch, { fuzzySearch } from 'react-select-search';
2021-04-04 17:07:03 +00:00
2021-05-08 11:25:54 +00:00
class Restapi extends React.Component {
2021-04-30 06:31:32 +00:00
constructor(props) {
super(props);
this.state = {
options: this.props.options
2021-04-30 06:31:32 +00:00
};
}
componentDidMount() {
this.setState({
options: this.props.options
2021-04-30 06:31:32 +00:00
});
}
changeOption = (option, value) => {
const { options } = this.state;
const newOptions = { ...options, [option]: value };
this.setState({ options: newOptions });
this.props.optionsChanged(newOptions);
};
addNewKeyValuePair = (option) => {
const { options } = this.state;
const newOptions = { ...options, [option]: [...options[option], ['', '']] };
this.setState({
options: newOptions
2021-04-30 06:31:32 +00:00
});
this.props.optionsChanged(newOptions);
};
removeKeyValuePair = (option, index) => {
const { options } = this.state;
options[option].splice(index, 1);
this.setState({ options });
this.props.optionsChanged(options);
};
keyValuePairValueChanged = (value, keyIndex, option, index) => {
2021-04-30 06:31:32 +00:00
const { options } = this.state;
options[option][index][keyIndex] = value;
2021-04-30 06:31:32 +00:00
this.setState({ options });
this.props.optionsChanged(options);
};
render() {
const { options } = this.state;
return (
<div>
<div className="mb-3 mt-2">
<div className="mb-3">
<div className="row g-2">
2021-05-08 11:25:54 +00:00
<div className="col-auto" style={{ width: '120px' }} >
2021-05-03 12:19:19 +00:00
<SelectSearch
options={[
{ name: 'GET', value: 'get' },
{ name: 'POST', value: 'post' },
{ name: 'PUT', value: 'put' },
{ name: 'PATCH', value: 'patch' },
{ name: 'DELETE', value: 'delete' }
]}
2021-04-30 06:31:32 +00:00
value={options.method}
2021-05-03 12:19:19 +00:00
search={false}
closeOnSelect={true}
onChange={(value) => {
this.changeOption('method', value);
}}
filterOptions={fuzzySearch}
placeholder="Method"
/>
2021-04-30 06:31:32 +00:00
</div>
2021-05-07 18:13:11 +00:00
<div className="col">
<CodeHinter
currentState={this.props.currentState}
initialValue={options.url}
2021-05-09 05:25:00 +00:00
className="codehinter-query-editor-input"
2021-05-08 11:25:54 +00:00
onChange={(value) => { this.changeOption('url', value); }}
2021-04-30 06:31:32 +00:00
/>
</div>
</div>
</div>
{[{name: 'URL parameters', value: 'url_params'},{name: 'Headers', value: 'headers'},{name: 'Body', value: 'body'}].map((option) => (
<div className="mb-3" key={option}>
<div className="row g-2">
<div className="col-md-2">
<label className="form-label pt-2">{option.name}</label>
2021-04-30 06:31:32 +00:00
</div>
<div className="col-md-10">
{(options[option.value] || []).map((pair, index) => (
<div className="input-group" key={index}>
<CodeHinter
currentState={this.props.currentState}
initialValue={pair[0]}
2021-05-09 05:25:00 +00:00
className="form-control codehinter-query-editor-input"
onChange={(value) => this.keyValuePairValueChanged(value, 0, option.value, index)}
2021-04-30 06:31:32 +00:00
/>
<CodeHinter
currentState={this.props.currentState}
2021-05-09 05:25:00 +00:00
className="form-control codehinter-query-editor-input"
initialValue={pair[1]}
onChange={(value) => this.keyValuePairValueChanged(value, 1, option.value, index)}
2021-04-30 06:31:32 +00:00
/>
<span
2021-05-09 05:25:00 +00:00
className="input-group-text btn-sm"
2021-04-30 06:31:32 +00:00
role="button"
onClick={() => {
this.removeKeyValuePair(option.value, index);
2021-04-30 06:31:32 +00:00
}}
>
x
</span>
2021-04-04 17:07:03 +00:00
</div>
2021-04-30 06:31:32 +00:00
))}
<button className="btn btn-sm btn-outline-azure" onClick={() => this.addNewKeyValuePair(option.value)}>
+
2021-04-30 06:31:32 +00:00
</button>
</div>
</div>
</div>
))}
</div>
</div>
);
}
2021-04-04 17:07:03 +00:00
}
2021-05-08 11:25:54 +00:00
export { Restapi };