insomnia/app/ui/components/modals/workspace-environments-edit-modal.js

300 lines
9.6 KiB
JavaScript
Raw Normal View History

import React, {PropTypes, PureComponent} from 'react';
import autobind from 'autobind-decorator';
import classnames from 'classnames';
import {Dropdown, DropdownButton, DropdownItem} from '../base/dropdown';
import PromptButton from '../base/prompt-button';
import Button from '../base/button';
import Link from '../base/link';
import EnvironmentEditor from '../editors/environment-editor';
import Editable from '../base/editable';
import Modal from '../base/modal';
import ModalBody from '../base/modal-body';
import ModalHeader from '../base/modal-header';
import ModalFooter from '../base/modal-footer';
import * as models from '../../../models';
import {trackEvent} from '../../../analytics/index';
@autobind
class WorkspaceEnvironmentsEditModal extends PureComponent {
constructor (props) {
super(props);
this.state = {
workspace: null,
isValid: true,
subEnvironments: [],
rootEnvironment: null,
activeEnvironmentId: null
};
}
2017-03-29 02:21:49 +00:00
hide () {
this.modal.hide();
}
_setEditorRef (n) {
this._envEditor = n;
}
_setModalRef (n) {
this.modal = n;
}
async show (workspace) {
this.modal.show();
await this._load(workspace);
trackEvent('Environment Editor', 'Show');
}
async toggle (workspace) {
this.modal.toggle();
await this._load(workspace);
}
async _load (workspace, environmentToActivate = null) {
2016-11-10 01:15:27 +00:00
const rootEnvironment = await models.environment.getOrCreateForWorkspace(workspace);
const subEnvironments = await models.environment.findByParentId(rootEnvironment._id);
let activeEnvironmentId;
if (environmentToActivate) {
activeEnvironmentId = environmentToActivate._id;
} else if (this.state.workspace && workspace._id !== this.state.workspace._id) {
// We've changed workspaces, so load the root one
activeEnvironmentId = rootEnvironment._id;
} else {
// We haven't changed workspaces, so try loading the last environment, and fall back
// to the root one
activeEnvironmentId = this.state.activeEnvironmentId || rootEnvironment._id;
}
this.setState({
workspace,
rootEnvironment,
subEnvironments,
activeEnvironmentId
});
}
async _handleAddEnvironment (isPrivate = false) {
const {rootEnvironment, workspace} = this.state;
2016-10-26 20:19:18 +00:00
const parentId = rootEnvironment._id;
const environment = await models.environment.create({parentId, isPrivate});
await this._load(workspace, environment);
trackEvent(
'Environment',
isPrivate ? 'Create' : 'Create Private'
);
}
async _handleShowEnvironment (environment) {
// Don't allow switching if the current one has errors
if (!this._envEditor.isValid()) {
return;
}
if (environment === this._getActiveEnvironment()) {
return;
}
const {workspace} = this.state;
await this._load(workspace, environment);
trackEvent('Environment Editor', 'Show Environment');
}
async _handleDeleteEnvironment () {
const {rootEnvironment, workspace} = this.state;
const environment = this._getActiveEnvironment();
// Don't delete the root environment
if (environment === rootEnvironment) {
return;
}
// Delete the current one, then activate the root environment
2016-11-10 01:15:27 +00:00
await models.environment.remove(environment);
await this._load(workspace, rootEnvironment);
trackEvent('Environment', 'Delete');
}
async _handleChangeEnvironmentName (environment, name) {
const {workspace} = this.state;
2016-10-26 20:19:18 +00:00
// NOTE: Fetch the environment first because it might not be up to date.
// For example, editing the body updates silently.
2016-11-10 01:15:27 +00:00
const realEnvironment = await models.environment.getById(environment._id);
await models.environment.update(realEnvironment, {name});
await this._load(workspace);
trackEvent('Environment', 'Rename');
}
_didChange () {
const isValid = this._envEditor.isValid();
if (this.state.isValid !== isValid) {
this.setState({isValid});
}
this._saveChanges();
}
_getActiveEnvironment () {
const {activeEnvironmentId, subEnvironments, rootEnvironment} = this.state;
if (rootEnvironment && rootEnvironment._id === activeEnvironmentId) {
return rootEnvironment;
} else {
return subEnvironments.find(e => e._id === activeEnvironmentId);
}
}
_saveChanges () {
// Only save if it's valid
if (!this._envEditor.isValid()) {
return;
}
2016-10-26 20:19:18 +00:00
const data = this._envEditor.getValue();
const activeEnvironment = this._getActiveEnvironment();
2016-11-10 01:15:27 +00:00
models.environment.update(activeEnvironment, {data});
}
render () {
const {
editorFontSize,
editorIndentSize,
editorKeyMap,
lineWrapping,
render,
getRenderContext
} = this.props;
const {
subEnvironments,
rootEnvironment,
isValid
} = this.state;
const activeEnvironment = this._getActiveEnvironment();
return (
<Modal ref={this._setModalRef} wide tall {...this.props}>
<ModalHeader>Manage Environments</ModalHeader>
<ModalBody noScroll className="env-modal">
<div className="env-modal__sidebar">
<li className={classnames(
'env-modal__sidebar-root-item',
{'env-modal__sidebar-item--active': activeEnvironment === rootEnvironment}
)}>
<Button onClick={this._handleShowEnvironment} value={rootEnvironment}>
{rootEnvironment ? rootEnvironment.name : ''}
</Button>
</li>
<div className="pad env-modal__sidebar-heading">
<h3 className="no-margin">Sub Environments</h3>
<Dropdown right>
<DropdownButton>
<i className="fa fa-plus-circle"/>
</DropdownButton>
<DropdownItem onClick={this._handleAddEnvironment} value={false}>
<i className="fa fa-eye"/> Environment
</DropdownItem>
<DropdownItem onClick={this._handleAddEnvironment} value={true}
title="Environment will not be exported or synced">
<i className="fa fa-eye-slash"/> Private Environment
</DropdownItem>
</Dropdown>
</div>
<ul>
{subEnvironments.map(environment => {
const classes = classnames(
'env-modal__sidebar-item',
{'env-modal__sidebar-item--active': activeEnvironment === environment}
);
return (
<li key={environment._id} className={classes}>
<Button onClick={this._handleShowEnvironment} value={environment}>
{environment.isPrivate ? (
<i className="fa fa-eye-slash faint"
title="Environment will not be exported or synced"
/>
) : (
<i className="fa fa-blank faint"/>
)}
&nbsp;&nbsp;
<Editable
className="inline-block"
onSubmit={name => this._handleChangeEnvironmentName(environment, name)}
value={environment.name}
/>
</Button>
</li>
);
})}
</ul>
</div>
<div className="env-modal__main">
<div className="env-modal__main__header">
<h1>
<Editable singleClick
onSubmit={name => this._handleChangeEnvironmentName(activeEnvironment, name)}
value={activeEnvironment ? activeEnvironment.name : ''}/>
</h1>
{rootEnvironment !== activeEnvironment && (
<PromptButton className="btn btn--clicky"
confirmMessage="Confirm"
onClick={this._handleDeleteEnvironment}>
<i className="fa fa-trash-o"/> Delete
</PromptButton>
)}
</div>
<div className="env-modal__editor">
<EnvironmentEditor
2017-01-23 22:41:31 +00:00
editorFontSize={editorFontSize}
editorIndentSize={editorIndentSize}
2017-01-24 22:18:11 +00:00
editorKeyMap={editorKeyMap}
2017-02-26 23:47:45 +00:00
lineWrapping={lineWrapping}
ref={this._setEditorRef}
key={activeEnvironment ? activeEnvironment._id : 'n/a'}
environment={activeEnvironment ? activeEnvironment.data : {}}
didChange={this._didChange}
render={render}
getRenderContext={getRenderContext}
/>
</div>
</div>
</ModalBody>
<ModalFooter>
<div className="margin-left italic txt-sm tall">
* Environment data can be used for&nbsp;
2017-01-19 22:36:42 +00:00
<Link href="https://insomnia.rest/documentation/templating/">
Nunjucks Templating
</Link> in your requests
</div>
2017-03-29 02:21:49 +00:00
<button className="btn" disabled={!isValid} onClick={this.hide}>
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
Done
</button>
</ModalFooter>
</Modal>
);
}
}
WorkspaceEnvironmentsEditModal.propTypes = {
2017-01-23 22:41:31 +00:00
onChange: PropTypes.func.isRequired,
editorFontSize: PropTypes.number.isRequired,
editorIndentSize: PropTypes.number.isRequired,
2017-01-24 22:18:11 +00:00
editorKeyMap: PropTypes.string.isRequired,
render: PropTypes.func.isRequired,
getRenderContext: PropTypes.func.isRequired,
lineWrapping: PropTypes.bool.isRequired
};
export default WorkspaceEnvironmentsEditModal;
export let show = null;