diff --git a/lib/modules/scaffolding-react/templates/dapp.js.tpl b/lib/modules/scaffolding-react/templates/dapp.js.tpl index c0827fc7..c02bba74 100644 --- a/lib/modules/scaffolding-react/templates/dapp.js.tpl +++ b/lib/modules/scaffolding-react/templates/dapp.js.tpl @@ -1,13 +1,13 @@ import EmbarkJS from 'Embark/EmbarkJS'; import {{contractName}} from 'Embark/contracts/{{contractName}}'; -import React from 'react'; +import React, { Component, Fragment } from 'react'; import ReactDOM from 'react-dom'; import { FormGroup, ControlLabel, FormControl, Checkbox, Button, Alert, InputGroup } from 'react-bootstrap'; {{#each functions}} -class {{capitalize name}}Form{{@index}} extends React.Component { +class {{capitalize name}}Form{{@index}} extends Component { constructor(props){ super(props); this.state = { @@ -45,45 +45,42 @@ class {{capitalize name}}Form{{@index}} extends React.Component { try { {{#ifview stateMutability}} - {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.input.{{name}}{{#unless @last}}, {{/unless}}{{/each}}) - .call() - .then((result) => { + const result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.input.{{name}}{{#unless @last}}, {{/unless}}{{/each}}).call() {{#iflengthgt outputs 1}} - this.setState({output: { + this.setState({output: { {{#each outputs}} - {{emptyname name @index}}: result[{{@index}}]{{#unless @last}},{{/unless}} + {{emptyname name @index}}: result[{{@index}}]{{#unless @last}},{{/unless}} {{/each}} - }}); + }}); {{else}} - this.setState({output: result}); - {{/iflengthgt}} - }) - .catch((err) => { - this.setState({error: err.message}); - }); + this.setState({output: result}); + {{/iflengthgt}} {{else}} - {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.input.{{name}}{{#unless @last}}, {{/unless}}{{/each}}) - .send({ - {{#if payable}} - value: this.state.value, - {{/if}} - from: web3.eth.defaultAccount - }) - .then((_receipt) => { - console.log(_receipt); - this.setState({receipt: _receipt}) - }) - .catch((err) => { - console.log(err); - this.setState({error: err.message}); - }); + const toSend = {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.input.{{name}}{{#unless @last}}, {{/unless}}{{/each}}); + + const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount}); + + const receipt = await toSend.send({ + {{#if payable}} + value: this.state.value, + {{/if}} + from: web3.eth.defaultAccount, + gasLimit: estimatedGas + }); + + console.log(receipt); + + this.setState({receipt}); {{/ifview}} } catch(err) { + console.error(err); this.setState({error: err.message}); } } render(){ + const {input, value, error, output, receipt} = this.state; + return

{{name}}

@@ -98,7 +95,7 @@ class {{capitalize name}}Form{{@index}} extends React.Component { {{else}} this.handleChange(e, '{{name}}')} /> @@ -113,7 +110,7 @@ class {{capitalize name}}Form{{@index}} extends React.Component { Ξ { this.setState({value: e.target.value}); }} /> @@ -121,37 +118,34 @@ class {{capitalize name}}Form{{@index}} extends React.Component { {{/if}} - { - this.state.error != null ? - {this.state.error} - : '' - } + + { error != null && {error} } + {{#ifview stateMutability}} { - this.state.output != null ? - + output && +

Results

{{#iflengthgt outputs 1}}
    {{#each outputs}} -
  • {{emptyname name @index}}: { this.state.output.{{emptyname name @index}} }
  • +
  • {{emptyname name @index}}: { output.{{emptyname name @index}} }
  • {{/each}}
{{else}} - {this.state.output.toString()} + {output.toString()} {{/iflengthgt}} -
- : '' + } {{else}} { - this.state.receipt != null ? - - {this.state.receipt.status == "0x1" ? 'Success' : 'Failure / Revert'} - Transaction Hash: {this.state.receipt.transactionHash} - - : '' + receipt && + + {receipt.status == "0x1" ? 'Success' : 'Failure / Revert'} - Transaction Hash: {receipt.transactionHash} + + } {{/ifview}} @@ -161,8 +155,7 @@ class {{capitalize name}}Form{{@index}} extends React.Component { {{/each}} - -class {{contractName}}UI extends React.Component { +class {{contractName}}UI extends Component { constructor (props) { super(props); this.state = { @@ -171,6 +164,7 @@ class {{contractName}}UI extends React.Component { render(){ return (
+

{{title}}

{{#each functions}} <{{capitalize name}}Form{{@index}} /> {{/each}} @@ -179,10 +173,4 @@ class {{contractName}}UI extends React.Component { } -ReactDOM.render(
-

{{title}}

- <{{contractName}}UI /> -
, - document.getElementById('app') -); - +ReactDOM.render(<{{contractName}}UI />, document.getElementById('app'));