Using notifications when app is connected to web3 provider

This commit is contained in:
apanizo 2018-10-09 16:50:34 +02:00
parent 2b45b24378
commit e2d95c03fb
4 changed files with 40 additions and 13 deletions

View File

@ -2,6 +2,7 @@
import * as React from 'react' import * as React from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { logComponentStack, type Info } from '~/utils/logBoundaries' import { logComponentStack, type Info } from '~/utils/logBoundaries'
import { SharedSnackbarConsumer, type Variant } from '~/components/SharedSnackBar/Context'
import ProviderInfo from './component/ProviderInfo' import ProviderInfo from './component/ProviderInfo'
import ProviderDetails from './component/ProviderInfo/UserDetails' import ProviderDetails from './component/ProviderInfo/UserDetails'
import ProviderDisconnected from './component/ProviderDisconnected' import ProviderDisconnected from './component/ProviderDisconnected'
@ -10,33 +11,36 @@ import Layout from './component/Layout'
import actions, { type Actions } from './actions' import actions, { type Actions } from './actions'
import selector, { type SelectorProps } from './selector' import selector, { type SelectorProps } from './selector'
type Props = Actions & SelectorProps type Props = Actions & SelectorProps & {
openSnackbar: (message: string, variant: Variant) => void,
}
type State = { type State = {
hasError: boolean, hasError: boolean,
} }
class Header extends React.PureComponent<Props, State> { class HeaderComponent extends React.PureComponent<Props, State> {
state = { state = {
hasError: false, hasError: false,
} }
componentDidMount() { componentDidMount() {
this.props.fetchProvider() this.props.fetchProvider(this.props.openSnackbar)
} }
componentDidCatch(error: Error, info: Info) { componentDidCatch(error: Error, info: Info) {
this.setState({ hasError: true }) this.setState({ hasError: true })
this.props.openSnackbar('Error connecting to your wallet', 'error')
logComponentStack(error, info) logComponentStack(error, info)
} }
onDisconnect = () => { onDisconnect = () => {
this.props.removeProvider() this.props.removeProvider(this.props.openSnackbar)
} }
onConnect = () => { onConnect = () => {
this.props.fetchProvider() this.props.fetchProvider(this.props.openSnackbar)
} }
getProviderInfoBased = () => { getProviderInfoBased = () => {
@ -79,4 +83,14 @@ class Header extends React.PureComponent<Props, State> {
} }
} }
export default connect(selector, actions)(Header) const Header = connect(selector, actions)(HeaderComponent)
const HeaderSnack = () => (
<SharedSnackbarConsumer>
{({ openSnackbar }) => (
<Header openSnackbar={openSnackbar} />
)}
</SharedSnackbarConsumer>
)
export default HeaderSnack

View File

@ -2,6 +2,7 @@
import React from 'react' import React from 'react'
import Footer from '~/components/Footer' import Footer from '~/components/Footer'
import Header from '~/components/Header' import Header from '~/components/Header'
import { SharedSnackbarProvider } from '~/components/SharedSnackBar/Context'
import styles from './index.scss' import styles from './index.scss'
type Props = { type Props = {
@ -9,11 +10,13 @@ type Props = {
} }
const PageFrame = ({ children }: Props) => ( const PageFrame = ({ children }: Props) => (
<div className={styles.frame}> <SharedSnackbarProvider>
<Header /> <div className={styles.frame}>
{children} <Header />
<Footer /> {children}
</div> <Footer />
</div>
</SharedSnackbarProvider>
) )
export default PageFrame export default PageFrame

View File

@ -17,8 +17,16 @@ export const processProviderResponse = (dispatch: ReduxDispatch<*>, response: Pr
dispatch(addProvider(walletRecord)) dispatch(addProvider(walletRecord))
} }
export default () => async (dispatch: ReduxDispatch<*>) => { const SUCCESS_MSG = 'Wallet connected sucessfully'
const UNLOCK_MSG = 'Unlock your wallet to connect'
export default (openSnackbar: Function) => async (dispatch: ReduxDispatch<*>) => {
const response: ProviderProps = await getProviderInfo() const response: ProviderProps = await getProviderInfo()
const { loaded } = response
const msg = loaded ? SUCCESS_MSG : UNLOCK_MSG
const variant = loaded ? 'success' : 'warning'
openSnackbar(msg, variant)
processProviderResponse(dispatch, response) processProviderResponse(dispatch, response)
} }

View File

@ -3,7 +3,7 @@ import type { Dispatch as ReduxDispatch } from 'redux'
import { makeProvider, type ProviderProps, type Provider } from '~/logic/wallets/store/model/provider' import { makeProvider, type ProviderProps, type Provider } from '~/logic/wallets/store/model/provider'
import addProvider from './addProvider' import addProvider from './addProvider'
export default () => async (dispatch: ReduxDispatch<*>) => { export default (openSnackbar: Function) => async (dispatch: ReduxDispatch<*>) => {
const providerProps: ProviderProps = { const providerProps: ProviderProps = {
name: '', name: '',
available: false, available: false,
@ -13,5 +13,7 @@ export default () => async (dispatch: ReduxDispatch<*>) => {
} }
const provider: Provider = makeProvider(providerProps) const provider: Provider = makeProvider(providerProps)
openSnackbar('Wallet disconnected succesfully', 'info')
dispatch(addProvider(provider)) dispatch(addProvider(provider))
} }