visual-identity/app/components/accountlist.js

113 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-05-18 19:17:46 +00:00
import web3 from "Embark/web3"
import EmbarkJS from 'Embark/EmbarkJS';
import React from 'react';
import { Nav, MenuItem , NavDropdown} from 'react-bootstrap';
import Blockies from 'react-blockies';
import './accountlist.css';
class AccList extends React.Component {
constructor(props) {
super(props);
this.state = {
classNameNavDropdown: props.classNameNavDropdown,
defaultAccount: "0x0000000000000000000000000000000000000000",
addresses: [],
balances: []
2018-05-18 19:17:46 +00:00
}
__embarkContext.execWhenReady(() => {
this.load()
2018-05-18 19:17:46 +00:00
});
}
load() {
web3.eth.getAccounts((err, addresses) => {
if (addresses) {
var defaultAccount = web3.eth.defaultAccount;
if(!defaultAccount){
web3.eth.defaultAccount = addresses[0];
}
var balances = [];
balances.length == addresses.length;
addresses.forEach((address, index) => {
web3.eth.getBalance(address, 'latest', (err, balance) => {
balances[index] = balance;
if(index+1 == balances.length){
this.setState({
balances: balances
});
}
})
})
this.setState({
defaultAccount: defaultAccount,
addresses: addresses
});
2018-05-18 19:17:46 +00:00
} else {
console.log("No addresses available.");
2018-05-18 19:17:46 +00:00
}
})
}
setDefaultAccount(index) {
var defaultAcc = this.state.addresses[index];
2018-05-18 19:17:46 +00:00
if(defaultAcc){
web3.eth.defaultAccount = defaultAcc;
this.setState({defaultAccount: defaultAcc });
2018-05-22 13:49:22 +00:00
this.props.accountUpdateHandler(defaultAcc);
2018-05-18 19:17:46 +00:00
} else {
console.log("invalid account")
}
}
render() {
var accsTitle;
var accsList = [];
if (this.state.addresses) {
2018-05-18 19:17:46 +00:00
accsTitle = this.state.defaultAccount;
this.state.addresses.forEach(
2018-05-18 19:17:46 +00:00
(name, index) => {
accsList.push(
<MenuItem key={index} onClick={(e) => this.setDefaultAccount(index) }>
<div className="account">
<div className="accountIdenticon">
<Blockies seed={name} />
</div>
<div className="accountHexString">
2018-05-18 19:17:46 +00:00
{name}
</div>
<div className="accountBalance">
Ξ {this.state.balances[index] / (10**18)}
</div>
2018-05-18 19:17:46 +00:00
</div>
</MenuItem>);
}
)
}
return (
<React.Fragment>
<div className="accounts">
<div className="selectedIdenticon">
<Blockies seed={ this.state.defaultAccount } />
</div>
<div className="accountList">
<Nav>
<NavDropdown key={1} title={accsTitle} id="basic-nav-dropdown" className={ this.state.classNameNavDropdown }>
2018-05-18 19:17:46 +00:00
{accsList}
</NavDropdown>
</Nav>
</div>
</div>
</React.Fragment>
)
}
}
export default AccList;