-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use state machine to update vote choice
- Loading branch information
Showing
8 changed files
with
159 additions
and
104 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
app/components/views/ProposalDetails/ChooseVoteOption.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { FormattedMessage as T } from "react-intl"; | ||
import { PassphraseModalButton } from "buttons"; | ||
import { fetchMachine } from "stateMachines/FetchStateMachine"; | ||
import { useMachine } from "@xstate/react"; | ||
import { StakeyBounceXs } from "indicators"; | ||
import { useDispatch } from "react-redux"; | ||
import { useState } from "react"; | ||
import { ProposalError } from "./helpers" | ||
import * as gov from "actions/GovernanceActions"; | ||
|
||
const VoteOption = ({ value, description, onClick, checked }) => ( | ||
<div className="proposal-vote-option"> | ||
<input className={value} type="radio" id={value} name="proposalVoteChoice" | ||
readOnly={!onClick} onChange={onClick} | ||
value={value} | ||
checked ={checked} | ||
/> | ||
<label className={"radio-label " + value} htmlFor={value}/>{description} | ||
</div> | ||
); | ||
|
||
function UpdateVoteChoiceModalButton({ onSubmit, newVoteChoice, eligibleTicketCount }) { | ||
return ( | ||
<PassphraseModalButton | ||
modalTitle={ | ||
<> | ||
<T id="proposals.updateVoteChoiceModal.title" m="Confirm Your Vote" /> | ||
<div className="proposal-vote-confirmation"> | ||
<div className={newVoteChoice+"-proposal"}/> | ||
{newVoteChoice} | ||
</div> | ||
</> | ||
} | ||
modalDescription={ | ||
<T | ||
id="proposalDetails.votingInfo.eligibleCount" | ||
m="You have {count, plural, one {one ticket} other {# tickets}} eligible for voting" | ||
values={{ count: eligibleTicketCount }} | ||
/> | ||
} | ||
disabled={!newVoteChoice} | ||
onSubmit={onSubmit} | ||
buttonLabel={<T id="proposals.updateVoteChoiceModal.btnLabel" m="Cast Vote" />} | ||
/> | ||
)}; | ||
|
||
function getError(error) { | ||
if (!error) return; | ||
if (typeof error === "string") return error; | ||
if (typeof error === "object") { | ||
if (error.message) return error.message; | ||
return JSON.stringify(error); | ||
} | ||
} | ||
|
||
function ChooseVoteOption({ | ||
viewedProposalDetails, voteOptions, currentVoteChoice, votingComplete, eligibleTicketCount | ||
}) { | ||
const [ newVoteChoice, setVoteOption ] = useState(null); | ||
|
||
const dispatch = useDispatch(); | ||
const onUpdateVoteChoice = (privatePassphrase) => dispatch( | ||
gov.updateVoteChoice(viewedProposalDetails, newVoteChoice, privatePassphrase) | ||
); | ||
const [ state, send ] = useMachine(fetchMachine, { | ||
actions: { | ||
initial: () => ({}), | ||
load: (context, event) => { | ||
const { privatePassphrase } = event; | ||
if(!newVoteChoice) return; | ||
onUpdateVoteChoice(privatePassphrase) | ||
.then(() => send("RESOLVE")) | ||
.catch(error => send({ type: "REJECT", error })); | ||
} | ||
} | ||
}); | ||
|
||
const error = state && state.context && getError(state.context.error); | ||
const ChooseOptions = () => ( | ||
<> | ||
<div className="proposal-details-voting-preference"> | ||
<div className="proposal-details-voting-preference-title"><T id="proposalDetails.votingInfo.votingPreferenceTitle" m="My Voting Preference" /></div> | ||
<div className="proposal-details-current-choice-box"> | ||
{ voteOptions.map(o => { | ||
return <VoteOption | ||
value={o.id} key={o.id} | ||
description={o.id.charAt(0).toUpperCase()+o.id.slice(1)} | ||
onClick={ () => currentVoteChoice === "abstain" && setVoteOption(o.id) } | ||
checked={ newVoteChoice ? newVoteChoice === o.id : currentVoteChoice !== "abstain" ? currentVoteChoice.id === o.id : null } | ||
/> | ||
})} | ||
</div> | ||
</div> | ||
{ !votingComplete && | ||
<UpdateVoteChoiceModalButton {...{ | ||
newVoteChoice, onSubmit: (privatePassphrase) => send({type: "FETCH", privatePassphrase }), eligibleTicketCount | ||
}} /> | ||
} | ||
</> | ||
); | ||
|
||
switch (state.value) { | ||
case "idle": | ||
return <ChooseOptions {...{ | ||
setVoteOption, newVoteChoice, eligibleTicketCount, currentVoteChoice, | ||
voteOptions, votingComplete | ||
}} />; | ||
case "loading": | ||
return ( | ||
<div className="proposal-details-updating-vote-choice"> | ||
<StakeyBounceXs /> | ||
<T id="proposalDetails.votingInfo.updatingVoteChoice" m="Updating vote choice" />... | ||
</div> | ||
); | ||
case "success": | ||
return <ChooseOptions {...{ | ||
setVoteOption, newVoteChoice, eligibleTicketCount, currentVoteChoice, | ||
voteOptions, votingComplete | ||
}} />; | ||
case "failure": | ||
return <ProposalError {...{ error }} />; | ||
} | ||
} | ||
|
||
export default ChooseVoteOption; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
app/components/views/ProposalDetails/UpdateVoteChoiceModalButton.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters