-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
92 lines (85 loc) · 2.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import React from 'react'
import { render } from 'react-dom'
import { Router, Route, Link, IndexRoute } from 'react-router'
import createBrowserHistory from 'history/lib/createBrowserHistory'
import {} from './app.css'
import Web3 from 'web3'
import Pudding from 'ether-pudding'
import SimpleStorage from './contracts/SimpleStorage.sol'
// Preform the normal web3 configurations
var web3 = new Web3()
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'))
Pudding.setWeb3(web3)
// Load our contract, giving it the Pudding object (standard pudding stuff)
SimpleStorage.load(Pudding)
// Create our Pudding contract object
var simpleStorage = SimpleStorage.deployed()
var App = React.createClass({
propTypes: {
children: React.PropTypes.element.isRequired
},
render: function () {
return (
<div>
<header>
<ul>
<li><Link to='/simplestorage'>SimpleStorage</Link></li>
</ul>
</header>
{this.props.children}
</div>
)
}
})
var SimpleStorageView = React.createClass({
getInitialState: function () {
return {simpleStorageVal: '0', inputVal: ''}
},
getSimpleStorage: function () {
const self = this
simpleStorage.get().then(function (tx) {
self.setState({simpleStorageVal: tx.toNumber()})
})
},
setSimpleStorage: function () {
const self = this
const val = parseInt(this.state.inputVal)
// console.log(val)
simpleStorage.set(val, {from: web3.eth.accounts[0]}).then(function (tx) {
self.setState({inputVal: ''})
console.log(tx)
}).catch(function (err) {
self.setState({inputVal: err})
console.error(err)
})
},
handleChange (event) {
const text = event.target.value
this.setState({inputVal: text})
},
render: function () {
return (
<div>
<p>SimpleStorage</p>
<div>
<input type='button' value='Get SimpleStorage' onClick={this.getSimpleStorage}/>
<span> {this.state.simpleStorageVal}</span>
</div>
<div>
<input type='button' value='Set SimpleStorage' onClick={this.setSimpleStorage}/>
<input onChange={this.handleChange} value={this.state.inputVal}/>
</div>
</div>
)
}
})
let history = createBrowserHistory()
render((
<Router history={history}>
<Route path='/' component={App}>
<IndexRoute component={SimpleStorageView}/>
<Route path='simplestorage' component={SimpleStorageView}/>
<Route path='*' component={SimpleStorageView}/>
</Route>
</Router>
), document.getElementById('root'))