visual-identity/app/dapp.js

74 lines
1.9 KiB
JavaScript
Raw Normal View History

import React, { Fragment } from 'react';
2018-05-13 04:31:01 +00:00
import ReactDOM from 'react-dom';
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';
window['SNT'] = SNT;
2018-05-13 04:31:01 +00:00
import './dapp.css';
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);
}
state = { admin: false };
2018-05-13 04:31:01 +00:00
2018-05-21 18:29:57 +00:00
componentDidMount(){
2018-05-13 04:31:01 +00:00
__embarkContext.execWhenReady(() => {
2018-06-27 14:27:16 +00:00
this._getPolls();
this._setVotingOptions();
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;
const polls = await nPolls.call();
const total = await polls.call();
2018-06-28 13:03:58 +00:00
if (total) getPolls(total, poll).then(rawPolls => { this.setState({ rawPolls })});
else this.setState({ rawPolls: [] });
2018-06-27 14:27:16 +00:00
}
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-06-27 16:32:11 +00:00
const { admin, rawPolls } = this.state;
const { _getPolls } = this;
const toggleAdmin = () => this.setState({ admin: true });
const votingContext = { getPolls: _getPolls, rawPolls, toggleAdmin };
2018-05-17 19:53:23 +00:00
return (
<VotingContext.Provider value={votingContext}>
<Fragment>
{admin ?
<AdminView setAccount={this.setAccount} /> :
<Voting />}
</Fragment>
</VotingContext.Provider>
);
2018-05-13 04:31:01 +00:00
}
}
ReactDOM.render(<App></App>, document.getElementById('app'));