2018-08-02 19:17:40 +00:00
|
|
|
import React from 'react';
|
|
|
|
import ContractContext from './contract-context';
|
|
|
|
import CardAlert from './card-alert';
|
2018-04-19 18:42:11 +00:00
|
|
|
|
2018-08-02 19:17:40 +00:00
|
|
|
class AccountList extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
error: false,
|
|
|
|
errorMessage: "",
|
|
|
|
accounts: []
|
|
|
|
};
|
2018-04-18 19:06:41 +00:00
|
|
|
|
2018-08-02 19:17:40 +00:00
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
this.handleCopyClick = this.handleCopyClick.bind(this);
|
|
|
|
}
|
2018-04-18 19:06:41 +00:00
|
|
|
|
|
|
|
|
2018-08-02 19:17:40 +00:00
|
|
|
handleClick(e, updateAccountsCallback) {
|
|
|
|
e.preventDefault();
|
2018-04-18 19:06:41 +00:00
|
|
|
|
2018-08-02 19:17:40 +00:00
|
|
|
try {
|
|
|
|
updateAccountsCallback();
|
|
|
|
} catch (err) {
|
|
|
|
this.setState({
|
|
|
|
error: true,
|
|
|
|
errorMessage: e.name + ': ' + e.message
|
|
|
|
});
|
2018-04-18 19:06:41 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 19:17:40 +00:00
|
|
|
}
|
2018-04-18 19:06:41 +00:00
|
|
|
|
2018-08-02 19:17:40 +00:00
|
|
|
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);
|
|
|
|
}
|
2018-04-18 19:06:41 +00:00
|
|
|
|
2018-08-02 19:17:40 +00:00
|
|
|
render() {
|
|
|
|
return <ContractContext.Consumer>
|
|
|
|
{(context) => (
|
|
|
|
|
|
|
|
<div className="card function">
|
|
|
|
<div className="card-header">
|
|
|
|
<h3 className="card-title">Get Accounts</h3>
|
|
|
|
<div className="card-options">
|
|
|
|
<a href="#" onClick={this.handleCopyClick} title="Copy to clipboard"><i className="fe fe-copy"></i></a>
|
2018-04-18 19:06:41 +00:00
|
|
|
</div>
|
2018-08-02 19:17:40 +00:00
|
|
|
</div>
|
|
|
|
<CardAlert show={this.state.error} message={this.state.errorMessage}/>
|
|
|
|
<div className="card-body row">
|
|
|
|
<div className="col-md-11">
|
|
|
|
<code>
|
|
|
|
await web3.eth.getAccounts();
|
|
|
|
<input type="text" id="accountsTxt"/>
|
|
|
|
</code>
|
|
|
|
</div>
|
|
|
|
<div className="col-md-1">
|
|
|
|
<button className="btn btn-primary ml-auto"
|
|
|
|
onClick={event => this.handleClick(event, context.updateAccounts)}>⏎</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="card-footer">
|
|
|
|
<p><tt>accounts</tt> variable is available in the console</p>
|
|
|
|
<ul>
|
|
|
|
{
|
|
|
|
context.accounts.map((account, i) => <li key={i}>{account}</li>)
|
|
|
|
}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</ContractContext.Consumer>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AccountList;
|