2016-07-16 02:06:10 +00:00
|
|
|
import electron from 'electron';
|
2016-10-21 17:20:36 +00:00
|
|
|
import {combineReducers} from 'redux';
|
2016-07-19 04:01:31 +00:00
|
|
|
import fs from 'fs';
|
2016-07-07 20:10:55 +00:00
|
|
|
|
2016-11-19 07:11:10 +00:00
|
|
|
import {importRaw, exportJSON} from '../../../common/import';
|
2016-11-10 05:47:20 +00:00
|
|
|
import {trackEvent} from '../../../analytics';
|
2016-11-07 20:24:38 +00:00
|
|
|
import AlertModal from '../../components/modals/AlertModal';
|
2016-11-19 07:11:10 +00:00
|
|
|
import {showModal} from '../../components/modals';
|
2016-11-17 23:22:23 +00:00
|
|
|
import PaymentNotificationModal from '../../components/modals/PaymentNotificationModal';
|
2016-11-07 20:24:38 +00:00
|
|
|
import LoginModal from '../../components/modals/LoginModal';
|
2016-11-16 17:18:39 +00:00
|
|
|
import * as models from '../../../models';
|
|
|
|
|
|
|
|
const LOCALSTORAGE_PREFIX = `insomnia::meta`;
|
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-11-12 00:58:23 +00:00
|
|
|
const REQUEST_GROUP_TOGGLE_COLLAPSE = 'global/request-group-toggle';
|
2016-11-16 17:18:39 +00:00
|
|
|
const SET_ACTIVE_WORKSPACE = 'global/activate-workspace';
|
2016-11-07 20:24:38 +00:00
|
|
|
const COMMAND_ALERT = 'app/alert';
|
|
|
|
const COMMAND_LOGIN = 'app/auth/login';
|
|
|
|
const COMMAND_TRIAL_END = 'app/billing/trial-end';
|
2016-04-23 06:08:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ~~~~~~~~ //
|
|
|
|
// REDUCERS //
|
|
|
|
// ~~~~~~~~ //
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
function activeWorkspaceReducer (state = null, action) {
|
2016-10-21 17:20:36 +00:00
|
|
|
switch (action.type) {
|
2016-11-16 17:18:39 +00:00
|
|
|
case SET_ACTIVE_WORKSPACE:
|
|
|
|
return action.workspaceId;
|
2016-10-21 17:20:36 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadingReducer (state = false, action) {
|
2016-04-23 06:08:52 +00:00
|
|
|
switch (action.type) {
|
2016-07-06 20:18:26 +00:00
|
|
|
case LOAD_START:
|
2016-10-21 17:20:36 +00:00
|
|
|
return true;
|
2016-07-06 20:18:26 +00:00
|
|
|
case LOAD_STOP:
|
2016-10-21 17:20:36 +00:00
|
|
|
return false;
|
2016-11-07 20:24:38 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
2016-04-23 06:08:52 +00:00
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
export default combineReducers({
|
2016-11-16 17:18:39 +00:00
|
|
|
isLoading: loadingReducer,
|
2016-10-21 17:20:36 +00:00
|
|
|
activeWorkspaceId: activeWorkspaceReducer,
|
|
|
|
});
|
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
|
|
|
|
// ~~~~~~~ //
|
|
|
|
// ACTIONS //
|
|
|
|
// ~~~~~~~ //
|
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
export function newCommand (command, args) {
|
|
|
|
// TODO: Make this use reducer when Modals ported to Redux
|
|
|
|
if (command === COMMAND_ALERT) {
|
|
|
|
const {message, title} = args;
|
|
|
|
showModal(AlertModal, {title, message});
|
|
|
|
} else if (command === COMMAND_LOGIN) {
|
2016-11-12 00:58:23 +00:00
|
|
|
const {title, message} = args;
|
|
|
|
showModal(LoginModal, {title, message});
|
2016-11-07 20:24:38 +00:00
|
|
|
} else if (command === COMMAND_TRIAL_END) {
|
2016-11-17 23:22:23 +00:00
|
|
|
showModal(PaymentNotificationModal);
|
2016-11-07 20:24:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {type: command, ...args};
|
|
|
|
}
|
|
|
|
|
2016-04-23 06:08:52 +00:00
|
|
|
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
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
export function setActiveWorkspace (workspaceId) {
|
|
|
|
localStorage.setItem(`${LOCALSTORAGE_PREFIX}::activeWorkspaceId`, JSON.stringify(workspaceId));
|
|
|
|
return {type: SET_ACTIVE_WORKSPACE, workspaceId};
|
2016-10-21 17:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 00:58:23 +00:00
|
|
|
export function toggleRequestGroup (requestGroup) {
|
|
|
|
return {
|
|
|
|
type: REQUEST_GROUP_TOGGLE_COLLAPSE,
|
|
|
|
requestGroupId: requestGroup._id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
export function importFile (workspaceId) {
|
|
|
|
return async dispatch => {
|
2016-07-07 20:10:55 +00:00
|
|
|
dispatch(loadStart());
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
const workspace = await models.workspace.getById(workspaceId);
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
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
|
2016-11-30 23:11:36 +00:00
|
|
|
name: 'Insomnia Import', extensions: [
|
|
|
|
'', 'sh', 'txt', 'json', 'har', 'curl', 'bash', 'shell'
|
|
|
|
]
|
2016-07-07 20:10:55 +00:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
2016-11-20 07:43:22 +00:00
|
|
|
electron.remote.dialog.showOpenDialog(options, async paths => {
|
2016-07-07 20:10:55 +00:00
|
|
|
if (!paths) {
|
|
|
|
// It was cancelled, so let's bail out
|
|
|
|
dispatch(loadStop());
|
2016-11-07 20:24:38 +00:00
|
|
|
trackEvent('Import', 'Cancel');
|
2016-07-07 20:10:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's import all the paths!
|
2016-11-20 07:43:22 +00:00
|
|
|
for (const path of paths) {
|
|
|
|
try {
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
2016-10-02 20:57:00 +00:00
|
|
|
dispatch(loadStop());
|
|
|
|
|
2016-12-01 00:02:35 +00:00
|
|
|
const {summary, source} = await importRaw(workspace, data);
|
2016-11-20 07:43:22 +00:00
|
|
|
|
|
|
|
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);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2016-11-20 07:43:22 +00:00
|
|
|
let message;
|
|
|
|
if (statements.length === 0) {
|
|
|
|
message = 'Nothing was found to import.';
|
|
|
|
} else {
|
|
|
|
message = `You imported ${statements.join(', ')}!`;
|
|
|
|
}
|
|
|
|
showModal(AlertModal, {title: 'Import Succeeded', message});
|
2016-12-01 00:02:35 +00:00
|
|
|
trackEvent('Import', source, 'Success');
|
2016-11-20 07:43:22 +00:00
|
|
|
} catch (e) {
|
|
|
|
showModal(AlertModal, {title: 'Import Failed', message: e + ''});
|
2016-12-01 00:02:35 +00:00
|
|
|
trackEvent('Import', source, 'Failure');
|
2016-11-20 07:43:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-07 20:10:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-07-19 04:01:31 +00:00
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
export function exportFile (workspaceId = null) {
|
2016-10-02 20:57:00 +00:00
|
|
|
return async dispatch => {
|
2016-07-19 04:01:31 +00:00
|
|
|
dispatch(loadStart());
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
const workspace = await models.workspace.getById(workspaceId);
|
|
|
|
const json = await exportJSON(workspace);
|
2016-10-02 20:57:00 +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-11-07 20:24:38 +00:00
|
|
|
trackEvent('Export', 'Cancel');
|
2016-10-02 20:57:00 +00:00
|
|
|
// It was cancelled, so let's bail out
|
|
|
|
dispatch(loadStop());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFile(filename, json, {}, err => {
|
|
|
|
if (err) {
|
|
|
|
console.warn('Export failed', err);
|
2016-10-27 03:41:30 +00:00
|
|
|
trackEvent('Export', 'Failure');
|
2016-07-19 04:01:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-10-27 03:41:30 +00:00
|
|
|
trackEvent('Export', 'Success');
|
2016-10-02 20:57:00 +00:00
|
|
|
dispatch(loadStop());
|
2016-07-19 04:01:31 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 17:18:39 +00:00
|
|
|
|
|
|
|
export function init () {
|
|
|
|
let workspaceId = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
workspaceId = JSON.parse(localStorage.getItem(`${LOCALSTORAGE_PREFIX}::activeWorkspaceId`));
|
|
|
|
} catch (e) {
|
|
|
|
// Nothing here...
|
|
|
|
}
|
|
|
|
|
|
|
|
return setActiveWorkspace(workspaceId);
|
|
|
|
}
|