From b615d20a65fc54b8d8bf145bbc77ea78456c6cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Mart=C3=ADnez?= Date: Thu, 6 Jun 2019 20:31:58 +0200 Subject: [PATCH] Refactor fetch provider --- src/components/Header/actions.js | 4 +--- src/components/Header/index.jsx | 23 ++++++++++++------- .../wallets/store/actions/fetchProvider.js | 21 ++--------------- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/src/components/Header/actions.js b/src/components/Header/actions.js index 9eff46e2..e96a2379 100644 --- a/src/components/Header/actions.js +++ b/src/components/Header/actions.js @@ -1,14 +1,12 @@ // @flow -import { fetchProvider, recurrentFetchProvider, removeProvider } from '~/logic/wallets/store/actions' +import { fetchProvider, removeProvider } from '~/logic/wallets/store/actions' export type Actions = { fetchProvider: typeof fetchProvider, - recurrentFetchProvider: typeof recurrentFetchProvider, removeProvider: typeof removeProvider, } export default { fetchProvider, - recurrentFetchProvider, removeProvider, } diff --git a/src/components/Header/index.jsx b/src/components/Header/index.jsx index 98fa0bf6..a45b2611 100644 --- a/src/components/Header/index.jsx +++ b/src/components/Header/index.jsx @@ -4,6 +4,7 @@ import { connect } from 'react-redux' import { logComponentStack, type Info } from '~/utils/logBoundaries' import { SharedSnackbarConsumer, type Variant } from '~/components/SharedSnackBar/Context' import { WALLET_ERROR_MSG } from '~/logic/wallets/store/actions' +import { getProviderInfo } from '~/logic/wallets/getWeb3' import ProviderAccesible from './component/ProviderInfo/ProviderAccesible' import UserDetails from './component/ProviderDetails/UserDetails' import ProviderDisconnected from './component/ProviderInfo/ProviderDisconnected' @@ -26,7 +27,7 @@ class HeaderComponent extends React.PureComponent { hasError: false, } - async componentDidMount() { + componentDidMount() { this.onConnect() } @@ -39,19 +40,25 @@ class HeaderComponent extends React.PureComponent { } onDisconnect = () => { - const { removeProvider } = this.props + const { removeProvider, openSnackbar } = this.props clearInterval(this.providerListener) - removeProvider(this.props.openSnackbar) + removeProvider(openSnackbar) } onConnect = async () => { - const { fetchProvider, recurrentFetchProvider } = this.props - + const { fetchProvider, openSnackbar } = this.props + clearInterval(this.providerListener) - let currentProvider: ProviderProps = await fetchProvider(this.props.openSnackbar) - this.providerListener = setInterval(async () => { - currentProvider = await recurrentFetchProvider(currentProvider, this.props.openSnackbar) + let currentProvider: ProviderProps = await getProviderInfo() + fetchProvider(currentProvider, openSnackbar) + + this.providerListener = setInterval(async () => { + const newProvider: ProviderProps = await getProviderInfo() + if (JSON.stringify(currentProvider) !== JSON.stringify(newProvider)) { + fetchProvider(newProvider, openSnackbar) + } + currentProvider = newProvider }, 2000) } diff --git a/src/logic/wallets/store/actions/fetchProvider.js b/src/logic/wallets/store/actions/fetchProvider.js index db33eab7..c19f52ce 100644 --- a/src/logic/wallets/store/actions/fetchProvider.js +++ b/src/logic/wallets/store/actions/fetchProvider.js @@ -1,6 +1,6 @@ // @flow import type { Dispatch as ReduxDispatch } from 'redux' -import { getProviderInfo, ETHEREUM_NETWORK_IDS, ETHEREUM_NETWORK } from '~/logic/wallets/getWeb3' +import { ETHEREUM_NETWORK_IDS, ETHEREUM_NETWORK } from '~/logic/wallets/getWeb3' import type { ProviderProps } from '~/logic/wallets/store/model/provider' import { makeProvider } from '~/logic/wallets/store/model/provider' import addProvider from './addProvider' @@ -44,24 +44,7 @@ const handleProviderNotification = (openSnackbar: Function, provider: ProviderPr openSnackbar(msg, variant) } -export const recurrentFetchProvider = (currentProvider: ProviderProps, openSnackbar: Function) => async ( - dispatch: ReduxDispatch<*>, -) => { - const newProvider: ProviderProps = await getProviderInfo() - - if (JSON.stringify(currentProvider) !== JSON.stringify(newProvider)) { - handleProviderNotification(openSnackbar, newProvider) - processProviderResponse(dispatch, newProvider) - } - - return newProvider -} - -export default (openSnackbar: Function) => async (dispatch: ReduxDispatch<*>) => { - const provider: ProviderProps = await getProviderInfo() +export default (provider: ProviderProps, openSnackbar: Function) => (dispatch: ReduxDispatch<*>) => { handleProviderNotification(openSnackbar, provider) - processProviderResponse(dispatch, provider) - - return provider }