mirror of
https://github.com/status-im/embark-area-51.git
synced 2025-01-10 05:56:00 +00:00
Form generation for view/constant/pure functions
This commit is contained in:
parent
dd1413a037
commit
43e7da5b7b
@ -120,7 +120,7 @@ Plugin.prototype.registerPipeline = function(matcthingFiles, cb) {
|
|||||||
|
|
||||||
Plugin.prototype.registerCustomType = function(type){
|
Plugin.prototype.registerCustomType = function(type){
|
||||||
this.pluginTypes.push(type);
|
this.pluginTypes.push(type);
|
||||||
}
|
};
|
||||||
|
|
||||||
Plugin.prototype.addFileToPipeline = function(file, intendedPath, options) {
|
Plugin.prototype.addFileToPipeline = function(file, intendedPath, options) {
|
||||||
this.pipelineFiles.push({file: file, intendedPath: intendedPath, options: options});
|
this.pipelineFiles.push({file: file, intendedPath: intendedPath, options: options});
|
||||||
|
@ -1,8 +1,41 @@
|
|||||||
|
|
||||||
const Handlebars = require('handlebars');
|
const Handlebars = require('handlebars');
|
||||||
const fs = require('../../core/fs');
|
const fs = require('../../core/fs');
|
||||||
const utils = require('../../utils/utils');
|
|
||||||
|
|
||||||
|
Handlebars.registerHelper('capitalize', function(word) {
|
||||||
|
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
Handlebars.registerHelper('ifview', function(stateMutability, options) {
|
||||||
|
let result = stateMutability == 'view' || stateMutability == 'pure' || stateMutability == 'constant';
|
||||||
|
if (result) {
|
||||||
|
return options.fn(this);
|
||||||
|
} else {
|
||||||
|
return options.inverse(this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Handlebars.registerHelper('ifarrlengthgt', function(arr, val, options) {
|
||||||
|
if (arr.length > val) {
|
||||||
|
return options.fn(this);
|
||||||
|
} else {
|
||||||
|
return options.inverse(this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Handlebars.registerHelper('emptyname', function(name, index) {
|
||||||
|
return name ? name : 'outputParam' + index;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
Handlebars.registerHelper('methodname', function(abiDefinition, functionName, inputs){
|
||||||
|
let funCount = abiDefinition.filter(x => x.name == functionName).length;
|
||||||
|
if(funCount == 1){
|
||||||
|
return '.' + functionName;
|
||||||
|
} else {
|
||||||
|
return new Handlebars.SafeString(`['${functionName}(${inputs !== null ? inputs.map(input => input.type).join(',') : '' })']`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
class ScaffoldingReact {
|
class ScaffoldingReact {
|
||||||
constructor(embark, options){
|
constructor(embark, options){
|
||||||
@ -43,7 +76,8 @@ class ScaffoldingReact {
|
|||||||
this._generateFile(contract, 'dapp.js.tpl', 'js',
|
this._generateFile(contract, 'dapp.js.tpl', 'js',
|
||||||
{
|
{
|
||||||
'title': contract.className + ' autogenerated UI',
|
'title': contract.className + ' autogenerated UI',
|
||||||
'contractName': contract.className
|
'contractName': contract.className,
|
||||||
|
'functions': contract.abiDefinition.filter(x => x.type == 'function')
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update config
|
// Update config
|
||||||
@ -51,10 +85,8 @@ class ScaffoldingReact {
|
|||||||
let embarkJson = JSON.parse(contents);
|
let embarkJson = JSON.parse(contents);
|
||||||
embarkJson.app["js/" + filename + ".js"] = "app/" + filename + '.js';
|
embarkJson.app["js/" + filename + ".js"] = "app/" + filename + '.js';
|
||||||
embarkJson.app[filename + ".html"] = "app/" + filename + '.html';
|
embarkJson.app[filename + ".html"] = "app/" + filename + '.html';
|
||||||
|
|
||||||
fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4));
|
fs.writeFileSync("./embark.json", JSON.stringify(embarkJson, null, 4));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,102 @@ import {{contractName}} from 'Embark/contracts/{{contractName}}';
|
|||||||
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import { Tabs, Tab } from 'react-bootstrap';
|
import { FormGroup, ControlLabel, FormControl, Button } 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}}
|
||||||
|
{{name}}: ''{{#unless @last}},{{/unless}}
|
||||||
|
{{/each}}
|
||||||
|
},
|
||||||
|
{{/if}}
|
||||||
|
{{#ifview stateMutability}}
|
||||||
|
output: null,
|
||||||
|
{{/ifview}}
|
||||||
|
message: ''
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
handleChange(e, name){
|
||||||
|
this.state[name] = e.target.value;
|
||||||
|
this.setState(this.state);
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleClick(e){
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
{{#ifview stateMutability}}
|
||||||
|
this.setState({output: null});
|
||||||
|
|
||||||
|
let result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}this.state.{{name}}{{#unless @last}}, {{/unless}}{{/each}}).call();
|
||||||
|
{{#ifarrlengthgt outputs 1}}
|
||||||
|
this.setState({output: {
|
||||||
|
{{#each outputs}}
|
||||||
|
{{emptyname name @index}}: result[{{@index}}]{{#unless @last}},{{/unless}}
|
||||||
|
{{/each}}
|
||||||
|
}});
|
||||||
|
{{else}}
|
||||||
|
this.setState({output: result});
|
||||||
|
{{/ifarrlengthgt}}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// TODO show on screen
|
||||||
|
{{/ifview}}
|
||||||
|
|
||||||
|
// TODO validate
|
||||||
|
}
|
||||||
|
|
||||||
|
render(){
|
||||||
|
return <div className="formSection">
|
||||||
|
<h3>{{name}}</h3>
|
||||||
|
<form>
|
||||||
|
{{#if inputs.length}}
|
||||||
|
{{#each inputs}}
|
||||||
|
<FormGroup>
|
||||||
|
<ControlLabel>{{name}}</ControlLabel>
|
||||||
|
<FormControl
|
||||||
|
type="text"
|
||||||
|
defaultValue={ this.state.input.{{name}} }
|
||||||
|
placeholder="{{type}}"
|
||||||
|
onChange={(e) => this.handleChange(e, '{{name}}')}
|
||||||
|
/>
|
||||||
|
</FormGroup>
|
||||||
|
{{/each}}
|
||||||
|
{{/if}}
|
||||||
|
{{#ifview stateMutability}}
|
||||||
|
<Button type="submit" bsStyle="primary" onClick={(e) => this.handleClick(e)}>Call</Button>
|
||||||
|
{
|
||||||
|
this.state.output != null ?
|
||||||
|
<React.Fragment>
|
||||||
|
<h4>Results</h4>
|
||||||
|
{{#ifarrlengthgt outputs 1}}
|
||||||
|
<ul>
|
||||||
|
{{#each outputs}}
|
||||||
|
<li>{{emptyname name @index}}: { this.state.output.{{emptyname name @index}} }</li>
|
||||||
|
{{/each}}
|
||||||
|
</ul>
|
||||||
|
{{else}}
|
||||||
|
{this.state.output}
|
||||||
|
{{/ifarrlengthgt}}
|
||||||
|
</React.Fragment>
|
||||||
|
: ''
|
||||||
|
}
|
||||||
|
|
||||||
|
{{/ifview}}
|
||||||
|
</form>
|
||||||
|
</div>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
|
|
||||||
class {{contractName}}UI extends React.Component {
|
class {{contractName}}UI extends React.Component {
|
||||||
constructor (props) {
|
constructor (props) {
|
||||||
@ -13,7 +108,11 @@ class {{contractName}}UI extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render(){
|
render(){
|
||||||
return (<h2>Test</h2>);
|
return (<div>
|
||||||
|
{{#each functions}}
|
||||||
|
<{{capitalize name}}_{{@index}}_Form />
|
||||||
|
{{/each}}
|
||||||
|
</div>);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,11 @@
|
|||||||
<head>
|
<head>
|
||||||
<title>{{title}}</title>
|
<title>{{title}}</title>
|
||||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
|
||||||
|
<style type="text/css">
|
||||||
|
.formSection{
|
||||||
|
margin-bottom: 5em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body class="container">
|
<body class="container">
|
||||||
<div id="app"></div>
|
<div id="app"></div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user