2016-07-14 22:48:56 +00:00
|
|
|
import React, {PropTypes} from 'react'
|
2016-04-23 06:08:52 +00:00
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
import Link from './base/Link'
|
2016-04-23 06:16:23 +00:00
|
|
|
import Modal from './base/Modal'
|
|
|
|
import ModalBody from './base/ModalBody'
|
|
|
|
import ModalHeader from './base/ModalHeader'
|
|
|
|
import ModalFooter from './base/ModalFooter'
|
|
|
|
import KeyValueEditor from './base/KeyValueEditor'
|
2016-07-14 22:48:56 +00:00
|
|
|
import ModalComponent from './lib/ModalComponent'
|
2016-04-15 05:23:54 +00:00
|
|
|
|
2016-07-14 22:48:56 +00:00
|
|
|
class EnvironmentEditModal extends ModalComponent {
|
2016-07-07 20:10:55 +00:00
|
|
|
constructor (props) {
|
2016-04-15 05:23:54 +00:00
|
|
|
super(props);
|
2016-07-06 20:18:26 +00:00
|
|
|
this.requestGroup = null;
|
2016-04-15 05:23:54 +00:00
|
|
|
this.state = {
|
2016-07-14 22:48:56 +00:00
|
|
|
pairs: [],
|
|
|
|
uniquenessKey: ''
|
2016-04-15 05:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
_saveChanges () {
|
2016-04-17 05:35:19 +00:00
|
|
|
const environment = this._mapPairsToData(this.state.pairs);
|
2016-07-06 20:18:26 +00:00
|
|
|
this.props.onChange(Object.assign({}, this.requestGroup, {environment}));
|
|
|
|
this.hide();
|
2016-04-15 05:23:54 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
_keyValueChange (pairs) {
|
2016-04-15 05:23:54 +00:00
|
|
|
this.setState({pairs});
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
_mapPairsToData (pairs) {
|
2016-04-17 01:52:10 +00:00
|
|
|
return pairs.reduce((prev, curr) => {
|
|
|
|
return Object.assign({}, prev, {[curr.name]: curr.value});
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
_mapDataToPairs (data) {
|
2016-04-17 01:52:10 +00:00
|
|
|
return Object.keys(data).map(key => ({name: key, value: data[key]}));
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
toggle (requestGroup) {
|
2016-07-14 22:48:56 +00:00
|
|
|
super.toggle();
|
2016-04-17 01:52:10 +00:00
|
|
|
|
2016-07-14 22:48:56 +00:00
|
|
|
this.requestGroup = requestGroup;
|
|
|
|
this.setState({
|
|
|
|
pairs: this._mapDataToPairs(requestGroup.environment),
|
|
|
|
uniquenessKey: requestGroup._id
|
|
|
|
})
|
2016-07-06 20:18:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
render () {
|
2016-07-14 22:48:56 +00:00
|
|
|
const {uniquenessKey, pairs} = this.state;
|
2016-07-06 22:11:37 +00:00
|
|
|
|
2016-04-15 05:23:54 +00:00
|
|
|
return (
|
2016-07-07 20:10:55 +00:00
|
|
|
<Modal ref="modal" tall={true} {...this.props}>
|
2016-04-15 05:23:54 +00:00
|
|
|
<ModalHeader>Environment Variables</ModalHeader>
|
2016-05-01 20:46:11 +00:00
|
|
|
<ModalBody>
|
2016-07-06 20:18:26 +00:00
|
|
|
<KeyValueEditor onChange={this._keyValueChange.bind(this)}
|
2016-07-06 22:11:37 +00:00
|
|
|
uniquenessKey={uniquenessKey}
|
|
|
|
pairs={pairs}
|
2016-07-06 20:18:26 +00:00
|
|
|
namePlaceholder="BASE_URL"
|
|
|
|
valuePlaceholder="https://api.insomnia.com/v1"/>
|
2016-04-15 05:23:54 +00:00
|
|
|
</ModalBody>
|
2016-07-07 20:10:55 +00:00
|
|
|
<ModalFooter>
|
2016-07-09 04:49:09 +00:00
|
|
|
<div className="pull-right">
|
|
|
|
<button className="btn" onClick={this._saveChanges.bind(this)}>Save</button>
|
|
|
|
</div>
|
|
|
|
<div className="pad txt-sm">
|
2016-07-07 20:10:55 +00:00
|
|
|
This data can be used for
|
|
|
|
<Link href="https://mozilla.github.io/nunjucks/templating.html">Nunjucks Templating</Link>
|
|
|
|
in your requests.
|
|
|
|
</div>
|
2016-04-15 05:23:54 +00:00
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
EnvironmentEditModal.propTypes = {
|
2016-04-15 05:23:54 +00:00
|
|
|
onChange: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
export default EnvironmentEditModal;
|