-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement envelope * Add translations * Update translation * Export vote package type * Minor fixes * Refactor component name
- Loading branch information
Showing
5 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
packages/chakra-components/src/components/Election/Envelope.tsx
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,116 @@ | ||
import { useDatesLocale, useElection } from '@vocdoni/react-providers' | ||
import { | ||
ElectionResultsTypeNames, | ||
ElectionStatus, | ||
IChoice, | ||
IQuestion, | ||
IVoteEncryptedPackage, | ||
IVotePackage, | ||
PublishedElection, | ||
} from '@vocdoni/sdk' | ||
import { Text } from '@chakra-ui/react' | ||
import { chakra, ChakraProps, useMultiStyleConfig } from '@chakra-ui/system' | ||
import { format } from 'date-fns' | ||
|
||
export type VotePackageType = IVotePackage | IVoteEncryptedPackage | ||
|
||
export const Envelope = ({ | ||
votePackage, | ||
...props | ||
}: { | ||
votePackage: VotePackageType | ||
} & ChakraProps) => { | ||
const styles = useMultiStyleConfig('Envelope') | ||
const { election, localize } = useElection() | ||
const locale = useDatesLocale() | ||
|
||
if ( | ||
!election || | ||
'encrypted' in votePackage || | ||
!(election instanceof PublishedElection) || | ||
election?.status === ElectionStatus.CANCELED | ||
) | ||
return null | ||
|
||
if (election?.electionType.secretUntilTheEnd && election.status !== ElectionStatus.RESULTS) { | ||
return ( | ||
<Text sx={styles.secret} {...props}> | ||
{localize('results.secret_until_the_end', { | ||
endDate: format(election.endDate, localize('results.date_format'), { locale }), | ||
})} | ||
</Text> | ||
) | ||
} | ||
|
||
return ( | ||
<chakra.div sx={styles.wrapper} {...props}> | ||
{election.questions.map((q, i) => { | ||
return ( | ||
<chakra.div sx={styles.question}> | ||
<Text sx={styles.title}>{localize('envelopes.question_title', { title: q.title.default })}</Text> | ||
<SelectedOptions question={q} questionIndex={i} votes={votePackage.votes} /> | ||
</chakra.div> | ||
) | ||
})} | ||
</chakra.div> | ||
) | ||
} | ||
|
||
const SelectedOptions = ({ | ||
question, | ||
questionIndex, | ||
votes, | ||
}: { | ||
question: IQuestion | ||
questionIndex: number | ||
votes: number[] | ||
}) => { | ||
const { election, localize } = useElection() | ||
const styles = useMultiStyleConfig('Envelope') | ||
|
||
if (!election || !(election instanceof PublishedElection)) return null | ||
|
||
const selectedOptions: IChoice[] = [] | ||
switch (election.resultsType.name) { | ||
case ElectionResultsTypeNames.MULTIPLE_CHOICE: | ||
const abstainValues = election.resultsType?.properties?.abstainValues ?? [] | ||
let abstainCount = 0 | ||
votes.forEach((v) => { | ||
if (abstainValues.includes(v.toString())) { | ||
abstainCount++ | ||
return | ||
} | ||
selectedOptions.push(question.choices[v]) | ||
}) | ||
if (abstainCount > 0) { | ||
selectedOptions.push({ | ||
title: { | ||
default: localize('envelopes.envelope_abstain_count', { count: abstainCount }), | ||
}, | ||
results: abstainCount.toString(), | ||
value: -1, | ||
} as IChoice) | ||
} | ||
break | ||
case ElectionResultsTypeNames.APPROVAL: | ||
votes.forEach((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) => ( | ||
<chakra.div key={i} sx={styles.choiceWrapper}> | ||
<Text sx={styles.choiceTitle}>{c.title.default}</Text> | ||
</chakra.div> | ||
))} | ||
</> | ||
) | ||
} |
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
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,38 @@ | ||
import { createMultiStyleConfigHelpers } from '@chakra-ui/styled-system' | ||
|
||
export const envelopeAnatomy = [ | ||
// all questions wrapper | ||
'wrapper', | ||
// individual question wrapper | ||
'question', | ||
// question title | ||
'title', | ||
// choice wrapper | ||
'choiceWrapper', | ||
// choice title | ||
'choiceTitle', | ||
// secret envelope (no results until the end text) | ||
'secret', | ||
] | ||
|
||
const { defineMultiStyleConfig, definePartsStyle } = createMultiStyleConfigHelpers(envelopeAnatomy) | ||
|
||
const baseStyle = definePartsStyle({ | ||
wrapper: { | ||
flexDirection: 'column', | ||
gap: 2, | ||
}, | ||
title: { | ||
pb: 0, | ||
fontWeight: 'bold', | ||
}, | ||
secret: { | ||
color: 'red.200', | ||
textAlign: 'center', | ||
fontWeight: 'bold', | ||
}, | ||
}) | ||
|
||
export const EnvelopeTheme = defineMultiStyleConfig({ | ||
baseStyle, | ||
}) |
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