Filter council by proposal end (#2593)

This commit is contained in:
Jaco Greeff 2020-04-20 21:57:51 +02:00 committed by GitHub
parent d9d33d7a1b
commit ecf795c249
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View File

@ -3,11 +3,11 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { DeriveCollectiveProposals, DeriveCollectiveProposal } from '@polkadot/api-derive/types';
import { AccountId } from '@polkadot/types/interfaces';
import { BlockNumber, AccountId } from '@polkadot/types/interfaces';
import React from 'react';
import { Button, Table } from '@polkadot/react-components';
import { useMembers } from '@polkadot/react-hooks';
import { useApi, useCall, useMembers } from '@polkadot/react-hooks';
import { useTranslation } from '../translate';
import Motion from './Motion';
@ -23,7 +23,10 @@ interface Props {
function Proposals ({ className, motions, prime }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const bestNumber = useCall<BlockNumber>(api.derive.chain.bestNumber, []);
const { isMember, members } = useMembers();
const filtered = bestNumber && motions?.filter(({ votes }) => votes?.end.gt(bestNumber));
return (
<div className={className}>
@ -44,7 +47,7 @@ function Proposals ({ className, motions, prime }: Props): React.ReactElement<Pr
/>
</Button.Group>
<Table
empty={motions && t('No council motions')}
empty={filtered && t('No council motions')}
header={[
[t('motions'), 'start', 2],
[t('threshold')],
@ -54,7 +57,7 @@ function Proposals ({ className, motions, prime }: Props): React.ReactElement<Pr
[undefined, undefined, 2]
]}
>
{motions?.map((motion: DeriveCollectiveProposal): React.ReactNode => (
{filtered?.map((motion: DeriveCollectiveProposal): React.ReactNode => (
<Motion
isMember={isMember}
key={motion.hash.toHex()}

View File

@ -3,18 +3,26 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { DeriveCollectiveProposals } from '@polkadot/api-derive/types';
import { BlockNumber } from '@polkadot/types/interfaces';
import { useEffect, useState } from 'react';
import { useApi, useCall } from '@polkadot/react-hooks';
function transform (motions: DeriveCollectiveProposals): number {
return motions.filter(({ votes }): boolean => !!votes).length;
}
export default function useCounter (): number {
const { api, isApiReady } = useApi();
const counter = useCall<number>(isApiReady && api.derive.council?.proposals, [], {
transform
}) || 0;
const [counter, setCounter] = useState(0);
const bestNumber = useCall<BlockNumber>(isApiReady && api.derive.chain.bestNumber, []);
const proposals = useCall<DeriveCollectiveProposals>(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]);
return counter;
}