Fixing notifications WIP

This commit is contained in:
Mikhail Mikheev 2019-11-08 16:05:28 +04:00
parent 9e5a4f93a3
commit 351b2c8592
6 changed files with 54 additions and 57 deletions

View File

@ -1,6 +1,5 @@
// @flow // @flow
import React, { useEffect } from 'react' import React from 'react'
import { withSnackbar } from 'notistack'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import Web3Connect from 'web3connect' import Web3Connect from 'web3connect'
import Torus from '@toruslabs/torus-embed' import Torus from '@toruslabs/torus-embed'
@ -10,6 +9,7 @@ import Squarelink from 'squarelink'
import Button from '~/components/layout/Button' import Button from '~/components/layout/Button'
import { fetchProvider } from '~/logic/wallets/store/actions' import { fetchProvider } from '~/logic/wallets/store/actions'
import { getNetwork } from '~/config' import { getNetwork } from '~/config'
import { store } from '~/store'
const PORTIS_DAPP_ID = process.env.REACT_APP_NETWORK === 'mainnet' ? process.env.REACT_APP_PORTIS_ID : '852b763d-f28b-4463-80cb-846d7ec5806b' const PORTIS_DAPP_ID = process.env.REACT_APP_NETWORK === 'mainnet' ? process.env.REACT_APP_PORTIS_ID : '852b763d-f28b-4463-80cb-846d7ec5806b'
const SQUARELINK_CLIENT_ID = process.env.REACT_APP_NETWORK === 'mainnet' ? process.env.REACT_APP_SQUARELINK_ID : '46ce08fe50913cfa1b78' const SQUARELINK_CLIENT_ID = process.env.REACT_APP_NETWORK === 'mainnet' ? process.env.REACT_APP_SQUARELINK_ID : '46ce08fe50913cfa1b78'
@ -47,44 +47,36 @@ export const web3Connect = new Web3Connect.Core({
}, },
}) })
web3Connect.on('connect', (provider: any) => {
if (provider) {
store.dispatch(fetchProvider(provider))
}
})
type Props = { type Props = {
registerProvider: Function, registerProvider: Function,
enqueueSnackbar: Function, enqueueSnackbar: Function,
closeSnackbar: Function, closeSnackbar: Function,
} }
let web3connectEventListenerAdded = false
const ConnectButton = ({ const ConnectButton = ({
registerProvider, enqueueSnackbar, closeSnackbar, ...props registerProvider, ...props
}: Props) => { }: Props) => (
useEffect(() => {
if (!web3connectEventListenerAdded) {
web3Connect.on('connect', (provider: any) => {
if (provider) {
registerProvider(provider, enqueueSnackbar, closeSnackbar)
}
})
web3connectEventListenerAdded = true
}
}, [])
return ( <Button
<Button color="primary"
color="primary" variant="contained"
variant="contained" minWidth={140}
minWidth={140} onClick={() => {
onClick={() => { web3Connect.toggleModal()
web3Connect.toggleModal() }}
}} {...props}
{...props} >
> Connect
Connect </Button>
</Button> )
)
}
export default connect( export default connect(
null, null,
{ registerProvider: fetchProvider }, { registerProvider: fetchProvider },
)(withSnackbar(ConnectButton)) )(ConnectButton)

View File

@ -1,16 +1,10 @@
// @flow // @flow
import enqueueSnackbar from '~/logic/notifications/store/actions/enqueueSnackbar'
import closeSnackbar from '~/logic/notifications/store/actions/closeSnackbar'
import removeSnackbar from '~/logic/notifications/store/actions/removeSnackbar' import removeSnackbar from '~/logic/notifications/store/actions/removeSnackbar'
export type Actions = { export type Actions = {
enqueueSnackbar: typeof enqueueSnackbar,
closeSnackbar: typeof closeSnackbar,
removeSnackbar: typeof removeSnackbar, removeSnackbar: typeof removeSnackbar,
} }
export default { export default {
enqueueSnackbar,
closeSnackbar,
removeSnackbar, removeSnackbar,
} }

View File

