snt-voting/app/components/flow/VotingCredits.js

117 lines
4.0 KiB
JavaScript
Raw Normal View History

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';
import Typography from '@material-ui/core/Typography'
import Card from '@material-ui/core/Card';
import CardContent from '@material-ui/core/CardContent';
2018-10-17 11:02:31 -04:00
import { withRouter } from 'react-router-dom'
2018-10-31 14:47:15 -04:00
import HelpDialog from './HelpDialog';
2018-10-27 13:12:35 -04:00
Date.prototype.DDMMYYYY = function () {
var yyyy = this.getFullYear().toString();
var MM = pad(this.getMonth() + 1,2);
var dd = pad(this.getDate(), 2);
return dd + '/' + MM + '/' + yyyy ;
};
function pad(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
}
class VotingCredits extends Component {
2018-10-31 14:47:15 -04:00
state = {
open: false,
};
handleClickOpen = () => {
this.setState({ open: true });
};
handleClose = () => {
this.setState({ open: false });
};
2018-10-17 11:02:31 -04:00
componentDidMount(){
const {polls, balances, history} = this.props;
if(!polls || !balances || !polls.length || !balances.length){
history.push('/');
}
}
render(){
const {polls, balances, idPoll} = this.props;
2018-10-17 11:02:31 -04:00
if(!polls || !balances) return null;
2018-10-29 02:51:25 -04:00
const poll = polls[polls.length - 1];
let title = poll.content.title;
let description = poll.content.description;
let ethBalance = web3.utils.fromWei(balances[idPoll].ethBalance, "ether");
let tokenBalance = Math.floor(web3.utils.fromWei(balances[idPoll].tokenBalance, "ether"));
2018-10-27 13:12:35 -04:00
2018-10-29 02:51:25 -04:00
const d = new Date(poll.blockInfo.timestamp * 1000);
2018-10-27 13:12:35 -04:00
2018-10-18 16:10:48 -04:00
return (polls ? <Fragment><div className="section">
<Typography variant="headline">{title}</Typography>
<Typography variant="body1" component="div" dangerouslySetInnerHTML={{__html: description}}></Typography>
2018-10-18 20:01:49 -04:00
<Card className="card credits">
<CardContent>
2018-10-18 20:01:49 -04:00
<Typography component="div">
<span className="title">Voting Credits</span>
<span className="value">{tokenBalance}</span>
</Typography>
2018-10-17 11:02:31 -04:00
{ tokenBalance > 0 &&
2018-10-18 20:01:49 -04:00
<Typography component="p" className="text">
2018-10-27 13:12:35 -04:00
You get one credit for each SNT held in your wallet <b>at the time of poll was created ({d.DDMMYYYY()})</b>. They are usable only in this poll.
2018-10-17 11:02:31 -04:00
</Typography> }
{ tokenBalance == 0 &&
2018-10-18 20:01:49 -04:00
<div className="warning">
<Typography component="h2" >
2018-10-17 11:02:31 -04:00
No SNT in your wallet
</Typography>
<Typography component="p">
2018-10-27 13:12:35 -04:00
To vote, you need to connect with a wallet that holds SNT tokens <b>when the poll was created ({d.DDMMYYYY()})</b>.
2018-10-17 11:02:31 -04:00
</Typography>
</div>
}
</CardContent>
</Card>
2018-10-18 20:01:49 -04:00
{ ethBalance == 0 && <Card className="card credits">
2018-10-17 11:02:31 -04:00
<CardContent>
2018-10-18 20:01:49 -04:00
<Typography component="div">
<span className="title">ETH</span>
<span className="value">{ethBalance}</span>
2018-10-17 11:02:31 -04:00
</Typography>
2018-10-18 20:01:49 -04:00
<div className="warning">
<Typography component="h2">
Not enough ETH in your wallet
</Typography>
<Typography component="p">
You will sign a transaction to confirm your vote. No tokens are sent, but you need ETH to pay for gas (Ethereum network fee).
</Typography>
</div>
2018-10-17 11:02:31 -04:00
</CardContent>
</Card> }
2018-10-31 14:47:15 -04:00
<p className="helpLink">Need help? <a onClick={this.handleClickOpen}>Chat with us</a></p>
2018-10-18 16:10:48 -04:00
</div>
2018-10-18 20:01:49 -04:00
<div className={(ethBalance == 0 || tokenBalance == 0) ? 'buttonNav back' : 'buttonNav'}>
{ (ethBalance == 0 || tokenBalance == 0) && <Link to={"/wallet/" + idPoll}><Button variant="text">Back</Button></Link> }
{ (ethBalance > 0 && tokenBalance > 0) && <Link to={"/voting/" + idPoll}><Button variant="text">Vote</Button></Link> }
2018-10-18 16:10:48 -04:00
</div>
2018-10-31 14:47:15 -04:00
<HelpDialog open={this.state.open} handleClose={this.handleClose} />
2018-10-18 16:10:48 -04:00
</Fragment> : null);
}
}
2018-10-17 11:02:31 -04:00
export default withRouter(VotingCredits);