100 lines
2.8 KiB
JavaScript
Raw Normal View History

import React, { Fragment } from 'react';
2018-05-13 01:31:01 -03:00
import ReactDOM from 'react-dom';
2018-06-28 16:24:28 -04:00
import web3 from "Embark/web3"
2018-05-13 01:31:01 -03:00
import EmbarkJS from 'Embark/EmbarkJS';
2018-06-27 10:27:16 -04: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';
window['SNT'] = SNT;
2018-05-13 01:31:01 -03:00
import './dapp.css';
2018-06-27 10:27:16 -04: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 10:27:16 -04:00
}
2018-05-13 01:31:01 -03:00
class App extends React.Component {
constructor(props) {
super(props);
}
2018-06-29 14:39:07 -04:00
state = { admin: false, pollOrder: 'NEWEST_ADDED' };
2018-05-13 01:31:01 -03:00
2018-05-21 14:29:57 -04:00
componentDidMount(){
2018-05-13 01:31:01 -03:00
__embarkContext.execWhenReady(() => {
2018-06-27 10:27:16 -04:00
this._getPolls();
2018-06-28 16:24:28 -04:00
this._setAccounts();
2018-05-13 01:31:01 -03:00
});
}
2018-05-22 09:49:22 -04:00
setAccount(_account){
this.setState({account: _account});
}
2018-05-13 01:31:01 -03:00
_getPolls = async () => {
2018-06-27 10:27:16 -04:00
const { nPolls, poll } = PollManager.methods;
2018-06-30 15:08:47 -04:00
const polls = await nPolls().call();
if (polls) getPolls(polls, poll).then(rawPolls => { this.setState({ rawPolls })});
2018-06-28 09:03:58 -04:00
else this.setState({ rawPolls: [] });
2018-06-27 10:27:16 -04:00
}
2018-06-28 16:24:28 -04: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 15:06:23 -04:00
appendToPoll = (idPoll, data) => {
const { rawPolls } = this.state;
const newPolls = [...rawPolls];
newPolls[idPoll] = { ...newPolls[idPoll], ...data }
this.setState({ rawPolls: newPolls });
}
2018-06-29 13:51:49 -04:00
setPollOrder = pollOrder => { this.setState({ pollOrder }) }
2018-05-17 16:53:23 -03:00
_renderStatus(title, available) {
2018-05-13 01:31:01 -03:00
let className = available ? 'pull-right status-online' : 'pull-right status-offline';
return <Fragment>
2018-05-21 14:29:57 -04:00
{title}
2018-05-13 01:31:01 -03:00
<span className={className}></span>
</Fragment>;
2018-05-13 01:31:01 -03:00
}
render(){
2018-06-29 13:51:49 -04:00
const { admin } = this.state;
2018-06-29 15:06:23 -04:00
const { _getPolls, updatePoll, setPollOrder, appendToPoll } = this;
const toggleAdmin = () => this.setState({ admin: true });
2018-06-29 15:06:23 -04:00
const votingContext = { getPolls: _getPolls, toggleAdmin, updatePoll, appendToPoll, setPollOrder, ...this.state };
2018-05-17 16:53:23 -03:00
return (
<VotingContext.Provider value={votingContext}>
<Fragment>
{admin ?
<AdminView setAccount={this.setAccount} /> :
<Voting />}
</Fragment>
</VotingContext.Provider>
);
2018-05-13 01:31:01 -03:00
}
}
ReactDOM.render(<App></App>, document.getElementById('app'));