From 1e69ed13c728a6348ba07ed730527f796c1f5262 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 21 Apr 2020 07:18:44 +0200 Subject: [PATCH] Re-add unclosed proposals (#2594) * Re-add unclosed proposals * Treasury checks end --- packages/page-council/src/Motions/Motion.tsx | 23 ++++++++++--------- packages/page-council/src/Motions/index.tsx | 11 ++++----- packages/page-council/src/useCounter.ts | 22 ++++++------------ .../page-treasury/src/Overview/Voting.tsx | 13 +++++++---- packages/react-query/src/BlockToTime.tsx | 6 ++++- 5 files changed, 36 insertions(+), 39 deletions(-) diff --git a/packages/page-council/src/Motions/Motion.tsx b/packages/page-council/src/Motions/Motion.tsx index d1e31c237d..f065a444c6 100644 --- a/packages/page-council/src/Motions/Motion.tsx +++ b/packages/page-council/src/Motions/Motion.tsx @@ -5,7 +5,6 @@ import { AccountId, BlockNumber } from '@polkadot/types/interfaces'; import { DeriveCollectiveProposal } from '@polkadot/api-derive/types'; -import BN from 'bn.js'; import React from 'react'; import ProposalCell from '@polkadot/app-democracy/Overview/ProposalCell'; import { LinkExternal } from '@polkadot/react-components'; @@ -26,7 +25,7 @@ interface Props { function Motion ({ className, isMember, members, motion: { hash, proposal, votes }, prime }: Props): React.ReactElement | null { const { api } = useApi(); - const bestNumber = useCall(api.derive.chain.bestNumber, []) || new BN(0); + const bestNumber = useCall(api.derive.chain.bestNumber, []); if (!votes) { return null; @@ -45,7 +44,7 @@ function Motion ({ className, isMember, members, motion: { hash, proposal, votes {formatNumber(ayes.length)}/{formatNumber(threshold)} - {end && ( + {bestNumber && end && ( <> #{formatNumber(end)} @@ -55,14 +54,16 @@ function Motion ({ className, isMember, members, motion: { hash, proposal, votes - + {bestNumber && end.gt(bestNumber) && ( + + )} { const { t } = useTranslation(); - const { api } = useApi(); - const bestNumber = useCall(api.derive.chain.bestNumber, []); const { isMember, members } = useMembers(); - const filtered = bestNumber && motions?.filter(({ votes }) => votes?.end.gt(bestNumber)); return (
@@ -47,7 +44,7 @@ function Proposals ({ className, motions, prime }: Props): React.ReactElement - {filtered?.map((motion: DeriveCollectiveProposal): React.ReactNode => ( + {motions?.map((motion: DeriveCollectiveProposal): React.ReactNode => ( !!votes).length; +} + export default function useCounter (): number { const { api, isApiReady } = useApi(); - const [counter, setCounter] = useState(0); - const bestNumber = useCall(isApiReady && api.derive.chain.bestNumber, []); - const proposals = useCall(isApiReady && api.derive.council?.proposals, []); - - useEffect((): void => { - bestNumber && proposals && setCounter((prev: number): number => { - const filtered = proposals.filter(({ votes }) => votes?.end.gt(bestNumber)); - - return filtered.length !== prev - ? filtered.length - : prev; - }); - }, [bestNumber, proposals]); + const counter = useCall(isApiReady && api.derive.council?.proposals, [], { + transform + }) || 0; return counter; } diff --git a/packages/page-treasury/src/Overview/Voting.tsx b/packages/page-treasury/src/Overview/Voting.tsx index 0eff2b179b..990cf8190b 100644 --- a/packages/page-treasury/src/Overview/Voting.tsx +++ b/packages/page-treasury/src/Overview/Voting.tsx @@ -3,11 +3,11 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { DeriveCollectiveProposal } from '@polkadot/api-derive/types'; -import { ProposalIndex, Hash } from '@polkadot/types/interfaces'; +import { BlockNumber, Hash, ProposalIndex } from '@polkadot/types/interfaces'; import React, { useCallback, useEffect, useState } from 'react'; import { Button, Dropdown, Input, Modal, VoteAccount, VoteActions, VoteToggle } from '@polkadot/react-components'; -import { useAccounts, useToggle } from '@polkadot/react-hooks'; +import { useAccounts, useApi, useCall, useToggle } from '@polkadot/react-hooks'; import { isBoolean } from '@polkadot/util'; import { useTranslation } from '../translate'; @@ -25,6 +25,8 @@ interface Option { function Voting ({ councilProposals, isDisabled }: Props): React.ReactElement | null { const { t } = useTranslation(); const { hasAccounts } = useAccounts(); + const { api } = useApi(); + const bestNumber = useCall(api.derive.chain.bestNumber, []); const [councilOpts, setCouncilOpts] = useState([]); const [councilOptId, setCouncilOptId] = useState(0); const [accountId, setAccountId] = useState(null); @@ -38,15 +40,16 @@ function Voting ({ councilProposals, isDisabled }: Props): React.ReactElement { const available = councilProposals + .filter(({ votes }) => bestNumber && votes?.end.gt(bestNumber)) .map(({ proposal: { methodName, sectionName }, votes }): Option => ({ text: `Council #${votes?.index.toNumber()}: ${sectionName}.${methodName} `, value: votes ? votes?.index.toNumber() : -1 })) - .filter(({ value }): boolean => value !== -1); + .filter(({ value }) => value !== -1); setCouncilOptId(available.length ? available[0].value : 0); setCouncilOpts(available); - }, [councilProposals]); + }, [bestNumber, councilProposals]); const _onChangeVote = useCallback( (vote?: boolean) => setVoteValue(isBoolean(vote) ? vote : true), @@ -55,7 +58,7 @@ function Voting ({ councilProposals, isDisabled }: Props): React.ReactElement { - const councilProp = councilProposals.find(({ votes }): boolean => !!(votes?.index.eq(optionId))); + const councilProp = councilProposals.find(({ votes }) => votes?.index.eq(optionId)); if (councilProp && councilProp.votes) { setCouncilInfo({ councilHash: councilProp.hash, councilId: councilProp.votes.index }); diff --git a/packages/react-query/src/BlockToTime.tsx b/packages/react-query/src/BlockToTime.tsx index 40caae241b..9eaa9438dc 100644 --- a/packages/react-query/src/BlockToTime.tsx +++ b/packages/react-query/src/BlockToTime.tsx @@ -54,7 +54,7 @@ function extractTime (value?: number): Time { return addTime([round, 0, 0, 0], extractTime(value - (round * DAY))); } -function BlockToTime ({ blocks, children, className, label, style }: Props): React.ReactElement { +function BlockToTime ({ blocks, children, className, label, style }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); const time = useMemo((): string => { @@ -73,6 +73,10 @@ function BlockToTime ({ blocks, children, className, label, style }: Props): Rea ].filter((value): value is string => !!value).slice(0, 2).join(' '); }, [api, blocks, t]); + if (blocks?.ltn(0)) { + return null; + } + return (