From 42db8258e018fee58d3ca3f4ab025dd7d13513ee Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 19 Apr 2018 14:42:11 -0400 Subject: [PATCH] Allows copying commands to clipboard --- .../contracts/components/account-list.js | 15 ++++++++ .../contracts/components/function-form.js | 26 ++++++++------ .../backend/contracts/components/function.js | 35 +++++++++++++++---- 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/lib/modules/webserver/backend/contracts/components/account-list.js b/lib/modules/webserver/backend/contracts/components/account-list.js index abb3b96a1..7fc3ad95c 100644 --- a/lib/modules/webserver/backend/contracts/components/account-list.js +++ b/lib/modules/webserver/backend/contracts/components/account-list.js @@ -6,6 +6,9 @@ class AccountList extends React.Component { errorMessage: "", accounts: [] }; + + this.handleClick = this.handleClick.bind(this); + this.handleCopyClick = this.handleCopyClick.bind(this); } @@ -23,7 +26,16 @@ class AccountList extends React.Component { } + handleCopyClick(e){ + e.preventDefault(); + var dummy = document.createElement("input"); + document.body.appendChild(dummy); + dummy.setAttribute('value', "await web3.eth.getAccounts();"); + dummy.select(); + document.execCommand("copy"); + document.body.removeChild(dummy); + } render(){ return @@ -32,6 +44,9 @@ class AccountList extends React.Component {

Get Accounts

+
+ +
diff --git a/lib/modules/webserver/backend/contracts/components/function-form.js b/lib/modules/webserver/backend/contracts/components/function-form.js index 50ac2d3b0..43ca43d69 100644 --- a/lib/modules/webserver/backend/contracts/components/function-form.js +++ b/lib/modules/webserver/backend/contracts/components/function-form.js @@ -8,7 +8,14 @@ class FunctionForm extends React.Component { receipt: null }; + this.child = React.createRef(); + this.showResults = this.showResults.bind(this); + this.handleCopyClick = this.handleCopyClick.bind(this); + } + + handleCopyClick(e){ + this.child.current.copyCommand(e); } _getFunctionParamFields(elem){ @@ -30,21 +37,18 @@ class FunctionForm extends React.Component { const receipt = this.state.receipt; return
- { - this.props.abi.type == "function" || this.props.abi.type == "fallback" - ? -
-

{this.props.abi.type == 'function' ? this.props.abi.name : (this.props.abi.type == 'fallback' ? '(fallback)' : this.props.abi.name)}

+ +
+

{this.props.abi.type == 'function' ? this.props.abi.name : (this.props.abi.type == 'fallback' ? '(fallback)' : this.props.abi.name)}

+
+
- : - "" - } - - +
+
- +
{ receipt != null || !this.state.error && this.state.message != null diff --git a/lib/modules/webserver/backend/contracts/components/function.js b/lib/modules/webserver/backend/contracts/components/function.js index 4f8320321..37ff4442e 100644 --- a/lib/modules/webserver/backend/contracts/components/function.js +++ b/lib/modules/webserver/backend/contracts/components/function.js @@ -20,6 +20,30 @@ class Function extends React.Component { this.handleClick = this.handleClick.bind(this); } + + copyCommand(e){ + e.preventDefault(); + + let fields = this.state.fields; + + let functionLabel = this._getFunctionLabel(); + let functionParams = this._getFunctionParamString(); + let methodParams = this._getMethodString(); + if(this.props.abi.type == "constructor") + functionParams = `{arguments: [${functionParams}]}`; + + const command = `await ${functionLabel}(${functionParams})${this.props.abi.type != 'fallback' ? '.' + this._getMethodType() : ''}${methodParams}`; + + var dummy = document.createElement("input"); + document.body.appendChild(dummy); + dummy.setAttribute('value', command); + dummy.select(); + document.execCommand("copy"); + document.body.removeChild(dummy); + } + + + async handleClick(e, instanceUpdateCallback){ e.preventDefault(); @@ -70,7 +94,6 @@ class Function extends React.Component { [this._getMethodType()](executionParams) if(this._getMethodType() == 'call'){ - console.log("A"); this.props.resultHandler(false, _receipt, null); } else { this.props.resultHandler(false, null, _receipt); @@ -173,21 +196,21 @@ class Function extends React.Component { _getFunctionParamString(){ if(this.props.abi.type == 'fallback') return ''; return this.props.abi.inputs - .map((input, i) => (input.type.indexOf('int') == -1 ? '"' : '') + this.state.fields[input.name] + (input.type.indexOf('int') == -1 ? '"' : '')) + .map((input, i) => (input.type.indexOf('int') == -1 ? '"' : '') + (this.state.fields[input.name] || (input.type.indexOf('int') == -1 ? '' : '0')) + (input.type.indexOf('int') == -1 ? '"' : '')) .join(', '); } _getMethodString(elem){ let methodParams = "({"; - methodParams += `from: ` + this.state.selectedAccount; + methodParams += `from: ` + (this.state.selectedAccount || '"0x00"'); if(this._getMethodType() == 'send'){ - methodParams += ', gasLimit: ' + this.state.methodFields.gasLimit + methodParams += ', gasLimit: ' + (this.state.methodFields.gasLimit || 0) if(this.props.abi.payable){ - methodParams += ', value: ' + this.state.methodFields.value + methodParams += ', value: ' + (this.state.methodFields.value || 0) } if(this.props.abi.type == 'fallback'){ - methodParams += ', data: "' + this.state.methodFields.data + '", to: "' + this.state.methodFields.to + '"' + methodParams += ', data: "' + (this.state.methodFields.data || '"0x00"' ) + '", to: "' + (this.state.methodFields.to || '"0x00"') + '"' } } return methodParams + "})";