select the network from metamask when first visiting

This commit is contained in:
Max Alekseenko 2019-11-18 14:38:14 +03:00
parent 795472ec57
commit fd545e65bb
2 changed files with 17 additions and 4 deletions

View File

@ -37,7 +37,7 @@ class AppMainRouter extends Component {
}
initChain = () => {
const netId = window.localStorage.netId
const netId = window.sessionStorage.netId
getWeb3(netId, this.onAccountChange)
.then(async web3Config => {
await this.initialize(web3Config)
@ -102,7 +102,7 @@ class AppMainRouter extends Component {
onNetworkChange = e => {
commonStore.showLoading(getNetworkBranch(e.value))
window.localStorage.netId = e.value
window.sessionStorage.netId = e.value
contractsStore.resetContracts()
ballotsStore.reset()
this.initChain()

View File

@ -4,9 +4,8 @@ import { constants } from './constants'
const defaultNetId = helpers.netIdByBranch(constants.CORE)
export default async function getWeb3(netId = defaultNetId, onAccountChange) {
export default async function getWeb3(netId, onAccountChange) {
let web3 = null
netId = Number(netId)
// Checking if Web3 has been injected by the browser (Mist/MetaMask)
if (window.ethereum) {
@ -23,6 +22,20 @@ export default async function getWeb3(netId = defaultNetId, onAccountChange) {
console.log('Injected web3 detected.')
}
if (!netId) {
if (web3) {
netId = await web3.eth.net.getId()
if (!(netId in constants.NETWORKS)) {
netId = defaultNetId
}
} else {
netId = defaultNetId
}
window.sessionStorage.netId = netId
}
netId = Number(netId)
const network = constants.NETWORKS[netId]
const injectedWeb3 = web3 !== null
let netIdName = network.NAME