@ -9,6 +9,8 @@ import selector from './selector'
type Props = Actions & { type Props = Actions & {
notifications: List<Notification>, notifications: List<Notification>,
closeSnackbar: Function,
enqueueSnackbar: Function,
} }
class Notifier extends Component<Props> { class Notifier extends Component<Props> {

View File

@ -3,6 +3,8 @@ import * as React from 'react'
import { IconButton } from '@material-ui/core' import { IconButton } from '@material-ui/core'
import { Close as IconClose } from '@material-ui/icons' import { Close as IconClose } from '@material-ui/icons'
import { TX_NOTIFICATION_TYPES } from '~/logic/safe/transactions' import { TX_NOTIFICATION_TYPES } from '~/logic/safe/transactions'
import { store } from '~/store'
import closeSnackbarAction from '~/logic/notifications/store/actions/closeSnackbar'
import { type Notification, NOTIFICATIONS } from './notificationTypes' import { type Notification, NOTIFICATIONS } from './notificationTypes'
export type NotificationsQueue = { export type NotificationsQueue = {
@ -145,11 +147,20 @@ export const getNotificationsFromTxType = (txType: string) => {
return notificationsQueue return notificationsQueue
} }
export const showSnackbar = ( export const enhanceSnackbarForAction = (notification: Notification) => ({
notification: Notification, ...notification,
enqueueSnackbar: Function, options: {
closeSnackbar: Function, ...notification.options,
) => enqueueSnackbar(notification.message, { action: (key) => (
<IconButton onClick={() => store.dispatch(closeSnackbarAction(key))}>
{console.log(key, notification.message)}
<IconClose />
</IconButton>
),
},
})
export const showSnackbar = (notification: Notification, enqueueSnackbar: Function, closeSnackbar: Function) => enqueueSnackbar(notification.message, {
...notification.options, ...notification.options,
action: (key) => ( action: (key) => (
<IconButton onClick={() => closeSnackbar(key)}> <IconButton onClick={() => closeSnackbar(key)}>

View File

@ -15,6 +15,7 @@ const enqueueSnackbar = (notification: NotificationProps) => (
...notification, ...notification,
key: new Date().getTime(), key: new Date().getTime(),
} }
dispatch(addSnackbar(newNotification)) dispatch(addSnackbar(newNotification))
} }

View File

@ -4,7 +4,10 @@ import { ETHEREUM_NETWORK_IDS, ETHEREUM_NETWORK, getProviderInfo } from '~/logic
import { getNetwork } from '~/config' import { getNetwork } from '~/config'
import type { ProviderProps } from '~/logic/wallets/store/model/provider' import type { ProviderProps } from '~/logic/wallets/store/model/provider'
import { makeProvider } from '~/logic/wallets/store/model/provider' import { makeProvider } from '~/logic/wallets/store/model/provider'
import { NOTIFICATIONS, showSnackbar } from '~/logic/notifications' import { NOTIFICATIONS, showSnackbar, enhanceSnackbarForAction } from '~/logic/notifications'
import enqueueSnackbar from '~/logic/notifications/store/actions/enqueueSnackbar'
import closeSnackbar from '~/logic/notifications/store/actions/closeSnackbar'
import addProvider from './addProvider' import addProvider from './addProvider'
export const processProviderResponse = (dispatch: ReduxDispatch<*>, provider: ProviderProps) => { export const processProviderResponse = (dispatch: ReduxDispatch<*>, provider: ProviderProps) => {
@ -23,24 +26,20 @@ export const processProviderResponse = (dispatch: ReduxDispatch<*>, provider: Pr
dispatch(addProvider(walletRecord)) dispatch(addProvider(walletRecord))
} }
const handleProviderNotification = ( const handleProviderNotification = (provider: ProviderProps, dispatch: Function) => {
provider: ProviderProps,
enqueueSnackbar: Function,
closeSnackbar: Function,
) => {
const { loaded, network, available } = provider const { loaded, network, available } = provider
if (!loaded) { if (!loaded) {
showSnackbar(NOTIFICATIONS.CONNECT_WALLET_ERROR_MSG, enqueueSnackbar, closeSnackbar) dispatch(enqueueSnackbar(enhanceSnackbarForAction(NOTIFICATIONS.CONNECT_WALLET_ERROR_MSG)))
return return
} }
if (ETHEREUM_NETWORK_IDS[network] !== getNetwork()) { if (ETHEREUM_NETWORK_IDS[network] !== getNetwork()) {
showSnackbar(NOTIFICATIONS.WRONG_NETWORK_MSG, enqueueSnackbar, closeSnackbar) dispatch(enqueueSnackbar(enhanceSnackbarForAction(NOTIFICATIONS.WRONG_NETWORK_MSG)))
return return
} }
if (ETHEREUM_NETWORK.RINKEBY === getNetwork()) { if (ETHEREUM_NETWORK.RINKEBY === getNetwork()) {
showSnackbar(NOTIFICATIONS.RINKEBY_VERSION_MSG, enqueueSnackbar, closeSnackbar) dispatch(enqueueSnackbar(enhanceSnackbarForAction(NOTIFICATIONS.RINKEBY_VERSION_MSG)))
} }
if (available) { if (available) {
@ -49,16 +48,14 @@ const handleProviderNotification = (
// you SHOULD pass your own `key` in the options. `key` can be any sequence // you SHOULD pass your own `key` in the options. `key` can be any sequence
// of number or characters, but it has to be unique to a given snackbar. // of number or characters, but it has to be unique to a given snackbar.
showSnackbar(NOTIFICATIONS.WALLET_CONNECTED_MSG, enqueueSnackbar, closeSnackbar) dispatch(enqueueSnackbar(enhanceSnackbarForAction(NOTIFICATIONS.WALLET_CONNECTED_MSG)))
} else { } else {
showSnackbar(NOTIFICATIONS.UNLOCK_WALLET_MSG, enqueueSnackbar, closeSnackbar) dispatch(enqueueSnackbar(enhanceSnackbarForAction(NOTIFICATIONS.UNLOCK_WALLET_MSG)))
} }
} }
export default (provider: Object, enqueueSnackbar: Function, closeSnackbar: Function) => async ( export default (provider: Object) => async (dispatch: ReduxDispatch<*>) => {
dispatch: ReduxDispatch<*>,
) => {
const providerInfo: ProviderProps = await getProviderInfo(provider) const providerInfo: ProviderProps = await getProviderInfo(provider)
await handleProviderNotification(providerInfo, enqueueSnackbar, closeSnackbar) await handleProviderNotification(providerInfo, dispatch)
processProviderResponse(dispatch, providerInfo) processProviderResponse(dispatch, providerInfo)
} }