Refactor fetch provider

This commit is contained in:
Germán Martínez 2019-06-06 20:31:58 +02:00
parent 5444e56401
commit b615d20a65
3 changed files with 18 additions and 30 deletions

View File

@ -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,
}

View File

@ -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<Props, State> {
hasError: false,
}
async componentDidMount() {
componentDidMount() {
this.onConnect()
}
@ -39,19 +40,25 @@ class HeaderComponent extends React.PureComponent<Props, State> {
}
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)
}

View File

@ -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
}