Add network prop to provider entity

This commit is contained in:
apanizo 2018-08-28 14:13:42 +02:00
parent 58a84c1d6a
commit b1244a22fd
4 changed files with 24 additions and 5 deletions

View File

@ -7,11 +7,11 @@ import addProvider from './addProvider'
export const processProviderResponse = (dispatch: ReduxDispatch<*>, response: ProviderProps) => {
const {
name, available, loaded, account,
name, available, loaded, account, network,
} = response
const walletRecord = makeProvider({
name, available, loaded, account,
name, available, loaded, account, network,
})
dispatch(addProvider(walletRecord))

View File

@ -7,6 +7,7 @@ export type ProviderProps = {
loaded: boolean,
available: boolean,
account: string,
network: number,
}
export const makeProvider: RecordFactory<ProviderProps> = Record({
@ -14,6 +15,7 @@ export const makeProvider: RecordFactory<ProviderProps> = Record({
loaded: false,
available: false,
account: '',
network: 0,
})
export type Provider = RecordOf<ProviderProps>

View File

@ -2,6 +2,8 @@
import { createSelector } from 'reselect'
import type { Provider } from '~/logic/wallets/store/model/provider'
import { PROVIDER_REDUCER_ID } from '~/logic/wallets/store/reducer/provider'
import { upperFirst } from '~/utils/css'
import { ETHEREUM_NETWORK_IDS, ETHEREUM_NETWORK } from '~/logic/wallets/getWeb3'
const providerSelector = (state: any): Provider => state[PROVIDER_REDUCER_ID]
@ -24,3 +26,18 @@ export const providerNameSelector = createSelector(
return loaded && available ? name.toLowerCase() : undefined
},
)
export const networkSelector = createSelector(
providerSelector,
(provider: Provider) => {
const networkId = provider.get('network')
const network = ETHEREUM_NETWORK_IDS[networkId] || ETHEREUM_NETWORK.UNKNOWN
return upperFirst(network)
},
)
export const connectedSelector = createSelector(
providerSelector,
(provider: Provider) => provider.get('loaded') && provider.get('available'),
)

View File

@ -25,7 +25,7 @@ const providerReducerTests = () => {
it('reducer should return default Provider record when no Metamask is loaded', () => {
// GIVEN
const emptyResponse: ProviderProps = {
name: '', loaded: false, available: false, account: '',
name: '', loaded: false, available: false, account: '', network: 0,
}
// WHEN
@ -39,7 +39,7 @@ const providerReducerTests = () => {
it('reducer should return avaiable with its default value when is loaded but not available', () => {
// GIVEN
const metamaskLoaded: ProviderProps = {
name: 'METAMASK', loaded: true, available: false, account: '',
name: 'METAMASK', loaded: true, available: false, account: '', network: 0,
}
// WHEN
@ -53,7 +53,7 @@ const providerReducerTests = () => {
it('reducer should return metamask provider when it is loaded and available', () => {
// GIVEN
const metamask: ProviderProps = {
name: 'METAMASK', loaded: true, available: true, account: '',
name: 'METAMASK', loaded: true, available: true, account: '', network: 0,
}
// WHEN