2021-04-04 17:07:03 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
|
2021-04-06 04:38:36 +00:00
|
|
|
class Restapi extends React.Component {
|
2021-04-30 06:31:32 +00:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2021-04-30 08:10:57 +00:00
|
|
|
options: this.props.options
|
2021-04-30 06:31:32 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.setState({
|
2021-04-30 08:10:57 +00:00
|
|
|
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({
|
2021-04-30 08:10:57 +00:00
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-30 08:10:57 +00:00
|
|
|
keyValuePairValueChanged = (e, keyIndex, option, index) => {
|
2021-04-30 06:31:32 +00:00
|
|
|
const value = e.target.value;
|
|
|
|
|
const { options } = this.state;
|
|
|
|
|
|
2021-04-30 08:10:57 +00:00
|
|
|
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>
|
2021-04-30 08:10:57 +00:00
|
|
|
<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
|
2021-04-30 08:10:57 +00:00
|
|
|
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>
|
2021-04-30 08:10:57 +00:00
|
|
|
<div className="col">
|
2021-04-30 06:31:32 +00:00
|
|
|
<input
|
|
|
|
|
type="text"
|
2021-04-30 08:10:57 +00:00
|
|
|
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) => (
|
2021-04-30 08:10:57 +00:00
|
|
|
<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) => (
|
2021-04-30 08:10:57 +00:00
|
|
|
<div className="input-group" key={index}>
|
2021-04-30 06:31:32 +00:00
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
value={pair[0]}
|
2021-04-30 08:10:57 +00:00
|
|
|
className="form-control"
|
2021-04-30 06:31:32 +00:00
|
|
|
placeholder="key"
|
2021-04-30 08:10:57 +00:00
|
|
|
autoComplete="off"
|
2021-04-30 06:31:32 +00:00
|
|
|
onChange={(e) => this.keyValuePairValueChanged(e, 0, option, index)}
|
|
|
|
|
/>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
value={pair[1]}
|
2021-04-30 08:10:57 +00:00
|
|
|
className="form-control"
|
2021-04-30 06:31:32 +00:00
|
|
|
placeholder="value"
|
2021-04-30 08:10:57 +00:00
|
|
|
autoComplete="off"
|
2021-04-30 06:31:32 +00:00
|
|
|
onChange={(e) => this.keyValuePairValueChanged(e, 1, option, index)}
|
|
|
|
|
/>
|
|
|
|
|
<span
|
2021-04-30 08:10:57 +00:00
|
|
|
className="input-group-text"
|
2021-04-30 06:31:32 +00:00
|
|
|
role="button"
|
2021-04-30 08:10:57 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-04-06 04:38:36 +00:00
|
|
|
export { Restapi };
|