src/components/RadioPoll.js
/**
   * Handles the "submit" button being clicked. The submit
   * button is part of the Radio form and allows users to
   * confirm a previously selected choice.
   */
Handles the "submit" button being clicked. The submit
button is part of the Radio form and allows users to
confirm a previously selected choice.
Handles the " submit " button being clicked . The submit button is part of the Radio form and allows users to confirm a previously selected choice .
async handleSubmit(e) {
    e.preventDefault();
     Note: This operation has the potential to fail, and a more
     robust design would include error handling for that case.
    const votes = await addVotesToBackend(this.state.choices);
     calculate the total number of votes based on individual votes
     provided by the backend
    const totalVotes = Object.keys(votes).reduce((aggr, curr) => aggr + votes[curr], 0);
     calculate the winner based on backend votes
    const winningChoice = getWinningChoice(votes, totalVotes);
     update this components state to reflect the vote tallies
    this.setState((prevState) => {
      const newChoices = prevState.choices.map((choice) => {
        const extraInfo = {
          isWinner: choice.label === winningChoice,
          percentVote: (votes[choice.label] / totalVotes) * 100,
          votes: votes[choice.label],
        };
        return { ...choice, ...extraInfo }
      });
       set the nextState to be the prevState + global votes and a flag
       indicating that the current user has already voted
      return { ...prevState, choices: newChoices, votes, hasVoted: true };
    });
  }
async handleSubmit ( e ) { e . preventDefault ( ) ; const votes = await addVotesToBackend ( this . state . choices ) ; const totalVotes = Object . keys ( votes ) . reduce ( ( aggr , curr ) => aggr + votes [ curr ] , 0 ) ; const winningChoice = getWinningChoice ( votes , totalVotes ) ; this . setState ( ( prevState ) => { const newChoices = prevState . choices . map ( ( choice ) => { const extraInfo = { isWinner : choice . label === winningChoice , percentVote : ( votes [ choice . label ] / totalVotes ) * 100 , votes : votes [ choice . label ] , } ; return { ... choice , ... extraInfo } } ) ; return { ... prevState , choices : newChoices , votes , hasVoted : true } ; } ) ; }
Handles the "submit" button being clicked.
Handles the " submit " button being clicked .
// since we use 'await' it's absolutely crucial that we preventDefault // as the first call in this method // @BACKEND // update the backend with the users single vote, then store // the response as it represents the global vote state // // Note: This operation has the potential to fail, and a more // robust design would include error handling for that case. // calculate the total number of votes based on individual votes // provided by the backend // calculate the winner based on backend votes // update this components state to reflect the vote tallies // set the nextState to be the prevState + global votes and a flag // indicating that the current user has already voted