mirror of
https://github.com/Kong/insomnia.git
synced 2026-04-21 06:37:36 -04:00
Ask user on import whether they want to use current workspace
This commit is contained in:
@@ -37,7 +37,11 @@ const MODELS = {
|
||||
export async function importUri(
|
||||
getWorkspaceId: () => Promise<string | null>,
|
||||
uri: string,
|
||||
): Promise<void> {
|
||||
): Promise<{
|
||||
source: string,
|
||||
error: Error | null,
|
||||
summary: { [string]: Array<BaseModel> },
|
||||
}> {
|
||||
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(
|
||||
|
||||
@@ -10,10 +10,10 @@ export function init(): { import: Object, export: Object } {
|
||||
return {
|
||||
import: {
|
||||
async uri(uri: string, options: { workspaceId?: string } = {}): Promise<void> {
|
||||
await importUri(options.workspaceId || null, uri);
|
||||
await importUri(() => Promise.resolve(options.workspaceId || null), uri);
|
||||
},
|
||||
async raw(text: string, options: { workspaceId?: string } = {}): Promise<void> {
|
||||
await importRaw(options.workspaceId || null, text);
|
||||
await importRaw(() => Promise.resolve(options.workspaceId || null), text);
|
||||
},
|
||||
},
|
||||
export: {
|
||||
|
||||
@@ -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 (
|
||||
<Modal noEscape ref={this._setModalRef} closeOnKeyCodes={[13]}>
|
||||
@@ -68,10 +75,10 @@ class AskModal extends PureComponent {
|
||||
<ModalFooter>
|
||||
<div>
|
||||
<button className="btn" onClick={this._handleNo}>
|
||||
No
|
||||
{noText}
|
||||
</button>
|
||||
<button ref={this._setYesButtonRef} className="btn" onClick={this._handleYes}>
|
||||
Yes
|
||||
{yesText}
|
||||
</button>
|
||||
</div>
|
||||
</ModalFooter>
|
||||
|
||||
@@ -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);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user