Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Total Votes per candidate #4312 #4475

Merged
merged 7 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BN from 'bn.js'
import React, { useCallback } from 'react'
import styled from 'styled-components'

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -97,9 +100,9 @@ export const CandidateCard = ({
{stake && (
<CandidateCardStake>
<StatsValue>
<TokenValue value={stake} />
<TokenValue value={myStake || stake} />
</StatsValue>
<Subscription>Staked</Subscription>
<Subscription>{voted && myStake ? 'My Stake' : 'Staked'}</Subscription>
</CandidateCardStake>
)}
{withdrawable && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BN from 'bn.js'
import React from 'react'
import styled from 'styled-components'

Expand All @@ -10,6 +11,7 @@ import { CandidateCardProps, CandidateCard, CandidateCardCandidate } from './Can
interface CandidateCardListCandidate extends CandidateCardCandidate {
isMyCandidate?: boolean
voted?: boolean
myStake?: BN
}

interface CandidatesListProps extends Pick<CandidateCardProps, 'canVote' | 'isPreview'> {
Expand All @@ -34,8 +36,15 @@ export const CandidateCardList = ({ candidates = [], isLoading, canVote }: Candi

return (
<CandidatesListStyles>
{candidates.map(({ voted, isMyCandidate, ...candidate }, index) => (
<CandidateCard key={index} candidate={candidate} voted={voted} withdrawable={isMyCandidate} canVote={canVote} />
{candidates.map(({ voted, isMyCandidate, myStake, ...candidate }, index) => (
<CandidateCard
key={index}
candidate={candidate}
voted={voted}
withdrawable={isMyCandidate}
canVote={canVote}
myStake={myStake}
/>
))}
</CandidatesListStyles>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -18,6 +20,7 @@ interface VotingStageProps {
export const VotingStage = ({ election, isLoading }: VotingStageProps) => {
const [tab, setTab] = useState<VotingStageTab>('candidates')
const { votesTotal } = useMyCurrentVotesCount(election?.cycleId)
const { votes } = useMyCastVotes(election?.cycleId)

const { allAccounts } = useMyAccounts()
const myVotes = useVerifiedVotingAttempts(election?.cycleId)
Expand All @@ -31,7 +34,17 @@ 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])
Expand Down