useVotingBatches.ts
1 import { config } from '../config' 2 import voting from '../helpers/voting' 3 import { DetailedVotingRoom } from '../models/smartContract' 4 import { useRoomAggregateVotes } from './useRoomAggregateVotes' 5 import { useVotesAggregate } from './useVotesAggregate' 6 7 interface Props { 8 room: DetailedVotingRoom 9 } 10 11 export const useVotingBatches = ({ room }: Props) => { 12 const vote = voting.fromRoom(room) 13 const { votesToSend, allVotes } = useVotesAggregate(vote.ID, room.verificationStartAt, room.startAt) 14 const { evaluated, finalized, evaluatingPos } = useRoomAggregateVotes(room, false) 15 16 const beingFinalized = !evaluated && finalized 17 const beingEvaluated = evaluated && !finalized 18 const firstFinalization = beingEvaluated && evaluatingPos === allVotes.length + 1 19 20 const votesLeftCount = allVotes.length - evaluatingPos + 1 21 const finalizeVotingLimit = firstFinalization 22 ? Math.min(allVotes.length, config.votesLimit) 23 : Math.min(votesLeftCount, config.votesLimit) 24 25 const batchCount = Math.ceil((beingFinalized ? allVotes.length + 1 : allVotes.length) / config.votesLimit) 26 const batchLeftCount = Math.ceil(votesLeftCount / config.votesLimit) 27 const batchDoneCount = batchCount - batchLeftCount 28 const batchedVotes = votesToSend.slice(0, finalizeVotingLimit) 29 30 return { 31 finalizeVotingLimit, 32 batchCount, 33 batchDoneCount, 34 beingFinalized, 35 beingEvaluated, 36 batchedVotes, 37 } 38 }