Fix council voting balance (#3363)

* fix council voting balance

* locked balance as default

* Update packages/react-components/src/VoteValue.tsx

Co-authored-by: Jaco Greeff <jacogr@gmail.com>

Co-authored-by: Jaco Greeff <jacogr@gmail.com>
This commit is contained in:
Thibaut Sardan 2020-08-07 14:37:41 +02:00 committed by GitHub
parent 29b9c2241f
commit 4486532534
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 6 deletions

View File

@ -5,7 +5,7 @@
import { DeriveBalancesAll } from '@polkadot/api-derive/types';
import BN from 'bn.js';
import React, { useCallback, useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState, useMemo } from 'react';
import { useApi, useCall } from '@polkadot/react-hooks';
import { BalanceVoting } from '@polkadot/react-query';
import { isBn } from '@polkadot/util';
@ -30,6 +30,11 @@ function VoteValue ({ accountId, autoFocus, isCouncil, onChange }: Props): React
const { api } = useApi();
const allBalances = useCall<DeriveBalancesAll>(api.derive.balances.all, [accountId]);
const [{ selectedId, value }, setValue] = useState<ValueState>({});
const maxVotingBalance = useMemo(() =>
isCouncil
? allBalances?.votingBalance.add(allBalances?.reservedBalance)
: allBalances?.votingBalance
, [allBalances?.reservedBalance, allBalances?.votingBalance, isCouncil]);
useEffect((): void => {
// if the set accountId changes and the new balances is for that id, set it
@ -37,7 +42,7 @@ function VoteValue ({ accountId, autoFocus, isCouncil, onChange }: Props): React
selectedId: accountId,
value: allBalances.lockedBalance
});
}, [accountId, selectedId, allBalances]);
}, [allBalances, accountId, maxVotingBalance, selectedId]);
// only do onChange to parent when the BN value comes in, not our formatted version
useEffect((): void => {
@ -54,7 +59,10 @@ function VoteValue ({ accountId, autoFocus, isCouncil, onChange }: Props): React
return (
<InputBalance
autoFocus={autoFocus}
defaultValue={accountId !== selectedId ? undefined : allBalances?.lockedBalance}
defaultValue={accountId !== selectedId
? undefined
: allBalances?.lockedBalance
}
help={t<string>('The amount that is associated with this vote. This value is is locked for the duration of the vote.')}
isDisabled={isDisabled}
isZeroable
@ -66,7 +74,7 @@ function VoteValue ({ accountId, autoFocus, isCouncil, onChange }: Props): React
params={accountId}
/>
}
maxValue={allBalances?.votingBalance}
maxValue={maxVotingBalance}
onChange={_setValue}
/>
);

View File

@ -18,7 +18,7 @@ interface Props {
params?: AccountId | AccountIndex | Address | string | Uint8Array | null;
}
function BalanceVoting ({ children, className = '', label, params }: Props): React.ReactElement<Props> {
function BalanceVoting ({ children, className = '', isCouncil, label, params }: Props): React.ReactElement<Props> {
const { api } = useApi();
const allBalances = useCall<DeriveBalancesAll>(api.derive.balances.all, [params]);
@ -26,7 +26,10 @@ function BalanceVoting ({ children, className = '', label, params }: Props): Rea
<FormatBalance
className={className}
label={label}
value={allBalances?.votingBalance}
value={isCouncil
? allBalances?.votingBalance.add(allBalances?.reservedBalance)
: allBalances?.votingBalance
}
>
{children}
</FormatBalance>