// Copyright 2017-2019 @polkadot/ui-staking authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. import { VoteIndex } from '@polkadot/types/interfaces'; import { DerivedVoterPositions } from '@polkadot/api-derive/types'; import { ApiProps } from '@polkadot/ui-api/types'; import { ComponentProps } from './types'; import BN from 'bn.js'; import React from 'react'; import styled from 'styled-components'; import { createType } from '@polkadot/types'; import { withApi, withCalls, withMulti } from '@polkadot/ui-api'; import { AddressRow, Button, Icon, Toggle, TxButton } from '@polkadot/ui-app'; import TxModal, { TxModalState, TxModalProps } from '@polkadot/ui-app/TxModal'; import translate from '../translate'; interface Props extends ApiProps, ComponentProps, TxModalProps { voterPositions?: DerivedVoterPositions; } interface State extends TxModalState { approvals: boolean[] | null; oldApprovals: boolean[] | null; // voterPositions: DerivedVoterPositions; } const AlreadyVoted = styled.article` display: flex; align-items: center; margin: 0.5rem 0; & > :first-child { flex: 1 1; } & > :not(:first-child) { margin: 0; } `; const Candidates = styled.div` display: flex; flex-wrap: wrap; `; const Candidate = styled.div` cursor: pointer; width: 25rem; min-width: calc(50% - 1rem); border-radius: 0.5rem; border: 1px solid #eee; padding: 0.5rem; margin: 0.5rem; transition: all 0.2s; b { min-width: 5rem; } &.aye { background-color: rgba(0, 255, 0, 0.05); b { color: green; } } &.nay { background-color: rgba(0, 0, 0, 0.05); } `; class Vote extends TxModal { public static emptyApprovals (length: number): boolean[] { return [...new Array(length).keys()].map((): boolean => false); } public static getDerivedStateFromProps ({ electionsInfo: { candidateCount } }: Props, { approvals }: State): Partial { const state: Partial = {}; // if (voterPositions) { // state.voters = Object.keys(voterSets).reduce( // (result: Record, accountId, globalIndex): Record => { // result[accountId] = { // setIndex: voterSets[accountId], // globalIndex: new BN(globalIndex) // }; // return result; // }, // {} // ); // } if (candidateCount && !approvals) { state.approvals = state.oldApprovals || Vote.emptyApprovals(candidateCount.toNumber()); } return state; } public constructor (props: Props) { super(props); this.defaultState = { ...this.defaultState, approvals: null, oldApprovals: null }; this.state = { ...this.defaultState }; } public componentDidMount (): void { this.fetchApprovals(); } public componentDidUpdate (_: Props, prevState: State): void { const { accountId } = this.state; if (accountId !== prevState.accountId) { this.fetchApprovals(); } } 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 => 'elections.setApprovals'; protected txParams = (): [boolean[] | null, VoteIndex, BN | null] => { const { electionsInfo: { nextVoterSet, voteCount }, voterPositions } = this.props; const { accountId, approvals } = this.state; return [ approvals ? approvals.slice(0, 1 + approvals.lastIndexOf(true)) : [], createType('VoteIndex', voteCount), voterPositions && accountId && voterPositions[accountId] ? voterPositions[accountId].setIndex : nextVoterSet ]; } protected isDisabled = (): boolean => { const { accountId, oldApprovals } = this.state; return !accountId || !!oldApprovals; } protected renderTrigger = (): React.ReactNode => { const { electionsInfo: { candidates }, t } = this.props; return (