Fix on poll Ids when omitting some idx, and extracting getVote

This commit is contained in:
Richard Ramos 2018-07-17 17:21:24 -04:00
parent 33aaa4f679
commit fce74ddcd8
2 changed files with 13 additions and 18 deletions

View File

@ -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 }) =>
<Fragment>
{rawPolls
.map((poll, i) => ({ ...poll, idPoll: i }) )
.sort(sortingFn[pollOrder])
.map((poll) => <Poll key={poll._token} classes={classes} appendToPoll={appendToPoll} updatePoll={updatePoll} ideaSites={ideaSites} {...poll} />)}
</Fragment>

View File

@ -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();
}