From 056a0c2fe78f8dd9840084a7c122fc434e6b4ffe Mon Sep 17 00:00:00 2001 From: pan Date: Sun, 24 Oct 2021 22:25:50 +0800 Subject: [PATCH] fix vote input bug (#6113) * update bifrost types * update bifrost types * update bifrost types * update bifrost types * fix vote input bug * update code structure --- packages/react-components/src/VoteValue.tsx | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/react-components/src/VoteValue.tsx b/packages/react-components/src/VoteValue.tsx index e6155cf40d..a3c2dfc7ce 100644 --- a/packages/react-components/src/VoteValue.tsx +++ b/packages/react-components/src/VoteValue.tsx @@ -21,6 +21,7 @@ interface Props { } interface ValueState { + defaultValue: BN; maxValue: BN; selectedId?: string | null; value: BN; @@ -29,15 +30,17 @@ interface ValueState { function getValues (selectedId: string | null | undefined, isCouncil: boolean | undefined, allBalances: DeriveBalancesAll, existential: BN): ValueState { const value = allBalances.lockedBalance; const maxValue = allBalances.votingBalance.add(isCouncil ? allBalances.reservedBalance : BN_ZERO); + const defaultValue = value.isZero() + ? maxValue.gt(existential) + ? maxValue.sub(existential) + : BN_ZERO + : value; return { + defaultValue, maxValue, selectedId, - value: value.isZero() - ? maxValue.gt(existential) - ? maxValue.sub(existential) - : BN_ZERO - : value + value: defaultValue }; } @@ -45,7 +48,7 @@ function VoteValue ({ accountId, autoFocus, isCouncil, onChange }: Props): React const { t } = useTranslation(); const { api } = useApi(); const allBalances = useCall(api.derive.balances?.all, [accountId]); - const [{ maxValue, selectedId, value }, setValue] = useState({ maxValue: BN_ZERO, value: BN_ZERO }); + const [{ defaultValue, maxValue, selectedId, value }, setValue] = useState({ defaultValue: BN_ZERO, maxValue: BN_ZERO, value: BN_ZERO }); useEffect((): void => { // if the set accountId changes and the new balances is for that id, set it @@ -78,7 +81,7 @@ function VoteValue ({ accountId, autoFocus, isCouncil, onChange }: Props): React defaultValue={ isDisabled ? undefined - : value + : defaultValue } help={t('The amount that is associated with this vote. This value is locked for the duration of the vote.')} isDisabled={isDisabled}