insomnia/app/components/EnvironmentEditModal.js

94 lines
2.3 KiB
JavaScript
Raw Normal View History

import React, {PropTypes} from 'react';
2016-04-23 06:08:52 +00:00
import Link from './base/Link';
import Editor from './base/Editor';
import Modal from './base/Modal';
import ModalBody from './base/ModalBody';
import ModalHeader from './base/ModalHeader';
import ModalFooter from './base/ModalFooter';
import ModalComponent from './lib/ModalComponent';
2016-04-15 05:23:54 +00:00
2016-07-15 00:26:04 +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-04-15 05:23:54 +00:00
this.state = {
environmentJSON: '{}',
requestGroup: null
2016-04-15 05:23:54 +00:00
}
}
2016-07-07 20:10:55 +00:00
_saveChanges () {
const {requestGroup, environmentJSON} = this.state;
2016-04-15 05:23:54 +00:00
let environment;
try {
environment = JSON.parse(environmentJSON);
} catch (e) {
// That's OK. The user will (hopefully) fix the problem
console.warn('Failed to parse environment JSON', e);
return;
}
2016-04-15 05:23:54 +00:00
this.props.onChange(Object.assign({}, requestGroup, {environment}));
this.hide();
}
_handleChange (environmentJSON) {
this.setState({environmentJSON});
}
2016-07-15 00:26:04 +00:00
_update (requestGroup) {
const environmentJSON = JSON.stringify(requestGroup.environment, null, '\t');
this.setState({environmentJSON, requestGroup});
}
2016-07-15 00:26:04 +00:00
show (requestGroup) {
super.show();
this._update(requestGroup);
}
toggle (requestGroup) {
super.toggle();
this._update(requestGroup);
}
2016-07-07 20:10:55 +00:00
render () {
const {environmentJSON} = this.state;
2016-04-15 05:23:54 +00:00
return (
2016-07-20 23:16:28 +00:00
<Modal ref="modal" top={true} {...this.props}>
2016-04-15 05:23:54 +00:00
<ModalHeader>Environment Variables</ModalHeader>
2016-05-01 20:46:11 +00:00
<ModalBody>
<div className="pad-bottom">
<Editor
onChange={this._handleChange.bind(this)}
value={environmentJSON}
lightTheme={true}
mode="application/json"
/>
</div>
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 faint italic txt-sm tall">
* this data can be used for&nbsp;
<Link href="https://mozilla.github.io/nunjucks/templating.html">
Nunjucks Templating
</Link> in your requests
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;