/
App.js
160 lines (140 loc) · 4.31 KB
/
App.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* eslint-disable prefer-destructuring,prefer-const */
import React, { Component } from 'react';
import MachineOwnerContract from '../build/contracts/MachineOwner.json';
import getWeb3 from './utils/getWeb3';
import './App.css';
import '../node_modules/uikit/dist/css/uikit.min.css';
import RegisterOwnerForm from './components/form/RegisterOwnerForm';
import RegisterMachineForm from './components/form/RegisterMachineForm';
import MachineList from './components/MachineList';
const contract = require('truffle-contract');
class App extends Component {
constructor(props) {
super(props);
this.state = {
web3: null,
forms: {
registerOwner: {
_disabled: false,
},
registerMachine: {
_disabled: true,
model: '',
make: '',
vin: ''
},
},
contracts: {
machineOwner: {
address: null,
machines: []
}
}
};
}
componentWillMount() {
// Get network provider and web3 instance.
// See utils/getWeb3 for more info.
getWeb3
.then((results) => {
this.setState({ web3: results.web3 });
})
.catch(() => {
console.log('Error finding web3.');
});
}
instantiateMachineOwnerContract() {
const MachineOwner = contract(MachineOwnerContract);
MachineOwner.setProvider(this.state.web3.currentProvider);
this.state.web3.eth.getAccounts((error, accounts) => {
MachineOwner.new({ from: accounts[0], gas: 2000000 })
.then((instance) => {
const state = { ...this.state };
state.contracts.machineOwner.address = instance.address;
state.forms.registerMachine._disabled = false;
this.setState(state);
});
});
}
callCreateMachine() {
const MachineOwner = contract(MachineOwnerContract);
MachineOwner.setProvider(this.state.web3.currentProvider);
const { forms, contracts } = this.state;
const address = contracts.machineOwner.address;
// eslint-disable-next-line one-var,no-unused-vars
let _,
model,
make,
vin;
({
_, model, make, vin
} = forms.registerMachine);
return this.state.web3.eth.getAccounts((error, accounts) => {
MachineOwner.at(address)
.then(instance => instance.createNewMachine(model, make, vin, { from: accounts[0] }))
.then((result) => {
const state = { ...this.state };
const machine = {
model,
make,
vin,
address: result.logs[0].args.newMachine
};
contracts.machineOwner.machines.push(machine);
this.setState(state);
});
});
}
machineFormChangedHandler(e) {
const name = e.target.name;
const value = e.target.value;
const address = this.state.contracts.machineOwner.address;
// Update form
const form = { ...this.state.forms.registerMachine };
form[name] = value;
// Validate form input
const {
model, make, vin
} = form;
const isValid = make !== '' && model !== '' && vin !== '' && address;
form._disabled = !isValid;
// Update state
const state = { ...this.state };
state.forms.registerMachine = form;
this.setState(state);
}
machineFormSubmittedHandler(e) {
e.preventDefault();
if (!this.state.forms.registerMachine._disabled) {
this.callCreateMachine();
}
}
ownerFormSubmittedHandler(e) {
e.preventDefault();
if (!this.state.forms.registerOwner._disabled) {
this.instantiateMachineOwnerContract();
}
}
render() {
const { contracts, forms } = this.state;
return (
<div className="App">
<div className="uk-container uk-container-center uk-margin-top uk-margin-large-bottom">
<div className="uk-grid-match" data-uk-grid-margin>
<RegisterOwnerForm
onSubmit={this.ownerFormSubmittedHandler.bind(this)}
state={forms.registerOwner}
/>
<RegisterMachineForm
onChange={this.machineFormChangedHandler.bind(this)}
onSubmit={this.machineFormSubmittedHandler.bind(this)}
state={forms.registerMachine}
/>
<MachineList machines={contracts.machineOwner.machines} />
</div>
</div>
</div>
);
}
}
export default App;