add fetchData to prevent blocking calls

This commit is contained in:
Barry Gitarts 2018-07-18 06:29:59 -04:00
parent fce74ddcd8
commit 40f009cad6
3 changed files with 19 additions and 15 deletions

View File

@ -8,6 +8,7 @@ import StatusLogo from '../ui/components/StatusLogo';
import Collapse from '@material-ui/core/Collapse'; import Collapse from '@material-ui/core/Collapse';
import Hidden from '@material-ui/core/Hidden'; import Hidden from '@material-ui/core/Hidden';
import Typography from '@material-ui/core/Typography'; import Typography from '@material-ui/core/Typography';
import LinearProgress from '@material-ui/core/LinearProgress';
import { VotingContext } from '../context'; import { VotingContext } from '../context';
class Voting extends PureComponent { class Voting extends PureComponent {
@ -18,10 +19,11 @@ class Voting extends PureComponent {
const togglePoll = () => { this.setState({ addPoll: !addPoll })}; const togglePoll = () => { this.setState({ addPoll: !addPoll })};
return ( return (
<VotingContext.Consumer> <VotingContext.Consumer>
{({ getPolls, rawPolls }) => {({ getPolls, rawPolls, loading }) =>
<div> <div>
<CssBaseline /> <CssBaseline />
<AppBar togglePoll={togglePoll} /> <AppBar togglePoll={togglePoll} />
{loading && <LinearProgress />}
<div style={{ margin: '30px', textAlign: 'center' }}> <div style={{ margin: '30px', textAlign: 'center' }}>
<img src="images/logo.png" width="200" /> <img src="images/logo.png" width="200" />
<Hidden smUp> <Hidden smUp>

View File

@ -21,7 +21,7 @@ class App extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
} }
state = { admin: false, pollOrder: 'NEWEST_ADDED', web3Provider: true }; state = { admin: false, pollOrder: 'NEWEST_ADDED', web3Provider: true, loading: true };
componentDidMount(){ componentDidMount(){
EmbarkJS.onReady((err) => { EmbarkJS.onReady((err) => {
@ -42,10 +42,11 @@ class App extends React.Component {
} }
_getPolls = async () => { _getPolls = async () => {
this.setState({ loading: true })
const { nPolls, poll } = PollManager.methods; const { nPolls, poll } = PollManager.methods;
const polls = await nPolls().call(); const polls = await nPolls().call();
if (polls) getPolls(polls, poll).then(omitPolls).then(rawPolls => { this.setState({ rawPolls })}); if (polls) getPolls(polls, poll).then(omitPolls).then(rawPolls => { this.setState({ rawPolls, loading: false })});
else this.setState({ rawPolls: [] }); else this.setState({ rawPolls: [], loading: false });
} }
_setAccounts() { _setAccounts() {
@ -86,7 +87,7 @@ class App extends React.Component {
} }
render(){ render(){
const { admin, web3Provider } = this.state; const { admin, web3Provider, loading } = this.state;
const { _getPolls, updatePoll, setPollOrder, appendToPoll } = this; const { _getPolls, updatePoll, setPollOrder, appendToPoll } = this;
const toggleAdmin = () => this.setState({ admin: true }); const toggleAdmin = () => this.setState({ admin: true });
const votingContext = { getPolls: _getPolls, toggleAdmin, updatePoll, appendToPoll, setPollOrder, ...this.state }; const votingContext = { getPolls: _getPolls, toggleAdmin, updatePoll, appendToPoll, setPollOrder, ...this.state };

View File

@ -18,22 +18,23 @@ export const getBalance = async (idPoll, token, startBlock) => {
export const getVote = async(idPoll) => { export const getVote = async(idPoll) => {
const { fromWei } = web3.utils; const { fromWei } = web3.utils;
const votes = await PollManager.methods.getVote(idPoll, web3.eth.defaultAccount).call();
const votes = await PollManager.methods.getVote(idPoll, web3.eth.defaultAccount)
.call();
return parseInt(Math.sqrt(fromWei(votes))); return parseInt(Math.sqrt(fromWei(votes)));
} }
export const getPolls = async (number, pollMethod) => { const fetchPollData = async (index, pollMethod) => {
const poll = await pollMethod(index).call();
const balance = await getBalance(index, poll._token, poll._startBlock);
const votes = await getVote(index);
return { ...poll, idPoll: index, balance, votes };
}
export const getPolls = (number, pollMethod) => {
const polls = []; const polls = [];
for (let i = number-1; i >= 0; i--) { for (let i = number-1; i >= 0; i--) {
const poll = await pollMethod(i).call(); polls.push(fetchPollData(i, pollMethod));
const balance = await getBalance(i, poll._token, poll._startBlock);
const votes = await getVote(i);
polls.push({ ...poll, idPoll: i, balance, votes });
} }
return polls.reverse(); return Promise.all(polls.reverse());
} }
const excludedPolls = new Set(Object.values(excluded)); const excludedPolls = new Set(Object.values(excluded));