FunctionWrapper.jsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //takes instance and its part of the ABI + its part of the template
  2. var FunctionWrapper = React.createClass({
  3. executeFunction: function(type) {
  4. args = {};
  5. //get inputs [replace with pure js as this requires jquery as a dependency]
  6. $.each(this.refs, function(i, obj) {
  7. args[obj.props.arg] = obj.state.value; //map inputs to a dictionary
  8. });
  9. var function_name = this.props.data.name.split("(")[0]; //seems very hacky to get only function name. It's written as 'function(args)' usually.
  10. if(type == "call") {
  11. result = this.props.instance[function_name].call(args);
  12. console.log(result);
  13. } else if(type == "transact") {
  14. result = this.props.instance[function_name].sendTransaction(args);
  15. console.log(result);
  16. }
  17. },
  18. render: function() {
  19. //use react refs to keep track of inputs to a function.
  20. if(this.props.function_template.button != undefined) {
  21. var button = <div><button className={"btn btn-default"} onClick={this.executeFunction.bind(this,"transact")}>{this.props.function_template.button}</button></div>;
  22. } else {
  23. var button = <div><button className={"btn btn-default"} onClick={this.executeFunction.bind(this,"call")}>Call() {this.props.data.name}</button> - <button className={"btn btn-default"} onClick={this.executeFunction.bind(this,"transact")}>Transact() {this.props.data.name}</button></div>;
  24. }
  25. return (
  26. <div>
  27. {this.props.data.inputs.map(function(result) {
  28. var input_template = {};
  29. var arg = result.name;
  30. if (this.props.function_template.inputs.hasOwnProperty(result.name)) { //if a specific function has a template for it.
  31. input_template = this.props.function_template.inputs[result.name];
  32. arg = this.props.function_template.inputs[result.name].default_value;
  33. }
  34. return <div key={result.name}><InputWrapper input_template={input_template} ref={result.name} arg={arg} /></div>
  35. }, this)}
  36. <br />
  37. {button}
  38. </div>
  39. );
  40. }
  41. });