Skip to content

Commit df51480

Browse files
committed
Adds tutorial code
1 parent 9c0e223 commit df51480

File tree

3 files changed

+85
-19
lines changed

3 files changed

+85
-19
lines changed

src/components/App.js

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,65 @@
11
import React, { Component } from 'react';
2+
import Web3 from 'web3'
23
import logo from '../logo.png';
34
import './App.css';
5+
import Authorized from './Authorized'
6+
import Unauthorized from './Unauthorized'
47

58
class App extends Component {
9+
async componentWillMount() {
10+
await this.loadWeb3()
11+
await this.fetchAccount()
12+
await this.checkAuthorization()
13+
}
14+
15+
async loadWeb3() {
16+
if (window.ethereum) {
17+
window.web3 = new Web3(window.ethereum)
18+
await window.ethereum.enable()
19+
}
20+
else if (window.web3) {
21+
window.web3 = new Web3(window.web3.currentProvider)
22+
}
23+
else {
24+
window.alert('Non-Ethereum browser detected. You should consider trying MetaMask!')
25+
}
26+
}
27+
28+
async fetchAccount() {
29+
const web3 = window.web3
30+
const accounts = await web3.eth.getAccounts()
31+
this.setState({ account: accounts[0] })
32+
}
33+
34+
async checkAuthorization() {
35+
const authorizedAccounts = [
36+
'0x33a75943Ca7Ed31C66199FE851AeaF0A758837E3',
37+
'0xAE2f34aEead72Bfd46138A4E662FE284F9a4DB43',
38+
'0x1E2418fe04D20cD7eE6A91A3AD1d299fa8c9e20c',
39+
'0x8A97d72B4c823d96f18d76bA668e2a5CDcAC95Af',
40+
'0x719C632328eB541183F34C5c616ac359E828ec21'
41+
]
42+
const authorized = authorizedAccounts.includes(this.state.account)
43+
this.setState({ authorized })
44+
}
45+
46+
constructor(props) {
47+
super(props)
48+
this.state = {
49+
account: '',
50+
authorized: false
51+
}
52+
}
53+
654
render() {
55+
let body
56+
57+
if (this.state.authorized) {
58+
body = <Authorized account={this.state.account} />
59+
} else {
60+
body = <Unauthorized account={this.state.account} />
61+
}
62+
763
return (
864
<div>
965
<nav className="navbar navbar-dark fixed-top bg-dark flex-md-nowrap p-0 shadow">
@@ -20,25 +76,7 @@ class App extends Component {
2076
<div className="row">
2177
<main role="main" className="col-lg-12 d-flex text-center">
2278
<div className="content mr-auto ml-auto">
23-
<a
24-
href="http://www.dappuniversity.com/bootcamp"
25-
target="_blank"
26-
rel="noopener noreferrer"
27-
>
28-
<img src={logo} className="App-logo" alt="logo" />
29-
</a>
30-
<h1>Dapp University Starter Kit</h1>
31-
<p>
32-
Edit <code>src/components/App.js</code> and save to reload.
33-
</p>
34-
<a
35-
className="App-link"
36-
href="http://www.dappuniversity.com/bootcamp"
37-
target="_blank"
38-
rel="noopener noreferrer"
39-
>
40-
LEARN BLOCKCHAIN <u><b>NOW! </b></u>
41-
</a>
79+
{body}
4280
</div>
4381
</main>
4482
</div>

src/components/Authorized.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React, { Component } from 'react';
2+
3+
class Authorized extends Component {
4+
render() {
5+
return (
6+
<div>
7+
<h1>Contrats, you're authorized!</h1>
8+
<p>Your account {this.props.account} has been authorized.</p>
9+
</div>
10+
);
11+
}
12+
}
13+
14+
export default Authorized;

src/components/Unauthorized.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React, { Component } from 'react';
2+
3+
class Unauthorized extends Component {
4+
render() {
5+
return (
6+
<div>
7+
<h1>Sorry, you're unauthorized!</h1>
8+
<p>Your account {this.props.account} is unauthorized. Please sign up.</p>
9+
</div>
10+
);
11+
}
12+
}
13+
14+
export default Unauthorized;

0 commit comments

Comments
 (0)