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';
|
2017-03-08 05:52:17 +00:00
|
|
|
import AlertModal from '../../components/modals/alert-modal';
|
2016-11-19 07:11:10 +00:00
|
|
|
import {showModal} from '../../components/modals';
|
2017-03-08 05:52:17 +00:00
|
|
|
import PaymentNotificationModal from '../../components/modals/payment-notification-modal';
|
|
|
|
import LoginModal from '../../components/modals/login-modal';
|
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-12-01 01:50:59 +00:00
|
|
|
const LOAD_REQUEST_START = 'global/load-request-start';
|
|
|
|
const LOAD_REQUEST_STOP = 'global/load-request-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-12-01 01:50:59 +00:00
|
|
|
function loadingRequestsReducer (state = {}, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case LOAD_REQUEST_START:
|
|
|
|
return Object.assign({}, state, {[action.requestId]: action.time});
|
|
|
|
case LOAD_REQUEST_STOP:
|
|
|
|
return Object.assign({}, state, {[action.requestId]: -1});
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
export const reducer = combineReducers({
|
2016-11-16 17:18:39 +00:00
|
|
|
isLoading: loadingReducer,
|
2016-12-01 01:50:59 +00:00
|
|
|
loadingRequestIds: loadingRequestsReducer,
|
2017-03-03 20:09:08 +00:00
|
|
|
activeWorkspaceId: activeWorkspaceReducer
|
2016-10-21 17:20:36 +00:00
|
|
|
});
|
|
|
|
|
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-12-01 01:50:59 +00:00
|
|
|
export function loadRequestStart (requestId) {
|
|
|
|
return {type: LOAD_REQUEST_START, requestId, time: Date.now()};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loadRequestStop (requestId) {
|
|
|
|
return {type: LOAD_REQUEST_STOP, requestId};
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
export function setActiveWorkspace (workspaceId) {
|
2017-03-03 20:09:08 +00:00
|
|
|
window.localStorage.setItem(`${LOCALSTORAGE_PREFIX}::activeWorkspaceId`, JSON.stringify(workspaceId));
|
2016-11-16 17:18:39 +00:00
|
|
|
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
|
2017-03-03 20:09:08 +00:00
|
|
|
name: 'Insomnia Import',
|
|
|
|
extensions: [
|
2016-11-30 23:11:36 +00:00
|
|
|
'', '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 03:54:26 +00:00
|
|
|
const result = await importRaw(workspace, data);
|
|
|
|
const {summary, source, error} = result;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
showModal(AlertModal, {title: 'Import Failed', message: error});
|
|
|
|
return;
|
|
|
|
}
|
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);
|
2016-12-01 03:54:26 +00:00
|
|
|
return count === 0 ? null : `${count} ${name}`;
|
2016-11-20 07:43:22 +00:00
|
|
|
}).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 03:54:26 +00:00
|
|
|
trackEvent('Import', 'Success', source);
|
2016-11-20 07:43:22 +00:00
|
|
|
} catch (e) {
|
|
|
|
showModal(AlertModal, {title: 'Import Failed', message: e + ''});
|
2016-12-01 03:54:26 +00:00
|
|
|
trackEvent('Import', 'Failure');
|
2016-11-20 07:43:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-07 20:10:55 +00:00
|
|
|
});
|
2017-03-03 20:09:08 +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
|
|
|
});
|
|
|
|
});
|
2017-03-03 20:09:08 +00:00
|
|
|
};
|
2016-07-19 04:01:31 +00:00
|
|
|
}
|
2016-11-16 17:18:39 +00:00
|
|
|
|
|
|
|
export function init () {
|
|
|
|
let workspaceId = null;
|
|
|
|
|
|
|
|
try {
|
2017-03-03 20:09:08 +00:00
|
|
|
const key = `${LOCALSTORAGE_PREFIX}::activeWorkspaceId`;
|
|
|
|
const item = window.localStorage.getItem(key);
|
|
|
|
workspaceId = JSON.parse(item);
|
2016-11-16 17:18:39 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Nothing here...
|
|
|
|
}
|
|
|
|
|
|
|
|
return setActiveWorkspace(workspaceId);
|
|
|
|
}
|