insomnia/app/ui/components/modals/nunjucks-modal.js
Gregory Schier 56a86168c4 Nunjucks Variable Editor Modal (#110)
* Fixed duplication kve bug

* Some minor css

* Fix modal refs

* Got variable modal showing

* A bit more

* Errors and debounced render

* Finished naive editors

* Some key value editor fixes

* Yet even more one-line fixes

* Added some transitions

* Small fixes

* Added key flattening util
2017-03-08 22:23:23 -08:00

102 lines
2.3 KiB
JavaScript

import React, {PureComponent, PropTypes} from 'react';
import autobind from 'autobind-decorator';
import VariableEditor from '../templating/variable-editor';
import TagEditor from '../templating/tag-editor';
import Modal from '../base/modal';
import ModalBody from '../base/modal-body';
import ModalHeader from '../base/modal-header';
import ModalFooter from '../base/modal-footer';
import {trackEvent} from '../../../analytics';
@autobind
class NunjucksModal extends PureComponent {
constructor (props) {
super(props);
this.state = {
defaultTemplate: '',
key: 0
};
this._onDone = null;
this._currentTemplate = null;
}
_setModalRef (n) {
this.modal = n;
}
_handleTemplateChange (template) {
this._currentTemplate = template;
}
show ({template, onDone}) {
this._onDone = onDone;
this.setState({
defaultTemplate: template,
key: this.state.key + 1
});
this.modal.show();
trackEvent('Nunjucks', 'Editor', 'Show');
}
hide () {
this.modal.hide();
trackEvent('Nunjucks', 'Editor', 'Hide');
}
_handleSubmit (e) {
e.preventDefault();
this._onDone && this._onDone(this._currentTemplate);
this.hide();
}
render () {
const {handleRender} = this.props;
const {defaultTemplate, key} = this.state;
let editor = null;
let title = '';
if (defaultTemplate.indexOf('{{') === 0) {
title = 'Variable Reference';
editor = (
<VariableEditor
onChange={this._handleTemplateChange}
defaultValue={defaultTemplate}
handleRender={handleRender}
/>
);
} else if (defaultTemplate.indexOf('{%') === 0) {
title = 'Tag';
editor = (
<TagEditor
onChange={this._handleTemplateChange}
defaultValue={defaultTemplate}
handleRender={handleRender}
/>
);
}
return (
<Modal ref={this._setModalRef}>
<form onSubmit={this._handleSubmit}>
<ModalHeader>Edit {title}</ModalHeader>
<ModalBody className="pad" key={key}>
{editor}
</ModalBody>
<ModalFooter>
<button className="btn">Done</button>
</ModalFooter>
</form>
</Modal>
);
}
}
NunjucksModal.propTypes = {
handleRender: PropTypes.func.isRequired
};
export default NunjucksModal;