visual-identity/app/components/simple-voting/PollsList.js

107 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-06-28 00:45:31 +00:00
import React, { Fragment, Component } from 'react';
2018-06-27 16:32:11 +00:00
import Card from '@material-ui/core/Card';
import CardContent from '@material-ui/core/CardContent';
2018-06-27 18:40:45 +00:00
import CardActions from '@material-ui/core/CardActions';
2018-06-27 16:32:11 +00:00
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
2018-06-27 18:40:45 +00:00
import Slider from '@material-ui/lab/Slider';
import Tooltip from '@material-ui/core/Tooltip';
2018-06-28 00:45:31 +00:00
import PollManager from 'Embark/contracts/PollManager';
2018-06-27 16:32:11 +00:00
2018-06-28 00:45:31 +00:00
class Poll extends Component {
constructor(props){
super(props);
this.state = { value: 0, isSubmitting: false, ...props };
}
2018-06-27 18:40:45 +00:00
handleChange = (event, value) => {
this.setState({ value });
};
2018-06-28 00:45:31 +00:00
handleClick = (event) => {
event.preventDefault();
this.setState({isSubmitting: true});
const { customVote, poll } = PollManager.methods;
const { idPoll, value } = this.state;
const balance4Voting = value * value;
const toSend = customVote(idPoll, balance4Voting);
toSend.estimateGas()
.then(gasEstimated => {
console.log("customVote gas estimated: " + gasEstimated);
return toSend.send({gas: gasEstimated + 1000});
})
.then(res => {
console.log('sucess:', res);
this.setState({isSubmitting: false});
return poll(idPoll).call();
})
.then(poll => {
this.setState(poll);
})
.catch(res => {
console.log('fail:', res);
})
.finally(() => {
this.setState({isSubmitting: false});
});
}
2018-06-27 18:40:45 +00:00
render(){
2018-06-28 00:45:31 +00:00
const { _description,
_totalCensus,
_voters,
_qvResults,
_results,
_canVote,
value,
isSubmitting } = this.state;
const disableVote = !_canVote || isSubmitting;
const tokenBalance = 100; // TODO: read balance from cloned token
const maxValue = Math.floor(Math.sqrt(tokenBalance));
2018-06-27 18:40:45 +00:00
return (
<Card>
<CardContent>
2018-06-28 00:45:31 +00:00
<Typography variant="headline">Proposal: {_description}</Typography>
2018-06-27 18:40:45 +00:00
<Typography variant="subheading" color="textSecondary">
2018-06-28 00:45:31 +00:00
Number of voters: {_voters}
2018-06-27 18:40:45 +00:00
</Typography>
<Typography variant="subheading" color="textSecondary">
2018-06-28 00:45:31 +00:00
Votes: {_qvResults}
</Typography>
<Typography variant="subheading" color="textSecondary">
SNT Allocated: {_results}
2018-06-27 18:40:45 +00:00
</Typography>
</CardContent>
2018-06-28 00:45:31 +00:00
<Tooltip id="tooltip-icon" placement="top" title={`${value} votes`}>
2018-06-27 18:40:45 +00:00
<CardActions>
2018-06-28 00:45:31 +00:00
<Slider value={value} min={0} max={maxValue} step={1} onChange={this.handleChange} />
<Button variant="contained" disabled={disableVote} color="primary" onClick={this.handleClick}>Vote</Button>
2018-06-27 18:40:45 +00:00
</CardActions>
</Tooltip>
</Card>
)
}
}
2018-06-27 16:32:11 +00:00
2018-06-28 00:45:31 +00:00
2018-06-27 16:32:11 +00:00
const PollsList = ({ rawPolls }) => (
<Fragment>
2018-06-28 00:45:31 +00:00
{rawPolls.map((poll, idx) => <Poll key={idx} idPoll={idx} {...poll} />)}
2018-06-27 16:32:11 +00:00
</Fragment>
)
export default PollsList