insomnia/app/ui/components/modals/WorkspaceEnvironmentsEditModal.js

232 lines
7.4 KiB
JavaScript
Raw Normal View History

import React, {PropTypes, Component} from 'react';
import classnames from 'classnames';
2016-09-21 20:32:45 +00:00
import PromptButton from '../base/PromptButton';
import Link from '../base/Link';
import EnvironmentEditor from '../editors/EnvironmentEditor';
import Editable from '../base/Editable';
import Modal from '../base/Modal';
import ModalBody from '../base/ModalBody';
import ModalHeader from '../base/ModalHeader';
import ModalFooter from '../base/ModalFooter';
2016-11-10 05:56:23 +00:00
import * as models from '../../../models'
import {trackEvent} from '../../../analytics/index';
class WorkspaceEnvironmentsEditModal extends Component {
2016-11-26 08:29:16 +00:00
state = {
workspace: null,
isValid: true,
subEnvironments: [],
rootEnvironment: null,
activeEnvironmentId: null,
forceRefreshKey: 0,
};
show (workspace) {
this.modal.show();
this._load(workspace);
trackEvent('Environment Editor', 'Show');
}
toggle (workspace) {
this.modal.toggle();
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 {
activeEnvironmentId = this.state.activeEnvironmentId || rootEnvironment._id;
}
this.setState({
workspace,
rootEnvironment,
subEnvironments,
2016-11-24 06:35:39 +00:00
activeEnvironmentId,
forceRefreshKey: Date.now(),
});
}
async _handleAddEnvironment () {
const {rootEnvironment, workspace} = this.state;
2016-10-26 20:19:18 +00:00
const parentId = rootEnvironment._id;
2016-11-10 01:15:27 +00:00
const environment = await models.environment.create({parentId});
this._load(workspace, environment);
trackEvent('Environment', 'Create');
}
_handleShowEnvironment (environment) {
// Don't allow switching if the current one has errors
if (!this._envEditor.isValid()) {
return;
}
const {workspace} = this.state;
this._load(workspace, environment);
trackEvent('Environment Editor', 'Show Environment');
}
async _handleDeleteEnvironment (environment) {
const {rootEnvironment, workspace} = this.state;
// 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);
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});
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 () {
2016-11-24 06:35:39 +00:00
const {subEnvironments, rootEnvironment, isValid, forceRefreshKey} = this.state;
const activeEnvironment = this._getActiveEnvironment();
return (
2016-09-21 20:32:45 +00:00
<Modal ref={m => this.modal = m} wide={true} top={true}
tall={true} {...this.props}>
<ModalHeader>Manage Environments (JSON Format)</ModalHeader>
<ModalBody noScroll={true} className="env-modal">
<div className="env-modal__sidebar">
<li onClick={() => this._handleShowEnvironment(rootEnvironment)}
className={classnames(
'env-modal__sidebar-root-item',
{'env-modal__sidebar-item--active': activeEnvironment === rootEnvironment}
)}>
<button>{rootEnvironment ? rootEnvironment.name : ''}</button>
</li>
<div className="pad env-modal__sidebar-heading">
<h3>Sub Environments</h3>
<button onClick={() => this._handleAddEnvironment()}>
<i className="fa fa-plus-circle"></i>
</button>
</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}>
2016-09-21 20:32:45 +00:00
<button
onClick={() => this._handleShowEnvironment(environment)}>
<Editable
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={true}
onSubmit={name => this._handleChangeEnvironmentName(activeEnvironment, name)}
value={activeEnvironment ? activeEnvironment.name : ''}
/>
</h1>
{rootEnvironment !== activeEnvironment ? (
2016-11-26 00:49:38 +00:00
<PromptButton className="btn btn--clicky"
2016-09-21 20:32:45 +00:00
confirmMessage="Confirm"
onClick={() => this._handleDeleteEnvironment(activeEnvironment)}>
<i className="fa fa-trash-o"></i>
2016-09-21 20:32:45 +00:00
</PromptButton>
) : null}
</div>
<div className="env-modal__editor">
<EnvironmentEditor
ref={n => this._envEditor = n}
2016-11-24 06:35:39 +00:00
key={`${forceRefreshKey}::${(activeEnvironment ? activeEnvironment._id : 'n/a')}`}
environment={activeEnvironment ? activeEnvironment.data : {}}
didChange={this._didChange.bind(this)}
lightTheme={true}
/>
</div>
</div>
</ModalBody>
<ModalFooter>
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
<div className="margin-left faint italic txt-sm tall">
* environment data can be used for&nbsp;
<Link href="https://mozilla.github.io/nunjucks/templating.html">
Nunjucks Templating
</Link> in your requests
</div>
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
<button className="btn" disabled={!isValid}
onClick={e => this.modal.hide()}>
Done
</button>
</ModalFooter>
</Modal>
);
}
}
WorkspaceEnvironmentsEditModal.propTypes = {
onChange: PropTypes.func.isRequired
};
export default WorkspaceEnvironmentsEditModal;
export let show = null;