Swap council voting away from TxModal (#2521)

* Swap council voting to non-TxModal

* No fat spinner

* Use bundled node
This commit is contained in:
Jaco Greeff 2020-04-06 12:29:00 +02:00 committed by GitHub
parent 31796b87f3
commit 00e0498ce2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 132 deletions

View File

@ -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<Props, State> {
constructor (props: Props) {
super(props);
function Vote ({ electionsInfo }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const [isVisible, toggleVisible] = useToggle();
const [accountId, setAccountId] = useState<string | null>(null);
const [available, setAvailable] = useState<string[]>([]);
const [votes, setVotes] = useState<string[]>([]);
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 (
<>
<Button
icon='check'
isDisabled={available.length === 0}
label={this.props.t('Vote')}
onClick={this.showModal}
label={t('Vote')}
onClick={toggleVisible}
/>
);
}
protected renderContent = (): React.ReactNode => {
if (!this.props.electionsInfo) {
return null;
}
const { electionsInfo: { candidates, members, runnersUp }, t } = this.props;
const { accountId, votes } = this.state;
const available = members
.map(([accountId]): string => accountId.toString())
.concat(runnersUp.map(([accountId]): string => accountId.toString()))
.concat(candidates.map((accountId): string => accountId.toString()));
return (
<>
<VoteValue
accountId={accountId}
onChange={this.setVoteValue}
/>
<InputAddressMulti
available={available}
availableLabel={t('council candidates')}
help={t('Select and order council candidates you wish to vote for.')}
maxCount={MAX_VOTES}
onChange={this.onChangeVotes}
value={votes}
valueLabel={t('my ordered votes')}
/>
</>
);
}
private setVoteValue = (voteValue: BN): void => {
this.setState({ voteValue });
}
protected onChangeAccount = (accountId: string | null): void => {
const { api } = this.props;
this.setState({ accountId });
if (accountId) {
api.derive.council
.votesOf(accountId)
.then(({ votes }): void => {
if (!this.props.electionsInfo) {
return;
}
const { electionsInfo: { candidates, members, runnersUp } } = this.props;
const available = members
.map(([accountId]): string => accountId.toString())
.concat(runnersUp.map(([accountId]): string => accountId.toString()))
.concat(candidates.map((accountId): string => accountId.toString()));
this.setState({
votes: votes
.map((accountId): string => accountId.toString())
.filter((accountId): boolean => available.includes(accountId))
});
});
}
}
private onChangeVotes = (votes: string[]): void => {
this.setState({ votes });
}
{isVisible && (
<Modal header={t('Vote for current candidates')}>
<Modal.Content>
<InputAddress
help={t('This account will be use to approve each candidate.')}
label={t('voting account')}
onChange={setAccountId}
type='account'
/>
<VoteValue
accountId={accountId}
onChange={setVoteValue}
/>
<InputAddressMulti
available={available}
availableLabel={t('council candidates')}
help={t('Select and order council candidates you wish to vote for.')}
maxCount={MAX_VOTES}
onChange={setVotes}
value={votes}
valueLabel={t('my ordered votes')}
/>
</Modal.Content>
<Modal.Actions onCancel={toggleVisible}>
<TxButton
accountId={accountId}
isDisabled={!accountId || votes.length === 0 || voteValue.lten(0)}
onStart={toggleVisible}
params={[votes, voteValue]}
tx={
api.tx.electionsPhragmen
? 'electionsPhragmen.vote'
: 'elections.vote'
}
/>
</Modal.Actions>
</Modal>
)}
</>
);
}
export default translate(withApi(Vote));
export default React.memo(Vote);

View File

@ -228,6 +228,13 @@ export default React.memo(styled(Status)`
vertical-align: middle;
position: relative;
.ui--highlight--spinner {
&:after {
border-color: #fff transparent transparent !important;
font-size: 1rem;
}
}
.desc {
flex: 1;
overflow: hidden;