lotus/lotuspond/front/src/NodeList.js

84 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-07-24 17:10:44 +00:00
import React from 'react';
import FullNode from "./FullNode";
2019-07-24 18:42:02 +00:00
import ConnMgr from "./ConnMgr";
2019-07-25 16:13:46 +00:00
import Consensus from "./Consensus";
2019-07-24 17:10:44 +00:00
class NodeList extends React.Component {
constructor(props) {
2019-07-24 18:42:02 +00:00
super(props)
2019-07-24 17:10:44 +00:00
this.state = {
existingLoaded: false,
2019-07-24 20:00:11 +00:00
nodes: {},
2019-07-24 18:42:02 +00:00
showConnMgr: false,
2019-07-25 16:13:46 +00:00
showConsensus: false,
2019-07-24 18:42:02 +00:00
}
2019-07-24 17:10:44 +00:00
// This binding is necessary to make `this` work in the callback
2019-07-24 18:42:02 +00:00
this.spawnNode = this.spawnNode.bind(this)
this.connMgr = this.connMgr.bind(this)
2019-07-25 16:13:46 +00:00
this.consensus = this.consensus.bind(this)
2019-07-24 17:10:44 +00:00
2019-07-24 20:00:11 +00:00
this.getNodes()
}
async getNodes() {
const nds = await this.props.client.call('Pond.Nodes')
const nodes = nds.reduce((o, i) => {o[i.ID] = i; return o}, {})
console.log('nds', nodes)
this.setState({existingLoaded: true, nodes: nodes})
2019-07-24 17:10:44 +00:00
}
async spawnNode() {
const node = await this.props.client.call('Pond.Spawn')
console.log(node)
2019-07-24 20:00:11 +00:00
this.setState(state => ({nodes: {...state.nodes, [node.ID]: node}}))
2019-07-24 17:10:44 +00:00
}
2019-07-24 18:42:02 +00:00
connMgr() {
this.setState({showConnMgr: true})
}
2019-07-25 16:13:46 +00:00
consensus() {
this.setState({showConsensus: true})
}
2019-07-24 17:10:44 +00:00
render() {
2019-07-24 18:42:02 +00:00
let connMgr
if (this.state.showConnMgr) {
connMgr = (<ConnMgr nodes={this.state.nodes}/>)
}
2019-07-25 16:13:46 +00:00
let consensus
if (this.state.showConsensus) {
consensus = (<Consensus nodes={this.state.nodes}/>)
}
2019-07-24 17:10:44 +00:00
return (
<div>
<div>
<button onClick={this.spawnNode} disabled={!this.state.existingLoaded}>Spawn Node</button>
2019-07-24 18:42:02 +00:00
<button onClick={this.connMgr} disabled={!this.state.existingLoaded && !this.state.showConnMgr}>Connections</button>
2019-07-25 16:13:46 +00:00
<button onClick={this.consensus} disabled={!this.state.existingLoaded && !this.state.showConsensus}>Consensus</button>
2019-07-24 17:10:44 +00:00
</div>
<div>
{
2019-07-24 20:00:11 +00:00
Object.keys(this.state.nodes).map(n => {
const node = this.state.nodes[n]
2019-07-24 18:42:02 +00:00
return (<FullNode key={node.ID}
2019-07-24 20:00:11 +00:00
node={{...node}}
pondClient={this.props.client}
2019-07-25 00:55:19 +00:00
onConnect={(conn, id) => this.setState(prev => ({nodes: {...prev.nodes, [n]: {...node, conn: conn, peerid: id}}}))}/>)
2019-07-24 18:42:02 +00:00
})
2019-07-24 17:10:44 +00:00
}
2019-07-24 18:42:02 +00:00
{connMgr}
2019-07-25 16:13:46 +00:00
{consensus}
2019-07-24 17:10:44 +00:00
</div>
</div>
);
}
}
export default NodeList