From 2f3117dc0560f864cc459f5aefb1af3caf8091bb Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 18 Oct 2019 10:24:03 +0200 Subject: [PATCH] Droptdown functional (#1731) --- .../src/Contracts/ValidateAddr.tsx | 79 ++++----- packages/react-components/src/Dropdown.tsx | 154 +++++++----------- 2 files changed, 89 insertions(+), 144 deletions(-) diff --git a/packages/app-contracts/src/Contracts/ValidateAddr.tsx b/packages/app-contracts/src/Contracts/ValidateAddr.tsx index d6ba6bdda9..e977a9f4cc 100644 --- a/packages/app-contracts/src/Contracts/ValidateAddr.tsx +++ b/packages/app-contracts/src/Contracts/ValidateAddr.tsx @@ -7,7 +7,7 @@ import { ContractInfo } from '@polkadot/types/interfaces'; import { I18nProps } from '@polkadot/react-components/types'; import { ApiProps } from '@polkadot/react-api/types'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { Option } from '@polkadot/types'; import { withCalls } from '@polkadot/react-api'; import { InfoForInput } from '@polkadot/react-components'; @@ -21,68 +21,47 @@ interface Props extends ApiProps, I18nProps { onChange: (isValid: boolean) => void; } -interface State { - isStored: boolean; - isValidAddr: boolean; - isValid: boolean; -} - -class ValidateAddr extends React.PureComponent { - public state: State = { - isStored: false, - isValidAddr: false, - isValid: false - }; - - public static getDerivedStateFromProps ({ address, contracts_contractInfoOf, onChange }: Props): State { - let isValidAddr = false; +function ValidateAddr ({ address, contracts_contractInfoOf, onChange, t }: Props): React.ReactElement | null { + const [isAddress, setIsAddress] = useState(false); + const [isStored, setIsStored] = useState(false); + useEffect((): void => { try { keyring.decodeAddress(address || ''); - - isValidAddr = true; + setIsAddress(true); } catch (error) { - // ignore + setIsAddress(false); } + }, [address]); - const isStored = ( - (!!contracts_contractInfoOf && contracts_contractInfoOf.isSome) - // (!!contract_codeHashOf && contract_codeHashOf.isSome) - ); - const isValid = isValidAddr && isStored; + useEffect((): void => { + setIsStored(!!contracts_contractInfoOf && contracts_contractInfoOf.isSome); + }, [contracts_contractInfoOf]); - // FIXME Really not convinced this is the correct place to do this type of callback? - onChange(isValid); + useEffect((): void => { + onChange(isAddress && isStored); + }, [isAddress, isStored]); - return { - isStored, - isValidAddr, - isValid - }; + if (isStored || !isAddress) { + return null; } - public render (): React.ReactNode { - const { t } = this.props; - const { isValid, isValidAddr } = this.state; - - if (isValid || !isValidAddr) { - return null; - } - - return ( - - { - isValidAddr - ? t('Unable to find deployed contract code at the specified address') - : t('The value is not in a valid address format') - } - - ); - } + return ( + + { + isAddress + ? t('Unable to find deployed contract code at the specified address') + : t('The value is not in a valid address format') + } + + ); } export default translate( withCalls( - ['query.contracts.contractInfoOf', { fallbacks: ['query.contract.contractInfoOf'], paramName: 'address' }] + ['query.contracts.contractInfoOf', { + fallbacks: ['query.contract.contractInfoOf'], + paramName: 'address' + }] )(ValidateAddr) ); diff --git a/packages/react-components/src/Dropdown.tsx b/packages/react-components/src/Dropdown.tsx index 7a47b20cfb..73bc455d0f 100644 --- a/packages/react-components/src/Dropdown.tsx +++ b/packages/react-components/src/Dropdown.tsx @@ -4,7 +4,7 @@ import { BareProps } from './types'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; import styled from 'styled-components'; import SUIButton from 'semantic-ui-react/dist/commonjs/elements/Button/Button'; import SUIDropdown, { DropdownProps } from 'semantic-ui-react/dist/commonjs/modules/Dropdown/Dropdown'; @@ -39,110 +39,76 @@ interface Props