contracts/app/dapp.js

111 lines
3.2 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';
2018-06-27 14:27:16 +00:00
import PollManager from 'Embark/contracts/PollManager';
import AdminView from './components/AdminView';
import Voting from './components/Voting';
import SNT from 'Embark/contracts/SNT';
import { VotingContext } from './context';
2018-07-04 18:46:52 +00:00
import Web3Render from './components/standard/Web3Render'
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-06-27 14:27:16 +00:00
const getPolls = (number, pollMethod) => {
const polls = [];
for (let i = number-1; i >= 0; i--) {
const poll = pollMethod(i).call();
polls.push(poll);
}
return Promise.all(polls.reverse());
2018-06-27 14:27:16 +00:00
}
2018-05-13 04:31:01 +00:00
class App extends React.Component {
constructor(props) {
super(props);
}
2018-07-04 18:46:52 +00:00
state = { admin: false, pollOrder: 'NEWEST_ADDED', web3Provider: 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._getPolls();
this._setAccounts();
}
2018-07-05 18:31:51 +00:00
web3.eth.net.getId((err, netId) => {
if (netId !== MAINNET) this.setState({ web3Provider: false})
})
2018-07-04 18:46:52 +00:00
})
2018-05-13 04:31:01 +00:00
}
2018-05-22 13:49:22 +00:00
setAccount(_account){
this.setState({account: _account});
}
2018-05-13 04:31:01 +00:00
_getPolls = async () => {
2018-06-27 14:27:16 +00:00
const { nPolls, poll } = PollManager.methods;
2018-06-30 19:08:47 +00:00
const polls = await nPolls().call();
if (polls) getPolls(polls, poll).then(rawPolls => { this.setState({ rawPolls })});
2018-06-28 13:03:58 +00:00
else this.setState({ rawPolls: [] });
2018-06-27 14:27:16 +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) }});
})
}
updatePoll = async (idPoll) => {
const { poll } = PollManager.methods;
const { rawPolls } = this.state;
const newPolls = [...rawPolls];
const updatedPoll = await poll(idPoll).call();
newPolls[idPoll] = updatedPoll;
this.setState({ rawPolls: newPolls });
}
2018-06-29 19:06:23 +00:00
appendToPoll = (idPoll, data) => {
const { rawPolls } = this.state;
const newPolls = [...rawPolls];
newPolls[idPoll] = { ...newPolls[idPoll], ...data }
this.setState({ rawPolls: newPolls });
}
2018-06-29 17:51:49 +00:00
setPollOrder = pollOrder => { this.setState({ pollOrder }) }
2018-05-17 19:53:23 +00:00
_renderStatus(title, available) {
2018-05-13 04:31:01 +00:00
let className = available ? 'pull-right status-online' : 'pull-right status-offline';
return <Fragment>
2018-05-21 18:29:57 +00:00
{title}
2018-05-13 04:31:01 +00:00
<span className={className}></span>
</Fragment>;
2018-05-13 04:31:01 +00:00
}
render(){
2018-07-04 18:46:52 +00:00
const { admin, web3Provider } = this.state;
2018-06-29 19:06:23 +00:00
const { _getPolls, updatePoll, setPollOrder, appendToPoll } = this;
const toggleAdmin = () => this.setState({ admin: true });
2018-06-29 19:06:23 +00:00
const votingContext = { getPolls: _getPolls, toggleAdmin, updatePoll, appendToPoll, setPollOrder, ...this.state };
2018-05-17 19:53:23 +00:00
return (
2018-07-04 18:46:52 +00:00
<Web3Render ready={web3Provider}>
<VotingContext.Provider value={votingContext}>
<Fragment>
{admin ?
<AdminView setAccount={this.setAccount} /> :
<Voting />}
</Fragment>
</VotingContext.Provider>
</Web3Render>
);
2018-05-13 04:31:01 +00:00
}
}
ReactDOM.render(<App></App>, document.getElementById('app'));