1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

leaner bootstrap, remove window any casting

This commit is contained in:
Jernej Pregelj 2019-02-08 11:44:02 +01:00
parent 513d615df3
commit b968952ef4
2 changed files with 52 additions and 46 deletions

View File

@ -8,9 +8,17 @@ import './styles/global.scss'
import { nodeHost, nodePort, nodeScheme } from './config'
declare global {
interface Window {
web3: Web3,
ethereum: any
}
}
interface AppState {
isLogged: boolean
web3: any
isLoading: boolean
web3: Web3
ocean: {}
startLogin: () => void
}
@ -25,7 +33,12 @@ class App extends Component<{}, AppState> {
public state = {
isLogged: false,
web3: {},
isLoading: true,
web3: new Web3(
new Web3.providers.HttpProvider(
`${nodeScheme}://${nodeHost}:${nodePort}`
)
),
ocean: {},
startLogin: this.startLogin
}
@ -45,62 +58,54 @@ class App extends Component<{}, AppState> {
}
private startLoginProcess = async () => {
if ((window as any).web3) {
const web3 = new Web3((window as any).web3.currentProvider)
this.setState({isLoading: true})
if (window.web3) {
const web3 = new Web3(window.web3.currentProvider)
try {
const accounts = await web3.eth.getAccounts()
if (accounts.length === 0 && (window as any).ethereum) {
await (window as any).ethereum.enable()
const { ocean } = await provideOcean()
this.setState(state => ({
if (accounts.length > 0) {
this.setState({
isLogged: true,
web3,
ocean
}))
web3
})
} else {
if (accounts.length === 0 && window.ethereum) {
await window.ethereum.enable()
this.setState({
isLogged: true,
web3
})
}
}
} catch (e) {
// something went wrong, show error?
}
} else {
// no metamask/mist, show installation guide!
}
this.setState({isLoading: false})
}
private bootstrap = async () => {
if (window.web3) {
const web3 = new Web3(window.web3.currentProvider)
try {
const accounts = await web3.eth.getAccounts()
if (accounts.length > 0) {
this.setState(state => ({
isLogged: true,
web3
}))
}
} catch (e) {
this.setDefaultProvider()
// continue with default
}
} else {
this.setDefaultProvider()
}
}
private bootstrap = async () => {
if ((window as any).web3) {
const web3 = new Web3((window as any).web3.currentProvider)
try {
const accounts = await web3.eth.getAccounts()
if (accounts.length > 0) {
const { ocean } = await provideOcean()
this.setState(state => ({
isLogged: true,
web3,
ocean
}))
}
} catch (e) {
this.setDefaultProvider()
}
} else {
this.setDefaultProvider()
}
}
private setDefaultProvider = () => {
this.setState(state => ({
isLogged: false,
web3: new Web3(
new Web3.providers.HttpProvider(
`${nodeScheme}://${nodeHost}:${nodePort}`
)
)
}))
const { ocean } = await provideOcean()
this.setState({
isLoading: false,
ocean
})
}
}

View File

@ -2,6 +2,7 @@ import React from 'react'
export const User = React.createContext({
isLogged: false,
isLoading: false,
web3: {},
ocean: {},
startLogin: () => {