Skip to content

Commit

Permalink
multi-choice lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
elboletaire committed Jan 18, 2024
1 parent 4623c0a commit 6964770
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 60 deletions.
5 changes: 4 additions & 1 deletion mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export const handlers = [
title: { default: 'das ist eine andere frage' },
},
],
results: { aggregation: 'discrete-counting', display: 'multiple-question' },
type: {
name: 'single-choice-multiquestion',
properties: {},
},
},
})
)
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@testing-library/react": "14.0.0",
"@types/jest": "^29.5.3",
"@types/testing-library__jest-dom": "5.14.8",
"@vocdoni/sdk": "./vocdoni-sdk-v0.7.2-beta1.tgz",
"@vocdoni/sdk": "^0.7.2",
"axios": "^0.27.0",
"eslint-plugin-testing-library": "5.5.1",
"ethers": "^5.7.0",
Expand All @@ -40,8 +40,5 @@
"clean": "turbo clean --filter=@vocdoni* && rm -fr node_modules",
"test": "jest"
},
"resolutions": {
"@vocdoni/sdk": "./vocdoni-sdk-v0.7.2-beta1.tgz"
},
"packageManager": "[email protected]"
}
4 changes: 2 additions & 2 deletions packages/chakra-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@chakra-ui/toast": "^6.1.1",
"@ethersproject/abstract-signer": "^5.7.0",
"@ethersproject/wallet": "^5.7.0",
"@vocdoni/sdk": "~0.7.0",
"@vocdoni/sdk": "~0.7.2",
"react": ">= 16.8.0",
"react-dom": ">= 16.8.0",
"react-markdown": ">= 8.0.0",
Expand Down Expand Up @@ -74,7 +74,7 @@
"@ethersproject/wallet": "^5.7.0",
"@types/react": "^18.0.30",
"@types/react-dom": "^18.0.11",
"@vocdoni/sdk": "~0.7.0",
"@vocdoni/sdk": "~0.7.2",
"clean-package": "^2.2.0",
"date-fns": "^2.29.3",
"eslint": "^8.42.0",
Expand Down
73 changes: 38 additions & 35 deletions packages/chakra-components/src/components/Election/Questions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ export const ElectionQuestions = (props: ElectionQuestionsProps) => {
throw new Error('Unknown or invalid election type')
}

// console.log(results)
// return

return bvote(results)
}

Expand Down Expand Up @@ -135,8 +132,10 @@ export const QuestionsConfirmation = ({ answers, election, ...rest }: QuestionsC
)
}
const choices = answers[0]
.map((a) => (q.choices[Number(a)] ? q.choices[Number(a)].title.default : localize('vote.abstain')))
.map((a) => (
.map((a: string) =>
q.choices[Number(a)] ? q.choices[Number(a)].title.default : localize('vote.abstain')
)
.map((a: string) => (
<span>
- {a}
<br />
Expand Down Expand Up @@ -263,13 +262,15 @@ const MultiChoice = ({ index, question }: QuestionProps) => {
}

const choices = [...question.choices]
for (const abstain of election.resultsType.properties.abstainValues) {
choices.push({
title: {
default: localize('vote.abstain'),
},
value: abstain,
})
if (election.resultsType.properties.canAbstain) {
for (const abstain of election.resultsType.properties.abstainValues) {
choices.push({
title: {
default: localize('vote.abstain'),
},
value: parseInt(abstain, 10),
})
}
}

return (
Expand All @@ -286,29 +287,31 @@ const MultiChoice = ({ index, question }: QuestionProps) => {
},
}}
name={index}
render={({ field }) =>
choices.map((choice, ck) => (
<Checkbox
{...field}
key={ck}
sx={styles.checkbox}
value={choice.value.toString()}
isDisabled={disabled}
onChange={(e) => {
if (values.includes(e.target.value)) {
setValue(
index,
values.filter((v: string) => v !== e.target.value)
)
} else {
setValue(index, [...values, e.target.value])
}
}}
>
{choice.title.default}
</Checkbox>
))
}
render={({ field }) => (
<>
{choices.map((choice, ck) => (
<Checkbox
{...field}
key={ck}
sx={styles.checkbox}
value={choice.value.toString()}
isDisabled={disabled}
onChange={(e) => {
if (values.includes(e.target.value)) {
setValue(
index,
values.filter((v: string) => v !== e.target.value)
)
} else {
setValue(index, [...values, e.target.value])
}
}}
>
{choice.title.default}
</Checkbox>
))}
</>
)}
/>
<FormErrorMessage sx={styles.error}>{errors[index]?.message as string}</FormErrorMessage>
</Stack>
Expand Down
32 changes: 14 additions & 18 deletions packages/chakra-components/src/components/Election/Results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ElectionResultsTypeNames,
ElectionStatus,
IChoice,
IQuestion,
InvalidElection,
PublishedElection,
formatUnits,
Expand Down Expand Up @@ -34,17 +35,15 @@ export const ElectionResults = (props: ChakraProps) => {
)
}

console.log('election:', election)
const decimals = (election.meta as any)?.token?.decimals || 0
const totals = election?.questions
.map((el) => el.choices.reduce((acc, curr) => acc + Number(curr.results), 0))
.map((votes: number) => results(votes, decimals))
console.log('totals:', totals)

return (
<Flex sx={styles.wrapper} {...props}>
{election?.questions.map((q: any, idx: number) => {
const choices = electionChoices(election, q.choices, localize('vote.abstain'))
console.log('choices:', choices)
{election?.questions.map((q: IQuestion, idx: number) => {
const choices = electionChoices(election, q, localize('vote.abstain'))
return (
<chakra.div key={idx} sx={styles.question}>
<chakra.div sx={styles.header}>
Expand Down Expand Up @@ -78,22 +77,19 @@ export const ElectionResults = (props: ChakraProps) => {
)
}

const electionChoices = (election: PublishedElection, choices: IChoice[], abstainLabel: string) => {
const nchoices = [...choices]
if (election.resultsType.name === ElectionResultsTypeNames.MULTIPLE_CHOICE) {
console.log('received results:', election.results)
const results = election.results[0].map((val, key) => parseInt(val, 10) + parseInt(election.results[1][key], 10))
for (const k in nchoices) {
nchoices[k].results = results[parseInt(nchoices[k].value, 10)]
}
const abstain = {
const electionChoices = (election: PublishedElection, q: IQuestion, abstainLabel: string) => {
const nchoices = [...q.choices]

if (
election.resultsType.name === ElectionResultsTypeNames.MULTIPLE_CHOICE &&
election.resultsType.properties.canAbstain
) {
const abstain: IChoice = {
title: {
default: abstainLabel,
},
results: 0,
}
for (const id of election.resultsType.properties.abstainValues) {
abstain.results += parseInt(results[id], 10)
results: q.numAbstains as string,
value: -1,
}
nchoices.push(abstain)
}
Expand Down

0 comments on commit 6964770

Please sign in to comment.