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

fix: correct proposal state store #5474

Merged
merged 3 commits into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,4 +1,5 @@
<script lang="typescript">
import { VotingEventPayload, ParticipationEventType } from '@iota/wallet/out/types'
import { localize } from '@core/i18n'
import {
Button,
Expand All @@ -13,23 +14,23 @@
TextType,
} from '@ui'
import { Icon as IconEnum } from '@auxiliary/icon'
import { openPopup } from '@auxiliary/popup'
import { activeProfileId } from '@core/profile/stores'
import { networkStatus } from '@core/network/stores'
import { getVotingEvent } from '@core/profile-manager'
import { governanceRouter } from '@core/router'
import { getParticipationOverview, selectedAccount } from '@core/account'
import { VotingEventPayload, ParticipationEventType } from '@iota/wallet/out/types'
import type { IParticipations } from '@contexts/governance/interfaces'
import { openPopup } from '@auxiliary/popup'
import { ProposalStatus } from '@contexts/governance/enums'
import { proposalsState, selectedProposal } from '@contexts/governance/stores'
import { networkStatus } from '@core/network/stores'

let selectedIndices: number[] = []
let votingPayload: VotingEventPayload
let totalVotes = 0

$: void setVotingEventPayload($selectedProposal?.id)
$: void setTotalVotes()
$: proposalStatus = $proposalsState[$selectedProposal?.id]?.status
$: proposalStatus = $proposalsState[$activeProfileId]?.[$selectedProposal?.id]?.status

$: votesCounter = {
total: totalVotes,
Expand Down
12 changes: 6 additions & 6 deletions packages/shared/components/organisms/ProposalCard.svelte
@@ -1,18 +1,18 @@
<script lang="typescript">
import { ProposalStatusInfo, Text, TooltipIcon } from 'shared/components'
import { Icon } from '@auxiliary/icon/enums'
import { localize } from '@core/i18n'
import { activeProfileId } from '@core/profile/stores'
import { GovernanceRoute, governanceRouter } from '@core/router'
import { IProposal } from '@contexts/governance/interfaces'
import { selectedProposal } from '@contexts/governance/stores'
import { selectedProposal, proposalsState } from '@contexts/governance/stores'
import { ProposalStatus } from '@contexts/governance/enums'
import { GovernanceRoute, governanceRouter } from '@core/router'

import { FontWeight, Position } from '../enums'
import { Icon } from '@auxiliary/icon/enums'
import { localize } from '@core/i18n'
import { proposalsState } from '@contexts/governance/stores'

export let proposal: IProposal

$: proposalState = $proposalsState[proposal?.id]
$: proposalState = $proposalsState[$activeProfileId]?.[proposal?.id]

function handleProposalClick(): void {
$selectedProposal = proposal
Expand Down
@@ -1,3 +1,4 @@
export * from './organization.interface'
export * from './proposal.interface'
export * from './participation.interface'
export * from './proposal-state.interface'
@@ -0,0 +1,7 @@
import type { EventStatus } from '@iota/wallet'

export interface IProposalState {
Tuditi marked this conversation as resolved.
Show resolved Hide resolved
[profileId: string]: {
[eventId: string]: EventStatus
}
}
@@ -1,34 +1,42 @@
import { get } from 'svelte/store'
import type { EventStatus } from '@iota/wallet'
import { getVotingProposalState } from '@core/profile-manager/api'
import { activeProfileId } from '@core/profile/stores'
import { persistent } from '@core/utils/store'
import { IProposalState } from '../interfaces'
Comment on lines 1 to +5
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you mind cleaning the imports here too 🙏


export const proposalsState = persistent<{ [key in string]: EventStatus }>('proposalsState', {})
export const proposalsState = persistent<IProposalState>('proposalsState', {})

export async function addProposalState(eventId: string): Promise<void> {
const profileId = get(activeProfileId)
const _proposalsState = get(proposalsState)
const hasRegisteredProposalStatus = Object.keys(_proposalsState).includes(eventId)
if (!hasRegisteredProposalStatus) {
const proposalStatus = await getVotingProposalState(eventId)
_proposalsState[eventId] = proposalStatus
proposalsState.set(_proposalsState)

const votingProposalState = await getVotingProposalState(eventId)

if (!_proposalsState[profileId]) {
_proposalsState[profileId] = {}
}

_proposalsState[profileId][eventId] = votingProposalState
proposalsState.set(_proposalsState)
}

export function removeProposalState(eventId: string): void {
const profileId = get(activeProfileId)
const _proposalsState = get(proposalsState)
const hasRegisteredProposalStatus = Object.keys(_proposalsState).includes(eventId)
if (hasRegisteredProposalStatus) {
delete _proposalsState[eventId]

if (_proposalsState[profileId]?.[eventId]) {
delete _proposalsState[profileId][eventId]
proposalsState.set(_proposalsState)
}
}

export async function updateProposalsState(): Promise<void> {
const profileId = get(activeProfileId)
const _proposalsState = get(proposalsState)
for (const eventId in _proposalsState) {
const proposalStatus = await getVotingProposalState(eventId)
_proposalsState[eventId] = proposalStatus

for (const eventId of Object.keys(_proposalsState[profileId] ?? {})) {
const votingProposalState = await getVotingProposalState(eventId)
_proposalsState[profileId][eventId] = votingProposalState
}
proposalsState.set(_proposalsState)
}