diff --git a/packages/page-council/src/Overview/Vote.tsx b/packages/page-council/src/Overview/Vote.tsx index fcc07972cc..c0d7ec8aa1 100644 --- a/packages/page-council/src/Overview/Vote.tsx +++ b/packages/page-council/src/Overview/Vote.tsx @@ -127,8 +127,8 @@ class Vote extends TxModal { if (accountId) { (api.query.electionsPhragmen || api.query.elections) - .votesOf<[AccountId[]] & Codec>(accountId) - .then(([existingVotes]): void => { + .votesOf<([AccountId[]] & Codec) | AccountId[]>(accountId) + .then((existingVotes): void => { if (!this.props.electionsInfo) { return; } @@ -140,7 +140,11 @@ class Vote extends TxModal { .concat(candidates.map((accountId): string => accountId.toString())); this.setState({ - votes: existingVotes + votes: ( + Array.isArray(existingVotes[0]) + ? existingVotes[0] + : (existingVotes as AccountId[]) + ) .map((accountId): string => accountId.toString()) .filter((accountId): boolean => available.includes(accountId)) }); diff --git a/packages/page-council/src/Overview/index.tsx b/packages/page-council/src/Overview/index.tsx index f80ee063bf..52d049d22d 100644 --- a/packages/page-council/src/Overview/index.tsx +++ b/packages/page-council/src/Overview/index.tsx @@ -8,6 +8,7 @@ import { AccountId, BlockNumber } from '@polkadot/types/interfaces'; import React from 'react'; import { Button } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; +import { StorageKey } from '@polkadot/types'; import Candidates from './Candidates'; import Members from './Members'; @@ -20,7 +21,7 @@ interface Props { prime: AccountId | null; } -function transformVotes ([voters, casted]: [AccountId[], AccountId[][]]): Record { +function transformVotesPrev ([voters, casted]: [AccountId[], AccountId[][]]): Record { return voters.reduce((result: Record, voter, index): Record => { casted[index].forEach((candidate): void => { const address = candidate.toString(); @@ -36,13 +37,35 @@ function transformVotes ([voters, casted]: [AccountId[], AccountId[][]]): Record }, {}); } +function transformVotes (entries: [StorageKey, AccountId[]][]): Record { + return entries.reduce((result: Record, [key, casted]): Record => { + const voter = key.args[0] as AccountId; + + casted.forEach((candidate): void => { + const address = candidate.toString(); + + if (!result[address]) { + result[address] = []; + } + + result[address].push(voter); + }); + + return result; + }, {}); +} + function Overview ({ className, prime }: Props): React.ReactElement { const { api } = useApi(); const bestNumber = useCall(api.derive.chain.bestNumber, []); const electionsInfo = useCall(api.derive.elections.info, []); - const allVotes = useCall>(api.query.electionsPhragmen?.votesOf, [], { - transform: transformVotes - }); + const allVotes = api.query.electionsPhragmen?.votesOf.creator.meta.type.asMap.linked.isTrue + ? useCall>(api.query.electionsPhragmen?.votesOf, [], { + transform: transformVotesPrev + }) + : useCall>(api.query.electionsPhragmen?.votesOf.entries as any, [], { + transform: transformVotes + }); return (