From d520cb3adb84c5e7aebc33db252c2643ad19f902 Mon Sep 17 00:00:00 2001 From: Tuditi <45079109+Tuditi@users.noreply.github.com> Date: Sat, 17 Dec 2022 01:35:33 +0100 Subject: [PATCH] fix: proposal status reactive display on register (#5437) * feat: adds proposal status polling logic * chore: change proposals status to proposals state * fix: reactively add/remove proposals * chore: renames * fix: add optional chaining * fix: add void to properly handle promise return Co-authored-by: Jean Ribeiro Co-authored-by: Matthew Maxwell <44885822+maxwellmattryan@users.noreply.github.com> --- .../governance/views/ProposalsView.svelte | 13 +++-- .../lib/core/governance/stores/index.ts | 1 + .../stores/registered-event-ids.store.ts | 3 ++ .../utils/createProposalsFromEvents.ts | 48 ++++++++++--------- .../api/deregisterParticipationEvent.ts | 8 ++-- .../profile-manager/api/getVotingProposals.ts | 2 +- .../api/registerParticipationEvent.ts | 4 +- 7 files changed, 48 insertions(+), 31 deletions(-) create mode 100644 packages/shared/lib/core/governance/stores/registered-event-ids.store.ts diff --git a/packages/desktop/views/dashboard/governance/views/ProposalsView.svelte b/packages/desktop/views/dashboard/governance/views/ProposalsView.svelte index c6fb0377cc..381c5c20fe 100644 --- a/packages/desktop/views/dashboard/governance/views/ProposalsView.svelte +++ b/packages/desktop/views/dashboard/governance/views/ProposalsView.svelte @@ -1,16 +1,21 @@ diff --git a/packages/shared/lib/core/governance/stores/index.ts b/packages/shared/lib/core/governance/stores/index.ts index 8a2efd8c37..a9a42b293c 100644 --- a/packages/shared/lib/core/governance/stores/index.ts +++ b/packages/shared/lib/core/governance/stores/index.ts @@ -1,2 +1,3 @@ export * from './proposals-state.store' +export * from './registered-event-ids.store' export * from './selected-proposal.store' diff --git a/packages/shared/lib/core/governance/stores/registered-event-ids.store.ts b/packages/shared/lib/core/governance/stores/registered-event-ids.store.ts new file mode 100644 index 0000000000..4bcbf50f07 --- /dev/null +++ b/packages/shared/lib/core/governance/stores/registered-event-ids.store.ts @@ -0,0 +1,3 @@ +import { writable } from 'svelte/store' + +export const registeredEventIds = writable([]) diff --git a/packages/shared/lib/core/governance/utils/createProposalsFromEvents.ts b/packages/shared/lib/core/governance/utils/createProposalsFromEvents.ts index f193d1ded5..60acde11dc 100644 --- a/packages/shared/lib/core/governance/utils/createProposalsFromEvents.ts +++ b/packages/shared/lib/core/governance/utils/createProposalsFromEvents.ts @@ -6,31 +6,35 @@ import { ProposalStatus } from '@core/governance/enums' import { IProposal } from '@core/governance/interfaces' import { nodeInfo } from '@core/network' import { activeProfile } from '@core/profile' +import { getVotingEvents } from '@core/profile-manager' -export function createProposalsFromEvents(events: Event[]): IProposal[] { - const proposals: IProposal[] = events.map(({ data, id }) => { - const proposal = { - id, - title: data.name, - status: ProposalStatus.Upcoming, - milestones: { - [ProposalStatus.Upcoming]: 0, // TODO: fix this - [ProposalStatus.Commencing]: data.milestoneIndexCommence, - [ProposalStatus.Holding]: data.milestoneIndexStart, - [ProposalStatus.Ended]: data.milestoneIndexEnd, - }, - // TODO: figure out a better way to get the node URLs - nodeUrls: get(activeProfile)?.clientOptions?.nodes, - } +export async function createProposals(): Promise { + const events = await getVotingEvents() + const proposals: IProposal[] = events?.map(createProposalFromEvent) + return proposals +} - const status = getLatestStatus(proposal) - if (status) { - proposal.status = status - } - return proposal - }) +function createProposalFromEvent(event: Event): IProposal { + const { data, id } = event + const proposal = { + id, + title: event.data.name, + status: ProposalStatus.Upcoming, + milestones: { + [ProposalStatus.Upcoming]: 0, // TODO: fix this + [ProposalStatus.Commencing]: data.milestoneIndexCommence, + [ProposalStatus.Holding]: data.milestoneIndexStart, + [ProposalStatus.Ended]: data.milestoneIndexEnd, + }, + // TODO: figure out a better way to get the node URLs + nodeUrls: get(activeProfile)?.clientOptions?.nodes, + } - return proposals + const status = getLatestStatus(proposal) + if (status) { + proposal.status = status + } + return proposal } function getLatestStatus(proposal: IProposal): ProposalStatus { diff --git a/packages/shared/lib/core/profile-manager/api/deregisterParticipationEvent.ts b/packages/shared/lib/core/profile-manager/api/deregisterParticipationEvent.ts index 3c8761e3a4..218a732b45 100644 --- a/packages/shared/lib/core/profile-manager/api/deregisterParticipationEvent.ts +++ b/packages/shared/lib/core/profile-manager/api/deregisterParticipationEvent.ts @@ -1,10 +1,12 @@ import { get } from 'svelte/store' import type { EventId } from '@iota/wallet' -import { removeProposalState } from '@core/governance' +import { registeredEventIds, removeProposalState } from '@core/governance' import { profileManager } from '../stores' -export function deregisterParticipationEvent(eventId: EventId): Promise { +export async function deregisterParticipationEvent(eventId: EventId): Promise { const manager = get(profileManager) + await manager.deregisterParticipationEvent(eventId) + + registeredEventIds.update((ids) => ids.filter((id) => id !== eventId)) removeProposalState(eventId) - return manager.deregisterParticipationEvent(eventId) } diff --git a/packages/shared/lib/core/profile-manager/api/getVotingProposals.ts b/packages/shared/lib/core/profile-manager/api/getVotingProposals.ts index f574fccfe6..54607b1900 100644 --- a/packages/shared/lib/core/profile-manager/api/getVotingProposals.ts +++ b/packages/shared/lib/core/profile-manager/api/getVotingProposals.ts @@ -4,5 +4,5 @@ import { profileManager } from '@core/profile-manager' export function getVotingEvents(): Promise { const manager = get(profileManager) - return manager.getParticipationEvents() + return manager?.getParticipationEvents() } diff --git a/packages/shared/lib/core/profile-manager/api/registerParticipationEvent.ts b/packages/shared/lib/core/profile-manager/api/registerParticipationEvent.ts index 93b651d777..a831115578 100644 --- a/packages/shared/lib/core/profile-manager/api/registerParticipationEvent.ts +++ b/packages/shared/lib/core/profile-manager/api/registerParticipationEvent.ts @@ -1,4 +1,4 @@ -import { addProposalState } from '@core/governance' +import { addProposalState, registeredEventIds } from '@core/governance' import { profileManager } from '@core/profile-manager/stores' import type { Event, EventId, Node } from '@iota/wallet' import { get } from 'svelte/store' @@ -6,6 +6,8 @@ import { get } from 'svelte/store' export async function registerParticipationEvent(eventId: EventId, nodes: Node[]): Promise { const manager = get(profileManager) const event = await manager.registerParticipationEvent(eventId, nodes) + + registeredEventIds.update((ids) => [...ids, eventId]) await addProposalState(eventId) return event }