Skip to content

Commit

Permalink
Merge branch 'f/implement-multiprocess' into d/erc-paritary
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Oct 17, 2024
2 parents 02c95a3 + 4b32a38 commit f6462c3
Show file tree
Hide file tree
Showing 4 changed files with 952 additions and 790 deletions.
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"@emotion/styled": "^11.10.6",
"@rainbow-me/rainbowkit": "^2.1.2",
"@tanstack/react-query": "^5.40.1",
"@vocdoni/chakra-components": "./vocdoni-chakra-components-v0.9.5-2.tgz",
"@vocdoni/sdk": "~0.9.0",
"@vocdoni/chakra-components": "~0.8.3",
"@vocdoni/sdk": "~0.8.1",
"date-fns": "^3.6.0",
"ethers": "^5.7.2",
"framer-motion": "^11.2.10",
Expand All @@ -27,7 +27,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.51.5",
"react-i18next": "^14.1.2",
"react-i18next": "^15.0.3",
"react-icons": "^5.2.1",
"react-markdown": "^9.0.1",
"react-player": "^2.16.0",
Expand All @@ -48,8 +48,5 @@
"typescript": "^5.0.2",
"vite": "^5.2.13",
"vite-tsconfig-paths": "^4.0.7"
},
"resolutions": {
"@vocdoni/chakra-components": "./vocdoni-chakra-components-v0.9.5-1.tgz"
}
}
14 changes: 8 additions & 6 deletions src/components/Process/Aside.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ const ProcessAside = () => {
} = useElection()
const { isConnected } = useAccount()
const { env, clear } = useClient()

if (!election || !(election instanceof PublishedElection)) return null

const census: CensusMeta = dotobject(election?.meta || {}, 'census')

const isMultiProcess = !!election.get('multiprocess')
const renderVoteMenu =
voted ||
(voting && election?.electionType.anonymous) ||
(hasOverwriteEnabled(election) && isInCensus && votesLeft > 0 && voted)
!isMultiProcess &&
(voted ||
(voting && election?.electionType.anonymous) ||
(hasOverwriteEnabled(election) && isInCensus && votesLeft > 0 && voted))

const showVoters =
election?.status !== ElectionStatus.CANCELED &&
Expand Down Expand Up @@ -203,8 +205,8 @@ const ProcessAside = () => {
)
}

