From 257685ca47e835c484bfeebecc0be7e689441393 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Mon, 12 Aug 2019 17:15:44 -0400 Subject: [PATCH] Ask user on import whether they want to use current workspace --- packages/insomnia-app/app/common/import.js | 10 ++++- .../insomnia-app/app/plugins/context/data.js | 4 +- .../app/ui/components/modals/ask-modal.js | 17 +++++--- .../app/ui/redux/modules/global.js | 43 ++++++++++++++++--- 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/packages/insomnia-app/app/common/import.js b/packages/insomnia-app/app/common/import.js index 88559fb7b9..f69fe10c5f 100644 --- a/packages/insomnia-app/app/common/import.js +++ b/packages/insomnia-app/app/common/import.js @@ -37,7 +37,11 @@ const MODELS = { export async function importUri( getWorkspaceId: () => Promise, uri: string, -): Promise { +): Promise<{ + source: string, + error: Error | null, + summary: { [string]: Array }, +}> { let rawText; if (uri.match(/^(http|https):\/\//)) { const response = await window.fetch(uri); @@ -58,7 +62,7 @@ export async function importUri( error: error.message, message: 'Import failed', }); - return; + return result; } let statements = Object.keys(summary) @@ -76,6 +80,8 @@ export async function importUri( message = `You imported ${statements.join(', ')}!`; } showModal(AlertModal, { title: 'Import Succeeded', message }); + + return result; } export async function importRaw( diff --git a/packages/insomnia-app/app/plugins/context/data.js b/packages/insomnia-app/app/plugins/context/data.js index 8f62fd9fb2..c25333a447 100644 --- a/packages/insomnia-app/app/plugins/context/data.js +++ b/packages/insomnia-app/app/plugins/context/data.js @@ -10,10 +10,10 @@ export function init(): { import: Object, export: Object } { return { import: { async uri(uri: string, options: { workspaceId?: string } = {}): Promise { - await importUri(options.workspaceId || null, uri); + await importUri(() => Promise.resolve(options.workspaceId || null), uri); }, async raw(text: string, options: { workspaceId?: string } = {}): Promise { - await importRaw(options.workspaceId || null, text); + await importRaw(() => Promise.resolve(options.workspaceId || null), text); }, }, export: { diff --git a/packages/insomnia-app/app/ui/components/modals/ask-modal.js b/packages/insomnia-app/app/ui/components/modals/ask-modal.js index f81a84da1a..a941562b9f 100644 --- a/packages/insomnia-app/app/ui/components/modals/ask-modal.js +++ b/packages/insomnia-app/app/ui/components/modals/ask-modal.js @@ -13,6 +13,8 @@ class AskModal extends PureComponent { this.state = { title: '', message: '', + yesText: 'Yes', + noText: 'No', }; } @@ -41,11 +43,16 @@ class AskModal extends PureComponent { } show(options = {}) { - const { title, message, onDone } = options; + const { title, message, onDone, yesText, noText } = options; this._doneCallback = onDone; - this.setState({ title, message }); + this.setState({ + title: title || 'Confirm', + message: message || 'No message provided', + yesText: yesText || 'Yes', + noText: noText || 'No', + }); this.modal.show(); @@ -59,7 +66,7 @@ class AskModal extends PureComponent { } render() { - const { message, title } = this.state; + const { message, title, yesText, noText } = this.state; return ( @@ -68,10 +75,10 @@ class AskModal extends PureComponent {
diff --git a/packages/insomnia-app/app/ui/redux/modules/global.js b/packages/insomnia-app/app/ui/redux/modules/global.js index d57e21b087..73319921bc 100644 --- a/packages/insomnia-app/app/ui/redux/modules/global.js +++ b/packages/insomnia-app/app/ui/redux/modules/global.js @@ -132,10 +132,8 @@ export function loadRequestStop(requestId) { } export function setActiveWorkspace(workspaceId) { - window.localStorage.setItem( - `${LOCALSTORAGE_PREFIX}::activeWorkspaceId`, - JSON.stringify(workspaceId), - ); + const key = `${LOCALSTORAGE_PREFIX}::activeWorkspaceId`; + window.localStorage.setItem(key, JSON.stringify(workspaceId)); return { type: SET_ACTIVE_WORKSPACE, workspaceId }; } @@ -163,16 +161,22 @@ export function importFile(workspaceId) { } // Let's import all the paths! + let importedWorkspaces = []; for (const p of paths) { try { const uri = `file://${p}`; - await importUtils.importUri(workspaceId, uri); + const result = await importUtils.importUri(askToImportIntoWorkspace(workspaceId), uri); + importedWorkspaces = [...importedWorkspaces, ...result.summary[models.workspace.type]]; } catch (err) { showModal(AlertModal, { title: 'Import Failed', message: err + '' }); } finally { dispatch(loadStop()); } } + + if (importedWorkspaces.length === 1) { + dispatch(setActiveWorkspace(importedWorkspaces[0]._id)); + } }); }; } @@ -180,13 +184,20 @@ export function importFile(workspaceId) { export function importUri(workspaceId, uri) { return async dispatch => { dispatch(loadStart()); + + let importedWorkspaces = []; try { - await importUtils.importUri(workspaceId, uri); + const result = await importUtils.importUri(askToImportIntoWorkspace(workspaceId), uri); + importedWorkspaces = [...importedWorkspaces, ...result.summary[models.workspace.type]]; } catch (err) { showModal(AlertModal, { title: 'Import Failed', message: err + '' }); } finally { dispatch(loadStop()); } + + if (importedWorkspaces.length === 1) { + dispatch(setActiveWorkspace(importedWorkspaces[0]._id)); + } }; } @@ -434,3 +445,23 @@ export function init() { return setActiveWorkspace(workspaceId); } + +// ~~~~~~~ // +// HELPERS // +// ~~~~~~~ // + +function askToImportIntoWorkspace(workspaceId) { + return function() { + return new Promise(resolve => { + showModal(AskModal, { + title: 'Import', + message: 'Do you wand to import into the current workspace or a new one?', + yesText: 'Current', + noText: 'New Workspace', + onDone: yes => { + resolve(yes ? workspaceId : null); + }, + }); + }); + }; +}