2018-06-26 16:46:06 +00:00
|
|
|
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';
|
2018-06-26 16:46:06 +00:00
|
|
|
import AdminView from './components/AdminView';
|
|
|
|
import Voting from './components/Voting';
|
2018-05-24 12:11:33 +00:00
|
|
|
import SNT from 'Embark/contracts/SNT';
|
|
|
|
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);
|
|
|
|
}
|
2018-06-27 17:54:36 +00:00
|
|
|
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-06-26 16:46:06 +00:00
|
|
|
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();
|
2018-06-27 17:54:36 +00:00
|
|
|
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
|
|
|
|
2018-06-27 14:27:16 +00:00
|
|
|
async _getPolls(){
|
|
|
|
const { nPolls, poll } = PollManager.methods;
|
|
|
|
const polls = await nPolls.call();
|
|
|
|
const total = await polls.call();
|
|
|
|
getPolls(total, poll).then(rawPolls => { this.setState({ rawPolls })});
|
|
|
|
}
|
|
|
|
|
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';
|
2018-06-26 16:46:06 +00:00
|
|
|
return <Fragment>
|
2018-05-21 18:29:57 +00:00
|
|
|
{title}
|
2018-05-13 04:31:01 +00:00
|
|
|
<span className={className}></span>
|
2018-06-26 16:46:06 +00:00
|
|
|
</Fragment>;
|
2018-05-13 04:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render(){
|
2018-06-27 16:32:11 +00:00
|
|
|
const { admin, rawPolls } = this.state;
|
2018-06-26 16:46:06 +00:00
|
|
|
const toggleAdmin = () => this.setState({ admin: true });
|
2018-05-17 19:53:23 +00:00
|
|
|
return (
|
2018-06-26 16:46:06 +00:00
|
|
|
<Fragment>
|
2018-06-27 16:32:11 +00:00
|
|
|
{admin ?
|
|
|
|
<AdminView setAccount={this.setAccount} /> :
|
|
|
|
<Voting toggleAdmin={toggleAdmin} rawPolls={rawPolls} />}
|
2018-06-26 16:46:06 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
2018-05-13 04:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(<App></App>, document.getElementById('app'));
|