Skip to content

Commit

Permalink
Use SELECTED_ADDRESS_CHANGED state instead of cached address
Browse files Browse the repository at this point in the history
The `unconnected-account` reducer no longer caches the selected address
when the alert was opened. Instead a new event was added for when the
selected address changes (`SELECTED_ADDRESS_CHANGED`). Now that we know
exactly when the selected address changes, we don't need to cache the
original account for comparison anymore.
  • Loading branch information
Gudahtt committed Apr 28, 2020
1 parent fb02c58 commit bf55bba
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
1 change: 1 addition & 0 deletions test/unit/ui/app/actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ describe('Actions', function () {

const expectedActions = [
'SHOW_LOADING_INDICATION',
'SELECTED_ADDRESS_CHANGED',
'UPDATE_METAMASK_STATE',
'HIDE_LOADING_INDICATION',
'SHOW_ACCOUNTS_PAGE',
Expand Down
23 changes: 6 additions & 17 deletions ui/app/ducks/alerts/unconnected-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { captureException } from '@sentry/browser'

import actionConstants from '../../store/actionConstants'
import { addPermittedAccount } from '../../store/actions'
import { getOriginOfCurrentTab } from '../../selectors/selectors'
import { getOriginOfCurrentTab, getSelectedAddress } from '../../selectors/selectors'

// Constants

Expand All @@ -18,7 +18,6 @@ const name = 'unconnectedAccount'

const initialState = {
state: ALERT_STATE.CLOSED,
address: null,
}

// Slice (reducer plus auto-generated actions and action creators)
Expand All @@ -35,26 +34,19 @@ const slice = createSlice({
},
connectAccountSucceeded: (state) => {
state.state = ALERT_STATE.CLOSED
state.address = null
},
dismissAlert: (state) => {
state.state = ALERT_STATE.CLOSED
state.address = null
},
switchedToUnconnectedAccount: (state, action) => {
switchedToUnconnectedAccount: (state) => {
state.state = ALERT_STATE.OPEN
state.address = action.payload
},
},
extraReducers: {
[actionConstants.UPDATE_METAMASK_STATE]: (state, action) => {
[actionConstants.SELECTED_ADDRESS_CHANGED]: (state) => {
// close the alert if the account is switched while it's open
if (
state.state === ALERT_STATE.OPEN &&
state.address !== action.value.selectedAddress
) {
if (state.state === ALERT_STATE.OPEN) {
state.state = ALERT_STATE.CLOSED
state.address = null
}
},
},
Expand All @@ -68,11 +60,8 @@ export default reducer

export const getAlertState = (state) => state[name].state

export const getAlertAccountAddress = (state) => state[name].address

export const alertIsOpen = (state) => state[name].state !== ALERT_STATE.CLOSED


// Actions / action-creators

export const {
Expand All @@ -86,11 +75,11 @@ export const {
export const connectAccount = () => {
return async (dispatch, getState) => {
const state = getState()
const address = getAlertAccountAddress(state)
const selectedAddress = getSelectedAddress(state)
const origin = getOriginOfCurrentTab(state)
try {
await dispatch(connectAccountRequested())
await dispatch(addPermittedAccount(origin, address))
await dispatch(addPermittedAccount(origin, selectedAddress))
await dispatch(connectAccountSucceeded())
} catch (error) {
console.error(error)
Expand Down
1 change: 1 addition & 0 deletions ui/app/store/actionConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default {
NETWORK_DROPDOWN_CLOSE: 'UI_NETWORK_DROPDOWN_CLOSE',
// remote state
UPDATE_METAMASK_STATE: 'UPDATE_METAMASK_STATE',
SELECTED_ADDRESS_CHANGED: 'SELECTED_ADDRESS_CHANGED',
FORGOT_PASSWORD: 'FORGOT_PASSWORD',
CLOSE_WELCOME_SCREEN: 'CLOSE_WELCOME_SCREEN',
// unlock screen
Expand Down
15 changes: 12 additions & 3 deletions ui/app/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1094,12 +1094,21 @@ export function updateMetamaskState (newState) {
return (dispatch, getState) => {
const { metamask: currentState } = getState()

const { currentLocale } = currentState
const { currentLocale: newLocale } = newState
const {
currentLocale,
selectedAddress,
} = currentState
const {
currentLocale: newLocale,
selectedAddress: newSelectedAddress,
} = newState

if (currentLocale && newLocale && currentLocale !== newLocale) {
dispatch(updateCurrentLocale(newLocale))
}
if (selectedAddress !== newSelectedAddress) {
dispatch({ type: actionConstants.SELECTED_ADDRESS_CHANGED })
}

dispatch({
type: actionConstants.UPDATE_METAMASK_STATE,
Expand Down Expand Up @@ -1192,7 +1201,7 @@ export function showAccountDetail (address) {
value: address,
})
if (switchingToUnconnectedAddress) {
dispatch(switchedToUnconnectedAccount(address))
dispatch(switchedToUnconnectedAccount())
}
dispatch(setSelectedToken())
}
Expand Down

0 comments on commit bf55bba

Please sign in to comment.