2016-04-15 05:23:54 +00:00
|
|
|
import React, {Component, PropTypes} from 'react'
|
|
|
|
import Modal from '../base/Modal'
|
|
|
|
import ModalBody from '../base/ModalBody'
|
|
|
|
import ModalHeader from '../base/ModalHeader'
|
|
|
|
import ModalFooter from '../base/ModalFooter'
|
|
|
|
import Editor from '../base/Editor'
|
|
|
|
import KeyValueEditor from '../base/KeyValueEditor'
|
|
|
|
import * as modalIds from '../../constants/modals'
|
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
class EnvironmentEditModal extends Component {
|
2016-04-15 05:23:54 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2016-04-17 01:52:10 +00:00
|
|
|
pairs: this._mapDataToPairs(props.requestGroup.environment)
|
2016-04-15 05:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_saveChanges () {
|
2016-04-17 01:52:10 +00:00
|
|
|
this.props.onChange(this._mapPairsToData(this.state.pairs));
|
|
|
|
this.props.onClose();
|
2016-04-15 05:23:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_keyValueChange (pairs) {
|
|
|
|
this.setState({pairs});
|
|
|
|
}
|
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
_mapPairsToData (pairs) {
|
|
|
|
return pairs.reduce((prev, curr) => {
|
|
|
|
return Object.assign({}, prev, {[curr.name]: curr.value});
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
_mapDataToPairs (data) {
|
|
|
|
return Object.keys(data).map(key => ({name: key, value: data[key]}));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
this.setState({
|
|
|
|
pairs: this._mapDataToPairs(nextProps.requestGroup.environment)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-15 05:23:54 +00:00
|
|
|
render () {
|
|
|
|
const editorOptions = {
|
|
|
|
mode: 'application/json',
|
|
|
|
placeholder: '{ "array": [1, 2, 3, 4] }',
|
|
|
|
theme: 'neat'
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal {...this.props}>
|
|
|
|
<ModalHeader>Environment Variables</ModalHeader>
|
|
|
|
<ModalBody className="grid--v wide pad">
|
|
|
|
<div>
|
|
|
|
<KeyValueEditor onChange={this._keyValueChange.bind(this)}
|
2016-04-17 01:52:10 +00:00
|
|
|
uniquenessKey={this.props.requestGroup._id}
|
2016-04-15 05:23:54 +00:00
|
|
|
pairs={this.state.pairs}
|
|
|
|
namePlaceholder="BASE_URL"
|
|
|
|
valuePlaceholder="https://api.insomnia.com/v1"/>
|
|
|
|
</div>
|
|
|
|
{/*
|
|
|
|
<h3>Hello</h3>
|
|
|
|
<Editor value={undefined} options={editorOptions}/>
|
|
|
|
*/}
|
|
|
|
</ModalBody>
|
|
|
|
<ModalFooter className="text-right">
|
|
|
|
<button className="btn" onClick={this._saveChanges.bind(this)}>Done</button>
|
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
EnvironmentEditModal.propTypes = {
|
|
|
|
requestGroup: PropTypes.shape({
|
|
|
|
environment: PropTypes.object.isRequired
|
|
|
|
}),
|
2016-04-15 05:23:54 +00:00
|
|
|
onChange: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
EnvironmentEditModal.defaultProps = {
|
2016-04-15 05:23:54 +00:00
|
|
|
id: modalIds.ENVIRONMENT_EDITOR
|
|
|
|
};
|
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
export default EnvironmentEditModal;
|