diff --git a/mocks/handlers.ts b/mocks/handlers.ts index bf99ceac..e537af7f 100644 --- a/mocks/handlers.ts +++ b/mocks/handlers.ts @@ -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: {}, + }, }, }) ) diff --git a/package.json b/package.json index c294b403..9db5ce72 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": "yarn@1.22.19" } diff --git a/packages/chakra-components/package.json b/packages/chakra-components/package.json index 0b144af8..d274a063 100644 --- a/packages/chakra-components/package.json +++ b/packages/chakra-components/package.json @@ -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", @@ -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", diff --git a/packages/chakra-components/src/components/Election/Questions.tsx b/packages/chakra-components/src/components/Election/Questions.tsx index d461e643..938d6a6d 100644 --- a/packages/chakra-components/src/components/Election/Questions.tsx +++ b/packages/chakra-components/src/components/Election/Questions.tsx @@ -79,9 +79,6 @@ export const ElectionQuestions = (props: ElectionQuestionsProps) => { throw new Error('Unknown or invalid election type') } - // console.log(results) - // return - return bvote(results) } @@ -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) => ( - {a}
@@ -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 ( @@ -286,29 +287,31 @@ const MultiChoice = ({ index, question }: QuestionProps) => { }, }} name={index} - render={({ field }) => - choices.map((choice, ck) => ( - { - 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} - - )) - } + render={({ field }) => ( + <> + {choices.map((choice, ck) => ( + { + 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} + + ))} + + )} /> {errors[index]?.message as string} diff --git a/packages/chakra-components/src/components/Election/Results.tsx b/packages/chakra-components/src/components/Election/Results.tsx index 70d537cf..f048e339 100644 --- a/packages/chakra-components/src/components/Election/Results.tsx +++ b/packages/chakra-components/src/components/Election/Results.tsx @@ -6,6 +6,7 @@ import { ElectionResultsTypeNames, ElectionStatus, IChoice, + IQuestion, InvalidElection, PublishedElection, formatUnits, @@ -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 ( - {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 ( @@ -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) }