From 01b267c7fe9031006dc011fc5154c12b4c8ac8ae Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 4 Apr 2022 08:18:59 +0200 Subject: [PATCH] Adjust reason length input (#7299) --- packages/page-treasury/src/Tips/TipCreate.tsx | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/page-treasury/src/Tips/TipCreate.tsx b/packages/page-treasury/src/Tips/TipCreate.tsx index 4b66e18800..0e7b5c0633 100644 --- a/packages/page-treasury/src/Tips/TipCreate.tsx +++ b/packages/page-treasury/src/Tips/TipCreate.tsx @@ -3,10 +3,11 @@ import type { BN } from '@polkadot/util'; -import React, { useEffect, useState } from 'react'; +import React, { useMemo, useState } from 'react'; import { Button, Input, InputAddress, InputBalance, Modal, TxButton } from '@polkadot/react-components'; import { useApi, useToggle } from '@polkadot/react-hooks'; +import { BN_ZERO } from '@polkadot/util'; import { useTranslation } from '../translate'; @@ -14,7 +15,7 @@ interface Props { members: string[]; } -const MAX_REASON_LEN = 128; +const MAX_REASON_LEN = 256; const MIN_REASON_LEN = 5; function TipCreate ({ members }: Props): React.ReactElement { @@ -23,15 +24,21 @@ function TipCreate ({ members }: Props): React.ReactElement { const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); const [beneficiary, setBeneficiary] = useState(null); - const [isMember, setIsMember] = useState(false); const [reason, setReason] = useState(''); const [value, setValue] = useState(); - const hasValue = value?.gtn(0); - const hasReason = reason?.length >= MIN_REASON_LEN && reason?.length <= MAX_REASON_LEN; - - useEffect((): void => { - setIsMember(members.includes(accountId || '')); - }, [accountId, members]); + const maxReasonLen = useMemo( + () => Math.min(MAX_REASON_LEN, ( + (api.consts.tips || api.consts.treasury)?.maximumReasonLength?.toNumber() || + MAX_REASON_LEN + )), + [api] + ); + const isMember = useMemo( + () => !!accountId && members.includes(accountId), + [accountId, members] + ); + const hasValue = !!value && value.gt(BN_ZERO); + const hasReason = !!reason && (reason.length >= MIN_REASON_LEN) && (reason.length <= maxReasonLen); return ( <>