diff --git a/packages/page-council/src/Overview/Vote.tsx b/packages/page-council/src/Overview/Vote.tsx index 68c790b71e..767a13e9db 100644 --- a/packages/page-council/src/Overview/Vote.tsx +++ b/packages/page-council/src/Overview/Vote.tsx @@ -2,153 +2,102 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { ApiProps } from '@polkadot/react-api/types'; -import { AccountId, VoteIndex } from '@polkadot/types/interfaces'; -import { ComponentProps } from './types'; +import { DeriveElectionsInfo } from '@polkadot/api-derive/types'; import BN from 'bn.js'; -import React from 'react'; -import { withApi } from '@polkadot/react-api/hoc'; -import { Button, InputAddressMulti, VoteValue } from '@polkadot/react-components'; -import TxModal, { TxModalState, TxModalProps } from '@polkadot/react-components/TxModal'; +import React, { useEffect, useState } from 'react'; +import { Button, InputAddress, InputAddressMulti, Modal, TxButton, VoteValue } from '@polkadot/react-components'; +import { useApi, useToggle } from '@polkadot/react-hooks'; -import translate from '../translate'; +import { useTranslation } from '../translate'; -interface Props extends ApiProps, ComponentProps, TxModalProps {} - -interface State extends TxModalState { - votes: string[]; - voteValue: BN; +interface Props { + className?: string; + electionsInfo?: DeriveElectionsInfo; } const MAX_VOTES = 16; -class Vote extends TxModal { - constructor (props: Props) { - super(props); +function Vote ({ electionsInfo }: Props): React.ReactElement { + const { t } = useTranslation(); + const { api } = useApi(); + const [isVisible, toggleVisible] = useToggle(); + const [accountId, setAccountId] = useState(null); + const [available, setAvailable] = useState([]); + const [votes, setVotes] = useState([]); + const [voteValue, setVoteValue] = useState(new BN(0)); - this.defaultState = { - ...this.defaultState, - voteValue: new BN(0), - votes: [] - }; + useEffect((): void => { + if (electionsInfo) { + const { candidates, members, runnersUp } = electionsInfo; - this.state = { - ...this.defaultState - }; - } - - protected headerText = (): string => this.props.t('Vote for current candidates'); - - protected accountLabel = (): string => this.props.t('Voting account'); - - protected accountHelp = (): string => this.props.t('This account will be use to approve or disapprove each candidate.'); - - protected txMethod = (): string => - this.props.api.tx.electionsPhragmen - ? 'electionsPhragmen.vote' - : 'elections.vote'; - - protected txParams = (): [boolean[] | null, VoteIndex, BN | null] | [string[], BN] => { - const { voteValue, votes } = this.state; - - return [votes, voteValue]; - } - - protected isDisabled = (): boolean => { - const { accountId, voteValue, votes } = this.state; - - return !accountId || votes.length === 0 || voteValue.lten(0); - } - - protected renderTrigger = (): React.ReactNode => { - let available: AccountId[] = []; - - if (this.props.electionsInfo) { - const { electionsInfo: { candidates, members, runnersUp } } = this.props; - - available = members - .map(([accountId]): AccountId => accountId) - .concat(runnersUp.map(([accountId]): AccountId => accountId)) - .concat(candidates); + setAvailable( + members + .map(([accountId]): string => accountId.toString()) + .concat(runnersUp.map(([accountId]): string => accountId.toString())) + .concat(candidates.map((accountId): string => accountId.toString())) + ); } + }, [electionsInfo]); - return ( + useEffect((): void => { + accountId && api.derive.council.votesOf(accountId).then(({ votes }): void => { + setVotes( + votes + .map((accountId): string => accountId.toString()) + .filter((accountId): boolean => available.includes(accountId)) + ); + }); + }, [api, accountId, available]); + + return ( + <>