embark/lib/modules/scaffolding-react/templates/dapp.js.tpl

168 lines
5.6 KiB
Smarty
Raw Normal View History

2018-05-10 16:31:10 +00:00
import EmbarkJS from 'Embark/EmbarkJS';
import {{contractName}} from 'Embark/contracts/{{contractName}}';
import React from 'react';
import ReactDOM from 'react-dom';
2018-05-11 14:13:42 +00:00
import { FormGroup, ControlLabel, FormControl, Checkbox, Button, Alert } from 'react-bootstrap';
{{#each functions}}
class {{capitalize name}}_{{@index}}_Form extends React.Component {
constructor(props){
super(props);
this.state = {
{{#if inputs.length}}
input: {
{{#each inputs}}
2018-05-11 13:26:27 +00:00
{{name}}: {{#ifeq type 'bool'}}false{{else}}''{{/ifeq}}{{#unless @last}},{{/unless}}
{{/each}}
},
{{/if}}
{{#ifview stateMutability}}
output: null,
{{/ifview}}
error: null,
2018-05-11 17:15:06 +00:00
mined: null,
loading: false
};
}
handleChange(e, name){
2018-05-11 13:26:27 +00:00
this.state.input[name] = e.target.value;
this.setState(this.state);
}
handleCheckbox(e, name){
this.state.input[name] = e.target.checked;
this.setState(this.state);
}
async handleClick(e){
e.preventDefault();
2018-05-11 17:15:06 +00:00
this.setState({output: null, error: null, receipt: null, loading: true});
2018-05-11 14:13:42 +00:00
try {
{{#ifview stateMutability}}
2018-05-11 14:13:42 +00:00
{{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.input.{{name}}{{#unless @last}}, {{/unless}}{{/each}})
.call()
.then((result) => {
{{#iflengthgt outputs 1}}
this.setState({output: {
{{#each outputs}}
{{emptyname name @index}}: result[{{@index}}]{{#unless @last}},{{/unless}}
{{/each}}
2018-05-11 17:15:06 +00:00
}, loading: false});
2018-05-11 14:13:42 +00:00
{{else}}
2018-05-11 17:15:06 +00:00
this.setState({output: result, loading: false});
2018-05-11 14:13:42 +00:00
{{/iflengthgt}}
})
.catch((err) => {
2018-05-11 17:15:06 +00:00
this.setState({error: err.message, loading: false});
2018-05-11 14:13:42 +00:00
});
{{else}}
{{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.input.{{name}}{{#unless @last}}, {{/unless}}{{/each}})
.send({
from: web3.eth.defaultAccount
})
.then((_receipt) => {
console.log(_receipt);
2018-05-11 17:15:06 +00:00
this.setState({receipt: _receipt, loading: false})
})
.catch((err) => {
console.log(err);
2018-05-11 17:15:06 +00:00
this.setState({error: err.message, loading: false});
});
// TODO payable
{{/ifview}}
2018-05-11 14:13:42 +00:00
} catch(err) {
2018-05-11 17:15:06 +00:00
this.setState({error: err.message, loading: false});
2018-05-11 14:13:42 +00:00
}
// TODO validate
}
render(){
return <div className="formSection">
<h3>{{name}}</h3>
<form>
{{#if inputs.length}}
{{#each inputs}}
<FormGroup>
<ControlLabel>{{name}}</ControlLabel>
{{#ifeq type 'bool'}}
<Checkbox
onClick={(e) => this.handleCheckbox(e, '{{name}}')}
/>
{{else}}
<FormControl
type="text"
defaultValue={ this.state.input.{{name}} }
placeholder="{{type}}"
onChange={(e) => this.handleChange(e, '{{name}}')}
/>
{{/ifeq}}
</FormGroup>
{{/each}}
{{/if}}
{
this.state.error != null ?
<Alert bsStyle="danger">{this.state.error}</Alert>
: ''
}
{{#ifview stateMutability}}
2018-05-11 17:15:06 +00:00
<Button disabled={this.state.loading} type="submit" bsStyle="primary" onClick={(e) => this.handleClick(e)}>Call</Button>
{
this.state.output != null ?
<React.Fragment>
<h4>Results</h4>
2018-05-11 13:26:27 +00:00
{{#iflengthgt outputs 1}}
<ul>
{{#each outputs}}
<li>{{emptyname name @index}}: { this.state.output.{{emptyname name @index}} }</li>
{{/each}}
</ul>
{{else}}
2018-05-11 13:26:27 +00:00
{this.state.output.toString()}
{{/iflengthgt}}
</React.Fragment>
: ''
}
{{else}}
2018-05-11 17:15:06 +00:00
<Button disabled={this.state.loading} type="submit" bsStyle="primary" onClick={(e) => this.handleClick(e)}>{this.state.loading ? 'Sending...' : 'Send'}</Button>
{
this.state.receipt != null ?
<Alert bsStyle={this.state.receipt.status == "0x1" ? 'success' : 'danger'}>{this.state.receipt.status == "0x1" ? 'Success' : 'Failure / Revert'} - Transaction Hash: {this.state.receipt.transactionHash}</Alert>
: ''
}
{{/ifview}}
</form>
</div>;
}
}
{{/each}}
2018-05-10 16:31:10 +00:00
class {{contractName}}UI extends React.Component {
constructor (props) {
super(props);
this.state = {
};
}
render(){
return (<div>
{{#each functions}}
<{{capitalize name}}_{{@index}}_Form />
{{/each}}
</div>);
2018-05-10 16:31:10 +00:00
}
}
ReactDOM.render(<div>
2018-05-10 16:31:10 +00:00
<h1>{{title}}</h1>
<{{contractName}}UI />
</div>,
document.getElementById('app')
);