diff --git a/packages/page-council/src/Motions/ProposeMotion.tsx b/packages/page-council/src/Motions/ProposeMotion.tsx index 1e57e78025..0882cd97b8 100644 --- a/packages/page-council/src/Motions/ProposeMotion.tsx +++ b/packages/page-council/src/Motions/ProposeMotion.tsx @@ -3,135 +3,99 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { SubmittableExtrinsic } from '@polkadot/api/types'; -import { ApiProps } from '@polkadot/react-api/types'; -import { Proposal } from '@polkadot/types/interfaces'; import BN from 'bn.js'; -import React from 'react'; -import { registry, withApi, withMulti } from '@polkadot/react-api'; -import { Button, Extrinsic, InputNumber } from '@polkadot/react-components'; -import TxModal, { TxModalState, TxModalProps } from '@polkadot/react-components/TxModal'; -import { createType } from '@polkadot/types'; +import React, { useCallback, useEffect, useState } from 'react'; +import { Button, Extrinsic, InputAddress, InputNumber, Modal, TxButton } from '@polkadot/react-components'; +import { useApi, useToggle } from '@polkadot/react-hooks'; -import translate from '../translate'; +import { useTranslation } from '../translate'; -interface Props extends TxModalProps, ApiProps { +interface Props { isMember: boolean; members: string[]; } -interface State extends TxModalState { - method: SubmittableExtrinsic<'promise'> | null; - threshold: BN | null; +interface Threshold { + isThresholdValid: boolean; + threshold?: BN; } -class Propose extends TxModal { - constructor (props: Props) { - super(props); +function Propose ({ isMember, members }: Props): React.ReactElement { + const { t } = useTranslation(); + const { apiDefaultTxSudo } = useApi(); + const [isOpen, toggleOpen] = useToggle(); + const [accountId, setAcountId] = useState(null); + const [method, setMethod] = useState | null | undefined>(); + const [{ isThresholdValid, threshold }, setThreshold] = useState({ isThresholdValid: false }); - this.defaultState = { - ...this.defaultState, - method: null, - threshold: props.members.length ? new BN(Math.ceil(props.members.length * 0.5)) : null - }; - this.state = this.defaultState; - } + useEffect((): void => { + members && setThreshold({ + isThresholdValid: members.length !== 0, + threshold: new BN(Math.ceil(members.length * 0.5)) + }); + }, [members]); - public static getDerivedStateFromProps ({ members }: Props, { threshold }: State): Pick | null { - if (!threshold && members.length) { - return { threshold: new BN(Math.ceil(members.length * 0.5)) }; - } + const _setMethod = useCallback( + (method?: SubmittableExtrinsic<'promise'> | null) => setMethod(() => method), + [] + ); - return null; - } + const _setThreshold = useCallback( + (threshold?: BN) => setThreshold({ + isThresholdValid: !!threshold?.gtn(0), + threshold + }), + [] + ); - protected headerText = (): string => this.props.t('Propose a council motion'); - - protected txMethod = (): string => 'council.propose'; - - protected txParams = (): [BN | null, ...Proposal[]] => { - const { method, threshold } = this.state; - - return [ - threshold, - ...(method ? [createType(registry, 'Proposal', method)] : []) - ]; - } - - protected isDisabled = (): boolean => { - const { members } = this.props; - const { accountId, method, threshold } = this.state; - - const hasThreshold = !!threshold && threshold.gtn(0) && threshold.ltn(members.length + 1); - const hasMethod = !!method; - - return !accountId || !hasMethod || !hasThreshold; - } - - protected renderTrigger = (): React.ReactNode => { - const { isMember, t } = this.props; - - return ( + return ( + <>