diff --git a/src/actions/savedAccounts.js b/src/actions/savedAccounts.js index 2c9ba928b..8b90f35b3 100644 --- a/src/actions/savedAccounts.js +++ b/src/actions/savedAccounts.js @@ -24,6 +24,17 @@ export const accountRemoved = (publicKey) => { }; }; +/** + * An action to dispatch accountSwitched + */ +export const accountSwitched = (account) => { + setLastActiveAccount(account); + return { + data: account, + type: actionTypes.accountSwitched, + }; +}; + /** * The action to initiate savedAccounts store with the retrieved accounts * diff --git a/src/constants/actions.js b/src/constants/actions.js index 23f2dc9a9..f535562f8 100644 --- a/src/constants/actions.js +++ b/src/constants/actions.js @@ -32,6 +32,7 @@ const actionTypes = { accountsRetrieved: 'ACCOUNTS_RETRIEVED', accountSaved: 'ACCOUNT_SAVED', accountRemoved: 'ACCOUNT_REMOVED', + accountSwitched: 'ACCOUNT_SWITCHED', }; export default actionTypes; diff --git a/src/store/middlewares/savedAccounts.js b/src/store/middlewares/savedAccounts.js index 5144136d9..565372b0e 100644 --- a/src/store/middlewares/savedAccounts.js +++ b/src/store/middlewares/savedAccounts.js @@ -1,6 +1,9 @@ import i18next from 'i18next'; import actionTypes from '../../constants/actions'; import { successToastDisplayed } from '../../actions/toaster'; +import { accountLoggedOut } from '../../actions/account'; +import { activePeerSet } from '../../actions/peers'; +import getNetwork from '../../utils/getNetwork'; const savedAccountsMiddleware = store => next => (action) => { next(action); @@ -11,6 +14,13 @@ const savedAccountsMiddleware = store => next => (action) => { case actionTypes.accountRemoved: store.dispatch(successToastDisplayed({ label: i18next.t('Account was successfully forgotten.') })); break; + case actionTypes.accountSwitched: + store.dispatch(accountLoggedOut()); + store.dispatch(activePeerSet({ + publicKey: action.data.publicKey, + network: getNetwork(action.data.network), + })); + break; default: break; }