insomnia/app/components/modals/EnvironmentEditModal.js

86 lines
2.1 KiB
JavaScript
Raw Normal View History

import React, {PropTypes, Component} from 'react';
2016-04-23 06:08:52 +00:00
import EnvironmentEditor from '../editors/EnvironmentEditor';
import Modal from '../base/Modal';
import ModalBody from '../base/ModalBody';
import ModalHeader from '../base/ModalHeader';
import ModalFooter from '../base/ModalFooter';
2016-04-15 05:23:54 +00:00
2016-07-15 00:26:04 +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-04-15 05:23:54 +00:00
this.state = {
requestGroup: null,
isValid: true
2016-04-15 05:23:54 +00:00
}
}
2016-07-07 20:10:55 +00:00
_saveChanges () {
if (!this._envEditor.isValid()) {
return;
}
2016-04-15 05:23:54 +00:00
const environment = this._envEditor.getValue();
const {requestGroup} = this.state;
this.props.onChange(Object.assign({}, requestGroup, {environment}));
}
_didChange () {
this._saveChanges();
const isValid = this._envEditor.isValid();
if (this.state.isValid !== isValid) {
this.setState({isValid});
}
}
2016-07-15 00:26:04 +00:00
show (requestGroup) {
this.modal.show();
this.setState({requestGroup});
2016-07-15 00:26:04 +00:00
}
toggle (requestGroup) {
this.modal.toggle();
this.setState({requestGroup});
2016-07-15 00:26:04 +00:00
}
2016-07-07 20:10:55 +00:00
render () {
const {requestGroup, isValid} = this.state;
2016-04-15 05:23:54 +00:00
return (
<Modal ref={m => this.modal = m} tall={true} top={true} {...this.props}>
<ModalHeader>Environment Overrides (JSON Format)</ModalHeader>
<ModalBody noScroll={true}>
<EnvironmentEditor
ref={node => this._envEditor = node}
key={requestGroup ? requestGroup._id : 'n/a'}
environment={requestGroup ? requestGroup.environment : {}}
didChange={this._didChange.bind(this)}
lightTheme={true}
/>
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" disabled={!isValid} onClick={e => this.modal.hide()}>
Done
</button>
2016-07-09 04:49:09 +00:00
</div>
<div className="pad faint italic txt-sm tall">
* this can be used to override data in the global environment
2016-07-07 20:10:55 +00:00
</div>
2016-04-15 05:23:54 +00:00
</ModalFooter>
</Modal>
);
}
}
EnvironmentEditModal.propTypes = {
2016-04-15 05:23:54 +00:00
onChange: PropTypes.func.isRequired
};
export default EnvironmentEditModal;