import React, { Fragment } from 'react'; import ReactDOM from 'react-dom'; import web3 from "Embark/web3" import EmbarkJS from 'Embark/EmbarkJS'; import PollManager from 'Embark/contracts/PollManager'; import Voting from './components/Voting'; import SNT from 'Embark/contracts/SNT'; import { VotingContext } from './context'; import Web3Render from './components/standard/Web3Render'; import { getPolls, omitPolls } from './utils/polls'; import { HashRouter as Router, Route, Link, Switch } from "react-router-dom"; import OtherWallets from './components/flow/OtherWallets'; import Typography from '@material-ui/core/Typography' import './dapp.css'; window.PollManager = PollManager; const MAINNET = 1; const TESTNET = 3; // #38 setTimeout(() => { if(!(window.web3 || window.ethereum)){ window.location.reload(true); } }, 5000); const pollsPerLoad = 3; class App extends React.Component { constructor(props) { super(props); } state = { admin: false, pollOrder: 'NEWEST_ADDED', web3Provider: true, loading: true, symbol: "SNT", networkName: "" , rawPolls: [], pollsRequested: [], start: 0, end: pollsPerLoad}; componentDidMount(){ EmbarkJS.onReady((err) => { if (err) this.setState({ web3Provider: false }); else { if(!web3.eth.defaultAccount){ web3.eth.defaultAccount = "0x0000000000000000000000000000000000000000"; } this._getPolls(); /* this._setAccounts(); SNT.methods.symbol().call().then((symbol) => { this.setState({symbol}); })*/ } web3.eth.net.getId((err, netId) => { if(EmbarkJS.environment == 'testnet' && netId !== TESTNET){ this.setState({web3Provider: false, networkName: "Ropsten"}); } else if(EmbarkJS.environment == 'livenet' && netId !== MAINNET){ this.setState({web3Provider: false, networkName: "Mainnet"}); } }) }) } setAccount(_account){ this.setState({account: _account}); } _loadIPFSContent = async (polls) => { for(let i = 0; i < polls.length; i++){ try { let ipfsContent = await EmbarkJS.Storage.get(web3.utils.toAscii(polls[i]._description)); polls[i].content = JSON.parse(ipfsContent); } catch(err){ console.log(err); } } let oPolls = {}; for(let i = 0; i < polls.length; i++){ oPolls[polls[i].idPoll] = polls[i]; } this.setState({ rawPolls: oPolls, loading: false }); } _getPolls = async () => { this.setState({ loading: true }) const { nPolls, poll } = PollManager.methods; const polls = await nPolls().call({from: web3.eth.defaultAccount}); if (polls) getPolls(polls, poll) .then(omitPolls) .then(rawPolls => { // this._loadIPFSContent(rawPolls); rawPolls = rawPolls.sort((a,b) => { if(a.idPoll > b.idPoll) return -1; else if(a.idPoll < b.idPoll) return 1; else return 0; }); this.setState({rawPolls, loading: false}); }); else this.setState({ rawPolls: [], loading: false }); } _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, nPolls } = PollManager.methods; const { rawPolls } = this.state; const npolls = await nPolls().call(); // This check needs to be done because of a bug in web3 if (npolls !== rawPolls.length) return this._getPolls(); const newPolls = [...rawPolls]; const updatedPoll = await poll(idPoll).call(); newPolls[idPoll] = { ...updatedPoll }; this.setState({ rawPolls: newPolls }); } appendToPoll = (idPoll, data) => { const { rawPolls } = this.state; const newPolls = [...rawPolls]; newPolls[idPoll] = { ...newPolls[idPoll], ...data } this.setState({ rawPolls: newPolls }); } setPollOrder = pollOrder => { this.setState({ pollOrder }) } _renderStatus(title, available) { let className = available ? 'pull-right status-online' : 'pull-right status-offline'; return {title} ; } replacePoll = (poll) => { let rawPolls = this.state.rawPolls; for(let i = 0; i < rawPolls.length; i++){ if(rawPolls[i].idPoll == poll.idPoll){ rawPolls[i] = poll; this.setState({rawPolls, t: new Date().getTime()}); break; } } } loadPollContent = async (poll) => { if(!poll) return; let pollsRequested = this.state.pollsRequested; if(!pollsRequested.includes(poll.idPoll)) pollsRequested.push(poll.idPoll); this.setState({pollsRequested}); let ipfsContent = await EmbarkJS.Storage.get(web3.utils.toAscii(poll._description)); poll.content = JSON.parse(ipfsContent); this.replacePoll(poll); } loadMorePolls = async (filterFn) => { let start = this.state.start + pollsPerLoad; let end = this.state.end + pollsPerLoad; this.setState({start, end}); this.loadPollRange(filterFn, start, end); } resetPollCounter = () => { this.setState({start: 0, end: pollsPerLoad}); } loadPollRange = async (filterFn, start, end) => { let rawPolls = this.state.rawPolls; let polls = rawPolls.filter(filterFn).slice(start, end); if(!polls.length) return; let pollsRequested = this.state.pollsRequested; if(!pollsRequested) return; for(let i = 0; i < polls.length; i++){11 if(pollsRequested.includes(polls[i].idPoll)) continue; pollsRequested.push(polls[i].idPoll); let ipfsContent = await EmbarkJS.Storage.get(web3.utils.toAscii(polls[i]._description)); polls[i].content = JSON.parse(ipfsContent); for(let i = 0; i < rawPolls.length; i++){ for(let j = 0; j < polls.length; j++){ if(rawPolls[i].idPoll == polls[j].idPoll){ rawPolls[i] = polls[j]; break; } } } this.setState({rawPolls, pollsRequested}); } } render(){ let { web3Provider, networkName, } = this.state; const { _getPolls, updatePoll, setPollOrder, appendToPoll, replacePoll, loadPollContent, resetPollCounter, loadPollRange, loadMorePolls } = this; const votingContext = { getPolls: _getPolls, updatePoll, appendToPoll, setPollOrder, resetPollCounter, replacePoll, loadPollContent, loadPollRange, loadMorePolls, ...this.state }; if(web3Provider){ return } else { if(networkName){ return


Please connect to {networkName} to continue.
} else { return { return } } />
} />
} } } } ReactDOM.render(, document.getElementById('app'));