type VoteButtonProps = { isMultiElection?: boolean } & FlexProps
export const VoteButton = ({ isMultiElection = false, ...props }: VoteButtonProps) => {
type VoteButtonProps = FlexProps
export const VoteButton = (props: VoteButtonProps) => {
const { t } = useTranslation()
const { election, connected, isAbleToVote, isInCensus } = useElection()
const { isConnected } = useAccount()
Expand Down
75 changes: 50 additions & 25 deletions src/components/Process/Chained.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { Box, Progress } from '@chakra-ui/react'
import { ConnectButton } from '@rainbow-me/rainbowkit'
import {
ElectionQuestions,
ElectionQuestionsForm,
ElectionResults,
MultiElectionQuestionsForm,
MultiElectionsProvider,
RenderWith,
sameLengthValidator,
SpreadsheetAccess,
SubmitFormValidation,
QuestionsFormProvider,
} from '@vocdoni/chakra-components'
import { ElectionProvider, useElection } from '@vocdoni/react-providers'
import { InvalidElection, IVotePackage, PublishedElection, VocdoniSDKClient } from '@vocdoni/sdk'
Expand Down Expand Up @@ -78,7 +75,7 @@ const ChainedProcessesInner = ({ connected }: ChainedProcessesInnerProps) => {
if (!current || processes[current] instanceof InvalidElection) return
const currentElection = processes[current]
const meta = currentElection.get('multiprocess')
if (meta.renderWith) {
if (meta && meta.renderWith) {
setRenderWith(meta.renderWith)
}
}, [current, processes])
Expand All @@ -93,26 +90,27 @@ const ChainedProcessesInner = ({ connected }: ChainedProcessesInnerProps) => {
}

if (isRenderWith) {
const formValidation: SubmitFormValidation = (values) => {
if (!sameLengthValidator(values)) {
return t('errors.all_ballots_must_have_same_length')
}
return true
}
// const formValidation: SubmitFormValidation = (values) => {
// if (!sameLengthValidator(values)) {
// return t('errors.all_ballots_must_have_same_length')
// }
// return true
// }
return (
<MultiElectionsProvider renderWith={[{ id: current }, ...renderWith]} rootClient={client}>
<MultiElectionQuestionsForm validate={formValidation} ConnectButton={ConnectButton} />
<QuestionsFormProvider renderWith={renderWith}>

Check failure on line 100 in src/components/Process/Chained.tsx

View workflow job for this annotation

GitHub Actions / build-stg

Type '{ children: Element[]; renderWith: RenderWith[]; }' is not assignable to type 'IntrinsicAttributes & QuestionsFormProviderProps & { children?: ReactNode; }'.
<ElectionQuestionsForm />
<VoteButtonContainer>
<VoteButton isMultiElection={true} />
<VoteButton />
</VoteButtonContainer>
</MultiElectionsProvider>
</QuestionsFormProvider>
)
}

return (
<>
<ElectionQuestions
confirmContents={(election, answers) => <ConfirmVoteModal election={election} answers={answers} />}
// renderWith={renderWith}
// confirmContents={(election, answers) => <ConfirmVoteModal election={election} answers={answers} />}
/>
<VoteButtonContainer>
<VoteButton />
Expand Down Expand Up @@ -263,12 +261,16 @@ const getProcessIdsInFlowStep = (meta: FlowNode) => {
ids.push(meta.default)
}

if (!meta.conditions) {
return ids
if (meta.renderWith) {
for (const renderWith of meta.renderWith) {
ids.push(renderWith.id)
}
}

for (const condition of meta.conditions) {
ids.push(condition.goto)
if (meta.conditions) {
for (const condition of meta.conditions) {
ids.push(condition.goto)
}
}

return ids
Expand All @@ -288,7 +290,7 @@ export const getAllProcessesInFlow = async (
processes[id] = election

const meta = election.get('multiprocess')
if (meta && meta.default && !visited.has(meta.default)) {
if (meta && (meta.default || meta.renderWith) && !visited.has(meta.default)) {
const idsToFetch = getProcessIdsInFlowStep(meta)
for (const nextId of idsToFetch) {
await loadProcess(nextId)
Expand All @@ -304,6 +306,16 @@ export const getAllProcessesInFlow = async (
}
}

// Add renderWith processes
if (meta.renderWith) {
for (const renderWithElection of meta.renderWith as RenderWith[]) {
if (!visited.has(renderWithElection.id)) {
visited.add(renderWithElection.id)
ids.push(renderWithElection.id)
}
}
}

// Add defaults after conditions
if (!visited.has(meta.default)) {
visited.add(meta.default)
Expand All @@ -318,6 +330,7 @@ export const getAllProcessesInFlow = async (
if (meta) {
initialIds.push(...getProcessIdsInFlowStep(meta))
}

for (const id of initialIds) {
await loadProcess(id)
}
Expand All @@ -334,6 +347,18 @@ type FlowCondition = {
}

// FlowNode can have or conditions or renderWith, but not both
type FlowNode = {
default: string
} & ({ conditions?: FlowCondition[]; renderWith?: never } | { conditions?: never; renderWith: RenderWith[] })
export type FlowNode =
| {
conditions?: FlowCondition[]
renderWith?: never
default: string
}
| {
conditions?: never
renderWith: RenderWith[]
default?: string // Default is optional for renderWith elections
}

export type RenderWith = {
id: string
}
Loading

0 comments on commit f6462c3

Please sign in to comment.