From 91c4e2c602117e2ba9e0a9062a5366135ce83d51 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 27 Oct 2019 13:13:50 +0100 Subject: [PATCH] Allow voting for members & candidates (#1771) * Allow voting for members & candidates * Allow voting for mebers * Add defaultnames (candidate/member) --- packages/app-council/src/Overview/Vote.tsx | 139 +++++++++------------ 1 file changed, 58 insertions(+), 81 deletions(-) diff --git a/packages/app-council/src/Overview/Vote.tsx b/packages/app-council/src/Overview/Vote.tsx index 505280abf1..fecb8bf672 100644 --- a/packages/app-council/src/Overview/Vote.tsx +++ b/packages/app-council/src/Overview/Vote.tsx @@ -12,7 +12,7 @@ import React from 'react'; import styled from 'styled-components'; import { createType } from '@polkadot/types'; import { withCalls, withMulti } from '@polkadot/react-api'; -import { AddressRow, Button, Icon, InputBalance, Toggle, TxButton } from '@polkadot/react-components'; +import { AddressRow, Button, InputBalance, Toggle } from '@polkadot/react-components'; import TxModal, { TxModalState, TxModalProps } from '@polkadot/react-components/TxModal'; import translate from '../translate'; @@ -22,25 +22,24 @@ interface Props extends ApiProps, ComponentProps, TxModalProps { } interface State extends TxModalState { - approvals: boolean[] | null; - oldApprovals: boolean[] | null; + votes: Record; voteValue: BN; // voterPositions: DerivedVoterPositions; } -const AlreadyVoted = styled.article` - display: flex; - align-items: center; - margin: 0.5rem 0; +// const AlreadyVoted = styled.article` +// display: flex; +// align-items: center; +// margin: 0.5rem 0; - & > :first-child { - flex: 1 1; - } +// & > :first-child { +// flex: 1 1; +// } - & > :not(:first-child) { - margin: 0; - } -`; +// & > :not(:first-child) { +// margin: 0; +// } +// `; const Candidates = styled.div` display: flex; @@ -53,8 +52,8 @@ const Candidate = styled.div` min-width: calc(50% - 1rem); border-radius: 0.5rem; border: 1px solid #eee; - padding: 0.5rem; - margin: 0.5rem; + padding: 0.25rem; + margin: 0.25rem; transition: all 0.2s; b { @@ -79,23 +78,12 @@ class Vote extends TxModal { return [...new Array(length).keys()].map((): boolean => false); } - public static getDerivedStateFromProps ({ electionsInfo: { candidateCount } }: Props, { approvals }: State): Partial | null { - if (candidateCount && (!approvals || !candidateCount.eq(approvals.length))) { - return { - approvals: Vote.emptyApprovals(candidateCount.toNumber()) - }; - } - - return null; - } - public constructor (props: Props) { super(props); this.defaultState = { ...this.defaultState, - approvals: null, - oldApprovals: null, + votes: {}, voteValue: new BN(0) }; @@ -127,17 +115,19 @@ class Vote extends TxModal { ? 'electionsPhragmen.vote' : 'elections.setApprovals'; - protected txParams = (): [boolean[] | null, VoteIndex, BN | null] | [AccountId[], BN] => { + protected txParams = (): [boolean[] | null, VoteIndex, BN | null] | [string[], BN] => { const { api, electionsInfo: { candidates, nextVoterSet, voteCount }, voterPositions } = this.props; - const { accountId, approvals, voteValue } = this.state; + const { accountId, votes, voteValue } = this.state; if (api.tx.electionsPhragmen) { return [ - candidates.filter((candidate, index): boolean => !!approvals && approvals[index] === true), + Object.entries(votes).filter(([, vote]): boolean => vote).map(([accountId]): string => accountId), voteValue ]; } + const approvals = candidates.map((accountId): boolean => votes[accountId.toString()] === true); + return [ approvals ? approvals.slice(0, 1 + approvals.lastIndexOf(true)) @@ -150,20 +140,21 @@ class Vote extends TxModal { } protected isDisabled = (): boolean => { - const { accountId, approvals, oldApprovals } = this.state; + const { accountId, votes } = this.state; + const hasApprovals = Object.values(votes).some((vote): boolean => vote); - return !accountId || - !!oldApprovals || - !approvals || - !approvals.some((val): boolean => val === true); + return !accountId || !hasApprovals; } protected renderTrigger = (): React.ReactNode => { - const { electionsInfo: { candidates }, t } = this.props; + const { api, electionsInfo: { candidates, members }, t } = this.props; + const available = api.tx.electionsPhragmen + ? members.concat(candidates) + : candidates; return (