diff --git a/app/common/import.js b/app/common/import.js index de9c37207..1dafb01b5 100644 --- a/app/common/import.js +++ b/app/common/import.js @@ -41,6 +41,14 @@ export async function importRaw (workspace, rawContent, generateNewIds = false) // Also always replace __WORKSPACE_ID__ with the current workspace if we see it generatedIds['__WORKSPACE_ID__'] = workspace._id; + // Import everything backwards so they get inserted in the correct order + data.resources.reverse(); + + const importedDocs = {}; + for (const model of models.all()) { + importedDocs[model.type] = []; + } + for (const resource of data.resources) { // Buffer DB changes // NOTE: Doing it inside here so it's more "scalable" @@ -67,15 +75,16 @@ export async function importRaw (workspace, rawContent, generateNewIds = false) } const doc = await model.getById(resource._id); + const newDoc = doc ? + await model.update(doc, resource) : + await model.create(resource); - if (doc) { - await model.update(doc, resource) - } else { - await model.create(resource) - } + importedDocs[newDoc.type].push(newDoc); } db.flushChanges(); + + return importedDocs; } export async function exportJSON (parentDoc = null) { diff --git a/app/models/cookieJar.js b/app/models/cookieJar.js index 56e152ae6..d80336348 100644 --- a/app/models/cookieJar.js +++ b/app/models/cookieJar.js @@ -1,5 +1,6 @@ import * as db from '../common/database'; +export const name = 'Cookie Jar'; export const type = 'CookieJar'; export const prefix = 'jar'; export function init () { diff --git a/app/models/environment.js b/app/models/environment.js index a3fb96f92..0001aeeeb 100644 --- a/app/models/environment.js +++ b/app/models/environment.js @@ -1,5 +1,6 @@ import * as db from '../common/database'; +export const name = 'Environment'; export const type = 'Environment'; export const prefix = 'env'; export function init () { diff --git a/app/models/index.js b/app/models/index.js index 37d8a64f9..3431ddf51 100644 --- a/app/models/index.js +++ b/app/models/index.js @@ -40,6 +40,20 @@ export function getModel (type) { return _models[type] || null; } +export function getModelName (type, count = 1) { + const model = getModel(type); + if (!model) { + return 'Unknown'; + } else if (count === 1) { + return model.name; + } else if (!model.name.match(/s$/)) { + // Add an 's' if it doesn't already end in one + return `${model.name}s`; + } else { + return model.name + } +} + export function initModel (type) { const baseDefaults = { type: type, diff --git a/app/models/request.js b/app/models/request.js index bdd937a00..161d3934b 100644 --- a/app/models/request.js +++ b/app/models/request.js @@ -2,6 +2,7 @@ import {METHOD_GET} from '../common/constants'; import * as db from '../common/database'; import {getContentTypeHeader} from '../common/misc'; +export const name = 'Request'; export const type = 'Request'; export const prefix = 'req'; diff --git a/app/models/requestGroup.js b/app/models/requestGroup.js index 858bf6f6d..5e112293e 100644 --- a/app/models/requestGroup.js +++ b/app/models/requestGroup.js @@ -1,5 +1,6 @@ import * as db from '../common/database'; +export const name = 'Folder'; export const type = 'RequestGroup'; export const prefix = 'fld'; export function init () { diff --git a/app/models/response.js b/app/models/response.js index 18b663539..c03362077 100644 --- a/app/models/response.js +++ b/app/models/response.js @@ -1,5 +1,6 @@ import * as db from '../common/database'; +export const name = 'Response'; export const type = 'Response'; export const prefix = 'res'; export function init () { diff --git a/app/models/settings.js b/app/models/settings.js index 6e17a3f2f..4fb0f8b0f 100644 --- a/app/models/settings.js +++ b/app/models/settings.js @@ -1,5 +1,6 @@ import * as db from '../common/database'; +export const name = 'Settings'; export const type = 'Settings'; export const prefix = 'set'; export function init () { diff --git a/app/models/stats.js b/app/models/stats.js index 8e0499225..009595f06 100644 --- a/app/models/stats.js +++ b/app/models/stats.js @@ -1,5 +1,6 @@ import * as db from '../common/database'; +export const name = 'Stats'; export const type = 'Stats'; export const prefix = 'sta'; export function init () { diff --git a/app/models/workspace.js b/app/models/workspace.js index 7e7a52c4b..e57de6e5c 100644 --- a/app/models/workspace.js +++ b/app/models/workspace.js @@ -1,5 +1,6 @@ import * as db from '../common/database'; +export const name = 'Workspace'; export const type = 'Workspace'; export const prefix = 'wrk'; export function init () { diff --git a/app/ui/components/modals/AlertModal.js b/app/ui/components/modals/AlertModal.js index 841b2758d..024050ae9 100644 --- a/app/ui/components/modals/AlertModal.js +++ b/app/ui/components/modals/AlertModal.js @@ -10,8 +10,10 @@ class AlertModal extends Component { this.state = {}; } - show ({title, message}) { + show (options = {}) { this.modal.show(); + + const {title, message} = options; this.setState({title, message}); } diff --git a/app/ui/css/components/keyvalueeditor.less b/app/ui/css/components/keyvalueeditor.less index bacc9c15e..d7a27f6e2 100644 --- a/app/ui/css/components/keyvalueeditor.less +++ b/app/ui/css/components/keyvalueeditor.less @@ -34,6 +34,7 @@ &:hover { color: inherit; + background: transparent; } } } diff --git a/app/ui/redux/modules/global.js b/app/ui/redux/modules/global.js index edf6d1879..171bcf1b5 100644 --- a/app/ui/redux/modules/global.js +++ b/app/ui/redux/modules/global.js @@ -139,7 +139,7 @@ export function importFile (workspaceId) { }] }; - electron.remote.dialog.showOpenDialog(options, paths => { + electron.remote.dialog.showOpenDialog(options, async paths => { if (!paths) { // It was cancelled, so let's bail out dispatch(loadStop()); @@ -148,20 +148,32 @@ export function importFile (workspaceId) { } // Let's import all the paths! - paths.map(path => { - fs.readFile(path, 'utf8', async (err, data) => { + for (const path of paths) { + try { + const data = fs.readFileSync(path, 'utf8'); dispatch(loadStop()); - if (err) { - trackEvent('Import', 'Failure'); - console.warn('Import Failed', err); - return; - } + const summary = await importRaw(workspace, data); - importRaw(workspace, data); + let statements = Object.keys(summary).map(type => { + const count = summary[type].length; + const name = models.getModelName(type, count); + return count === 0 ? null :`${count} ${name}`; + }).filter(s => s !== null); + + let message; + if (statements.length === 0) { + message = 'Nothing was found to import.'; + } else { + message = `You imported ${statements.join(', ')}!`; + } + showModal(AlertModal, {title: 'Import Succeeded', message}); trackEvent('Import', 'Success'); - }); - }) + } catch (e) { + showModal(AlertModal, {title: 'Import Failed', message: e + ''}); + trackEvent('Import', 'Failure', e); + } + } }); } }