diff --git a/package.json b/package.json index d9bac43f90..588ab27f88 100644 --- a/package.json +++ b/package.json @@ -84,6 +84,7 @@ "react-dom": "^15.4.1", "react-ga": "^2.1.2", "react-redux": "^5.0.3", + "reduce-reducers": "^0.1.2", "redux": "^3.6.0", "redux-actions": "^1.2.1", "redux-immutable": "^3.0.11", diff --git a/src/actions/user.js b/src/actions/user.js index 9080c3f359..72c8390098 100644 --- a/src/actions/user.js +++ b/src/actions/user.js @@ -8,7 +8,7 @@ export const userAuthenticated = createAction( const resetWorkspace = createAction('RESET_WORKSPACE', identity); -const userLoggedOut = createAction('USER_LOGGED_OUT'); +export const userLoggedOut = createAction('USER_LOGGED_OUT'); export function logOut() { return (dispatch, getState) => { diff --git a/src/reducers/index.js b/src/reducers/index.js index 3bc85182f7..116098e452 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -1,13 +1,14 @@ import {combineReducers} from 'redux-immutable'; +import reduceReducers from 'reduce-reducers'; import user from './user'; -import projects from './projects'; +import projects, {reduceRoot as reduceRootForProjects} from './projects'; import currentProject from './currentProject'; import errors from './errors'; import runtimeErrors from './runtimeErrors'; import ui from './ui'; import clients from './clients'; -const reducers = combineReducers({ +const reduceRoot = combineReducers({ user, projects, currentProject, @@ -17,4 +18,7 @@ const reducers = combineReducers({ clients, }); -export default reducers; +export default reduceReducers( + reduceRoot, + reduceRootForProjects, +); diff --git a/src/reducers/projects.js b/src/reducers/projects.js index bc308bb389..0866348bb9 100644 --- a/src/reducers/projects.js +++ b/src/reducers/projects.js @@ -64,7 +64,29 @@ function importGist(state, projectKey, gistData) { ); } -export default function projects(stateIn, action) { +export function reduceRoot(stateIn, action) { + return stateIn.update('projects', (projects) => { + switch (action.type) { + case 'USER_LOGGED_OUT': + { + const currentProjectKey = + stateIn.getIn(['currentProject', 'projectKey']); + + if (isNil(currentProjectKey)) { + return new Immutable.Map(); + } + + return new Immutable.Map().set( + currentProjectKey, + projects.get(currentProjectKey), + ); + } + } + return projects; + }); +} + +export default function reduceProjects(stateIn, action) { let state; if (stateIn === undefined) { @@ -95,16 +117,6 @@ export default function projects(stateIn, action) { case 'CHANGE_CURRENT_PROJECT': return removePristineExcept(state, action.payload.projectKey); - case 'RESET_WORKSPACE': - if (isNil(action.payload.currentProjectKey)) { - return emptyMap; - } - - return new Immutable.Map().set( - action.payload.currentProjectKey, - state.get(action.payload.currentProjectKey), - ); - case 'GIST_IMPORTED': return importGist( state, diff --git a/test/unit/reducers/projects.js b/test/unit/reducers/projects.js index b1cf9e8db5..cf2762b7b8 100644 --- a/test/unit/reducers/projects.js +++ b/test/unit/reducers/projects.js @@ -7,7 +7,9 @@ import Immutable from 'immutable'; import reducerTest from '../../helpers/reducerTest'; import {projects as states} from '../../helpers/referenceStates'; import {gistData, project} from '../../helpers/factory'; -import reducer from '../../../src/reducers/projects'; +import reducer, { + reduceRoot as rootReducer, +} from '../../../src/reducers/projects'; import { changeCurrentProject, gistImported, @@ -15,6 +17,7 @@ import { projectLoaded, projectSourceEdited, } from '../../../src/actions/projects'; +import {userLoggedOut} from '../../../src/actions/user'; const now = Date.now(); const projectKey = '12345'; @@ -136,6 +139,18 @@ tap(project(), projectIn => )), ); +tap(initProjects({1: true, 2: true}), projects => + test('userLoggedOut', reducerTest( + rootReducer, + Immutable.fromJS({currentProject: {projectKey: '1'}, projects}), + userLoggedOut, + Immutable.fromJS({ + currentProject: {projectKey: '1'}, + projects: projects.take(1), + }), + )), +); + function initProjects(map = {}) { return reduce(map, (projectsIn, modified, key) => { const projects = reducer(projectsIn, projectCreated(key)); diff --git a/yarn.lock b/yarn.lock index 741adb01c2..5010baf52a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6515,7 +6515,7 @@ reduce-function-call@^1.0.1: dependencies: balanced-match "^0.4.2" -reduce-reducers@^0.1.0: +reduce-reducers@^0.1.0, reduce-reducers@^0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/reduce-reducers/-/reduce-reducers-0.1.2.tgz#fa1b4718bc5292a71ddd1e5d839c9bea9770f14b"