2020-04-29 19:10:51 +02:00
|
|
|
import { createSlice } from '@reduxjs/toolkit'
|
|
|
|
import { captureException } from '@sentry/browser'
|
|
|
|
|
2020-05-08 21:45:52 +02:00
|
|
|
import { ALERT_TYPES } from '../../../../app/scripts/controllers/alert'
|
2020-05-06 20:56:09 +02:00
|
|
|
import * as actionConstants from '../../store/actionConstants'
|
2020-06-15 21:08:53 +02:00
|
|
|
import {
|
|
|
|
addPermittedAccount,
|
|
|
|
setAlertEnabledness,
|
|
|
|
setSelectedAddress,
|
|
|
|
} from '../../store/actions'
|
2020-11-03 00:41:28 +01:00
|
|
|
import { getOriginOfCurrentTab, getSelectedAddress } from '../../selectors'
|
2020-10-06 19:57:02 +02:00
|
|
|
import { ALERT_STATE } from './enums'
|
2020-04-29 19:10:51 +02:00
|
|
|
|
|
|
|
// Constants
|
|
|
|
|
2020-05-08 21:45:52 +02:00
|
|
|
const name = ALERT_TYPES.unconnectedAccount
|
2020-04-29 19:10:51 +02:00
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
state: ALERT_STATE.CLOSED,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slice (reducer plus auto-generated actions and action creators)
|
|
|
|
|
|
|
|
const slice = createSlice({
|
|
|
|
name,
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
|
|
|
connectAccountFailed: (state) => {
|
|
|
|
state.state = ALERT_STATE.ERROR
|
|
|
|
},
|
|
|
|
connectAccountRequested: (state) => {
|
|
|
|
state.state = ALERT_STATE.LOADING
|
|
|
|
},
|
|
|
|
connectAccountSucceeded: (state) => {
|
|
|
|
state.state = ALERT_STATE.CLOSED
|
|
|
|
},
|
2020-05-08 21:45:52 +02:00
|
|
|
disableAlertFailed: (state) => {
|
|
|
|
state.state = ALERT_STATE.ERROR
|
|
|
|
},
|
|
|
|
disableAlertRequested: (state) => {
|
|
|
|
state.state = ALERT_STATE.LOADING
|
|
|
|
},
|
|
|
|
disableAlertSucceeded: (state) => {
|
|
|
|
state.state = ALERT_STATE.CLOSED
|
|
|
|
},
|
2020-06-15 21:08:53 +02:00
|
|
|
dismissAlert: (state) => {
|
|
|
|
state.state = ALERT_STATE.CLOSED
|
|
|
|
},
|
|
|
|
switchAccountFailed: (state) => {
|
|
|
|
state.state = ALERT_STATE.ERROR
|
|
|
|
},
|
|
|
|
switchAccountRequested: (state) => {
|
|
|
|
state.state = ALERT_STATE.LOADING
|
|
|
|
},
|
|
|
|
switchAccountSucceeded: (state) => {
|
|
|
|
state.state = ALERT_STATE.CLOSED
|
|
|
|
},
|
2020-04-29 19:10:51 +02:00
|
|
|
switchedToUnconnectedAccount: (state) => {
|
|
|
|
state.state = ALERT_STATE.OPEN
|
|
|
|
},
|
|
|
|
},
|
|
|
|
extraReducers: {
|
|
|
|
[actionConstants.SELECTED_ADDRESS_CHANGED]: (state) => {
|
|
|
|
// close the alert if the account is switched while it's open
|
|
|
|
if (state.state === ALERT_STATE.OPEN) {
|
|
|
|
state.state = ALERT_STATE.CLOSED
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const { actions, reducer } = slice
|
|
|
|
|
|
|
|
export default reducer
|
|
|
|
|
|
|
|
// Selectors
|
|
|
|
|
|
|
|
export const getAlertState = (state) => state[name].state
|
|
|
|
|
|
|
|
export const alertIsOpen = (state) => state[name].state !== ALERT_STATE.CLOSED
|
|
|
|
|
|
|
|
// Actions / action-creators
|
|
|
|
|
2020-05-08 21:45:52 +02:00
|
|
|
const {
|
2020-04-29 19:10:51 +02:00
|
|
|
connectAccountFailed,
|
|
|
|
connectAccountRequested,
|
|
|
|
connectAccountSucceeded,
|
2020-05-08 21:45:52 +02:00
|
|
|
disableAlertFailed,
|
|
|
|
disableAlertRequested,
|
|
|
|
disableAlertSucceeded,
|
2020-06-15 21:08:53 +02:00
|
|
|
dismissAlert,
|
|
|
|
switchAccountFailed,
|
|
|
|
switchAccountRequested,
|
|
|
|
switchAccountSucceeded,
|
2020-04-29 19:10:51 +02:00
|
|
|
switchedToUnconnectedAccount,
|
|
|
|
} = actions
|
|
|
|
|
2020-05-08 21:45:52 +02:00
|
|
|
export { dismissAlert, switchedToUnconnectedAccount }
|
|
|
|
|
|
|
|
export const dismissAndDisableAlert = () => {
|
|
|
|
return async (dispatch) => {
|
|
|
|
try {
|
|
|
|
await dispatch(disableAlertRequested())
|
2020-05-09 02:49:41 +02:00
|
|
|
await dispatch(setAlertEnabledness(name, false))
|
2020-05-08 21:45:52 +02:00
|
|
|
await dispatch(disableAlertSucceeded())
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
captureException(error)
|
|
|
|
await dispatch(disableAlertFailed())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:08:53 +02:00
|
|
|
export const switchToAccount = (address) => {
|
|
|
|
return async (dispatch) => {
|
|
|
|
try {
|
|
|
|
await dispatch(switchAccountRequested())
|
|
|
|
await dispatch(setSelectedAddress(address))
|
|
|
|
await dispatch(switchAccountSucceeded())
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
captureException(error)
|
|
|
|
await dispatch(switchAccountFailed())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:10:51 +02:00
|
|
|
export const connectAccount = () => {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const state = getState()
|
|
|
|
const selectedAddress = getSelectedAddress(state)
|
|
|
|
const origin = getOriginOfCurrentTab(state)
|
|
|
|
try {
|
|
|
|
await dispatch(connectAccountRequested())
|
|
|
|
await dispatch(addPermittedAccount(origin, selectedAddress))
|
|
|
|
await dispatch(connectAccountSucceeded())
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
captureException(error)
|
|
|
|
await dispatch(connectAccountFailed())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|