import React, {PropTypes, Component} from 'react'; import classnames from 'classnames'; 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'; import * as models from '../../../models' import {trackEvent} from '../../../analytics/index'; class WorkspaceEnvironmentsEditModal extends Component { 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) { 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, activeEnvironmentId, forceRefreshKey: Date.now(), }); } async _handleAddEnvironment () { const {rootEnvironment, workspace} = this.state; const parentId = rootEnvironment._id; 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 await models.environment.remove(environment); this._load(workspace, rootEnvironment); trackEvent('Environment', 'Delete'); } async _handleChangeEnvironmentName (environment, name) { const {workspace} = this.state; // NOTE: Fetch the environment first because it might not be up to date. // For example, editing the body updates silently. 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; } const data = this._envEditor.getValue(); const activeEnvironment = this._getActiveEnvironment(); models.environment.update(activeEnvironment, {data}); } render () { const {subEnvironments, rootEnvironment, isValid, forceRefreshKey} = this.state; const activeEnvironment = this._getActiveEnvironment(); return ( this.modal = m} wide={true} top={true} tall={true} {...this.props}> Manage Environments (JSON Format)
  • this._handleShowEnvironment(rootEnvironment)} className={classnames( 'env-modal__sidebar-root-item', {'env-modal__sidebar-item--active': activeEnvironment === rootEnvironment} )}>
  • Sub Environments

      {subEnvironments.map(environment => { const classes = classnames( 'env-modal__sidebar-item', {'env-modal__sidebar-item--active': activeEnvironment === environment} ); return (
    • ) })}

    this._handleChangeEnvironmentName(activeEnvironment, name)} value={activeEnvironment ? activeEnvironment.name : ''} />

    {rootEnvironment !== activeEnvironment ? ( this._handleDeleteEnvironment(activeEnvironment)}> ) : null}
    this._envEditor = n} key={`${forceRefreshKey}::${(activeEnvironment ? activeEnvironment._id : 'n/a')}`} environment={activeEnvironment ? activeEnvironment.data : {}} didChange={this._didChange.bind(this)} lightTheme={true} />
    * environment data can be used for  Nunjucks Templating in your requests
    ); } } WorkspaceEnvironmentsEditModal.propTypes = { onChange: PropTypes.func.isRequired }; export default WorkspaceEnvironmentsEditModal; export let show = null;