From edf874ce5c9ad7a3a3b47eb210d2f1403a8adb24 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Mon, 8 Jun 2020 15:32:49 -0700 Subject: [PATCH] Fix responses not being found if deleted active environment (#2265) * Fix responses not being found (Closes #2237) * Fix active envirnoment set Co-authored-by: Opender Singh --- .../workspace-environments-edit-modal.js | 12 ++++++++-- .../insomnia-app/app/ui/components/wrapper.js | 6 ++--- .../insomnia-app/app/ui/redux/selectors.js | 24 +++++++++++++++---- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/insomnia-app/app/ui/components/modals/workspace-environments-edit-modal.js b/packages/insomnia-app/app/ui/components/modals/workspace-environments-edit-modal.js index 8e1cbb65e..d94f3f806 100644 --- a/packages/insomnia-app/app/ui/components/modals/workspace-environments-edit-modal.js +++ b/packages/insomnia-app/app/ui/components/modals/workspace-environments-edit-modal.js @@ -24,6 +24,7 @@ import Tooltip from '../tooltip'; const ROOT_ENVIRONMENT_NAME = 'Base Environment'; type Props = { + handleChangeEnvironment: (id: string | null) => Promise, activeEnvironmentId: string | null, editorFontSize: number, editorIndentSize: number, @@ -208,10 +209,11 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent { async _handleDuplicateEnvironment(environment: Environment) { const { workspace } = this.state; const newEnvironment = await models.environment.duplicate(environment); - this._load(workspace, newEnvironment); + await this._load(workspace, newEnvironment); } async _handleDeleteEnvironment(environment: Environment) { + const { handleChangeEnvironment, activeEnvironmentId } = this.props; const { rootEnvironment, workspace } = this.state; // Don't delete the root environment @@ -219,8 +221,14 @@ class WorkspaceEnvironmentsEditModal extends React.PureComponent { return; } - // Delete the current one, then activate the root environment + // Unset active environment if it's being deleted + if (activeEnvironmentId === environment._id) { + await handleChangeEnvironment(null); + } + + // Delete the current one await models.environment.remove(environment); + await this._load(workspace, rootEnvironment); } diff --git a/packages/insomnia-app/app/ui/components/wrapper.js b/packages/insomnia-app/app/ui/components/wrapper.js index e351831e5..a19f6a2af 100644 --- a/packages/insomnia-app/app/ui/components/wrapper.js +++ b/packages/insomnia-app/app/ui/components/wrapper.js @@ -106,7 +106,7 @@ export type WrapperProps = { handleShowExportRequestsModal: Function, handleShowSettingsModal: Function, handleExportRequestsToFile: Function, - handleSetActiveWorkspace: (workspaceId: string) => void, + handleSetActiveWorkspace: (workspaceId: string | null) => void, handleSetActiveEnvironment: Function, handleMoveDoc: Function, handleCreateRequest: Function, @@ -462,7 +462,7 @@ class Wrapper extends React.PureComponent { handleCreateRequestGroup(activeWorkspace._id); } - _handleChangeEnvironment(id: string) { + _handleChangeEnvironment(id: string | null) { const { handleSetActiveEnvironment } = this.props; handleSetActiveEnvironment(id); } @@ -727,7 +727,7 @@ class Wrapper extends React.PureComponent { { + return entities.settings[0] || models.settings.init(); +}); + export const selectActiveWorkspace = createSelector( state => selectEntitiesLists(state).workspaces, state => state.entities, @@ -57,6 +61,18 @@ export const selectActiveWorkspaceMeta = createSelector( }, ); +export const selectActiveEnvironment = createSelector( + selectActiveWorkspaceMeta, + selectEntitiesLists, + (meta, entities) => { + if (!meta) { + return null; + } + + return entities.environments.find(e => e._id === meta.activeEnvironmentId) || null; + }, +); + export const selectActiveWorkspaceClientCertificates = createSelector( selectEntitiesLists, selectActiveWorkspace, @@ -271,10 +287,10 @@ export const selectActiveRequestMeta = createSelector( export const selectActiveRequestResponses = createSelector( selectActiveRequest, selectEntitiesLists, - selectActiveWorkspaceMeta, - (activeRequest, entities, meta) => { + selectActiveEnvironment, + selectSettings, + (activeRequest, entities, activeEnvironment, settings) => { const requestId = activeRequest ? activeRequest._id : 'n/a'; - const settings = entities.settings[0]; // Filter responses down if the setting is enabled return entities.responses @@ -282,7 +298,7 @@ export const selectActiveRequestResponses = createSelector( const requestMatches = requestId === response.parentId; if (settings.filterResponsesByEnv) { - const activeEnvironmentId = meta ? meta.activeEnvironmentId : 'n/a'; + const activeEnvironmentId = activeEnvironment ? activeEnvironment._id : null; const environmentMatches = response.environmentId === activeEnvironmentId; return requestMatches && environmentMatches; } else {