From e2d95c03fbd4c5b9f3bc1b7b57cdae878055d165 Mon Sep 17 00:00:00 2001 From: apanizo Date: Tue, 9 Oct 2018 16:50:34 +0200 Subject: [PATCH] Using notifications when app is connected to web3 provider --- src/components/Header/index.jsx | 26 ++++++++++++++----- src/components/layout/PageFrame/index.jsx | 13 ++++++---- .../wallets/store/actions/fetchProvider.js | 10 ++++++- .../wallets/store/actions/removeProvider.js | 4 ++- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/components/Header/index.jsx b/src/components/Header/index.jsx index f6ccca5d..ba6d93c0 100644 --- a/src/components/Header/index.jsx +++ b/src/components/Header/index.jsx @@ -2,6 +2,7 @@ import * as React from 'react' import { connect } from 'react-redux' import { logComponentStack, type Info } from '~/utils/logBoundaries' +import { SharedSnackbarConsumer, type Variant } from '~/components/SharedSnackBar/Context' import ProviderInfo from './component/ProviderInfo' import ProviderDetails from './component/ProviderInfo/UserDetails' import ProviderDisconnected from './component/ProviderDisconnected' @@ -10,33 +11,36 @@ import Layout from './component/Layout' import actions, { type Actions } from './actions' import selector, { type SelectorProps } from './selector' -type Props = Actions & SelectorProps +type Props = Actions & SelectorProps & { + openSnackbar: (message: string, variant: Variant) => void, +} type State = { hasError: boolean, } -class Header extends React.PureComponent { +class HeaderComponent extends React.PureComponent { state = { hasError: false, } componentDidMount() { - this.props.fetchProvider() + this.props.fetchProvider(this.props.openSnackbar) } componentDidCatch(error: Error, info: Info) { this.setState({ hasError: true }) + this.props.openSnackbar('Error connecting to your wallet', 'error') logComponentStack(error, info) } onDisconnect = () => { - this.props.removeProvider() + this.props.removeProvider(this.props.openSnackbar) } onConnect = () => { - this.props.fetchProvider() + this.props.fetchProvider(this.props.openSnackbar) } getProviderInfoBased = () => { @@ -79,4 +83,14 @@ class Header extends React.PureComponent { } } -export default connect(selector, actions)(Header) +const Header = connect(selector, actions)(HeaderComponent) + +const HeaderSnack = () => ( + + {({ openSnackbar }) => ( +
+ )} + +) + +export default HeaderSnack diff --git a/src/components/layout/PageFrame/index.jsx b/src/components/layout/PageFrame/index.jsx index 607cba51..28bf68f6 100644 --- a/src/components/layout/PageFrame/index.jsx +++ b/src/components/layout/PageFrame/index.jsx @@ -2,6 +2,7 @@ import React from 'react' import Footer from '~/components/Footer' import Header from '~/components/Header' +import { SharedSnackbarProvider } from '~/components/SharedSnackBar/Context' import styles from './index.scss' type Props = { @@ -9,11 +10,13 @@ type Props = { } const PageFrame = ({ children }: Props) => ( -
-
- {children} -
-
+ +
+
+ {children} +
+
) export default PageFrame diff --git a/src/logic/wallets/store/actions/fetchProvider.js b/src/logic/wallets/store/actions/fetchProvider.js index 3d523e46..6fe4e8da 100644 --- a/src/logic/wallets/store/actions/fetchProvider.js +++ b/src/logic/wallets/store/actions/fetchProvider.js @@ -17,8 +17,16 @@ export const processProviderResponse = (dispatch: ReduxDispatch<*>, response: Pr 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 { loaded } = response + const msg = loaded ? SUCCESS_MSG : UNLOCK_MSG + const variant = loaded ? 'success' : 'warning' + openSnackbar(msg, variant) + processProviderResponse(dispatch, response) } diff --git a/src/logic/wallets/store/actions/removeProvider.js b/src/logic/wallets/store/actions/removeProvider.js index c721cd32..00b35973 100644 --- a/src/logic/wallets/store/actions/removeProvider.js +++ b/src/logic/wallets/store/actions/removeProvider.js @@ -3,7 +3,7 @@ import type { Dispatch as ReduxDispatch } from 'redux' import { makeProvider, type ProviderProps, type Provider } from '~/logic/wallets/store/model/provider' import addProvider from './addProvider' -export default () => async (dispatch: ReduxDispatch<*>) => { +export default (openSnackbar: Function) => async (dispatch: ReduxDispatch<*>) => { const providerProps: ProviderProps = { name: '', available: false, @@ -13,5 +13,7 @@ export default () => async (dispatch: ReduxDispatch<*>) => { } const provider: Provider = makeProvider(providerProps) + openSnackbar('Wallet disconnected succesfully', 'info') + dispatch(addProvider(provider)) }