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

113 lines
2.9 KiB
TypeScript
Raw Normal View History

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'
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
declare global {
interface Window {
2019-02-08 12:04:19 +01:00
web3: Web3
ethereum: any
}
}
2019-02-05 17:05:28 +01:00
interface AppState {
2019-02-05 16:00:22 +01:00
isLogged: boolean
isLoading: boolean
web3: Web3
2019-02-05 16:00:22 +01:00
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,
isLoading: true,
web3: new Web3(
new Web3.providers.HttpProvider(
`${nodeScheme}://${nodeHost}:${nodePort}`
)
),
2019-02-05 17:05:28 +01:00
ocean: {},
startLogin: this.startLogin
2019-01-30 17:33:56 +01:00
}
public async componentDidMount() {
this.bootstrap()
}
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-30 17:33:56 +01:00
private startLoginProcess = async () => {
2019-02-08 12:04:19 +01:00
this.setState({ isLoading: true })
if (window.web3) {
const web3 = new Web3(window.web3.currentProvider)
2019-01-30 17:33:56 +01:00
try {
const accounts = await web3.eth.getAccounts()
if (accounts.length > 0) {
this.setState({
2019-01-30 17:33:56 +01:00
isLogged: true,
web3
})
} else {
if (accounts.length === 0 && window.ethereum) {
await window.ethereum.enable()
this.setState({
isLogged: true,
web3
})
}
2019-01-30 17:33:56 +01:00
}
} catch (e) {
// something went wrong, show error?
2019-01-30 17:33:56 +01:00
}
} else {
// no metamask/mist, show installation guide!
2019-01-30 17:33:56 +01:00
}
2019-02-08 12:04:19 +01:00
this.setState({ isLoading: false })
2019-01-30 17:33:56 +01:00
}
private bootstrap = async () => {
if (window.web3) {
const web3 = new Web3(window.web3.currentProvider)
2019-01-30 17:33:56 +01:00
try {
const accounts = await web3.eth.getAccounts()
if (accounts.length > 0) {
2019-02-08 12:04:19 +01:00
this.setState({
2019-01-30 17:33:56 +01:00
isLogged: true,
web3
2019-02-08 12:04:19 +01:00
})
2019-01-30 17:33:56 +01:00
}
} catch (e) {
// continue with default
2019-01-30 17:33:56 +01:00
}
}
const { ocean } = await provideOcean()
this.setState({
2019-02-08 12:04:19 +01:00
isLoading: false,
ocean
})
2019-01-30 17:33:56 +01:00
}
}
2019-01-23 11:15:27 +01:00
export default App