clean up poll retrieval

This commit is contained in:
Barry Gitarts 2018-07-13 10:15:27 -04:00
parent d83c17ec62
commit 93d02aaa8c
2 changed files with 15 additions and 10 deletions

View File

@ -9,21 +9,13 @@ import SNT from 'Embark/contracts/SNT';
import { VotingContext } from './context';
import Web3Render from './components/standard/Web3Render';
import fetchIdeas from './utils/fetchIdeas';
import { getPolls, omitUnvotedDupes } from './utils/polls';
window['SNT'] = SNT;
import './dapp.css';
const MAINNET = 1;
const getPolls = (number, pollMethod) => {
const polls = [];
for (let i = number-1; i >= 0; i--) {
const poll = pollMethod(i).call();
polls.push(poll);
}
return Promise.all(polls.reverse());
}
class App extends React.Component {
constructor(props) {
@ -52,7 +44,7 @@ class App extends React.Component {
_getPolls = async () => {
const { nPolls, poll } = PollManager.methods;
const polls = await nPolls().call();
if (polls) getPolls(polls, poll).then(rawPolls => { this.setState({ rawPolls })});
if (polls) getPolls(polls, poll).then(omitUnvotedDupes).then(rawPolls => { this.setState({ rawPolls })});
else this.setState({ rawPolls: [] });
}

13
app/utils/polls.js Normal file
View File

@ -0,0 +1,13 @@
export const getPolls = (number, pollMethod) => {
const polls = [];
for (let i = number-1; i >= 0; i--) {
const poll = pollMethod(i).call();
polls.push(poll);
}
return Promise.all(polls.reverse());
}
const unVotedDupes = new Set([3]);
const filterDupes = (poll, idx) => !unVotedDupes.has(idx);
export const omitUnvotedDupes = polls => polls.filter(filterDupes);