Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to accept multiquestion #204

Open
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

selankon
Copy link
Collaborator

@selankon selankon commented Oct 10, 2024

This PR enhances Chakra UI components to support multi-process pages. No breaking changes have been introduced; all components function as before. The QuestionsFormProvider is now extended to support multiple elections.

This changes are used by vocdoni/ui-voting-page#10.

MultiProcess pages

UI components support rendering multiple elections on the same page. To enable multi-election support, wrap your election components as shown below:

import {
  ElectionQuestionsForm,
  QuestionsFormProvider,
  RenderWith,
  VoteButton,
} from '@vocdoni/chakra-components'
import { ElectionProvider } from '@vocdoni/react-providers'

//....

const renderWith: RenderWith[] = {
  { id: "electionId1" },
  { id: "electionId2" },
}

<ElectionProvider id={rootElectionId}>
  <QuestionsFormProvider renderWith={renderWith}>
    <ElectionQuestionsForm />
    <VoteButton />
  </QuestionsFormProvider>
</ElectionProvider>

The renderWith option in QuestionsFormProvider is essential for rendering multiple elections, it includes the ids of the elections to be rendered excluding the root election id.

Within the QuestionsFormProvider, you can use the useQuestionsForm hook to access the state of all elections along with other useful data:

voting,        // Indicates if any election is currently voting
voteAll,       // Function to vote for all elections
rootClient,    // Client of the root election
elections,     // State of each election
addElection,   // Function to add an election to the context
isAbleToVote,  // Indicates if voting is enabled for any election
voted,         // Indicates if any election has already been voted
isDisabled,    // Indicates if the form is manually disabled
setIsDisabled, // Function to manually enable or disable the form

Key considerations

  • Place VoteButton inside the QuestionsFormProvider to ensure it properly reflects the election states across multiple elections. If VoteButton is outside this provider, it will only reflect the state of the root election (isAbleToVote, voted, voting, etc.).

Other inner changes consierations

  • VoteButton logic is splitted to be reused for the MultiprocessVoteButton
  • Also exported the logic that calculates the Vote Ballot

Relevant changes on Questions form:

  • The questions form now stores the answers inside a Record<string, FieldValues> type, where string is the election id. On this way, we can have multiple elections/questions on the same form.
  • This last point breaks compatibility with the Confirmation modals, that must be adapted.
  • If the user wants, could set the formId. Useful to use specific id non related to the root election (now the form id contains the root election id)
  • It can enable and disable the form programatically.
  • A custom onSubmit function can be added. This function is thought to programatically submit the form. For example, submiting the form with a default options when is disabled.
  • A global validation function can be added. This function is thought to be called before form submitting, in order to enable custom validations. A use case for example is to check that two elections contains the same number of choices.
  • Some styles must change with the package update because the form tag is not inside of the chakra component anymore
  • Voted component is modified to accept multiple nullifiers.

Copy link
Member

@elboletaire elboletaire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the wrongly imported components (using the package name instead of relative routes) could explain the context issues you told me about.

Edit: also, the tests should be passing 🥲

@selankon selankon force-pushed the f/implement-multiprocess-questions branch from a3bfe99 to ef03ef0 Compare October 17, 2024 06:56
@elboletaire elboletaire marked this pull request as draft October 18, 2024 07:41
@elboletaire
Copy link
Member

Set as draft, since it's a WIP

@selankon selankon force-pushed the f/implement-multiprocess-questions branch from b89b121 to 2c09335 Compare October 22, 2024 10:21
@selankon selankon force-pushed the f/implement-multiprocess-questions branch from 577884e to 32181c9 Compare October 24, 2024 09:02
@selankon selankon marked this pull request as ready for review October 25, 2024 07:12
@selankon selankon force-pushed the f/implement-multiprocess-questions branch from df32b57 to c33691e Compare October 29, 2024 11:19
@selankon selankon force-pushed the f/implement-multiprocess-questions branch from c33691e to ee98753 Compare October 30, 2024 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants