Skip to content

Commit

Permalink
fix: require at least one answer (#5718)
Browse files Browse the repository at this point in the history
* fix: adjust logic to automatically abstain

* fix: PR fixes

---------

Co-authored-by: Tuditi <45079109+Tuditi@users.noreply.github.com>
Co-authored-by: Mark Nardi <mark.nardi@iota.org>
  • Loading branch information
3 people committed Feb 1, 2023
1 parent 3d42057 commit b0b5a28
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
Expand Up @@ -20,14 +20,15 @@
import { governanceRouter } from '@core/router/routers'
import { selectedAccount, selectedAccountIndex } from '@core/account/stores'
import { getVotingEvent } from '@contexts/governance/actions'
import { ABSTAIN_VOTE_VALUE } from '@contexts/governance/constants'
import { ProposalStatus } from '@contexts/governance/enums'
import {
participationOverview,
proposalsState,
selectedProposal,
updateParticipationOverview,
} from '@contexts/governance/stores'
import { calculateWeightedVotes, getActiveParticipation } from '@contexts/governance/utils'
import { calculateWeightedVotes, getActiveParticipation, isProposalVotable } from '@contexts/governance/utils'
import { getBestTimeDuration, milestoneToDate } from '@core/utils'
import { networkStatus } from '@core/network/stores'
import { formatTokenAmountBestMatch } from '@core/wallet/utils'
Expand Down Expand Up @@ -59,19 +60,24 @@
$: if (questions?.length > 0 && selectedAnswerValues?.length === 0) {
selectedAnswerValues =
getActiveParticipation($selectedProposal?.id)?.answers ?? Array<number>(questions?.length)
getActiveParticipation($selectedProposal?.id)?.answers ?? Array.from({ length: questions?.length })
}
$: isVotingDisabled =
proposalState?.status === ProposalStatus.Upcoming ||
proposalState?.status === ProposalStatus.Ended ||
selectedAnswerValues?.length === 0 ||
selectedAnswerValues?.includes(undefined) ||
!hasChangedAnswers(selectedAnswerValues)
!isProposalVotable(proposalState?.status) ||
!hasChangedAnswers(selectedAnswerValues) ||
hasSelectedNoAnswers(selectedAnswerValues)
$: isTransferring = $selectedAccount?.isTransferring
$: proposalState, (textHintString = getTextHintString())
function hasSelectedNoAnswers(_selectedAnswerValues: number[]): boolean {
return (
_selectedAnswerValues.length === 0 ||
_selectedAnswerValues.every((answerValue) => answerValue === undefined)
)
}
function hasChangedAnswers(_selectedAnswerValues: number[]): boolean {
const activeParticipationAnswerValues = getActiveParticipation($selectedProposal?.id)?.answers
if (activeParticipationAnswerValues) {
Expand All @@ -87,7 +93,7 @@
* NOTE: If the user hasn't voted for the participation yet, the user has not changed (all) answers
* yet until every value is not undefined.
*/
return _selectedAnswerValues.every((selectedAnswerValue) => selectedAnswerValue !== undefined)
return _selectedAnswerValues.some((selectedAnswerValue) => selectedAnswerValue !== undefined)
}
}
Expand Down Expand Up @@ -141,9 +147,12 @@
}
function handleVoteClick(): void {
const chosenAnswerValues = selectedAnswerValues.map((answerValue) =>
answerValue === undefined ? ABSTAIN_VOTE_VALUE : answerValue
)
openPopup({
type: 'voteForProposal',
props: { selectedAnswerValues },
props: { selectedAnswerValues: chosenAnswerValues },
})
}
Expand Down
17 changes: 14 additions & 3 deletions packages/shared/components/popups/VoteForProposalPopup.svelte
@@ -1,15 +1,16 @@
<script lang="ts">
import { Button, Text, FontWeight, TextHint, TextType, KeyValueBox } from 'shared/components'
import { HTMLButtonType } from 'shared/components/enums'
import { closePopup } from '@auxiliary/popup/actions'
import { selectedAccount } from '@core/account/stores'
import { localize } from '@core/i18n'
import { BASE_TOKEN } from '@core/network/constants'
import { activeProfile } from '@core/profile/stores'
import { checkActiveProfileAuth } from '@core/profile/actions'
import { activeProfile } from '@core/profile/stores'
import { formatTokenAmountBestMatch } from '@core/wallet/utils'
import { selectedProposal } from '@contexts/governance/stores'
import { closePopup } from '@auxiliary/popup/actions'
import { vote } from '@contexts/governance/actions'
import { ABSTAIN_VOTE_VALUE } from '@contexts/governance/constants'
import { selectedProposal } from '@contexts/governance/stores'
export let selectedAnswerValues: number[]
Expand All @@ -21,6 +22,9 @@
$: isTransferring = $selectedAccount?.isTransferring
$: numberOfAbstainedQuestions =
selectedAnswerValues?.filter((answerValue) => answerValue === ABSTAIN_VOTE_VALUE).length ?? 0
async function handleSubmit(): Promise<void> {
await checkActiveProfileAuth(async () => {
await vote($selectedProposal?.id, selectedAnswerValues)
Expand Down Expand Up @@ -48,6 +52,13 @@
<KeyValueBox keyText={localize('popups.voteForProposal.key')} valueText={formattedVotingPower} />
{#if !hasVotingPower}
<TextHint danger text={localize('popups.voteForProposal.noVotingPower')} />
{:else if numberOfAbstainedQuestions > 0}
<TextHint
warning
text={localize('popups.voteForProposal.hasAbstained', {
values: { numberOfQuestions: numberOfAbstainedQuestions },
})}
/>
{/if}
</div>
<popup-buttons class="flex flex-row flex-nowrap w-full space-x-4">
Expand Down
1 change: 1 addition & 0 deletions packages/shared/lib/contexts/governance/utils/index.ts
Expand Up @@ -15,6 +15,7 @@ export * from './isAnyAccountVotingForSelectedProposal'
export * from './isParticipationOutput'
export * from './isProposalActive'
export * from './isProposalAlreadyAdded'
export * from './isProposalVotable'
export * from './isSelectedAccountVoting'
export * from './isValidProposalId'
export * from './isVisibleProposal'
Expand Down
13 changes: 13 additions & 0 deletions packages/shared/lib/contexts/governance/utils/isProposalVotable.ts
@@ -0,0 +1,13 @@
import { ProposalStatus } from '../enums'

export function isProposalVotable(status: ProposalStatus): boolean {
switch (status) {
case ProposalStatus.Commencing:
case ProposalStatus.Holding:
return true
case ProposalStatus.Upcoming:
case ProposalStatus.Ended:
default:
return false
}
}
1 change: 1 addition & 0 deletions packages/shared/locales/en.json
Expand Up @@ -1029,6 +1029,7 @@
"title": "Vote for proposal",
"body": "You're about to vote for {proposal} proposal.",
"key": "Your voting power",
"hasAbstained": "You are abstaining from answering {numberOfQuestions, plural, one {# question} other {# questions}}.",
"noVotingPower": "You do not have any voting power. Please increase your voting power from the Governance dashboard."
},
"revote": {
Expand Down

0 comments on commit b0b5a28

Please sign in to comment.