From abe8dd749b7c27e8714cdf98503776b9bf79eac2 Mon Sep 17 00:00:00 2001 From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com> Date: Wed, 7 Jul 2021 14:11:54 +0200 Subject: [PATCH] Show aggregated votes (#95) --- packages/DApp/src/components/Card.tsx | 9 ++++++--- .../DApp/src/hooks/useRoomAggregateVotes.ts | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 packages/DApp/src/hooks/useRoomAggregateVotes.ts diff --git a/packages/DApp/src/components/Card.tsx b/packages/DApp/src/components/Card.tsx index aa29b98..d748e68 100644 --- a/packages/DApp/src/components/Card.tsx +++ b/packages/DApp/src/components/Card.tsx @@ -19,6 +19,7 @@ import { VoteAnimatedModal } from './card/VoteAnimatedModal' import voting from '../helpers/voting' import { DetailedVotingRoom } from '../models/smartContract' import { VoteSubmitButton } from './card/VoteSubmitButton' +import { useRoomAggregateVotes } from '../hooks/useRoomAggregateVotes' interface CardCommunityProps { community: CommunityDetail @@ -122,8 +123,10 @@ export const CardVote = ({ room, hideModalFunction }: CardVoteProps) => { const { votingContract } = useContracts() + room = useRoomAggregateVotes(room, showConfirmModal) + const finalizeVoting = useContractFunction(votingContract, 'finalizeVotingRoom') - const community = room.details + const setNext = (val: boolean) => { setShowConfirmModal(val) setShowVoteModal(false) @@ -152,7 +155,7 @@ export const CardVote = ({ room, hideModalFunction }: CardVoteProps) => { return ( {showVoteModal && ( - + { Date.now() && showConfirmModal === false) { + const reducedVotes = votes.reduce( + (accumulator, vote) => { + if (vote[1].mod(2).toNumber()) { + return { for: accumulator.for.add(vote[2]), against: accumulator.against } + } + return { for: accumulator.for, against: accumulator.against.add(vote[2]) } + }, + { for: room.totalVotesFor, against: room.totalVotesAgainst } + ) + room = { ...room, totalVotesAgainst: reducedVotes.against, totalVotesFor: reducedVotes.for } + } + return room +}