2019-01-23 11:15:27 +01:00
|
|
|
import React, { Component } from 'react'
|
2019-01-30 17:33:56 +01:00
|
|
|
import Web3 from 'web3'
|
2019-01-23 13:03:41 +01:00
|
|
|
import styles from './App.module.scss'
|
2019-01-30 17:33:56 +01:00
|
|
|
import { User } from './context/User'
|
2019-02-01 13:01:36 +01:00
|
|
|
import { provideOcean } from './ocean'
|
2019-01-30 17:33:56 +01:00
|
|
|
import Routes from './Routes'
|
2019-01-23 13:03:41 +01:00
|
|
|
import './styles/global.scss'
|
|
|
|
|
2019-02-05 16:00:22 +01:00
|
|
|
import { nodeHost, nodePort, nodeScheme } from './config'
|
2019-01-30 17:33:56 +01:00
|
|
|
|
2019-02-05 17:05:28 +01:00
|
|
|
interface AppState {
|
2019-02-05 16:00:22 +01:00
|
|
|
isLogged: boolean
|
|
|
|
web3: any
|
|
|
|
ocean: {}
|
|
|
|
startLogin: () => void
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 17:05:28 +01:00
|
|
|
class App extends Component<{}, AppState> {
|
|
|
|
public startLogin = (event?: any) => {
|
|
|
|
if (event) {
|
|
|
|
event.preventDefault()
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
2019-02-05 17:05:28 +01:00
|
|
|
this.startLoginProcess()
|
|
|
|
}
|
|
|
|
|
|
|
|
public state = {
|
|
|
|
isLogged: false,
|
|
|
|
web3: {},
|
|
|
|
ocean: {},
|
|
|
|
startLogin: this.startLogin
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async componentDidMount() {
|
|
|
|
this.bootstrap()
|
|
|
|
}
|
2019-01-18 17:34:40 +01:00
|
|
|
|
2019-01-23 11:38:27 +01:00
|
|
|
public render() {
|
|
|
|
return (
|
2019-01-23 13:03:41 +01:00
|
|
|
<div className={styles.app}>
|
2019-01-30 17:33:56 +01:00
|
|
|
<User.Provider value={this.state}>
|
2019-01-23 13:03:41 +01:00
|
|
|
<Routes />
|
|
|
|
</User.Provider>
|
|
|
|
</div>
|
2019-01-23 11:38:27 +01:00
|
|
|
)
|
|
|
|
}
|
2019-01-30 17:33:56 +01:00
|
|
|
|
|
|
|
private startLoginProcess = async () => {
|
2019-02-05 16:00:22 +01:00
|
|
|
if ((window as any).web3) {
|
2019-01-30 17:33:56 +01:00
|
|
|
const web3 = new Web3((window as any).web3.currentProvider)
|
|
|
|
try {
|
|
|
|
const accounts = await web3.eth.getAccounts()
|
|
|
|
if (accounts.length === 0 && (window as any).ethereum) {
|
|
|
|
await (window as any).ethereum.enable()
|
2019-02-01 13:01:36 +01:00
|
|
|
const { ocean } = await provideOcean()
|
2019-01-30 17:33:56 +01:00
|
|
|
this.setState(state => ({
|
|
|
|
isLogged: true,
|
2019-02-01 13:01:36 +01:00
|
|
|
web3,
|
|
|
|
ocean
|
2019-01-30 17:33:56 +01:00
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
this.setState(state => ({
|
|
|
|
isLogged: true,
|
|
|
|
web3
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.setDefaultProvider()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.setDefaultProvider()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bootstrap = async () => {
|
2019-02-05 16:00:22 +01:00
|
|
|
if ((window as any).web3) {
|
2019-01-30 17:33:56 +01:00
|
|
|
const web3 = new Web3((window as any).web3.currentProvider)
|
|
|
|
try {
|
|
|
|
const accounts = await web3.eth.getAccounts()
|
|
|
|
if (accounts.length > 0) {
|
2019-02-01 13:01:36 +01:00
|
|
|
const { ocean } = await provideOcean()
|
2019-01-30 17:33:56 +01:00
|
|
|
this.setState(state => ({
|
|
|
|
isLogged: true,
|
2019-02-01 13:01:36 +01:00
|
|
|
web3,
|
|
|
|
ocean
|
2019-01-30 17:33:56 +01:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.setDefaultProvider()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.setDefaultProvider()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private setDefaultProvider = () => {
|
|
|
|
this.setState(state => ({
|
|
|
|
isLogged: false,
|
2019-02-05 16:00:22 +01:00
|
|
|
web3: new Web3(
|
|
|
|
new Web3.providers.HttpProvider(
|
|
|
|
`${nodeScheme}://${nodeHost}:${nodePort}`
|
|
|
|
)
|
|
|
|
)
|
2019-01-30 17:33:56 +01:00
|
|
|
}))
|
|
|
|
}
|
2019-01-18 17:34:40 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 11:15:27 +01:00
|
|
|
export default App
|