2017-03-09 06:23:23 +00:00
|
|
|
import React, {PropTypes, PureComponent} from 'react';
|
|
|
|
import autobind from 'autobind-decorator';
|
2017-06-09 01:10:12 +00:00
|
|
|
import {trackEvent} from '../../../analytics/index';
|
2017-03-09 06:23:23 +00:00
|
|
|
|
|
|
|
@autobind
|
|
|
|
class VariableEditor extends PureComponent {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
2017-03-30 18:52:07 +00:00
|
|
|
const inner = props.defaultValue
|
|
|
|
.replace(/\s*}}$/, '')
|
|
|
|
.replace(/^{{\s*/, '');
|
2017-03-09 06:23:23 +00:00
|
|
|
|
|
|
|
this.state = {
|
2017-03-30 18:52:07 +00:00
|
|
|
variables: [],
|
|
|
|
value: `{{ ${inner} }}`,
|
|
|
|
preview: '',
|
2017-03-09 06:23:23 +00:00
|
|
|
error: ''
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-03-30 18:52:07 +00:00
|
|
|
componentDidMount () {
|
|
|
|
this._update(this.state.value, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleChange (e) {
|
2017-06-09 01:10:12 +00:00
|
|
|
const name = e.target.value;
|
|
|
|
trackEvent('Variable Editor', 'Change Variable', name);
|
|
|
|
this._update(name);
|
2017-03-09 06:23:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 18:52:07 +00:00
|
|
|
_setSelectRef (n) {
|
|
|
|
this._select = n;
|
2017-03-09 06:23:23 +00:00
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
// Let it render, then focus the input
|
2017-03-09 06:23:23 +00:00
|
|
|
setTimeout(() => {
|
2017-03-30 18:52:07 +00:00
|
|
|
this._select && this._select.focus();
|
2017-03-09 06:23:23 +00:00
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
|
2017-03-30 18:52:07 +00:00
|
|
|
async _update (value, noCallback = false) {
|
2017-03-09 06:23:23 +00:00
|
|
|
const {handleRender} = this.props;
|
|
|
|
|
2017-03-30 18:52:07 +00:00
|
|
|
let preview = '';
|
2017-03-09 06:23:23 +00:00
|
|
|
let error = '';
|
|
|
|
|
|
|
|
try {
|
2017-03-30 18:52:07 +00:00
|
|
|
preview = await handleRender(value, true);
|
2017-03-09 06:23:23 +00:00
|
|
|
} catch (err) {
|
|
|
|
error = err.message;
|
|
|
|
}
|
|
|
|
|
2017-03-31 21:59:12 +00:00
|
|
|
const context = await this.props.handleGetRenderContext();
|
|
|
|
const variables = context.keys;
|
2017-03-30 18:52:07 +00:00
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
// Hack to skip updating if we unmounted for some reason
|
2017-03-30 18:52:07 +00:00
|
|
|
if (this._select) {
|
|
|
|
this.setState({preview, error, variables, value});
|
2017-03-16 17:51:56 +00:00
|
|
|
}
|
2017-03-09 06:23:23 +00:00
|
|
|
|
2017-03-16 17:51:56 +00:00
|
|
|
// Call the callback if we need to
|
2017-03-09 06:23:23 +00:00
|
|
|
if (!noCallback) {
|
2017-03-30 18:52:07 +00:00
|
|
|
this.props.onChange(value);
|
2017-03-09 06:23:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2017-03-30 18:52:07 +00:00
|
|
|
const {error, value, preview, variables} = this.state;
|
2017-03-31 21:59:12 +00:00
|
|
|
const isOther = !variables.find(v => value === `{{ ${v.name} }}`);
|
2017-03-09 06:23:23 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="form-control form-control--outlined">
|
2017-03-30 18:52:07 +00:00
|
|
|
<label>Environment Variable
|
2017-04-06 02:30:28 +00:00
|
|
|
<select ref={this._setSelectRef} value={value} onChange={this._handleChange}>
|
2017-03-31 21:59:12 +00:00
|
|
|
<option value={`{{ 'my custom template logic' | urlencode }}`}>
|
2017-04-04 23:06:43 +00:00
|
|
|
-- Custom --
|
2017-03-31 21:59:12 +00:00
|
|
|
</option>
|
2017-03-30 18:52:07 +00:00
|
|
|
{variables.map((v, i) => (
|
|
|
|
<option key={`${i}::${v.name}`} value={`{{ ${v.name} }}`}>
|
|
|
|
{v.name}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</select>
|
2017-03-09 06:23:23 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
2017-05-17 19:02:09 +00:00
|
|
|
{isOther && (
|
2017-03-31 21:59:12 +00:00
|
|
|
<div className="form-control form-control--outlined">
|
2017-05-23 22:05:31 +00:00
|
|
|
<input type="text" defaultValue={value} onChange={this._handleChange} />
|
2017-03-31 21:59:12 +00:00
|
|
|
</div>
|
2017-05-17 19:02:09 +00:00
|
|
|
)}
|
2017-03-09 06:23:23 +00:00
|
|
|
<div className="form-control form-control--outlined">
|
|
|
|
<label>Live Preview
|
|
|
|
{error
|
2017-03-31 21:59:12 +00:00
|
|
|
? <code className="block danger selectable">{error || <span> </span>}</code>
|
|
|
|
: <code className="block selectable">{preview || <span> </span>}</code>
|
2017-03-09 06:23:23 +00:00
|
|
|
}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VariableEditor.propTypes = {
|
|
|
|
handleRender: PropTypes.func.isRequired,
|
2017-03-30 18:52:07 +00:00
|
|
|
handleGetRenderContext: PropTypes.func.isRequired,
|
2017-03-09 06:23:23 +00:00
|
|
|
defaultValue: PropTypes.string.isRequired,
|
|
|
|
onChange: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
export default VariableEditor;
|