2016-07-16 02:06:10 +00:00
|
|
|
import electron from 'electron';
|
2016-07-19 04:01:31 +00:00
|
|
|
import fs from 'fs';
|
2016-07-07 20:10:55 +00:00
|
|
|
|
2016-07-19 04:01:31 +00:00
|
|
|
import {importJSON, exportJSON} from '../../lib/export/database';
|
2016-08-15 22:31:30 +00:00
|
|
|
import * as db from '../../database/index';
|
2016-08-22 19:05:17 +00:00
|
|
|
import {trackEvent} from '../../lib/analytics';
|
2016-07-07 20:10:55 +00:00
|
|
|
|
2016-07-06 20:18:26 +00:00
|
|
|
const LOAD_START = 'global/load-start';
|
|
|
|
const LOAD_STOP = 'global/load-stop';
|
2016-04-23 06:08:52 +00:00
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
loading: false
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ~~~~~~~~ //
|
|
|
|
// REDUCERS //
|
|
|
|
// ~~~~~~~~ //
|
|
|
|
|
|
|
|
export default function (state = initialState, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
|
2016-07-06 20:18:26 +00:00
|
|
|
case LOAD_START:
|
2016-04-23 06:08:52 +00:00
|
|
|
return Object.assign({}, state, {loading: true});
|
|
|
|
|
2016-07-06 20:18:26 +00:00
|
|
|
case LOAD_STOP:
|
2016-04-23 06:08:52 +00:00
|
|
|
return Object.assign({}, state, {loading: false});
|
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ~~~~~~~ //
|
|
|
|
// ACTIONS //
|
|
|
|
// ~~~~~~~ //
|
|
|
|
|
|
|
|
export function loadStart () {
|
2016-07-06 20:18:26 +00:00
|
|
|
return {type: LOAD_START};
|
2016-04-23 06:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function loadStop () {
|
2016-07-06 20:18:26 +00:00
|
|
|
return {type: LOAD_STOP};
|
2016-04-23 06:08:52 +00:00
|
|
|
}
|
2016-07-07 20:10:55 +00:00
|
|
|
|
|
|
|
export function importFile (workspace) {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch(loadStart());
|
|
|
|
|
|
|
|
const options = {
|
2016-07-19 04:01:31 +00:00
|
|
|
title: 'Import Insomnia Data',
|
|
|
|
buttonLabel: 'Import',
|
2016-07-07 20:10:55 +00:00
|
|
|
properties: ['openFile'],
|
|
|
|
filters: [{
|
2016-08-22 19:05:17 +00:00
|
|
|
// Allow empty extension and JSON
|
|
|
|
name: 'Insomnia Import', extensions: ['', 'json']
|
2016-07-07 20:10:55 +00:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
electron.remote.dialog.showOpenDialog(options, paths => {
|
|
|
|
if (!paths) {
|
|
|
|
// It was cancelled, so let's bail out
|
|
|
|
dispatch(loadStop());
|
2016-08-22 19:05:17 +00:00
|
|
|
trackEvent('Import Cancel');
|
2016-07-07 20:10:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's import all the paths!
|
|
|
|
paths.map(path => {
|
|
|
|
fs.readFile(path, 'utf8', (err, data) => {
|
2016-08-15 22:31:30 +00:00
|
|
|
// Unset the current active request first because we might be updating it
|
|
|
|
db.workspaceUpdate(workspace, {metaActiveRequestId: null}).then(() => {
|
|
|
|
dispatch(loadStop());
|
2016-08-22 19:05:17 +00:00
|
|
|
if (err) {
|
|
|
|
trackEvent('Import Fail');
|
|
|
|
console.warn('Import Failed', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
importJSON(workspace, data);
|
|
|
|
trackEvent('Import');
|
2016-08-15 22:31:30 +00:00
|
|
|
});
|
2016-07-07 20:10:55 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-07-19 04:01:31 +00:00
|
|
|
|
2016-08-15 22:31:30 +00:00
|
|
|
export function exportFile (parentDoc = null) {
|
2016-07-19 04:01:31 +00:00
|
|
|
return dispatch => {
|
|
|
|
dispatch(loadStart());
|
|
|
|
|
2016-08-15 22:31:30 +00:00
|
|
|
exportJSON(parentDoc).then(json => {
|
2016-07-19 04:01:31 +00:00
|
|
|
const options = {
|
|
|
|
title: 'Export Insomnia Data',
|
|
|
|
buttonLabel: 'Export',
|
|
|
|
filters: [{
|
|
|
|
name: 'Insomnia Export', extensions: ['json']
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
electron.remote.dialog.showSaveDialog(options, filename => {
|
|
|
|
if (!filename) {
|
2016-08-22 19:05:17 +00:00
|
|
|
trackEvent('Export Cancel');
|
2016-07-19 04:01:31 +00:00
|
|
|
// It was cancelled, so let's bail out
|
|
|
|
dispatch(loadStop());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFile(filename, json, {}, err => {
|
2016-08-22 19:05:17 +00:00
|
|
|
if (err) {
|
|
|
|
console.warn('Export failed', err);
|
|
|
|
trackEvent('Export Fail');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trackEvent('Export');
|
2016-07-19 04:01:31 +00:00
|
|
|
dispatch(loadStop());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|