Basic import summary

This commit is contained in:
Gregory Schier 2016-11-19 23:43:22 -08:00
parent 5f7c2ee18f
commit bd4076b6ff
13 changed files with 63 additions and 17 deletions

View File

@ -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) {

View File

@ -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 () {

View File

@ -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 () {

View File

@ -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,

View File

@ -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';

View File

@ -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 () {

View File

@ -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 () {

View File

@ -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 () {

View File

@ -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 () {

View File

@ -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 () {

View File

@ -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});
}

View File

@ -34,6 +34,7 @@
&:hover {
color: inherit;
background: transparent;
}
}
}

View File

@ -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);
}
}
});
}
}