From fce74ddcd89a791989ff934ad286643772dec00a Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Tue, 17 Jul 2018 17:21:24 -0400 Subject: [PATCH] Fix on poll Ids when omitting some idx, and extracting getVote --- app/components/simple-voting/PollsList.js | 18 +----------------- app/utils/polls.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/components/simple-voting/PollsList.js b/app/components/simple-voting/PollsList.js index 63d43e4..b195477 100644 --- a/app/components/simple-voting/PollsList.js +++ b/app/components/simple-voting/PollsList.js @@ -54,7 +54,7 @@ class Poll extends PureComponent { constructor(props){ super(props); - this.state = { value: 0, balance: 0, isSubmitting: false, open: false }; + this.state = { value: props.votes, originalValue: props.votes, balance: 0, isSubmitting: false, open: false }; } handleClickOpen = () => { @@ -101,21 +101,6 @@ class Poll extends PureComponent { }); } - componentDidMount() { - this.getVote(); - } - - getVote() { - const { fromWei } = web3.utils; - const { idPoll } = this.props; - PollManager.methods.getVote(idPoll, web3.eth.defaultAccount) - .call() - .then(vote => { - const value = parseInt(Math.sqrt(fromWei(vote))); - this.setState({ value, originalValue: value }); - }) - } - render(){ const { _description, @@ -198,7 +183,6 @@ const PollsList = ({ classes }) => ( {({ updatePoll, rawPolls, pollOrder, appendToPoll, ideaSites }) => {rawPolls - .map((poll, i) => ({ ...poll, idPoll: i }) ) .sort(sortingFn[pollOrder]) .map((poll) => )} diff --git a/app/utils/polls.js b/app/utils/polls.js index 0e39a0b..0dc98b9 100644 --- a/app/utils/polls.js +++ b/app/utils/polls.js @@ -1,5 +1,6 @@ import web3 from "Embark/web3" import MiniMeTokenInterface from 'Embark/contracts/MiniMeTokenInterface'; +import PollManager from 'Embark/contracts/PollManager'; const excluded = { PROPER_LIGHT_CLIENT_SUPPORT : 3, @@ -15,12 +16,22 @@ export const getBalance = async (idPoll, token, startBlock) => { return fromWei(balance); } +export const getVote = async(idPoll) => { + const { fromWei } = web3.utils; + + const votes = await PollManager.methods.getVote(idPoll, web3.eth.defaultAccount) + .call(); + + return parseInt(Math.sqrt(fromWei(votes))); +} + export const getPolls = async (number, pollMethod) => { const polls = []; for (let i = number-1; i >= 0; i--) { const poll = await pollMethod(i).call(); const balance = await getBalance(i, poll._token, poll._startBlock); - polls.push({ ...poll, balance }); + const votes = await getVote(i); + polls.push({ ...poll, idPoll: i, balance, votes }); } return polls.reverse(); }