2018-06-26 12:46:06 -04:00
|
|
|
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';
|
2018-06-26 12:46:06 -04:00
|
|
|
import Voting from './components/Voting';
|
2018-05-24 08:11:33 -04:00
|
|
|
import SNT from 'Embark/contracts/SNT';
|
2018-06-28 12:14:15 -04:00
|
|
|
import { VotingContext } from './context';
|
2018-07-12 15:31:47 -04:00
|
|
|
import Web3Render from './components/standard/Web3Render';
|
2018-07-16 09:20:42 -04:00
|
|
|
import { getPolls, omitPolls } from './utils/polls';
|
2018-10-27 13:12:35 -04:00
|
|
|
import { HashRouter as Router, Route, Link, Switch } from "react-router-dom";
|
|
|
|
import OtherWallets from './components/flow/OtherWallets';
|
2018-05-13 01:31:01 -03:00
|
|
|
|
|
|
|
import './dapp.css';
|
|
|
|
|
2018-10-29 02:51:25 -04:00
|
|
|
window.PollManager = PollManager;
|
|
|
|
|
2018-07-05 14:31:51 -04:00
|
|
|
const MAINNET = 1;
|
2018-09-27 10:51:40 -04:00
|
|
|
const TESTNET = 3;
|
2018-07-05 14:31:51 -04:00
|
|
|
|
2018-05-13 01:31:01 -03:00
|
|
|
class App extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
}
|
2018-09-27 15:30:23 -04:00
|
|
|
state = { admin: false, pollOrder: 'NEWEST_ADDED', web3Provider: true, loading: true, symbol: "SNT" };
|
2018-05-13 01:31:01 -03:00
|
|
|
|
2018-05-21 14:29:57 -04:00
|
|
|
componentDidMount(){
|
2018-07-04 14:46:52 -04:00
|
|
|
EmbarkJS.onReady((err) => {
|
|
|
|
if (err) this.setState({ web3Provider: false });
|
|
|
|
else {
|
|
|
|
this._getPolls();
|
|
|
|
this._setAccounts();
|
2018-09-27 15:30:23 -04:00
|
|
|
|
|
|
|
SNT.methods.symbol().call().then((symbol) => {
|
|
|
|
this.setState({symbol});
|
|
|
|
})
|
|
|
|
|
2018-07-04 14:46:52 -04:00
|
|
|
}
|
2018-07-05 14:31:51 -04:00
|
|
|
web3.eth.net.getId((err, netId) => {
|
2018-09-27 10:51:40 -04:00
|
|
|
// TODO: check the environment here
|
|
|
|
if (netId !== MAINNET && netId !== TESTNET && netId < 5) this.setState({ web3Provider: false})
|
2018-07-05 14:31:51 -04:00
|
|
|
})
|
2018-10-21 09:22:34 -04:00
|
|
|
// fetchIdeas().then(ideaSites => { this.setState({ ideaSites })});
|
2018-07-04 14:46:52 -04:00
|
|
|
})
|
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
|
|
|
|
2018-10-16 18:52:40 -04:00
|
|
|
_loadIPFSContent = async (polls) => {
|
|
|
|
let promises = polls.map((item,i) => EmbarkJS.Storage.get(web3.utils.toAscii(item._description)));
|
|
|
|
let ipfsContent = await Promise.all(promises);
|
|
|
|
|
|
|
|
for(let i = 0; i < polls.length; i++){
|
|
|
|
polls[i].content = JSON.parse(ipfsContent[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ rawPolls: polls, loading: false });
|
|
|
|
}
|
|
|
|
|
2018-06-28 12:14:15 -04:00
|
|
|
_getPolls = async () => {
|
2018-07-18 06:29:59 -04:00
|
|
|
this.setState({ loading: true })
|
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();
|
2018-10-16 16:20:54 -04:00
|
|
|
if (polls) getPolls(polls, poll).then(omitPolls).then(rawPolls => {
|
2018-10-16 18:52:40 -04:00
|
|
|
this._loadIPFSContent(rawPolls);
|
2018-10-16 16:20:54 -04:00
|
|
|
});
|
2018-07-18 06:29:59 -04:00
|
|
|
else this.setState({ rawPolls: [], loading: false });
|
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) }});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-28 15:05:53 -04:00
|
|
|
updatePoll = async (idPoll) => {
|
2018-07-17 12:02:17 -04:00
|
|
|
const { poll, nPolls } = PollManager.methods;
|
2018-06-28 15:05:53 -04:00
|
|
|
const { rawPolls } = this.state;
|
2018-07-17 12:02:17 -04:00
|
|
|
const npolls = await nPolls().call();
|
|
|
|
// This check needs to be done because of a bug in web3
|
|
|
|
if (npolls !== rawPolls.length) return this._getPolls();
|
2018-06-28 15:05:53 -04:00
|
|
|
const newPolls = [...rawPolls];
|
|
|
|
const updatedPoll = await poll(idPoll).call();
|
2018-07-17 12:02:17 -04:00
|
|
|
newPolls[idPoll] = { ...updatedPoll };
|
2018-06-28 15:05:53 -04:00
|
|
|
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';
|
2018-06-26 12:46:06 -04:00
|
|
|
return <Fragment>
|
2018-05-21 14:29:57 -04:00
|
|
|
{title}
|
2018-05-13 01:31:01 -03:00
|
|
|
<span className={className}></span>
|
2018-06-26 12:46:06 -04:00
|
|
|
</Fragment>;
|
2018-05-13 01:31:01 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
render(){
|
2018-10-27 13:12:35 -04:00
|
|
|
let { web3Provider } = this.state;
|
2018-06-29 15:06:23 -04:00
|
|
|
const { _getPolls, updatePoll, setPollOrder, appendToPoll } = this;
|
2018-10-27 13:12:35 -04:00
|
|
|
const votingContext = { getPolls: _getPolls, updatePoll, appendToPoll, setPollOrder, ...this.state };
|
|
|
|
|
|
|
|
if(web3Provider && !web3.eth.defaultAccount){
|
|
|
|
web3Provider = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(web3Provider){
|
|
|
|
return <Router>
|
|
|
|
<Web3Render ready={web3Provider}>
|
|
|
|
<VotingContext.Provider value={votingContext}>
|
|
|
|
<Voting />
|
|
|
|
</VotingContext.Provider>
|
|
|
|
</Web3Render>
|
|
|
|
</Router>
|
|
|
|
} else {
|
|
|
|
return (
|
2018-10-16 16:20:54 -04:00
|
|
|
<Router>
|
2018-10-27 13:12:35 -04:00
|
|
|
<Fragment>
|
|
|
|
<Switch>
|
|
|
|
<Route exact path="/" render={() => {
|
|
|
|
return <Web3Render ready={web3Provider}>
|
|
|
|
<VotingContext.Provider value={votingContext}>
|
|
|
|
<Voting />
|
|
|
|
</VotingContext.Provider>
|
|
|
|
</Web3Render>
|
|
|
|
}
|
|
|
|
} />
|
|
|
|
<Route path="/connectOtherWallet" render={() => <div id="votingDapp"><OtherWallets noWeb3Provider={true} /></div>} />
|
|
|
|
</Switch>
|
|
|
|
</Fragment>
|
2018-10-16 16:20:54 -04:00
|
|
|
</Router>
|
2018-06-26 12:46:06 -04:00
|
|
|
);
|
2018-10-27 13:12:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-05-13 01:31:01 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(<App></App>, document.getElementById('app'));
|