Skip to content

Commit

Permalink
fix: voting with same answers (#5674)
Browse files Browse the repository at this point in the history
* fix: add simple fix

* fix: allow switching single answer

* chore: revert line

* chore: adjust logic and reduce code

* fix: adjust logic if not voted yet

* chore: add explanative comments

Co-authored-by: Mark Nardi <mark.nardi@iota.org>
Co-authored-by: MarkNerdi <105642810+MarkNerdi@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 27, 2023
1 parent 0d3300f commit 6ff3705
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
Expand Up @@ -26,7 +26,7 @@
selectedProposal,
updateParticipationOverview,
} from '@contexts/governance/stores'
import { calculateWeightedVotes } from '@contexts/governance/utils'
import { calculateWeightedVotes, getActiveParticipation } 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 @@ -57,17 +57,39 @@
$: questions = votingPayload?.questions
$: if (questions?.length > 0 && selectedAnswerValues?.length === 0) {
selectedAnswerValues = Array<number>(questions?.length)
selectedAnswerValues =
getActiveParticipation($selectedProposal?.id)?.answers ?? Array<number>(questions?.length)
}
$: isVotingDisabled =
proposalState?.status === ProposalStatus.Upcoming ||
proposalState?.status === ProposalStatus.Ended ||
selectedAnswerValues?.length === 0 ||
selectedAnswerValues?.includes(undefined)
selectedAnswerValues?.includes(undefined) ||
!hasChangedAnswers(selectedAnswerValues)
$: isTransferring = $selectedAccount?.isTransferring
$: proposalState, (voteButtonText = getVoteButtonText())
function hasChangedAnswers(_selectedAnswerValues: number[]): boolean {
const activeParticipationAnswerValues = getActiveParticipation($selectedProposal?.id)?.answers
if (activeParticipationAnswerValues) {
/**
* NOTE: If any of the values between what's active and selected differ, it means
* that the user has changed at least one answer.
*/
return _selectedAnswerValues.some(
(selectedAnswerValue, idx) => selectedAnswerValue !== activeParticipationAnswerValues[idx]
)
} else {
/**
* 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)
}
}
async function setVotingEventPayload(eventId: string): Promise<void> {
const event = await getVotingEvent(eventId)
if (event?.data?.payload?.type === ParticipationEventType.Voting) {
Expand Down Expand Up @@ -103,7 +125,14 @@
function handleQuestionClick(event: CustomEvent): void {
const { questionIndex } = event.detail
openedQuestionIndex = openedQuestionIndex === questionIndex ? null : questionIndex
openedQuestionIndex = questionIndex
const selectedQuestionElement: HTMLElement = proposalQuestions?.querySelector(
'proposal-question:nth-child(' + openedQuestionIndex + ')'
)
setTimeout(() => {
proposalQuestions.scrollTo({ top: selectedQuestionElement?.offsetTop, behavior: 'smooth' })
}, 250)
}
function handleCancelClick(): void {
Expand All @@ -119,19 +148,7 @@
function handleAnswerClick(event: CustomEvent): void {
const { answerValue, questionIndex } = event.detail
if (selectedAnswerValues[questionIndex] === answerValue) {
selectedAnswerValues[questionIndex] = null
} else {
selectedAnswerValues[questionIndex] = answerValue
}
openedQuestionIndex = selectedAnswerValues.length === questionIndex + 1 ? null : questionIndex + 1
const selectedQuestionElement: HTMLElement = proposalQuestions?.querySelector(
'proposal-question:nth-child(' + (openedQuestionIndex + 1) + ')'
)
setTimeout(() => {
proposalQuestions.scrollTo({ top: selectedQuestionElement?.offsetTop, behavior: 'smooth' })
}, 250)
selectedAnswerValues[questionIndex] = answerValue
}
function getVoteButtonText(): string {
Expand Down
@@ -0,0 +1,13 @@
import { get } from 'svelte/store'

import { EventId, TrackedParticipationOverview } from '@iota/wallet'

import { participationOverview } from '../stores'

export function getActiveParticipation(proposalId: EventId): TrackedParticipationOverview {
const overview = get(participationOverview)
const participationsForProposal: TrackedParticipationOverview[] = Object.values(
overview?.participations?.[proposalId] ?? {}
)
return participationsForProposal.find((participation) => participation?.endMilestoneIndex === 0)
}
1 change: 1 addition & 0 deletions packages/shared/lib/contexts/governance/utils/index.ts
@@ -1,5 +1,6 @@
export * from './calculateWeightedVotes'
export * from './createProposalsFromEvents'
export * from './getActiveParticipation'
export * from './getNumberOfActiveProposals'
export * from './getNumberOfTotalProposals'
export * from './getNumberOfVotedProposals'
Expand Down

0 comments on commit 6ff3705

Please sign in to comment.