2021-04-04 17:07:03 +00:00
|
|
|
import React from 'react';
|
2021-04-18 13:17:55 +00:00
|
|
|
import { Transformation } from './Transformation';
|
2021-04-04 17:07:03 +00:00
|
|
|
|
2021-04-06 04:38:36 +00:00
|
|
|
class Restapi extends React.Component {
|
2021-04-04 17:07:03 +00:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
options: this.props.options,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
2021-04-07 05:02:44 +00:00
|
|
|
this.setState({
|
2021-04-04 17:07:03 +00:00
|
|
|
options: this.props.options,
|
2021-04-07 05:02:44 +00:00
|
|
|
});
|
2021-04-04 17:07:03 +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
|
|
|
|
|
});
|
|
|
|
|
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, key_index, option, index) => {
|
|
|
|
|
const value = e.target.value;
|
|
|
|
|
const { options } = this.state;
|
|
|
|
|
|
|
|
|
|
options[option][index][key_index] = value;
|
|
|
|
|
|
|
|
|
|
this.setState({ options });
|
|
|
|
|
this.props.optionsChanged(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { options } = this.state;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<div class="mb-3 mt-2">
|
|
|
|
|
<div class="mb-3">
|
|
|
|
|
<div class="row g-2">
|
|
|
|
|
<div class="col-auto">
|
2021-04-18 04:33:02 +00:00
|
|
|
<select class="form-select" value={options.method} onChange={(e) => this.changeOption('method', e.target.value)}>
|
2021-04-18 09:29:44 +00:00
|
|
|
<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>
|
2021-04-04 17:07:03 +00:00
|
|
|
</select>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="col">
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
class="form-control"
|
2021-04-18 04:33:02 +00:00
|
|
|
value={options.url}
|
2021-04-04 17:07:03 +00:00
|
|
|
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 class="mb-3">
|
|
|
|
|
<div class="row g-2">
|
|
|
|
|
<div class="col-md-2">
|
2021-04-18 10:53:50 +00:00
|
|
|
<label class="form-label pt-2">{option}</label>
|
2021-04-04 17:07:03 +00:00
|
|
|
</div>
|
|
|
|
|
<div className="col-md-10">
|
|
|
|
|
{options[option].map((pair, index) =>
|
|
|
|
|
<div class="input-group">
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
value={pair[0]}
|
|
|
|
|
class="form-control"
|
|
|
|
|
placeholder="key"
|
|
|
|
|
autocomplete="off"
|
|
|
|
|
onChange={(e) => this.keyValuePairValueChanged(e, 0, option, index)}
|
|
|
|
|
/>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
value={pair[1]}
|
|
|
|
|
class="form-control"
|
|
|
|
|
placeholder="value"
|
|
|
|
|
autocomplete="off"
|
|
|
|
|
onChange={(e) => this.keyValuePairValueChanged(e, 1, option, index)}
|
|
|
|
|
/>
|
|
|
|
|
<span
|
|
|
|
|
class="input-group-text"
|
|
|
|
|
role="button"
|
|
|
|
|
onClick={(e) => { this.removeKeyValuePair(option, index); } }
|
|
|
|
|
>
|
|
|
|
|
x
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<button
|
|
|
|
|
className="btn btn-outline-primary btn-sm"
|
|
|
|
|
onClick={() => this.addNewKeyValuePair(option)}
|
|
|
|
|
>
|
|
|
|
|
+ Add new
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2021-04-18 13:17:55 +00:00
|
|
|
</div>
|
|
|
|
|
<hr></hr>
|
|
|
|
|
<div className="mb-3 mt-2">
|
|
|
|
|
<Transformation
|
|
|
|
|
changeOption={this.changeOption}
|
|
|
|
|
options={options}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2021-04-04 17:07:03 +00:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-06 04:38:36 +00:00
|
|
|
export { Restapi };
|