125 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-10-18 10:32:38 -04:00
import {Link} from "react-router-dom";
import Button from '@material-ui/core/Button';
2018-10-21 08:23:53 -04:00
import React, {Component, Fragment} from 'react';
2018-10-18 10:32:38 -04:00
import Typography from '@material-ui/core/Typography'
2018-10-18 11:49:06 -04:00
import PollManager from 'Embark/contracts/PollManager';
2018-10-18 10:32:38 -04:00
class Results extends Component {
state = {
2018-10-18 11:49:06 -04:00
isError: false,
2018-10-21 08:23:53 -04:00
poll: null,
isPending: true,
2018-10-18 11:49:06 -04:00
}
constructor(props){
super(props);
if(props.polls && props.polls.length)
this.state.poll = props.polls[props.idPoll];
2018-10-18 11:49:06 -04:00
}
updatePoll(){
const idPoll = this.props.idPoll;
2018-10-18 11:49:06 -04:00
PollManager.methods.poll(idPoll).call().then(poll => {
this.setState({poll});
})
2018-10-18 10:32:38 -04:00
}
componentDidMount(){
const {transaction, idPoll} = this.props;
2018-10-18 10:32:38 -04:00
EmbarkJS.onReady(() => {
2018-10-18 11:49:06 -04:00
this.updatePoll();
if(transaction){
transaction.then(receipt => {
2018-10-21 08:23:53 -04:00
this.setState({isPending: false});
this.updatePoll();
})
.catch(err => {
this.setState({isError: true});
});
}
2018-10-18 11:49:06 -04:00
});
2018-10-18 10:32:38 -04:00
}
render(){
2018-10-21 08:23:53 -04:00
const {polls, idPoll, transaction, transactionHash} = this.props;
let {isError, poll, isPending} = this.state;
2018-10-18 10:32:38 -04:00
if(!poll || !polls){
2018-10-18 10:32:38 -04:00
return null;
}
2018-10-21 09:10:22 -04:00
const title = polls[idPoll].content.title;
const ballots = polls[idPoll].content.ballots;
2018-10-21 09:10:22 -04:00
const totalVotes = poll._quadraticVotes.map(x => parseInt(x, 10)).reduce((x, y) => x + y, 0);
2018-10-21 08:23:53 -04:00
return <Fragment>
2018-10-20 16:52:02 -04:00
{ isError && <div className="errorTrx">
<div className="image"><img src="images/sad-face.svg" width="24" /></div>
2018-10-18 10:32:38 -04:00
<Typography variant="headline">Transaction failed</Typography>
<Typography variant="body1">Copy with apologies and invitation to try again</Typography>
<Link to={"/review/" + idPoll}>
2018-10-20 16:52:02 -04:00
<Button color="primary" variant="contained">Try again</Button>
2018-10-18 10:32:38 -04:00
</Link>
</div> }
2018-10-21 08:23:53 -04:00
{ !isError && transaction && <div className="transactionArea">
{ isPending && <div className="pending">
<img src="images/pending.svg" width="40" />
<Typography variant="headline">Your vote will be posted once the transaction is complete.</Typography>
<Typography variant="body1">Your vote is in the process of being confirmed in the blockchain</Typography>
</div>
}
{ !isPending && transaction && <div className="confirmed">
<img src="images/confirmed.svg" width="40" />
<Typography variant="headline">Transaction confirmed!<br />
Your vote was posted.</Typography>
</div>}
{ transactionHash && <Typography variant="body1"><a href={"https://etherscan.io/tx/" + transactionHash}>View details on Etherscan</a></Typography> }
</div>
}
<div className="section">
{ !isError && <Fragment>
2018-10-21 09:10:22 -04:00
<Typography variant="headline" gutterBottom>{title}</Typography>
{ ballots.map((item, i) => <BallotResult title={item.title} totalVotes={totalVotes} quadraticVotes={poll._quadraticVotes[i]} tokenTotal={poll._tokenTotal[i]} key={i} />) }
2018-10-21 08:23:53 -04:00
</Fragment>
}
</div>
</Fragment>;
2018-10-18 10:32:38 -04:00
}
}
2018-10-21 09:10:22 -04:00
class BallotResult extends Component {
state = {
show: false
}
showDetails = () => {
const show = this.state.show;
this.setState({show: !show});
}
render(){
const {title, quadraticVotes, tokenTotal, totalVotes} = this.props;
const {show} = this.state;
return (<div className="ballotResult">
<Typography gutterBottom className={show ? 'collapse' : ''} component="h2" onClick={this.showDetails}><span>{totalVotes > 0 ? parseInt(quadraticVotes) / totalVotes * 100 : 0}%</span> {title}</Typography>
{show && <ul>
<Typography component="li">Voters: <span>???</span></Typography>
<Typography component="li">Total votes: <span>{quadraticVotes}</span></Typography>
<Typography component="li" className="noBorder">Total SNT: <span>{web3.utils.fromWei(tokenTotal, "ether")}</span></Typography>
</ul>}
</div>);
}
}
2018-10-18 10:32:38 -04:00
export default Results;