2016-04-15 05:23:54 +00:00
|
|
|
import React, {Component, 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'
|
|
|
|
import {MODAL_ENVIRONMENT_EDITOR} from '../lib/constants'
|
2016-04-15 05:23:54 +00:00
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
class EnvironmentEditModal extends Component {
|
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-06 20:18:26 +00:00
|
|
|
pairs: []
|
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-06 20:18:26 +00:00
|
|
|
this.requestGroup = requestGroup;
|
|
|
|
this.setState({pairs: this._mapDataToPairs(requestGroup.environment)})
|
|
|
|
this.refs.modal.toggle();
|
2016-04-17 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
hide () {
|
2016-07-06 20:18:26 +00:00
|
|
|
this.refs.modal.hide();
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
render () {
|
2016-07-06 22:11:37 +00:00
|
|
|
const {uniquenessKey} = this.props;
|
|
|
|
const {pairs} = this.state;
|
|
|
|
|
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>
|
|
|
|
<button className="btn pull-right" onClick={this._saveChanges.bind(this)}>
|
2016-07-06 22:11:37 +00:00
|
|
|
Done
|
|
|
|
</button>
|
2016-07-07 20:10:55 +00:00
|
|
|
<div className="pad">
|
|
|
|
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-07-06 20:18:26 +00:00
|
|
|
uniquenessKey: PropTypes.string.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-23 06:08:52 +00:00
|
|
|
id: MODAL_ENVIRONMENT_EDITOR
|
2016-04-15 05:23:54 +00:00
|
|
|
};
|
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
export default EnvironmentEditModal;
|