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-10-02 20:57:00 +00:00
|
|
|
import {importJSON, exportJSON} from '../../../backend/export/database';
|
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';
|
|
|
|
import {showModal} from '../../components/modals/index';
|
|
|
|
import PaymentModal from '../../components/modals/PaymentModal';
|
|
|
|
import LoginModal from '../../components/modals/LoginModal';
|
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-10-21 17:20:36 +00:00
|
|
|
const REQUEST_ACTIVATE = 'global/request-activate';
|
|
|
|
const CHANGE_FILTER = 'global/change-filter';
|
|
|
|
const TOGGLE_SIDEBAR = 'global/toggle-sidebar';
|
|
|
|
const ACTIVATE_WORKSPACE = 'global/activate-workspace';
|
|
|
|
const SET_SIDEBAR_WIDTH = 'global/set-sidebar-width';
|
|
|
|
const SET_PANE_WIDTH = 'global/set-pane-width';
|
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-07 20:24:38 +00:00
|
|
|
/** Helper to update workspace metadata */
|
|
|
|
function updateMeta (state = {}, workspaceId, value, key) {
|
|
|
|
const newState = Object.assign({}, state);
|
|
|
|
newState[workspaceId] = newState[workspaceId] || {};
|
|
|
|
newState[workspaceId][key] = value;
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
function workspaceMetaReducer (state = {}, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case SET_PANE_WIDTH:
|
2016-11-07 20:24:38 +00:00
|
|
|
return updateMeta(state, action.workspaceId, action.width, 'paneWidth');
|
2016-10-21 17:20:36 +00:00
|
|
|
case SET_SIDEBAR_WIDTH:
|
2016-11-07 20:24:38 +00:00
|
|
|
return updateMeta(state, action.workspaceId, action.width, 'sidebarWidth');
|
2016-10-21 17:20:36 +00:00
|
|
|
case CHANGE_FILTER:
|
2016-11-07 20:24:38 +00:00
|
|
|
return updateMeta(state, action.workspaceId, action.filter, 'filter');
|
2016-10-21 17:20:36 +00:00
|
|
|
case TOGGLE_SIDEBAR:
|
2016-11-07 20:24:38 +00:00
|
|
|
const meta = state[action.workspaceId];
|
|
|
|
const on = meta ? !meta.sidebarHidden : false;
|
|
|
|
return updateMeta(state, action.workspaceId, on, 'sidebarHidden');
|
2016-10-21 17:20:36 +00:00
|
|
|
case REQUEST_ACTIVATE:
|
2016-11-07 20:24:38 +00:00
|
|
|
return updateMeta(
|
|
|
|
state,
|
|
|
|
action.workspaceId,
|
|
|
|
action.requestId,
|
|
|
|
'activeRequestId'
|
|
|
|
);
|
2016-10-21 17:20:36 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function activeWorkspaceReducer (state = '', action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case ACTIVATE_WORKSPACE:
|
|
|
|
return action.workspace._id;
|
|
|
|
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-11-07 20:24:38 +00:00
|
|
|
function commandReducer (state = {}, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
// Nothing yet...
|
2016-04-23 06:08:52 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
export default combineReducers({
|
|
|
|
loading: loadingReducer,
|
|
|
|
workspaceMeta: workspaceMetaReducer,
|
|
|
|
activeWorkspaceId: activeWorkspaceReducer,
|
2016-11-07 20:24:38 +00:00
|
|
|
command: commandReducer,
|
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) {
|
|
|
|
const {title, message} = args;
|
|
|
|
showModal(LoginModal, {title, message});
|
|
|
|
} else if (command === COMMAND_TRIAL_END) {
|
|
|
|
const {message, title} = args;
|
|
|
|
showModal(PaymentModal, {message, title});
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-21 17:20:36 +00:00
|
|
|
export function activateWorkspace (workspace) {
|
|
|
|
return {type: ACTIVATE_WORKSPACE, workspace};
|
|
|
|
}
|
|
|
|
|
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-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!
|
|
|
|
paths.map(path => {
|
2016-10-02 20:57:00 +00:00
|
|
|
fs.readFile(path, 'utf8', async (err, data) => {
|
|
|
|
dispatch(loadStop());
|
|
|
|
|
|
|
|
if (err) {
|
2016-10-27 03:41:30 +00:00
|
|
|
trackEvent('Import', 'Failure');
|
2016-10-02 20:57:00 +00:00
|
|
|
console.warn('Import Failed', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
importJSON(workspace, data);
|
2016-10-27 03:41:30 +00:00
|
|
|
trackEvent('Import', 'Success');
|
2016-10-02 20:57:00 +00:00
|
|
|
});
|
2016-07-07 20:10:55 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-07-19 04:01:31 +00:00
|
|
|
|
2016-10-21 17:20:36 +00:00
|
|
|
export function setPaneWidth (workspace, width) {
|
|
|
|
return {
|
|
|
|
type: SET_PANE_WIDTH,
|
|
|
|
workspaceId: workspace._id,
|
|
|
|
width: width
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setSidebarWidth (workspace, width) {
|
|
|
|
return {
|
|
|
|
type: SET_SIDEBAR_WIDTH,
|
|
|
|
workspaceId: workspace._id,
|
|
|
|
width: width
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function toggleSidebar (workspace) {
|
|
|
|
return {
|
|
|
|
type: TOGGLE_SIDEBAR,
|
|
|
|
workspaceId: workspace._id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function changeFilter (workspace, filter) {
|
|
|
|
return {
|
|
|
|
type: CHANGE_FILTER,
|
|
|
|
filter: filter,
|
|
|
|
workspaceId: workspace._id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function activateRequest (workspace, request) {
|
|
|
|
return {
|
|
|
|
type: REQUEST_ACTIVATE,
|
|
|
|
requestId: request._id,
|
|
|
|
workspaceId: workspace._id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:31:30 +00:00
|
|
|
export function exportFile (parentDoc = null) {
|
2016-10-02 20:57:00 +00:00
|
|
|
return async dispatch => {
|
2016-07-19 04:01:31 +00:00
|
|
|
dispatch(loadStart());
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
const json = await exportJSON(parentDoc);
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|