visual-identity/app/dapp.js

88 lines
2.5 KiB
JavaScript
Raw Normal View History

import React, { Fragment } from 'react';
2018-05-13 04:31:01 +00:00
import ReactDOM from 'react-dom';
2018-06-28 20:24:28 +00:00
import web3 from "Embark/web3"
2018-05-13 04:31:01 +00:00
import EmbarkJS from 'Embark/EmbarkJS';
import SNT from 'Embark/contracts/SNT';
2018-07-12 19:31:47 +00:00
import Web3Render from './components/standard/Web3Render';
2018-07-19 21:28:32 +00:00
import DrawField from './components/draw/DrawField';
//import ContractClient, { createContract } from './contract_client'
2018-08-12 15:04:54 +00:00
import ContractClient from './client_contractgo';
import Events from './chain_client/events';
window['SNT'] = SNT;
2018-05-13 04:31:01 +00:00
import './dapp.css';
2018-07-05 18:31:51 +00:00
const MAINNET = 1;
2018-05-13 04:31:01 +00:00
class App extends React.Component {
constructor(props) {
super(props);
}
state = { admin: false, pollOrder: 'NEWEST_ADDED', web3Provider: true, loading: true };
2018-05-13 04:31:01 +00:00
2018-05-21 18:29:57 +00:00
componentDidMount(){
2018-07-04 18:46:52 +00:00
EmbarkJS.onReady((err) => {
if (err) this.setState({ web3Provider: false });
else {
this.createContract();
2018-07-04 18:46:52 +00:00
this._setAccounts();
}
2018-07-05 18:31:51 +00:00
web3.eth.net.getId((err, netId) => {
2018-07-19 21:28:32 +00:00
//if (netId !== MAINNET) this.setState({ web3Provider: false})
2018-07-05 18:31:51 +00:00
})
2018-07-04 18:46:52 +00:00
})
2018-05-13 04:31:01 +00:00
}
async createContract() {
2018-07-26 20:26:36 +00:00
const { tileStateUpdateHandler } = this;
this.contractClient = await new ContractClient();
2018-08-12 15:04:54 +00:00
this.events = new Events();
await this.contractClient.createContract();
2018-08-12 15:04:54 +00:00
this.events.onEvent = tileData => this.tileStateUpdateHandler(tileData)
this.requestUpdateTilesOnCanvas();
}
2018-07-26 20:26:36 +00:00
tileStateUpdateHandler = tileData => {
console.log('tile state update event received');
2018-08-12 15:04:54 +00:00
this.setState({ canvasState: tileData });
}
async requestUpdateTilesOnCanvas() {
const tileMapState = await this.contractClient.getTileMapState()
const tileData = tileMapState.getData()
if (tileData) {
const canvasState = JSON.parse(tileData);
this.setState({ canvasState });
}
}
setTileMapState = async (data) => {
await this.contractClient.setTileMapState(JSON.stringify(data))
}
2018-05-22 13:49:22 +00:00
setAccount(_account){
this.setState({account: _account});
}
2018-05-13 04:31:01 +00:00
2018-06-28 20:24:28 +00:00
_setAccounts() {
const { fromWei } = web3.utils;
web3.eth.getAccounts(async (err, [address]) => {
const balance = await SNT.methods.balanceOf(address).call();
this.setState({ snt: { balance: fromWei(balance) }});
})
}
2018-05-13 04:31:01 +00:00
render(){
const { setTileMapState } = this;
const { web3Provider, loading, canvasState } = this.state;
2018-05-17 19:53:23 +00:00
return (
2018-07-04 18:46:52 +00:00
<Web3Render ready={web3Provider}>
<DrawField setTileMapState={setTileMapState} canvasState={canvasState} request={this.requestUpdateTilesOnCanvas.bind(this)}/>
2018-07-04 18:46:52 +00:00
</Web3Render>
);
2018-05-13 04:31:01 +00:00
}
}
ReactDOM.render(<App></App>, document.getElementById('app'));