From 57ef231576ca1c774858dc834862d3c784c9e80a Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Thu, 28 Jun 2018 15:05:53 -0400 Subject: [PATCH] Update votes on update use global context for updating polls --- app/components/Voting.js | 2 +- app/components/simple-voting/PollsList.js | 97 ++++++++++++----------- app/dapp.js | 13 ++- 3 files changed, 62 insertions(+), 50 deletions(-) diff --git a/app/components/Voting.js b/app/components/Voting.js index 8b1aa7d..a465edc 100644 --- a/app/components/Voting.js +++ b/app/components/Voting.js @@ -26,7 +26,7 @@ class Voting extends PureComponent { - {rawPolls && } + {rawPolls && } } diff --git a/app/components/simple-voting/PollsList.js b/app/components/simple-voting/PollsList.js index 52196cf..1ebaa11 100644 --- a/app/components/simple-voting/PollsList.js +++ b/app/components/simple-voting/PollsList.js @@ -8,12 +8,14 @@ import Slider from '@material-ui/lab/Slider'; import Tooltip from '@material-ui/core/Tooltip'; import PollManager from 'Embark/contracts/PollManager'; import MiniMeTokenInterface from 'Embark/contracts/MiniMeTokenInterface'; +import CircularProgress from '@material-ui/core/CircularProgress'; +import { VotingContext } from '../../context'; class Poll extends Component { constructor(props){ super(props); - this.state = { value: 0, balance: 0, isSubmitting: false, ...props }; + this.state = { value: 0, balance: 0, isSubmitting: false }; } handleChange = (event, value) => { @@ -26,58 +28,55 @@ class Poll extends Component { this.setState({isSubmitting: true}); const { customVote, poll, unvote } = PollManager.methods; - const { idPoll, value } = this.state; + const { updatePoll, idPoll } = this.props; + const { value } = this.state; const balance4Voting = value * value; const toSend = balance4Voting === 0 ? unvote(idPoll) : customVote(idPoll, balance4Voting); - + toSend.estimateGas() - .then(gasEstimated => { - console.log("voting gas estimated: " + gasEstimated); - return toSend.send({gas: gasEstimated + 100000}); - }) - .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}); - }); + .then(gasEstimated => { + console.log("voting gas estimated: " + gasEstimated); + return toSend.send({gas: gasEstimated + 100000}); + }) + .then(res => { + console.log('sucess:', res); + this.setState({isSubmitting: false}); + return updatePoll(idPoll); + }) + .catch(res => { + console.log('fail:', res); + }) + .finally(() => { + this.setState({isSubmitting: false}); + }); } componentDidMount() { MiniMeTokenInterface.options.address = this.props._token; MiniMeTokenInterface.methods.balanceOfAt(web3.eth.defaultAccount, this.props._startBlock - 1) - .call() - .then(balance => { - this.setState({balance}); - }); + .call() + .then(balance => { + this.setState({balance}); + }); PollManager.methods.getVote(this.props.idPoll, web3.eth.defaultAccount) - .call() - .then(vote => { - this.setState({value: Math.sqrt(vote)}); - }) + .call() + .then(vote => { + this.setState({value: Math.sqrt(vote)}); + }) } render(){ - const { _description, - _totalCensus, - _voters, - _qvResults, - _results, - _canVote, - value, - isSubmitting, - balance, - votes } = this.state; + const { + _description, + _totalCensus, + _voters, + _qvResults, + _results, + _canVote, + } = this.props; + const { value, balance, isSubmitting } = this.state; const disableVote = balance == 0 || !_canVote || isSubmitting; const maxValue = Math.floor(Math.sqrt(balance)); @@ -97,21 +96,25 @@ class Poll extends Component { - + - - - + {isSubmitting ? : } + + ) } } -const PollsList = ({ rawPolls }) => ( - - {rawPolls.map((poll, idx) => )} - +const PollsList = () => ( + + {({ updatePoll, rawPolls }) => + + {rawPolls.map((poll, idx) => )} + + } + ) export default PollsList diff --git a/app/dapp.js b/app/dapp.js index 76a7a31..aa3407a 100644 --- a/app/dapp.js +++ b/app/dapp.js @@ -45,6 +45,15 @@ class App extends React.Component { else this.setState({ rawPolls: [] }); } + updatePoll = async (idPoll) => { + const { poll } = PollManager.methods; + const { rawPolls } = this.state; + const newPolls = [...rawPolls]; + const updatedPoll = await poll(idPoll).call(); + newPolls[idPoll] = updatedPoll; + this.setState({ rawPolls: newPolls }); + } + _renderStatus(title, available) { let className = available ? 'pull-right status-online' : 'pull-right status-offline'; return @@ -55,9 +64,9 @@ class App extends React.Component { render(){ const { admin, rawPolls } = this.state; - const { _getPolls } = this; + const { _getPolls, updatePoll } = this; const toggleAdmin = () => this.setState({ admin: true }); - const votingContext = { getPolls: _getPolls, rawPolls, toggleAdmin }; + const votingContext = { getPolls: _getPolls, rawPolls, toggleAdmin, updatePoll }; return (