insomnia/app/ui/redux/modules/global.js

221 lines
5.4 KiB
JavaScript
Raw Normal View History

import electron from 'electron';
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
import {combineReducers} from 'redux';
import fs from 'fs';
2016-07-07 20:10:55 +00:00
import {importJSON, exportJSON} from '../../../backend/export/database';
2016-10-27 03:41:30 +00:00
import {trackEvent} from '../../../backend/ganalytics';
2016-07-07 20:10:55 +00:00
const LOAD_START = 'global/load-start';
const LOAD_STOP = 'global/load-stop';
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
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-04-23 06:08:52 +00:00
// ~~~~~~~~ //
// REDUCERS //
// ~~~~~~~~ //
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
function workspaceMetaReducer (state = {}, action) {
let newState;
switch (action.type) {
case SET_PANE_WIDTH:
const {width: paneWidth} = action;
newState = Object.assign({}, state);
newState[action.workspaceId] = newState[action.workspaceId] || {};
newState[action.workspaceId].paneWidth = paneWidth || '';
return newState;
case SET_SIDEBAR_WIDTH:
let {width: sidebarWidth} = action;
newState = Object.assign({}, state);
newState[action.workspaceId] = newState[action.workspaceId] || {};
newState[action.workspaceId].sidebarWidth = sidebarWidth || '';
return newState;
case CHANGE_FILTER:
let {filter} = action;
newState = Object.assign({}, state);
newState[action.workspaceId] = newState[action.workspaceId] || {};
newState[action.workspaceId].filter = filter || '';
return newState;
case TOGGLE_SIDEBAR:
newState = Object.assign({}, state);
newState[action.workspaceId] = newState[action.workspaceId] || {};
const hidden = newState[action.workspaceId].sidebarHidden;
newState[action.workspaceId].sidebarHidden = !hidden;
return newState;
case REQUEST_ACTIVATE:
const {requestId} = action;
newState = Object.assign({}, state);
newState[action.workspaceId] = newState[action.workspaceId] || {};
newState[action.workspaceId].activeRequestId = requestId;
return newState;
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) {
case LOAD_START:
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
return true;
2016-04-23 06:08:52 +00:00
case LOAD_STOP:
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
return false;
2016-04-23 06:08:52 +00:00
default:
return state;
}
}
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
export default combineReducers({
loading: loadingReducer,
workspaceMeta: workspaceMetaReducer,
activeWorkspaceId: activeWorkspaceReducer,
});
2016-04-23 06:08:52 +00:00
// ~~~~~~~ //
// ACTIONS //
// ~~~~~~~ //
export function loadStart () {
return {type: LOAD_START};
2016-04-23 06:08:52 +00:00
}
export function loadStop () {
return {type: LOAD_STOP};
2016-04-23 06:08:52 +00:00
}
2016-07-07 20:10:55 +00:00
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
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 = {
title: 'Import Insomnia Data',
buttonLabel: 'Import',
2016-07-07 20:10:55 +00:00
properties: ['openFile'],
filters: [{
// 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-10-27 03:41:30 +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', async (err, data) => {
dispatch(loadStop());
if (err) {
2016-10-27 03:41:30 +00:00
trackEvent('Import', 'Failure');
console.warn('Import Failed', err);
return;
}
importJSON(workspace, data);
2016-10-27 03:41:30 +00:00
trackEvent('Import', 'Success');
});
2016-07-07 20:10:55 +00:00
})
});
}
}
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
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) {
return async dispatch => {
dispatch(loadStart());
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-10-27 03:41:30 +00:00
trackEvent('Export', 'Cancel');
// 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');
return;
}
2016-10-27 03:41:30 +00:00
trackEvent('Export', 'Success');
dispatch(loadStop());
});
});
}
}