Skip to content

Commit

Permalink
Implement blank vote form
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Oct 24, 2024
1 parent 64bc380 commit 26e1e75
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/components/Process/Chained.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Box, Progress, useBreakpointValue } from '@chakra-ui/react'
import { ConnectButton } from '@rainbow-me/rainbowkit'
import {
ElectionQuestions,
ElectionQuestionsForm,
ElectionResults,
QuestionsFormProvider,
SpreadsheetAccess,
Expand All @@ -14,7 +13,7 @@ import { Trans } from 'react-i18next'
import { VoteButton } from '~components/Process/Aside'
import BlindCSPConnect from '~components/Process/BlindCSPConnect'
import { ChainedProvider, useChainedProcesses } from './ChainedContext'
import { useFormValidation } from '~components/Process/ParitaryErc'
import { ParitaryErcQuestionsForm, useFormValidation } from '~components/Process/ParitaryErc'
import { SuccessVoteModal } from '~components/Process/SuccessVoteModal'
import VotingVoteModal from '~components/Process/VotingVoteModal'
import { ConfirmVoteModal } from '~components/Process/ConfirmVoteModal'
Expand Down Expand Up @@ -108,7 +107,7 @@ const ChainedProcessesInner = ({ connected }: ChainedProcessesInnerProps) => {
confirmContents={(elections, answers) => <ConfirmVoteModal elections={elections} answers={answers} />}
validate={formValidation}
>
<ElectionQuestionsForm />
<ParitaryErcQuestionsForm />
<VoteButtonContainer>
<VoteButton />
</VoteButtonContainer>
Expand Down
100 changes: 98 additions & 2 deletions src/components/Process/ParitaryErc.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { SubmitFormValidation } from '@vocdoni/chakra-components'
import { useToast } from '@chakra-ui/react'
import {
ElectionQuestionsForm,
ExtendedSubmitHandler,

Check failure on line 3 in src/components/Process/ParitaryErc.tsx

View workflow job for this annotation

GitHub Actions / build-stg

Module '"@vocdoni/chakra-components"' has no exported member 'ExtendedSubmitHandler'.
FormFieldValues,

Check failure on line 4 in src/components/Process/ParitaryErc.tsx

View workflow job for this annotation

GitHub Actions / build-stg

Module '"@vocdoni/chakra-components"' has no exported member 'FormFieldValues'.
Markdown,
SubmitFormValidation,
useQuestionsForm,
} from '@vocdoni/chakra-components'
import { Checkbox, useMultiStyleConfig, useToast, chakra, Stack } from '@chakra-ui/react'
import { useTranslation } from 'react-i18next'
import { useEffect, useMemo } from 'react'
import { useFormContext } from 'react-hook-form'

/**
* File to store paritary erc project specific code
Expand Down Expand Up @@ -37,3 +46,90 @@ export const sameLengthValidator: SubmitFormValidation = (answers) => {
}
return rest.every((ballot) => ballot[0].length === first[0].length)
}

const BlankVoteTitle = 'Vot en blanc'
type BlankChoiceStore = Record<string, string>

/**
* Implements specific logic for blank option.
*/
export const ParitaryErcQuestionsForm = () => {
const { elections, voteAll, isDisabled, setIsDisabled, isAbleToVote } = useQuestionsForm()

Check failure on line 57 in src/components/Process/ParitaryErc.tsx

View workflow job for this annotation

GitHub Actions / build-stg

Property 'isDisabled' does not exist on type 'QuestionsFormContextState'.

Check failure on line 57 in src/components/Process/ParitaryErc.tsx

View workflow job for this annotation

GitHub Actions / build-stg

Property 'setIsDisabled' does not exist on type 'QuestionsFormContextState'.

// Search which index contain blanc options (preventing unordered choices)
const blankOptions = useMemo(() => {
const blankOptions: BlankChoiceStore = {}
// Hardcode to check blankOption only when the number of elections is 2
if (!elections || (elections && Object.keys(elections).length !== 2)) return blankOptions
for (const { election } of Object.values(elections)) {
for (const question of election.questions) {
const blankChoice = question.choices.find((option) => option.title.default === BlankVoteTitle)
if (blankChoice) {
blankOptions[election.id] = blankChoice.value.toString()
}
}
}
return blankOptions
}, [elections])

const disableForm = () => {
setIsDisabled((prevState) => !prevState)

Check failure on line 76 in src/components/Process/ParitaryErc.tsx

View workflow job for this annotation

GitHub Actions / build-stg

Parameter 'prevState' implicitly has an 'any' type.
}

const onSubmit: ExtendedSubmitHandler<FormFieldValues> = (onSubmit, ...params) => {

Check failure on line 79 in src/components/Process/ParitaryErc.tsx

View workflow job for this annotation

GitHub Actions / build-stg

Parameter 'onSubmit' implicitly has an 'any' type.

Check failure on line 79 in src/components/Process/ParitaryErc.tsx

View workflow job for this annotation

GitHub Actions / build-stg

Rest parameter 'params' implicitly has an 'any[]' type.
if (!isAbleToVote) return
// If is disabled it will create a ballot with only blank options
if (isDisabled) {
const blankVotes = Object.entries(blankOptions).reduce((acc, [eId, option]) => {
acc[eId] = [[option]]
return acc
}, {} as FormFieldValues)
return voteAll(blankVotes)
}
return onSubmit(...params)
}

// Hide the en blanc options using display none
useEffect(() => {
// Find the element with the specific text content
const blankVoteElements = Array.from(document.querySelectorAll('span')).filter(
(el) => el.textContent === BlankVoteTitle
)
if (blankVoteElements) {
// Hide the element
for (const element of blankVoteElements) {
element.style.display = 'none'
}
}
}, [elections])
const styles = useMultiStyleConfig('ElectionQuestions')

return (
<>
<ElectionQuestionsForm onSubmit={onSubmit} />

Check failure on line 109 in src/components/Process/ParitaryErc.tsx

View workflow job for this annotation

GitHub Actions / build-stg

Type '{ onSubmit: ExtendedSubmitHandler<FormFieldValues>; }' is not assignable to type 'IntrinsicAttributes & ChakraProps & { onInvalid?: SubmitErrorHandler<FieldValues> | undefined; formId?: string | undefined; }'.
<chakra.div __css={styles.elections}>
<chakra.div __css={styles.wrapper}>
<div></div>
<chakra.div __css={styles.question}>
<chakra.div __css={styles.header}>
<chakra.label __css={styles.title}>{BlankVoteTitle}</chakra.label>
</chakra.div>
<chakra.div __css={styles.body}>
<chakra.div __css={styles.description}>
<Markdown>
Si vols votar en blanc, trobaràs l'opció al final del formulari, si es tria aquesta opció, no es
tindran en compte les opcions previament seleccionades
</Markdown>
</chakra.div>
<Stack sx={styles.stack}>
<Checkbox checked={isDisabled} onChange={disableForm} sx={styles.checkbox} isDisabled={!isAbleToVote}>
Vota en blanc a les dos llistes
</Checkbox>
</Stack>
</chakra.div>
</chakra.div>
</chakra.div>
</chakra.div>
</>
)
}

0 comments on commit 26e1e75

Please sign in to comment.