Skip to content

Commit

Permalink
Logout when the last saved account is removed - Closes #281
Browse files Browse the repository at this point in the history
  • Loading branch information
slaweet committed Feb 12, 2018
1 parent a91ee84 commit 2a401af
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 24 deletions.
9 changes: 7 additions & 2 deletions src/store/middlewares/savedAccounts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import actionTypes from '../../constants/actions';
import { accountLoading } from '../../actions/account';
import { accountLoading, accountLoggedOut } from '../../actions/account';
import { accountsRetrieved, accountSaved } from '../../actions/savedAccounts';
import { activePeerSet } from '../../actions/peers';
import getNetwork from '../../utils/getNetwork';
Expand Down Expand Up @@ -27,7 +27,7 @@ const savedAccountsMiddleware = (store) => {

return next => (action) => {
next(action);
const { peers, account } = store.getState();
const { peers, account, savedAccounts } = store.getState();
switch (action.type) {
case actionTypes.accountSwitched:
store.dispatch(accountLoading());
Expand Down Expand Up @@ -57,6 +57,11 @@ const savedAccountsMiddleware = (store) => {
address: peers.options.address,
}));
break;
case actionTypes.accountRemoved:
if (savedAccounts.accounts.length === 0) {
store.dispatch(accountLoggedOut());
}
break;
default:
break;
}
Expand Down
2 changes: 0 additions & 2 deletions src/store/reducers/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const peers = (state = { status: {}, options: {} }, action) => {
});
case actionTypes.activePeerUpdate:
return Object.assign({}, state, { status: action.data });
case actionTypes.accountLoggedOut:
return Object.assign({}, state, { data: {}, status: {}, options: {} });
default:
return state;
}
Expand Down
20 changes: 0 additions & 20 deletions src/store/reducers/peers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,5 @@ describe('Reducer: peers(state, action)', () => {
const changedState = peers(state, action);
expect(changedState).to.deep.equal(newState);
});

it('should return and empty state object if action is accountLoggedOut', () => {
const state = {
data: {
currentPeer: 'localhost',
port: 4000,
options: {
name: 'Custom Node',
},
},
status: { online: true },
};
const action = {
type: actionTypes.accountLoggedOut,
};

const newState = { status: {}, data: {}, options: {} };
const changedState = peers(state, action);
expect(changedState).to.deep.equal(newState);
});
});

0 comments on commit 2a401af

Please sign in to comment.