Skip to content

Commit

Permalink
Implement simple vote package component
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Aug 7, 2024
1 parent da525c8 commit 5b56476
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/components/Envelope/Detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { generatePath, Link as RouterLink } from 'react-router-dom'
import { CopyButton } from '~components/Layout/CopyButton'
import ShowRawButton from '~components/Layout/ShowRawButton'
import { RoutePath } from '~constants'
import { ElectionProvider } from '@vocdoni/react-providers'
import { VoteSelectedChoices } from '~components/Envelope/VoteSelectedChoices'

const EnvelopeDetail = (envelope: IVoteInfoResponse) => {
const { t } = useTranslation()
Expand Down Expand Up @@ -87,6 +89,10 @@ const EnvelopeDetail = (envelope: IVoteInfoResponse) => {
/>
</Text>
</Flex>

<ElectionProvider id={envelope.electionID}>
<VoteSelectedChoices {...envelope.package} />
</ElectionProvider>
<ShowRawButton obj={envelope} />
</Flex>
)
Expand Down
85 changes: 85 additions & 0 deletions src/components/Envelope/VoteSelectedChoices.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { useElection } from '@vocdoni/react-providers'
import {
ElectionResultsTypeNames,
IChoice,
InvalidElection as InvalidElectionType,
IQuestion,
IVoteEncryptedPackage,
IVotePackage,
PublishedElection,
} from '@vocdoni/sdk'
import InvalidElection from '~components/Process/InvalidElection'
import { Box, Text } from '@chakra-ui/react'
import { useTranslation } from 'react-i18next'

export const VoteSelectedChoices = (votePackage: IVotePackage | IVoteEncryptedPackage) => {
const { election } = useElection()
if (!election || 'encrypted' in votePackage) return null
if (election instanceof InvalidElectionType) {
return <InvalidElection />
}
return (
<>
{election.questions.map((q, i) => {
return (
<Box key={i}>
<Text>{q.title.default}</Text>
<ChoosedOptions question={q} questionIndex={i} votes={votePackage.votes} election={election} />
</Box>
)
})}
</>
)
}

const ChoosedOptions = ({
election,
question,
questionIndex,
votes,
}: {
election: PublishedElection
question: IQuestion
questionIndex: number
votes: number[]
}) => {
const { t } = useTranslation()
const selectedOptions: IChoice[] = []
switch (election.resultsType.name) {
case ElectionResultsTypeNames.MULTIPLE_CHOICE:
const abstainValues = election.resultsType?.properties?.abstainValues ?? []
let abstainCount = 0
votes.map((v) => {
if (abstainValues.includes(v.toString())) {
abstainCount++
return
}
selectedOptions.push(question.choices[v])
})
if (abstainCount > 0) {
selectedOptions.push({
title: {
default: t('vote.abstain', { defaultValue: 'Abstained times {{count}}', count: abstainCount }),
},
results: abstainCount.toString(),
value: -1,
} as IChoice)
}
break
case ElectionResultsTypeNames.APPROVAL:
votes.map((v, i) => {
if (v > 0) selectedOptions.push(question.choices[i])
})
break
case ElectionResultsTypeNames.SINGLE_CHOICE_MULTIQUESTION:
selectedOptions.push(question.choices[votes[questionIndex]])
break
default:
selectedOptions.push(question.choices[votes[0]])
}
return selectedOptions.map((c, i) => (
<Box key={i}>
<Text>{c.title.default}</Text>
</Box>
))
}

0 comments on commit 5b56476

Please sign in to comment.