-
Notifications
You must be signed in to change notification settings - Fork 2
feat(ElectionResults): enhance results display extended info #226
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||
| import { Box, chakra, ChakraProps, Flex, Progress, Text, useMultiStyleConfig } from '@chakra-ui/react' | ||||||
| import { Box, chakra, ChakraProps, Flex, Image, Progress, Text, useMultiStyleConfig } from '@chakra-ui/react' | ||||||
| import { useClient, useDatesLocale, useElection } from '@vocdoni/react-providers' | ||||||
| import { | ||||||
| ElectionResultsTypeNames, | ||||||
|
|
@@ -9,6 +9,7 @@ import { | |||||
| PublishedElection, | ||||||
| } from '@vocdoni/sdk' | ||||||
| import { format } from 'date-fns' | ||||||
| import { Markdown } from '../layout' | ||||||
|
|
||||||
| const percent = (result: number, total: number) => ((Number(result) / total) * 100 || 0).toFixed(1) + '%' | ||||||
| export const results = (result: number, decimals?: number) => | ||||||
|
|
@@ -53,25 +54,46 @@ export const ElectionResults = (props: ElectionResultsProps) => { | |||||
| <Text sx={styles.title}>{localize('results.title', { title: q.title.default })}</Text> | ||||||
| </chakra.div> | ||||||
| <chakra.div sx={styles.body}> | ||||||
| {choices.map((c: any, i: number) => ( | ||||||
| <Box key={i}> | ||||||
| {totals && ( | ||||||
| <> | ||||||
| <Text sx={styles.choiceTitle}>{c.title.default}</Text> | ||||||
| <Text sx={styles.choiceVotes}> | ||||||
| {localize('results.votes', { | ||||||
| votes: results(c.results, decimals) || 0, | ||||||
| percent: percent(results(c.results, decimals), totals[idx]), | ||||||
| })} | ||||||
| </Text> | ||||||
| <Progress | ||||||
| sx={styles.progress} | ||||||
| value={((Number(c.results) / totals[idx]) * 100) / 10 ** decimals || 0} | ||||||
| /> | ||||||
| </> | ||||||
| )} | ||||||
| </Box> | ||||||
| ))} | ||||||
| {choices.map((c: any, i: number) => { | ||||||
| if (!totals) return null | ||||||
|
|
||||||
| const meta = c.meta ?? {} | ||||||
| const description = meta.description as string | undefined | ||||||
| const imageSrc = meta.image?.default as string | undefined | ||||||
| const hasDescription = !!description | ||||||
| const hasImage = !!imageSrc | ||||||
|
|
||||||
| return ( | ||||||
| <Box key={i}> | ||||||
| <Progress | ||||||
| value={((Number(c.results) / totals[idx]) * 100) / 10 ** decimals || 0} | ||||||
| sx={styles.progress} | ||||||
| /> | ||||||
|
|
||||||
| <Flex sx={styles.choiceBody}> | ||||||
| <Box flex='1'> | ||||||
| <Flex justify='space-between' mb={hasDescription ? 1 : 0}> | ||||||
| <Text sx={styles.choiceTitle}>{c.title.default}</Text> | ||||||
| <Text sx={styles.choiceVotes}> | ||||||
| {localize('results.votes', { | ||||||
| votes: results(c.results, decimals) || 0, | ||||||
| percent: percent(results(c.results, decimals), totals[idx]), | ||||||
| })} | ||||||
| </Text> | ||||||
| </Flex> | ||||||
|
|
||||||
| {hasDescription && <Markdown sx={styles.choiceDescription}>{description}</Markdown>} | ||||||
| </Box> | ||||||
|
|
||||||
| {hasImage && ( | ||||||
| <Box sx={styles.choiceImageWrapper}> | ||||||
| <Image src={imageSrc} alt={c.title.default} sx={styles.choiceImage} loading='lazy' /> | ||||||
|
||||||
| <Image src={imageSrc} alt={c.title.default} sx={styles.choiceImage} loading='lazy' /> | |
| <Image src={imageSrc} alt={`Image for ${c.title.default}`} sx={styles.choiceImage} loading='lazy' /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
we should ideally use translations here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The metadata extraction logic is repeated for each choice in the map function. Consider extracting this into a helper function or defining a proper type for the metadata structure to improve type safety and maintainability.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, @AlexanderArce, you can use a helper method in the SDK named
dotobject()which allows you to extract parts of an object using dot notation. We actually have it as a helper for theelectionclass (viaelection.get()), but I think it does not work for the fields you're trying out here... I think it was implemented only for the custom meta field (which is actuallymetadata.meta, what you're searching lies undermetadatatoo, so probably won't work the election helper method, but you can still use the underlyingdotobject()one for these things).