From dd7b4b1e4d176ba350e58e0f8d7f9ea497b51080 Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 12 Jul 2023 06:29:15 +0100 Subject: [PATCH 1/7] Total Votes per candidate --- .../election/CandidateCard/CandidateCard.tsx | 7 +++++-- .../election/CandidateCard/CandidateCardList.tsx | 6 ++++-- .../components/election/voting/VotingStage.tsx | 13 ++++++++++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/ui/src/council/components/election/CandidateCard/CandidateCard.tsx b/packages/ui/src/council/components/election/CandidateCard/CandidateCard.tsx index 6dce1fbc01..7105f87e87 100644 --- a/packages/ui/src/council/components/election/CandidateCard/CandidateCard.tsx +++ b/packages/ui/src/council/components/election/CandidateCard/CandidateCard.tsx @@ -1,3 +1,4 @@ +import BN from 'bn.js' import React, { useCallback } from 'react' import styled from 'styled-components' @@ -31,11 +32,13 @@ export interface CandidateCardProps { loses?: number canVote?: boolean isPreview?: boolean + myStake?: BN } export const CandidateCard = ({ candidate: { id, member, info, stake }, voted, + myStake, withdrawable, canVote, isPreview, @@ -97,9 +100,9 @@ export const CandidateCard = ({ {stake && ( - + - Staked + {voted && myStake ? 'My Stake' : 'Staked'} )} {withdrawable && ( diff --git a/packages/ui/src/council/components/election/CandidateCard/CandidateCardList.tsx b/packages/ui/src/council/components/election/CandidateCard/CandidateCardList.tsx index b996171b5e..57299301cb 100644 --- a/packages/ui/src/council/components/election/CandidateCard/CandidateCardList.tsx +++ b/packages/ui/src/council/components/election/CandidateCard/CandidateCardList.tsx @@ -1,3 +1,4 @@ +import BN from 'bn.js' import React from 'react' import styled from 'styled-components' @@ -10,6 +11,7 @@ import { CandidateCardProps, CandidateCard, CandidateCardCandidate } from './Can interface CandidateCardListCandidate extends CandidateCardCandidate { isMyCandidate?: boolean voted?: boolean + myStake?: BN } interface CandidatesListProps extends Pick { @@ -34,8 +36,8 @@ export const CandidateCardList = ({ candidates = [], isLoading, canVote }: Candi return ( - {candidates.map(({ voted, isMyCandidate, ...candidate }, index) => ( - + {candidates.map(({ voted, isMyCandidate, myStake, ...candidate }, index) => ( + ))} ) diff --git a/packages/ui/src/council/components/election/voting/VotingStage.tsx b/packages/ui/src/council/components/election/voting/VotingStage.tsx index 8942cd9f96..b426428b2d 100644 --- a/packages/ui/src/council/components/election/voting/VotingStage.tsx +++ b/packages/ui/src/council/components/election/voting/VotingStage.tsx @@ -2,9 +2,11 @@ import React, { useMemo, useState } from 'react' import { useMyAccounts } from '@/accounts/hooks/useMyAccounts' import { NoData } from '@/common/components/NoData' +import { BN_ZERO } from '@/common/constants' import { isDefined } from '@/common/utils' import { CandidateCardList } from '@/council/components/election/CandidateCard/CandidateCardList' import { CurrentElectionTabs, VotingStageTab } from '@/council/components/election/CurrentElectionTabs' +import { useMyCastVotes } from '@/council/hooks/useMyCastVotes' import { useMyCurrentVotesCount } from '@/council/hooks/useMyCurrentVotesCount' import { useVerifiedVotingAttempts } from '@/council/hooks/useVerifiedVotingAttempts' import { CandidacyStatus } from '@/council/types' @@ -18,6 +20,7 @@ interface VotingStageProps { export const VotingStage = ({ election, isLoading }: VotingStageProps) => { const [tab, setTab] = useState('candidates') const { votesTotal } = useMyCurrentVotesCount(election?.cycleId) + const { votes } = useMyCastVotes(election?.cycleId) const { allAccounts } = useMyAccounts() const myVotes = useVerifiedVotingAttempts(election?.cycleId) @@ -31,7 +34,15 @@ export const VotingStage = ({ election, isLoading }: VotingStageProps) => { ...candidate, voted: optionIds?.has(candidate.member.id), })) - const votedForCandidates = allCandidates?.filter(({ voted }) => voted) + const votedForCandidates = allCandidates?.filter(({ voted }) => voted).map((candidate) => { + const myVotesForCandidate = votes?.filter((vote) => vote.optionId === candidate.member.id) ?? [] + + return { + ...candidate, + myVotes: myVotesForCandidate, + myStake: myVotesForCandidate.reduce((prev, next) => prev.add(next.stake), BN_ZERO), + } + }) return [allCandidates, votedForCandidates] }, [optionIds?.size, election?.candidates]) From 28ec86c0a65ad66021b91b6b046a05e12b5c4d7f Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 12 Jul 2023 06:47:16 +0100 Subject: [PATCH 2/7] lint fix --- .../CandidateCard/CandidateCardList.tsx | 9 ++++++++- .../components/election/voting/VotingStage.tsx | 18 ++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/ui/src/council/components/election/CandidateCard/CandidateCardList.tsx b/packages/ui/src/council/components/election/CandidateCard/CandidateCardList.tsx index 57299301cb..24679dd6f2 100644 --- a/packages/ui/src/council/components/election/CandidateCard/CandidateCardList.tsx +++ b/packages/ui/src/council/components/election/CandidateCard/CandidateCardList.tsx @@ -37,7 +37,14 @@ export const CandidateCardList = ({ candidates = [], isLoading, canVote }: Candi return ( {candidates.map(({ voted, isMyCandidate, myStake, ...candidate }, index) => ( - + ))} ) diff --git a/packages/ui/src/council/components/election/voting/VotingStage.tsx b/packages/ui/src/council/components/election/voting/VotingStage.tsx index b426428b2d..91c66a869c 100644 --- a/packages/ui/src/council/components/election/voting/VotingStage.tsx +++ b/packages/ui/src/council/components/election/voting/VotingStage.tsx @@ -34,15 +34,17 @@ export const VotingStage = ({ election, isLoading }: VotingStageProps) => { ...candidate, voted: optionIds?.has(candidate.member.id), })) - const votedForCandidates = allCandidates?.filter(({ voted }) => voted).map((candidate) => { - const myVotesForCandidate = votes?.filter((vote) => vote.optionId === candidate.member.id) ?? [] + const votedForCandidates = allCandidates + ?.filter(({ voted }) => voted) + .map((candidate) => { + const myVotesForCandidate = votes?.filter((vote) => vote.optionId === candidate.member.id) ?? [] - return { - ...candidate, - myVotes: myVotesForCandidate, - myStake: myVotesForCandidate.reduce((prev, next) => prev.add(next.stake), BN_ZERO), - } - }) + return { + ...candidate, + myVotes: myVotesForCandidate, + myStake: myVotesForCandidate.reduce((prev, next) => prev.add(next.stake), BN_ZERO), + } + }) return [allCandidates, votedForCandidates] }, [optionIds?.size, election?.candidates]) From f72a7cbba636d003299c2f8dae1c56e5bf30bdad Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 12 Jul 2023 08:02:46 +0100 Subject: [PATCH 3/7] voting test --- .../components/election/voting/VotingStage.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/ui/src/council/components/election/voting/VotingStage.tsx b/packages/ui/src/council/components/election/voting/VotingStage.tsx index 91c66a869c..2c3137dfb6 100644 --- a/packages/ui/src/council/components/election/voting/VotingStage.tsx +++ b/packages/ui/src/council/components/election/voting/VotingStage.tsx @@ -1,4 +1,5 @@ -import React, { useMemo, useState } from 'react' +/* eslint-disable no-console */ +import React, { useEffect, useMemo, useState } from 'react' import { useMyAccounts } from '@/accounts/hooks/useMyAccounts' import { NoData } from '@/common/components/NoData' @@ -38,16 +39,19 @@ export const VotingStage = ({ election, isLoading }: VotingStageProps) => { ?.filter(({ voted }) => voted) .map((candidate) => { const myVotesForCandidate = votes?.filter((vote) => vote.optionId === candidate.member.id) ?? [] - return { ...candidate, - myVotes: myVotesForCandidate, myStake: myVotesForCandidate.reduce((prev, next) => prev.add(next.stake), BN_ZERO), } }) return [allCandidates, votedForCandidates] - }, [optionIds?.size, election?.candidates]) + }, [optionIds?.size, election?.candidates, votes, election]) + + useEffect(() => { + console.log('My Votes length ',myVotes) + console.log('Voted for candidates ',votedForCandidates) + }) return ( <> From fd17b88cdd155652dc10aa235187314724bbdc87 Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 12 Jul 2023 08:03:13 +0100 Subject: [PATCH 4/7] voting test --- .../ui/src/council/components/election/voting/VotingStage.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/ui/src/council/components/election/voting/VotingStage.tsx b/packages/ui/src/council/components/election/voting/VotingStage.tsx index 2c3137dfb6..a2f857553c 100644 --- a/packages/ui/src/council/components/election/voting/VotingStage.tsx +++ b/packages/ui/src/council/components/election/voting/VotingStage.tsx @@ -49,8 +49,8 @@ export const VotingStage = ({ election, isLoading }: VotingStageProps) => { }, [optionIds?.size, election?.candidates, votes, election]) useEffect(() => { - console.log('My Votes length ',myVotes) - console.log('Voted for candidates ',votedForCandidates) + console.log('My Votes length ', myVotes) + console.log('Voted for candidates ', votedForCandidates) }) return ( From dcbd857065910e5b4feb94eaf2533117f1b5763b Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 12 Jul 2023 20:54:21 +0100 Subject: [PATCH 5/7] voting test --- .../components/election/voting/VotingStage.tsx | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/ui/src/council/components/election/voting/VotingStage.tsx b/packages/ui/src/council/components/election/voting/VotingStage.tsx index a2f857553c..80c35a82b9 100644 --- a/packages/ui/src/council/components/election/voting/VotingStage.tsx +++ b/packages/ui/src/council/components/election/voting/VotingStage.tsx @@ -37,18 +37,19 @@ export const VotingStage = ({ election, isLoading }: VotingStageProps) => { })) const votedForCandidates = allCandidates ?.filter(({ voted }) => voted) - .map((candidate) => { - const myVotesForCandidate = votes?.filter((vote) => vote.optionId === candidate.member.id) ?? [] - return { - ...candidate, - myStake: myVotesForCandidate.reduce((prev, next) => prev.add(next.stake), BN_ZERO), - } - }) + // .map((candidate) => { + // const myVotesForCandidate = votes?.filter((vote) => vote.optionId === candidate.member.id) ?? [] + // return { + // ...candidate, + // myStake: myVotesForCandidate.reduce((prev, next) => prev.add(next.stake), BN_ZERO), + // } + // }) return [allCandidates, votedForCandidates] }, [optionIds?.size, election?.candidates, votes, election]) useEffect(() => { + console.log('My Cast Votes ', votes) console.log('My Votes length ', myVotes) console.log('Voted for candidates ', votedForCandidates) }) From 009c90a6256ba677770b0943efdcfd69cc2c74b7 Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 12 Jul 2023 22:25:09 +0100 Subject: [PATCH 6/7] voting test --- .../ui/src/council/components/election/voting/VotingStage.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/ui/src/council/components/election/voting/VotingStage.tsx b/packages/ui/src/council/components/election/voting/VotingStage.tsx index 80c35a82b9..c0d9baf8f9 100644 --- a/packages/ui/src/council/components/election/voting/VotingStage.tsx +++ b/packages/ui/src/council/components/election/voting/VotingStage.tsx @@ -51,6 +51,7 @@ export const VotingStage = ({ election, isLoading }: VotingStageProps) => { useEffect(() => { console.log('My Cast Votes ', votes) console.log('My Votes length ', myVotes) + console.log('All candidates ', allCandidates) console.log('Voted for candidates ', votedForCandidates) }) From a9f13a86aec11e57e3513d704256f541c71baf88 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 13 Jul 2023 10:36:57 +0100 Subject: [PATCH 7/7] lint fix --- .../election/voting/VotingStage.tsx | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/packages/ui/src/council/components/election/voting/VotingStage.tsx b/packages/ui/src/council/components/election/voting/VotingStage.tsx index c0d9baf8f9..91c66a869c 100644 --- a/packages/ui/src/council/components/election/voting/VotingStage.tsx +++ b/packages/ui/src/council/components/election/voting/VotingStage.tsx @@ -1,5 +1,4 @@ -/* eslint-disable no-console */ -import React, { useEffect, useMemo, useState } from 'react' +import React, { useMemo, useState } from 'react' import { useMyAccounts } from '@/accounts/hooks/useMyAccounts' import { NoData } from '@/common/components/NoData' @@ -37,23 +36,18 @@ export const VotingStage = ({ election, isLoading }: VotingStageProps) => { })) const votedForCandidates = allCandidates ?.filter(({ voted }) => voted) - // .map((candidate) => { - // const myVotesForCandidate = votes?.filter((vote) => vote.optionId === candidate.member.id) ?? [] - // return { - // ...candidate, - // myStake: myVotesForCandidate.reduce((prev, next) => prev.add(next.stake), BN_ZERO), - // } - // }) + .map((candidate) => { + const myVotesForCandidate = votes?.filter((vote) => vote.optionId === candidate.member.id) ?? [] - return [allCandidates, votedForCandidates] - }, [optionIds?.size, election?.candidates, votes, election]) + return { + ...candidate, + myVotes: myVotesForCandidate, + myStake: myVotesForCandidate.reduce((prev, next) => prev.add(next.stake), BN_ZERO), + } + }) - useEffect(() => { - console.log('My Cast Votes ', votes) - console.log('My Votes length ', myVotes) - console.log('All candidates ', allCandidates) - console.log('Voted for candidates ', votedForCandidates) - }) + return [allCandidates, votedForCandidates] + }, [optionIds?.size, election?.candidates]) return ( <>