Skip to content

Logout handled by reducers #753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 0 additions & 56 deletions spec/examples/actions/user.spec.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {

import {
userAuthenticated,
logOut,
userLoggedOut,
} from './user';

function getCurrentPersistor(state) {
Expand Down Expand Up @@ -173,7 +173,7 @@ export {
updateProjectSource,
toggleLibrary,
userAuthenticated,
logOut,
userLoggedOut,
addRuntimeError,
clearRuntimeErrors,
minimizeComponent,
Expand Down
18 changes: 2 additions & 16 deletions src/actions/user.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import {createAction} from 'redux-actions';
import identity from 'lodash/identity';

export const userAuthenticated = createAction(
'USER_AUTHENTICATED',
identity,
);
export const userAuthenticated = createAction('USER_AUTHENTICATED', identity);

const resetWorkspace = createAction('RESET_WORKSPACE', identity);

const userLoggedOut = createAction('USER_LOGGED_OUT');

export function logOut() {
return (dispatch, getState) => {
const currentProjectKey =
getState().getIn(['currentProject', 'projectKey']);
dispatch(resetWorkspace({currentProjectKey}));
dispatch(userLoggedOut());
};
}
export const userLoggedOut = createAction('USER_LOGGED_OUT');
4 changes: 2 additions & 2 deletions src/components/Workspace.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
createProject,
updateProjectSource,
userAuthenticated,
logOut,
userLoggedOut,
toggleLibrary,
minimizeComponent,
maximizeComponent,
Expand Down Expand Up @@ -291,7 +291,7 @@ class Workspace extends React.Component {
onSignedIn(userCredential =>
this.props.dispatch(userAuthenticated(userCredential)),
);
onSignedOut(() => this.props.dispatch(logOut()));
onSignedOut(() => this.props.dispatch(userLoggedOut()));
}

_handleStartLogIn() {
Expand Down
10 changes: 7 additions & 3 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,4 +18,7 @@ const reducers = combineReducers({
clients,
});

export default reducers;
export default reduceReducers(
reduceRoot,
reduceRootForProjects,
);
34 changes: 23 additions & 11 deletions src/reducers/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 8 additions & 5 deletions src/reducers/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,14 @@ function ui(stateIn, action) {
),
);

case 'RESET_WORKSPACE':
return state.setIn(
['dashboard', 'activeSubmenu'],
null,
);
case 'USER_LOGGED_OUT':
if (state.getIn(['dashboard', 'activeSubmenu']) === 'projectList') {
return state.setIn(
['dashboard', 'activeSubmenu'],
null,
);
}
return state;

default:
return state;
Expand Down
17 changes: 16 additions & 1 deletion test/unit/reducers/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ 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,
projectCreated,
projectLoaded,
projectSourceEdited,
} from '../../../src/actions/projects';
import {userLoggedOut} from '../../../src/actions/user';

const now = Date.now();
const projectKey = '12345';
Expand Down Expand Up @@ -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));
Expand Down
25 changes: 24 additions & 1 deletion test/unit/reducers/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
gistNotFound,
gistImportError,
} from '../../../src/actions/projects';

import {userLoggedOut} from '../../../src/actions/user';

const initialState = Immutable.fromJS({
editors: {typing: false},
Expand Down Expand Up @@ -49,3 +49,26 @@ test('gistImportError', reducerTest(
})),
),
));

test('userLoggedOut', (t) => {
const libraryPickerOpen = initialState.setIn(
['dashboard', 'activeSubmenu'],
'libraryPicker',
);
t.test('with active submenu that is not projects', reducerTest(
reducer,
libraryPickerOpen,
userLoggedOut,
libraryPickerOpen,
));

t.test('with projectList active submenu', reducerTest(
reducer,
initialState.setIn(
['dashboard', 'activeSubmenu'],
'projectList',
),
userLoggedOut,
initialState,
));
});
29 changes: 19 additions & 10 deletions test/unit/reducers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@ import reducerTest from '../../helpers/reducerTest';
import {user as states} from '../../helpers/referenceStates';
import {userCredential} from '../../helpers/factory';
import reducer from '../../../src/reducers/user';
import {userAuthenticated} from '../../../src/actions/user';
import {userAuthenticated, userLoggedOut} from '../../../src/actions/user';

const userCredentialIn = userCredential();

const loggedInState = Immutable.fromJS({
authenticated: true,
id: userCredentialIn.user.uid,
displayName: userCredentialIn.user.providerData[0].displayName,
avatarUrl: userCredentialIn.user.providerData[0].photoURL,
accessTokens: {
'github.com': userCredentialIn.credential.accessToken,
},
});

test('userAuthenticated', reducerTest(
reducer,
states.initial,
partial(userAuthenticated, userCredentialIn),
Immutable.fromJS({
authenticated: true,
id: userCredentialIn.user.uid,
displayName: userCredentialIn.user.providerData[0].displayName,
avatarUrl: userCredentialIn.user.providerData[0].photoURL,
accessTokens: {
'github.com': userCredentialIn.credential.accessToken,
},
}),
loggedInState,
));

test('userLoggedOut', reducerTest(
reducer,
loggedInState,
userLoggedOut,
states.initial,
));
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down