insomnia/app/components/EnvironmentEditModal.js

89 lines
2.4 KiB
JavaScript
Raw Normal View History

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
class EnvironmentEditModal extends Component {
2016-07-07 20:10:55 +00:00
constructor (props) {
2016-04-15 05:23:54 +00:00
super(props);
this.requestGroup = null;
2016-04-15 05:23:54 +00:00
this.state = {
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);
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) {
return pairs.reduce((prev, curr) => {
return Object.assign({}, prev, {[curr.name]: curr.value});
}, {});
}
2016-07-07 20:10:55 +00:00
_mapDataToPairs (data) {
return Object.keys(data).map(key => ({name: key, value: data[key]}));
}
2016-07-07 20:10:55 +00:00
toggle (requestGroup) {
this.requestGroup = requestGroup;
this.setState({pairs: this._mapDataToPairs(requestGroup.environment)})
this.refs.modal.toggle();
}
2016-07-07 20:10:55 +00:00
hide () {
this.refs.modal.hide();
}
2016-07-07 20:10:55 +00:00
render () {
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>
<KeyValueEditor onChange={this._keyValueChange.bind(this)}
uniquenessKey={uniquenessKey}
pairs={pairs}
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&nbsp;
<Link href="https://mozilla.github.io/nunjucks/templating.html">Nunjucks Templating</Link>&nbsp;
in your requests.
</div>
2016-04-15 05:23:54 +00:00
</ModalFooter>
</Modal>
);
}
}
EnvironmentEditModal.propTypes = {
uniquenessKey: PropTypes.string.isRequired,
2016-04-15 05:23:54 +00:00
onChange: PropTypes.func.isRequired
};
EnvironmentEditModal.defaultProps = {
2016-04-23 06:08:52 +00:00
id: MODAL_ENVIRONMENT_EDITOR
2016-04-15 05:23:54 +00:00
};
export default EnvironmentEditModal;