-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement simple vote package component
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
)) | ||
} |