From 132df84f206c06447a5d2e5c4d6dd711730852bc Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 13 Apr 2022 14:23:40 +0200 Subject: [PATCH] Allow Vote.conviction entry in extrinsics (#7370) --- packages/react-params/src/Param/Vote.tsx | 66 ++++++++++++++---------- 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/packages/react-params/src/Param/Vote.tsx b/packages/react-params/src/Param/Vote.tsx index 1c6f282b62..9368b5cc94 100644 --- a/packages/react-params/src/Param/Vote.tsx +++ b/packages/react-params/src/Param/Vote.tsx @@ -3,7 +3,7 @@ import type { Props } from '../types'; -import React, { useRef } from 'react'; +import React, { useCallback, useEffect, useRef, useState } from 'react'; import { Dropdown } from '@polkadot/react-components'; import { GenericVote } from '@polkadot/types'; @@ -12,21 +12,34 @@ import { isBn } from '@polkadot/util'; import { useTranslation } from '../translate'; import Bare from './Bare'; -function doChange (onChange?: (value: any) => void): (_: number) => void { - return function (value: number): void { - onChange && onChange({ - isValid: true, - value - }); - }; +interface VoteParts { + aye: boolean; + conviction: number; } +const EMPTY_VOTE: VoteParts = { aye: true, conviction: 0 }; + function Vote ({ className = '', defaultValue: { value }, isDisabled, isError, onChange, withLabel }: Props): React.ReactElement { const { t } = useTranslation(); + const [vote, setVote] = useState(EMPTY_VOTE); + + useEffect((): void => { + onChange && onChange({ isValid: true, value: vote }); + }, [onChange, vote]); + + const onChangeVote = useCallback( + (aye: boolean) => setVote(({ conviction }) => ({ aye, conviction })), + [] + ); + + const onChangeConviction = useCallback( + (conviction: number) => setVote(({ aye }) => ({ aye, conviction })), + [] + ); const optAyeRef = useRef([ - { text: t('Nay'), value: 0 }, - { text: t('Aye'), value: -1 } + { text: t('Nay'), value: false }, + { text: t('Aye'), value: true } ]); const optConvRef = useRef([ @@ -39,11 +52,11 @@ function Vote ({ className = '', defaultValue: { value }, isDisabled, isError, o { text: t('Locked6x'), value: 6 } ]); - const defaultValue = isBn(value) - ? value.toNumber() + const defaultVote = isBn(value) + ? (value.toNumber() !== 0) : value instanceof GenericVote - ? (value.isAye ? -1 : 0) - : value as number; + ? value.isAye + : (value as number !== 0); const defaultConv = value instanceof GenericVote ? value.conviction.index : 0; @@ -52,25 +65,24 @@ function Vote ({ className = '', defaultValue: { value }, isDisabled, isError, o ('aye: bool')} - onChange={doChange(onChange)} + onChange={onChangeVote} options={optAyeRef.current} withLabel={withLabel} /> - {isDisabled && ( - ('conviction: Conviction')} - options={optConvRef.current} - withLabel={withLabel} - /> - )} + ('conviction: Conviction')} + onChange={onChangeConviction} + options={optConvRef.current} + withLabel={withLabel} + /> ); }