import React from 'react'; import { CodeHinter } from '../../CodeBuilder/CodeHinter'; import 'codemirror/theme/duotone-light.css'; import SelectSearch, { fuzzySearch } from 'react-select-search'; class Restapi extends React.Component { constructor(props) { super(props); this.state = { options: this.props.options }; } componentDidMount() { this.setState({ options: this.props.options }); } 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 = (value, keyIndex, option, index) => { const { options } = this.state; options[option][index][keyIndex] = value; this.setState({ options }); this.props.optionsChanged(options); }; render() { const { options } = this.state; return (
{ this.changeOption('method', value); }} filterOptions={fuzzySearch} placeholder="Method" />
{ this.changeOption('url', value); }} />
{[{name: 'URL parameters', value: 'url_params'},{name: 'Headers', value: 'headers'},{name: 'Body', value: 'body'}].map((option) => (
{(options[option.value] || []).map((pair, index) => (
this.keyValuePairValueChanged(value, 0, option.value, index)} /> this.keyValuePairValueChanged(value, 1, option.value, index)} /> { this.removeKeyValuePair(option.value, index); }} > x
))}
))}
); } } export { Restapi };