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

136 lines
4.1 KiB
React
Raw Normal View History

2021-04-04 17:07:03 +00:00
import React from 'react';
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 = (e, keyIndex, option, index) => {
2021-04-30 06:31:32 +00:00
const value = e.target.value;
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">
<div className="col-auto">
2021-04-30 06:31:32 +00:00
<select
className="form-select"
2021-04-30 06:31:32 +00:00
value={options.method}
onChange={(e) => this.changeOption('method', e.target.value)}
>
<option value="get">GET</option>
<option value="post">POST</option>
<option value="put">PUT</option>
<option value="patch">PATCH</option>
<option value="delete">DELETE</option>
</select>
</div>
<div className="col">
2021-04-30 06:31:32 +00:00
<input
type="text"
className="form-control"
2021-04-30 06:31:32 +00:00
value={options.url}
onChange={(e) => this.changeOption('url', e.target.value)}
placeholder="https://api.example.com/v2/endpoint.json"
/>
</div>
</div>
</div>
{['url_params', 'headers', '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}</label>
2021-04-30 06:31:32 +00:00
</div>
<div className="col-md-10">
{options[option].map((pair, index) => (
<div className="input-group" key={index}>
2021-04-30 06:31:32 +00:00
<input
type="text"
value={pair[0]}
className="form-control"
2021-04-30 06:31:32 +00:00
placeholder="key"
autoComplete="off"
2021-04-30 06:31:32 +00:00
onChange={(e) => this.keyValuePairValueChanged(e, 0, option, index)}
/>
<input
type="text"
value={pair[1]}
className="form-control"
2021-04-30 06:31:32 +00:00
placeholder="value"
autoComplete="off"
2021-04-30 06:31:32 +00:00
onChange={(e) => this.keyValuePairValueChanged(e, 1, option, index)}
/>
<span
className="input-group-text"
2021-04-30 06:31:32 +00:00
role="button"
onClick={() => {
2021-04-30 06:31:32 +00:00
this.removeKeyValuePair(option, index);
}}
>
x
</span>
2021-04-04 17:07:03 +00:00
</div>
2021-04-30 06:31:32 +00:00
))}
<button className="btn btn-outline-primary btn-sm" onClick={() => this.addNewKeyValuePair(option)}>
+ Add new
</button>
</div>
</div>
</div>
))}
</div>
</div>
);
}
2021-04-04 17:07:03 +00:00
}
export { Restapi };