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

180 lines
6.2 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, { Component, Fragment } from 'react';
import ReactDOM from 'react-dom';
2018-05-11 18:16:27 +00:00
import { FormGroup, ControlLabel, FormControl, Checkbox, Button, Alert, InputGroup } from 'react-bootstrap';
{{#each functions}}
class {{capitalize name}}Form{{@index}} extends Component {
constructor(props){
super(props);
this.state = {
{{#if inputs.length}}
input: {
{{#each inputs}}
2018-10-12 19:07:59 +00:00
{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}: {{#ifeq type 'bool'}}false{{else}}''{{/ifeq}}{{#unless @last}},{{/unless}}
{{/each}}
},
{{/if}}
2018-05-11 18:16:27 +00:00
{{#if payable}}
value: 0,
{{/if}}
{{#ifview stateMutability}}
output: null,
{{/ifview}}
error: null,
2018-05-11 18:16:27 +00:00
mined: null
};
}
handleChange(e, name){
const {input} = this.state;
input[name] = e.target.value;
this.setState({input});
2018-05-11 13:26:27 +00:00
}
handleCheckbox(e, name){
const {input} = this.state;
input[name] = e.target.checked;
this.setState({input});
}
async handleClick(e){
e.preventDefault();
const {input, value} = this.state;
2018-05-11 18:16:27 +00:00
this.setState({output: null, error: null, receipt: null});
2018-05-11 14:13:42 +00:00
try {
{{#ifview stateMutability}}
2018-10-12 19:07:59 +00:00
const result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}}).call()
2018-05-11 14:13:42 +00:00
{{#iflengthgt outputs 1}}
this.setState({output: {
2018-05-11 14:13:42 +00:00
{{#each outputs}}
{{emptyname name @index}}: result[{{@index}}]{{#unless @last}},{{/unless}}
2018-05-11 14:13:42 +00:00
{{/each}}
}});
2018-05-11 14:13:42 +00:00
{{else}}
this.setState({output: result});
{{/iflengthgt}}
{{else}}
2018-10-12 19:07:59 +00:00
{{#each inputs}}
{{#ifarr type}}
input.{{trim name}} = input.{{trim name}}.split(',').map(x => trim(x.toString()));
{{/ifarr}}
{{/each}}
const toSend = {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}});
const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount});
const receipt = await toSend.send({
{{#if payable}}
value: value,
{{/if}}
from: web3.eth.defaultAccount,
gasLimit: estimatedGas
});
console.log(receipt);
this.setState({receipt});
{{/ifview}}
2018-05-11 14:13:42 +00:00
} catch(err) {
console.error(err);
2018-05-11 18:16:27 +00:00
this.setState({error: err.message});
2018-05-11 14:13:42 +00:00
}
}
render(){
const {input, value, error, output, receipt} = this.state;
return <div className="formSection">
<h3>{{name}}</h3>
<form>
{{#if inputs.length}}
{{#each inputs}}
<FormGroup>
<ControlLabel>{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}</ControlLabel>
{{#ifeq type 'bool'}}
<Checkbox
2018-10-12 19:07:59 +00:00
onClick={(e) => this.handleCheckbox(e, '{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}')}
/>
{{else}}
<FormControl
type="text"
2018-10-12 19:07:59 +00:00
defaultValue={ input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}} }
placeholder="{{type}}"
2018-10-12 19:07:59 +00:00
onChange={(e) => this.handleChange(e, '{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}')}
/>
{{/ifeq}}
</FormGroup>
{{/each}}
{{/if}}
2018-05-11 18:16:27 +00:00
{{#if payable}}
<FormGroup>
<ControlLabel>Value</ControlLabel>
<InputGroup>
<InputGroup.Addon>Ξ</InputGroup.Addon>
<FormControl
type="text"
defaultValue={ value }
2018-05-11 18:16:27 +00:00
placeholder="{{type}}"
onChange={(e) => { this.setState({value: e.target.value}); }}
/>
<InputGroup.Addon>wei</InputGroup.Addon>
</InputGroup>
</FormGroup>
{{/if}}
{ error != null && <Alert bsStyle="danger">{error}</Alert> }
{{#ifview stateMutability}}
2018-05-11 18:16:27 +00:00
<Button type="submit" bsStyle="primary" onClick={(e) => this.handleClick(e)}>Call</Button>
{
output &&
<Fragment>
<h4>Results</h4>
2018-05-11 13:26:27 +00:00
{{#iflengthgt outputs 1}}
<ul>
{{#each outputs}}
<li>{{emptyname name @index}}: { output.{{emptyname name @index}} }</li>
{{/each}}
</ul>
{{else}}
{output.toString()}
2018-05-11 13:26:27 +00:00
{{/iflengthgt}}
</Fragment>
}
{{else}}
2018-05-11 18:16:27 +00:00
<Button type="submit" bsStyle="primary" onClick={(e) => this.handleClick(e)}>Send</Button>
{
receipt &&
<Fragment>
<Alert bsStyle={receipt.status == "0x1" ? 'success' : 'danger'}>{receipt.status == "0x1" ? 'Success' : 'Failure / Revert'} - Transaction Hash: {receipt.transactionHash}</Alert>
</Fragment>
}
{{/ifview}}
</form>
</div>;
}
}
{{/each}}
2018-05-15 13:53:05 +00:00
function {{contractName}}UI(props) {
return (<div>
<h1>{{title}}</h1>
2018-05-15 13:53:05 +00:00
{{#each functions}}
<{{capitalize name}}Form{{@index}} />
{{/each}}
</div>);
}
ReactDOM.render(<{{contractName}}UI />, document.getElementById('app'));