2018-06-25 17:42:50 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2021-02-02 22:23:42 +00:00
|
|
|
import { autoBindMethodsForReact } from 'class-autobind-decorator';
|
|
|
|
import { AUTOBIND_CFG } from '../../../common/constants';
|
2017-03-08 05:52:17 +00:00
|
|
|
import EnvironmentEditor from '../editors/environment-editor';
|
|
|
|
import Modal from '../base/modal';
|
|
|
|
import ModalBody from '../base/modal-body';
|
|
|
|
import ModalHeader from '../base/modal-header';
|
|
|
|
import ModalFooter from '../base/modal-footer';
|
2021-05-12 06:35:00 +00:00
|
|
|
import { RequestGroup } from '../../../models/request-group';
|
|
|
|
import { HandleGetRenderContext, HandleRender } from '../../../common/render';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
onChange: Function;
|
|
|
|
editorFontSize: number;
|
|
|
|
editorIndentSize: number;
|
|
|
|
editorKeyMap: string;
|
|
|
|
render: HandleRender;
|
|
|
|
getRenderContext: HandleGetRenderContext;
|
|
|
|
nunjucksPowerUserMode: boolean;
|
|
|
|
isVariableUncovered: boolean;
|
|
|
|
lineWrapping: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
requestGroup: RequestGroup | null;
|
|
|
|
isValid: boolean;
|
|
|
|
}
|
2016-04-15 05:23:54 +00:00
|
|
|
|
2021-02-02 22:23:42 +00:00
|
|
|
@autoBindMethodsForReact(AUTOBIND_CFG)
|
2021-05-12 06:35:00 +00:00
|
|
|
class EnvironmentEditModal extends PureComponent<Props, State> {
|
|
|
|
state: State = {
|
|
|
|
requestGroup: null,
|
|
|
|
isValid: true,
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
modal: Modal | null = null;
|
|
|
|
_envEditor: EnvironmentEditor | null = null;
|
|
|
|
|
|
|
|
_setModalRef(n: Modal) {
|
2017-03-03 01:44:07 +00:00
|
|
|
this.modal = n;
|
|
|
|
}
|
2016-04-15 05:23:54 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
_setEditorRef(n: EnvironmentEditor) {
|
2017-03-03 01:44:07 +00:00
|
|
|
this._envEditor = n;
|
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_saveChanges() {
|
2021-05-12 06:35:00 +00:00
|
|
|
if (!this._envEditor?.isValid()) {
|
2016-07-22 21:35:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-04-15 05:23:54 +00:00
|
|
|
|
2019-05-29 20:10:48 +00:00
|
|
|
let patch;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-05-29 20:10:48 +00:00
|
|
|
try {
|
|
|
|
const data = this._envEditor.getValue();
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2019-05-29 20:10:48 +00:00
|
|
|
patch = {
|
|
|
|
environment: data && data.object,
|
|
|
|
environmentPropertyOrder: data && data.propertyOrder,
|
|
|
|
};
|
|
|
|
} catch (err) {
|
|
|
|
// Invalid JSON probably
|
|
|
|
return;
|
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
const { requestGroup } = this.state;
|
2019-05-29 20:10:48 +00:00
|
|
|
this.props.onChange(Object.assign({}, requestGroup, patch));
|
2016-04-17 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
_didChange() {
|
2016-08-15 17:04:36 +00:00
|
|
|
this._saveChanges();
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
const isValid = Boolean(this._envEditor?.isValid());
|
2016-04-17 01:52:10 +00:00
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
if (this.state.isValid !== isValid) {
|
2018-06-25 17:42:50 +00:00
|
|
|
this.setState({ isValid });
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
2016-07-06 20:18:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
show(requestGroup) {
|
|
|
|
this.setState({ requestGroup });
|
2021-05-12 06:35:00 +00:00
|
|
|
this.modal?.show();
|
2016-07-15 00:26:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
hide() {
|
2021-05-12 06:35:00 +00:00
|
|
|
this.modal?.hide();
|
2017-03-09 06:23:23 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
2017-02-26 23:47:45 +00:00
|
|
|
const {
|
|
|
|
editorKeyMap,
|
|
|
|
editorFontSize,
|
2017-04-14 21:47:15 +00:00
|
|
|
editorIndentSize,
|
2017-02-26 23:47:45 +00:00
|
|
|
lineWrapping,
|
2017-02-27 21:00:13 +00:00
|
|
|
render,
|
2017-03-12 00:31:23 +00:00
|
|
|
getRenderContext,
|
2017-10-13 08:22:13 +00:00
|
|
|
nunjucksPowerUserMode,
|
2018-11-30 05:50:30 +00:00
|
|
|
isVariableUncovered,
|
2017-02-26 23:47:45 +00:00
|
|
|
...extraProps
|
|
|
|
} = this.props;
|
2018-06-25 17:42:50 +00:00
|
|
|
const { requestGroup, isValid } = this.state;
|
2019-05-29 20:10:48 +00:00
|
|
|
const environmentInfo = {
|
|
|
|
object: requestGroup ? requestGroup.environment : {},
|
|
|
|
propertyOrder: requestGroup && requestGroup.environmentPropertyOrder,
|
|
|
|
};
|
2016-04-15 05:23:54 +00:00
|
|
|
return (
|
2017-03-16 17:51:56 +00:00
|
|
|
<Modal ref={this._setModalRef} tall {...extraProps}>
|
2016-08-15 17:04:36 +00:00
|
|
|
<ModalHeader>Environment Overrides (JSON Format)</ModalHeader>
|
2017-03-28 22:45:23 +00:00
|
|
|
<ModalBody noScroll className="pad-top-sm">
|
2016-08-15 17:04:36 +00:00
|
|
|
<EnvironmentEditor
|
2017-01-23 22:41:31 +00:00
|
|
|
editorFontSize={editorFontSize}
|
2017-04-14 21:47:15 +00:00
|
|
|
editorIndentSize={editorIndentSize}
|
2017-01-24 22:18:11 +00:00
|
|
|
editorKeyMap={editorKeyMap}
|
2017-02-28 21:32:23 +00:00
|
|
|
ref={this._setEditorRef}
|
2016-08-15 17:04:36 +00:00
|
|
|
key={requestGroup ? requestGroup._id : 'n/a'}
|
2017-02-26 23:47:45 +00:00
|
|
|
lineWrapping={lineWrapping}
|
2019-05-29 20:10:48 +00:00
|
|
|
environmentInfo={environmentInfo}
|
2017-02-27 21:00:13 +00:00
|
|
|
didChange={this._didChange}
|
|
|
|
render={render}
|
2017-03-12 00:31:23 +00:00
|
|
|
getRenderContext={getRenderContext}
|
2017-10-13 08:22:13 +00:00
|
|
|
nunjucksPowerUserMode={nunjucksPowerUserMode}
|
2018-11-30 05:50:30 +00:00
|
|
|
isVariableUncovered={isVariableUncovered}
|
2016-08-15 17:04:36 +00:00
|
|
|
/>
|
2016-04-15 05:23:54 +00:00
|
|
|
</ModalBody>
|
2016-07-07 20:10:55 +00:00
|
|
|
<ModalFooter>
|
2017-03-28 22:45:23 +00:00
|
|
|
<div className="margin-left italic txt-sm">
|
|
|
|
* Used to override data in the global environment
|
2016-07-07 20:10:55 +00:00
|
|
|
</div>
|
2017-03-09 06:23:23 +00:00
|
|
|
<button className="btn" disabled={!isValid} onClick={this.hide}>
|
2016-10-21 17:20:36 +00:00
|
|
|
Done
|
|
|
|
</button>
|
2016-04-15 05:23:54 +00:00
|
|
|
</ModalFooter>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 01:52:10 +00:00
|
|
|
export default EnvironmentEditModal;
|