snt-voting/app/dapp.js

333 lines
8.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';
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 Voting from './components/Voting';
import DappToken from 'Embark/contracts/DappToken';
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';
import { HashRouter as Router, Route, Switch } from 'react-router-dom';
import OtherWallets from './components/flow/wallet/OtherWallets';
2019-02-09 15:41:33 -04:00
import Typography from '@material-ui/core/Typography';
2018-12-06 09:50:11 -04:00
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
2018-05-13 01:31:01 -03:00
import './dapp.css';
2018-12-06 09:50:11 -04:00
const muiTheme = createMuiTheme({
typography: {
fontFamily: 'Inter UI'
},
overrides: {
MuiButton: {
root: {
height: '44px',
fontSize: '15px',
lineHeight: '22px'
},
label: {
fontSize: '15px',
lineHeight: '22px'
}
}
2018-12-06 09:50:11 -04:00
}
});
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-11-06 13:49:25 -04:00
// #38
setTimeout(() => {
if (!(window.web3 || window.ethereum)) {
2018-11-06 13:49:25 -04:00
window.location.reload(true);
}
}, 5000);
2018-11-29 16:02:31 -04:00
const pollsPerLoad = 3;
2018-11-06 13:49:25 -04:00
2018-05-13 01:31:01 -03:00
class App extends React.Component {
state = {
admin: false,
pollOrder: 'NEWEST_ADDED',
web3Provider: true,
loading: true,
name: '----',
symbol: '',
decimals: '18',
networkName: '',
rawPolls: [],
pollsRequested: [],
start: 0,
end: pollsPerLoad
};
componentDidMount() {
EmbarkJS.onReady(err => {
2018-07-04 14:46:52 -04:00
if (err) this.setState({ web3Provider: false });
else {
if (!web3.eth.defaultAccount) {
web3.eth.defaultAccount = '0x0000000000000000000000000000000000000000';
2018-10-30 16:53:53 -04:00
}
2018-12-05 10:59:34 -04:00
DappToken.methods
.symbol()
.call({ from: web3.eth.defaultAccount })
.then(symbol => {
this.setState({ symbol });
});
DappToken.methods
.decimals()
.call({ from: web3.eth.defaultAccount })
.then(decimals => {
this.setState({ decimals });
});
DappToken.methods
.name()
.call({ from: web3.eth.defaultAccount })
.then(name => {
this.setState({ name });
});
2018-12-05 10:59:34 -04:00
2018-07-04 14:46:52 -04:00
this._getPolls();
}
2018-07-05 14:31:51 -04:00
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' });
}
});
});
2018-05-13 01:31:01 -03:00
}
setAccount(_account) {
this.setState({ account: _account });
2018-05-22 09:49:22 -04:00
}
2018-05-13 01:31:01 -03:00
_loadIPFSContent = async polls => {
for (let i = 0; i < polls.length; i++) {
2018-11-06 14:08:33 -04:00
try {
let ipfsContent = await EmbarkJS.Storage.get(web3.utils.toAscii(polls[i]._description));
polls[i].content = JSON.parse(ipfsContent);
} catch (error) {
console.log(error);
2018-11-06 14:08:33 -04:00
}
}
2018-11-06 13:49:25 -04:00
let oPolls = {};
for (let i = 0; i < polls.length; i++) {
2018-11-06 13:49:25 -04:00
oPolls[polls[i].idPoll] = polls[i];
}
this.setState({ rawPolls: oPolls, loading: false });
};
_getPolls = async () => {
2019-02-09 15:41:33 -04:00
this.setState({ loading: true });
2018-06-27 10:27:16 -04:00
const { nPolls, poll } = PollManager.methods;
const polls = await nPolls().call({ from: web3.eth.defaultAccount });
if (polls)
getPolls(polls, poll)
.then(omitPolls)
.then(rawPolls => {
rawPolls = rawPolls.sort((a, b) => {
if (a.idPoll > b.idPoll) return -1;
if (a.idPoll < b.idPoll) return 1;
return 0;
});
this.setState({ rawPolls, loading: false });
});
2019-02-09 15:41:33 -04:00
else this.setState({ rawPolls: [], loading: false });
};
2018-06-27 10:27:16 -04:00
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 });
};
2018-06-29 15:06:23 -04:00
appendToPoll = (idPoll, data) => {
const { rawPolls } = this.state;
const newPolls = [...rawPolls];
2019-02-09 15:41:33 -04:00
newPolls[idPoll] = { ...newPolls[idPoll], ...data };
2018-06-29 15:06:23 -04:00
this.setState({ rawPolls: newPolls });
};
2018-06-29 15:06:23 -04:00
setPollOrder = pollOrder => {
this.setState({ pollOrder });
};
2018-06-29 13:51:49 -04:00
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>
{title}
<span className={className} />
</Fragment>
);
2018-05-13 01:31:01 -03:00
}
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;
2018-11-29 16:02:31 -04:00
let pollsRequested = this.state.pollsRequested;
if (!pollsRequested.includes(poll.idPoll)) pollsRequested.push(poll.idPoll);
this.setState({ pollsRequested });
2018-11-29 16:02:31 -04:00
let ipfsContent = await EmbarkJS.Storage.get(web3.utils.toAscii(poll._description));
poll.content = JSON.parse(ipfsContent);
this.replacePoll(poll);
};
2018-11-29 16:02:31 -04:00
loadMorePolls = async filterFn => {
2018-11-29 16:02:31 -04:00
let start = this.state.start + pollsPerLoad;
let end = this.state.end + pollsPerLoad;
this.setState({ start, end });
2018-11-29 16:02:31 -04:00
this.loadPollRange(filterFn, start, end);
};
2018-11-29 16:02:31 -04:00
resetPollCounter = () => {
this.setState({ start: 0, end: pollsPerLoad });
};
2018-11-29 16:02:31 -04:00
loadPollRange = async (filterFn, start, end) => {
let rawPolls = this.state.rawPolls;
let polls = rawPolls.filter(filterFn).slice(start, end);
if (!polls.length) return;
2018-11-29 16:02:31 -04:00
let pollsRequested = this.state.pollsRequested;
if (!pollsRequested) return;
for (let i = 0; i < polls.length; i++) {
if (pollsRequested.includes(polls[i].idPoll)) continue;
2018-11-29 16:02:31 -04:00
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) {
2018-11-29 16:02:31 -04:00
rawPolls[i] = polls[j];
break;
}
}
}
this.setState({ rawPolls, pollsRequested });
2018-11-29 16:02:31 -04:00
}
};
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 (
<MuiThemeProvider theme={muiTheme}>
<Router>
<Web3Render ready={web3Provider}>
<VotingContext.Provider value={votingContext}>
<Voting />
</VotingContext.Provider>
</Web3Render>
</Router>
</MuiThemeProvider>
);
if (networkName)
return (
<MuiThemeProvider theme={muiTheme}>
2018-12-06 09:50:11 -04:00
<div>
<Typography variant="body1" style={{ marginTop: '40vh', textAlign: 'center' }}>
<img src="images/warning.svg" width="24" />
<br />
<br />
Please connect to {networkName} to continue.
</Typography>
2019-02-09 15:41:33 -04:00
</div>
</MuiThemeProvider>
);
return (
<MuiThemeProvider theme={muiTheme}>
<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>
)}
/>
2018-10-27 13:12:35 -04:00
</Switch>
</Fragment>
2018-10-16 16:20:54 -04:00
</Router>
</MuiThemeProvider>
);
2018-05-13 01:31:01 -03:00
}
}
ReactDOM.render(<App />, document.querySelector('#app'));