Keep netId in browser's storage and use it when page first loading if possible

This commit is contained in:
Vadim 2019-12-30 13:43:01 +03:00
parent fd545e65bb
commit 34aad2fb89
2 changed files with 14 additions and 1 deletions

View File

@ -102,6 +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()

View File

@ -23,14 +23,26 @@ export default async function getWeb3(netId, onAccountChange) {
}
if (!netId) {
// Load for the first time in the current browser's session
if (web3) {
// MetaMask (or another plugin) is injected
netId = await web3.eth.net.getId()
if (!(netId in constants.NETWORKS)) {
netId = defaultNetId
// If plugin's netId is unsupported, try to use
// the previously chosen netId
netId = window.localStorage.netId
}
} else {
// MetaMask (or another plugin) is not injected,
// so try to use the previously chosen netId
netId = window.localStorage.netId
}
if (!(netId in constants.NETWORKS)) {
// If plugin's netId and/or previously chosen netId are not supported,
// fallback to default netId
netId = defaultNetId
}
window.localStorage.netId = netId
window.sessionStorage.netId = netId
}