Cleanup lections, non-linked votesOf (#2411)

This commit is contained in:
Jaco Greeff 2020-03-18 07:36:10 +01:00 committed by GitHub
parent 6bb603d8eb
commit 3d9d9964a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 7 deletions

View File

@ -127,8 +127,8 @@ class Vote extends TxModal<Props, State> {
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<Props, State> {
.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))
});

View File

@ -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<string, AccountId[]> {
function transformVotesPrev ([voters, casted]: [AccountId[], AccountId[][]]): Record<string, AccountId[]> {
return voters.reduce((result: Record<string, AccountId[]>, voter, index): Record<string, AccountId[]> => {
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<string, AccountId[]> {
return entries.reduce((result: Record<string, AccountId[]>, [key, casted]): Record<string, AccountId[]> => {
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<Props> {
const { api } = useApi();
const bestNumber = useCall<BlockNumber>(api.derive.chain.bestNumber, []);
const electionsInfo = useCall<DerivedElectionsInfo>(api.derive.elections.info, []);
const allVotes = useCall<Record<string, AccountId[]>>(api.query.electionsPhragmen?.votesOf, [], {
transform: transformVotes
});
const allVotes = api.query.electionsPhragmen?.votesOf.creator.meta.type.asMap.linked.isTrue
? useCall<Record<string, AccountId[]>>(api.query.electionsPhragmen?.votesOf, [], {
transform: transformVotesPrev
})
: useCall<Record<string, AccountId[]>>(api.query.electionsPhragmen?.votesOf.entries as any, [], {
transform: transformVotes
});
return (
<div className={className}>