2018-10-17 09:16:09 -04:00
|
|
|
import {Link} from "react-router-dom";
|
|
|
|
import Button from '@material-ui/core/Button';
|
2018-10-18 16:10:48 -04:00
|
|
|
import React, {Component, Fragment} from 'react';
|
2018-10-17 09:16:09 -04:00
|
|
|
import Typography from '@material-ui/core/Typography'
|
2018-10-18 08:47:35 -04:00
|
|
|
import Card from '@material-ui/core/Card';
|
|
|
|
import CardContent from '@material-ui/core/CardContent';
|
|
|
|
import PollManager from 'Embark/contracts/PollManager';
|
|
|
|
import { withRouter } from 'react-router-dom'
|
2018-10-17 09:16:09 -04:00
|
|
|
class ReviewVotes extends Component {
|
|
|
|
|
2018-10-18 08:47:35 -04:00
|
|
|
state = {
|
|
|
|
isSubmitting: false
|
|
|
|
}
|
|
|
|
|
|
|
|
vote = () => {
|
|
|
|
this.setState({isSubmitting: true});
|
|
|
|
|
|
|
|
const { vote, unvote } = PollManager.methods;
|
2018-10-18 10:32:38 -04:00
|
|
|
const { polls, votes, history} = this.props;
|
2018-10-18 22:27:08 -04:00
|
|
|
const { toWei, toBN } = web3.utils;
|
2018-10-18 10:32:38 -04:00
|
|
|
|
2018-10-18 08:47:35 -04:00
|
|
|
const idPoll = 0; // TODO:
|
|
|
|
|
2018-10-18 22:27:08 -04:00
|
|
|
const ballots = votes.map(el => {
|
|
|
|
let num = toBN(el);
|
|
|
|
num = num.mul(num);
|
|
|
|
return toWei(num, "ether");
|
|
|
|
});
|
|
|
|
|
|
|
|
const balance4Voting = ballots.reduce((prev, curr) => prev.add(curr), toBN("0"));
|
|
|
|
|
2018-10-18 08:47:35 -04:00
|
|
|
const toSend = balance4Voting == 0 ? unvote(idPoll) : vote(idPoll, ballots);
|
|
|
|
|
|
|
|
toSend.estimateGas()
|
|
|
|
.then(gasEstimated => {
|
|
|
|
console.log("voting gas estimated: " + gasEstimated);
|
2018-10-18 10:32:38 -04:00
|
|
|
const transaction = toSend.send({gas: gasEstimated + 100000});
|
|
|
|
this.props.setTransactionPromise(transaction);
|
|
|
|
history.push('/results');
|
2018-10-18 08:47:35 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-17 09:16:09 -04:00
|
|
|
render(){
|
2018-10-18 08:47:35 -04:00
|
|
|
const {polls, balances, votes} = this.props;
|
|
|
|
const {isSubmitting} = this.state;
|
|
|
|
const {fromWei} = web3.utils;
|
|
|
|
|
|
|
|
if(!polls || !polls.length){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const poll = polls[0];
|
|
|
|
const ballots = poll.content.ballots
|
|
|
|
const balance = fromWei(balances[0].tokenBalance, "ether");
|
|
|
|
const availableCredits = parseInt(balance, 10) - votes.reduce((prev, curr) => prev + curr * curr, 0);
|
2018-10-17 09:16:09 -04:00
|
|
|
|
2018-10-18 16:10:48 -04:00
|
|
|
return (polls ? <Fragment><div className="section">
|
2018-10-17 09:16:09 -04:00
|
|
|
<Typography variant="headline">Review your vote</Typography>
|
2018-10-17 16:29:43 -04:00
|
|
|
|
2018-10-18 08:47:35 -04:00
|
|
|
{ ballots.map((item, i) => {
|
|
|
|
return <Card className="card" key={i}>
|
|
|
|
<CardContent>
|
|
|
|
<Typography gutterBottom component="h2">{item.title}</Typography>
|
|
|
|
<Typography component="p">{item.subtitle}</Typography>
|
|
|
|
<Typography component="p">{votes[i]} votes</Typography>
|
|
|
|
<Typography component="p">{votes[i] * votes[i]} credits</Typography>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
})}
|
|
|
|
|
|
|
|
<Card className="card creditsAvailable">
|
|
|
|
<CardContent>
|
|
|
|
<Typography gutterBottom component="p">Unused voting power</Typography>
|
|
|
|
<Typography gutterBottom component="h2">{availableCredits} credits</Typography>
|
|
|
|
<Link to="/voting"><Button variant="text">Add votes</Button></Link>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
2018-10-18 16:10:48 -04:00
|
|
|
</div>
|
|
|
|
<div className="buttonNav">
|
2018-10-18 08:47:35 -04:00
|
|
|
|
|
|
|
<Button variant="text" onClick={this.vote} disabled={isSubmitting}>Sign to confirm</Button>
|
2018-10-17 09:16:09 -04:00
|
|
|
|
|
|
|
|
2018-10-18 16:10:48 -04:00
|
|
|
</div></Fragment> : null);
|
2018-10-17 09:16:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-18 10:32:38 -04:00
|
|
|
export default withRouter(ReviewVotes);
|