From 5b12b3d6984d490d113780f88198f6685e8b53e4 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Sun, 24 Apr 2016 23:23:26 -0700 Subject: [PATCH] Better store stuff --- app/containers/RequestGroupActionsDropdown.js | 2 -- app/database/index.js | 23 +++++++++++++++---- app/lib/import.js | 20 ++++++++-------- app/redux/initstore.js | 23 +++++++++---------- app/redux/modules/entities.js | 13 +++-------- app/redux/modules/requestGroups.js | 22 ------------------ 6 files changed, 42 insertions(+), 61 deletions(-) diff --git a/app/containers/RequestGroupActionsDropdown.js b/app/containers/RequestGroupActionsDropdown.js index 1ae002b371..a950282fdc 100644 --- a/app/containers/RequestGroupActionsDropdown.js +++ b/app/containers/RequestGroupActionsDropdown.js @@ -38,8 +38,6 @@ class RequestGroupActionsDropdown extends Component { RequestGroupActionsDropdown.propTypes = { actions: PropTypes.shape({ - update: PropTypes.func.isRequired, - remove: PropTypes.func.isRequired, showUpdateNamePrompt: PropTypes.func.isRequired, showEnvironmentEditModal: PropTypes.func.isRequired }), diff --git a/app/database/index.js b/app/database/index.js index 7958267b1b..9bb5d51bc1 100644 --- a/app/database/index.js +++ b/app/database/index.js @@ -27,6 +27,10 @@ export function offChange (id) { delete changeListeners[id]; } +export function allDocs () { + return db.allDocs({include_docs: true}); +} + db.changes({ since: 'now', live: true, @@ -48,8 +52,7 @@ export function initDB () { console.log('-- Initializing Database --'); return Promise.all([ db.createIndex({index: {fields: ['parentId']}}), - db.createIndex({index: {fields: ['type']}}), - db.createIndex({index: {fields: ['activated', 'type']}}) + db.createIndex({index: {fields: ['type']}}) ]).catch(err => { console.error('Failed to PouchDB Indexes', err); }); @@ -103,7 +106,7 @@ function modelCreate (type, idPrefix, defaults, patch = {}) { parentId: null }; - const model = Object.assign( + const doc = Object.assign( baseDefaults, defaults, patch, @@ -118,8 +121,7 @@ function modelCreate (type, idPrefix, defaults, patch = {}) { } ); - update(model); - return model; + return update(doc).then(() => doc); } // ~~~~~~~ // @@ -195,6 +197,17 @@ export function workspaceActivate (workspace) { export function workspaceAll () { return db.find({ selector: {type: 'Workspace'} + }).then(res => { + if (res.docs.length) { + return res; + } else { + // No workspaces? Create first one and try again + // TODO: Replace this with UI flow maybe? + console.log('-- Creating First Workspace --'); + return workspaceCreate({name: 'First Workspace'}).then(() => { + return workspaceAll(); + }) + } }) } diff --git a/app/lib/import.js b/app/lib/import.js index ec12f5ead6..d70496f7a9 100644 --- a/app/lib/import.js +++ b/app/lib/import.js @@ -9,21 +9,21 @@ const FORMAT_MAP = { function importRequestGroup (iRequestGroup, parentId, exportFormat) { if (exportFormat === 1) { - const requestGroup = db.requestGroupCreate({ + db.requestGroupCreate({ parentId, collapsed: true, name: iRequestGroup.name, environment: (iRequestGroup.environments || {}).base || {} + }).then(requestGroup => { + // Sometimes (maybe all the time, I can't remember) requests will be nested + if (iRequestGroup.hasOwnProperty('requests')) { + // Let's process them oldest to newest + iRequestGroup.requests.reverse(); + iRequestGroup.requests.map( + r => importRequest(r, requestGroup._id, exportFormat) + ); + } }); - - // Sometimes (maybe all the time, I can't remember) requests will be nested - if (iRequestGroup.hasOwnProperty('requests')) { - // Let's process them oldest to newest - iRequestGroup.requests.reverse(); - iRequestGroup.requests.map( - r => importRequest(r, requestGroup._id, exportFormat) - ); - } } } diff --git a/app/redux/initstore.js b/app/redux/initstore.js index 0555c23130..0ad9fc654b 100644 --- a/app/redux/initstore.js +++ b/app/redux/initstore.js @@ -6,7 +6,7 @@ const CHANGE_ID = 'store.listener'; export function initStore (dispatch) { db.offChange(CHANGE_ID); - + // New stuff... const entities = bindActionCreators(entitiesActions, dispatch); @@ -14,7 +14,7 @@ export function initStore (dispatch) { if (!doc.hasOwnProperty('type')) { return; } - + // New stuff... entities[doc._deleted ? 'remove' : 'update'](doc); }; @@ -22,28 +22,27 @@ export function initStore (dispatch) { console.log('-- Restoring Store --'); const start = Date.now(); - return db.workspaceAll().then(res => { - res.docs.map(docChanged); - }).then(() => { - return db.workspaceGetActive() - }).then(res => { - const workspace = res.docs.length ? res.docs[0] : db.workspaceCreate(); - const restoreChildren = (doc, maxDepth, depth = 0) => { + return db.workspaceAll().then(res => { + const restoreChildren = (doc) => { docChanged(doc); return db.getChildren(doc).then(res => { + // Done condition + if (!res.docs.length) { + return; + } + return Promise.all( - res.docs.map(doc => restoreChildren(doc, maxDepth, depth++)) + res.docs.map(doc => restoreChildren(doc)) ); }) }; - return restoreChildren(workspace, 5); + return res.docs.map(restoreChildren) }).then(() => { console.log(`Restore took ${(Date.now() - start) / 1000} s`); }).then(() => { db.onChange(CHANGE_ID, res => docChanged(res.doc)); }); } - diff --git a/app/redux/modules/entities.js b/app/redux/modules/entities.js index 8d2a5a6fb3..bc7563e313 100644 --- a/app/redux/modules/entities.js +++ b/app/redux/modules/entities.js @@ -2,7 +2,6 @@ import {combineReducers} from 'redux' import {TYPE_WORKSPACE, TYPE_REQUEST_GROUP, TYPE_REQUEST, TYPE_RESPONSE} from '../../database/index' import * as workspaceFns from './workspaces' -import * as requestGroupFns from './requestGroups' const ENTITY_UPDATE = 'entities/update'; const ENTITY_REMOVE = 'entities/remove'; @@ -60,15 +59,9 @@ const workspaces = generateEntityReducer( workspaceFns.WORKSPACE_DELETE ); -const requestGroups = generateEntityReducer( - 'requestGroup', - requestGroupFns.REQUEST_GROUP_UPDATE, - requestGroupFns.REQUEST_GROUP_DELETE -); - export default combineReducers({ workspaces, - requestGroups, + requestGroups: genericEntityReducer('requestGroup'), requests: genericEntityReducer('request'), responses: genericEntityReducer('response') }) @@ -80,14 +73,14 @@ export default combineReducers({ const updateFns = { [TYPE_WORKSPACE]: workspaceFns.update, - [TYPE_REQUEST_GROUP]: requestGroupFns.update, + [TYPE_REQUEST_GROUP]: requestGroup => ({type: ENTITY_UPDATE, requestGroup}), [TYPE_RESPONSE]: response => ({type: ENTITY_UPDATE, response}), [TYPE_REQUEST]: request => ({type: ENTITY_UPDATE, request}) }; const removeFns = { [TYPE_WORKSPACE]: workspaceFns.remove, - [TYPE_REQUEST_GROUP]: requestGroupFns.remove, + [TYPE_REQUEST_GROUP]: requestGroup => ({type: ENTITY_UPDATE, requestGroup}), [TYPE_RESPONSE]: response => ({type: ENTITY_UPDATE, response}), [TYPE_REQUEST]: request => ({type: ENTITY_REMOVE, request}) }; diff --git a/app/redux/modules/requestGroups.js b/app/redux/modules/requestGroups.js index aae168e926..661c8fdf3f 100644 --- a/app/redux/modules/requestGroups.js +++ b/app/redux/modules/requestGroups.js @@ -3,8 +3,6 @@ import {combineReducers} from 'redux' import {show} from './modals' import {MODAL_ENVIRONMENT_EDITOR, MODAL_REQUEST_GROUP_RENAME} from '../../lib/constants' -export const REQUEST_GROUP_UPDATE = 'request-groups/update'; -export const REQUEST_GROUP_DELETE = 'request-groups/delete'; export const REQUEST_GROUP_TOGGLE = 'request-groups/toggle'; // ~~~~~~~~ // @@ -13,24 +11,12 @@ export const REQUEST_GROUP_TOGGLE = 'request-groups/toggle'; function allReducer (state = [], action) { switch (action.type) { - - case REQUEST_GROUP_UPDATE: - const i = state.findIndex(rg => rg._id === action.requestGroup._id); - - if (i === -1) { - return [action.requestGroup, ...state]; - } else { - return [...state.slice(0, i), action.requestGroup, ...state.slice(i + 1)] - } case REQUEST_GROUP_TOGGLE: return state.map( rg => rg._id === action._id ? Object.assign({}, rg, {collapsed: !rg.collapsed}) : rg ); - case REQUEST_GROUP_DELETE: - return state.filter(rg => rg._id !== action.requestGroup._id); - default: return state; } @@ -45,14 +31,6 @@ export default combineReducers({ // ACTIONS // // ~~~~~~~ // -export function update (requestGroup) { - return {type: REQUEST_GROUP_UPDATE, requestGroup}; -} - -export function remove (requestGroup) { - return {type: REQUEST_GROUP_DELETE, requestGroup}; -} - export function toggle (requestGroup) { return {type: REQUEST_GROUP_TOGGLE, requestGroup} }