From 24fdf6df1e8fc4adfe01e9e9e3f6391904e4e33a Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 19 May 2021 12:30:24 +0300 Subject: [PATCH] DispatchResult expansion (#5318) --- .../react-params/src/Param/DispatchError.tsx | 6 +- .../react-params/src/Param/DispatchResult.tsx | 56 +++++++++++++++++++ packages/react-params/src/Param/Static.tsx | 4 +- packages/react-params/src/Param/Unknown.tsx | 6 +- .../react-params/src/Param/findComponent.ts | 2 + 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 packages/react-params/src/Param/DispatchResult.tsx diff --git a/packages/react-params/src/Param/DispatchError.tsx b/packages/react-params/src/Param/DispatchError.tsx index 0df9018737..ee3a40c9e6 100644 --- a/packages/react-params/src/Param/DispatchError.tsx +++ b/packages/react-params/src/Param/DispatchError.tsx @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { DispatchError } from '@polkadot/types/interfaces'; -import type { Props } from '../types'; +import type { Props as BaseProps } from '../types'; import React, { useEffect, useState } from 'react'; @@ -17,6 +17,10 @@ interface Details { type?: string; } +interface Props extends BaseProps { + childrenPre?: React.ReactNode; +} + function isDispatchError (value?: unknown): value is DispatchError { return !!(value && ((value as DispatchError).isModule || (value as DispatchError).isToken)); } diff --git a/packages/react-params/src/Param/DispatchResult.tsx b/packages/react-params/src/Param/DispatchResult.tsx new file mode 100644 index 0000000000..dc5143c637 --- /dev/null +++ b/packages/react-params/src/Param/DispatchResult.tsx @@ -0,0 +1,56 @@ +// Copyright 2017-2021 @polkadot/react-params authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { DispatchResult } from '@polkadot/types/interfaces'; +import type { Props } from '../types'; + +import React, { useMemo } from 'react'; + +import { Input } from '@polkadot/react-components'; + +import DispatchError from './DispatchError'; +import Static from './Static'; +import Unknown from './Unknown'; + +function isDispatchResultErr (value?: unknown): value is DispatchResult { + return !!(value && (value as DispatchResult).isErr); +} + +function DispatchResultDisplay (props: Props): React.ReactElement { + const { defaultValue, isDisabled, label } = props; + const dispatchError = useMemo( + () => defaultValue && isDispatchResultErr(defaultValue.value) + ? { isValid: true, value: defaultValue.value.asErr } + : null, + [defaultValue] + ); + + if (!isDisabled) { + return ; + } else if (!dispatchError) { + return ( + + ); + } + + return ( + + } + defaultValue={dispatchError} + label='DispatchError' + /> + ); +} + +export default React.memo(DispatchResultDisplay); diff --git a/packages/react-params/src/Param/Static.tsx b/packages/react-params/src/Param/Static.tsx index 06ef3998d3..53228ab839 100644 --- a/packages/react-params/src/Param/Static.tsx +++ b/packages/react-params/src/Param/Static.tsx @@ -15,13 +15,14 @@ import Bare from './Bare'; interface Props { asHex?: boolean; children?: React.ReactNode; + childrenPre?: React.ReactNode; className?: string; defaultValue: RawParam; label?: React.ReactNode; withLabel?: boolean; } -function StaticParam ({ asHex, children, className = '', defaultValue, label }: Props): React.ReactElement { +function StaticParam ({ asHex, children, childrenPre, className = '', defaultValue, label }: Props): React.ReactElement { const { t } = useTranslation(); const value = defaultValue && (defaultValue.value as string) && ( asHex @@ -36,6 +37,7 @@ function StaticParam ({ asHex, children, className = '', defaultValue, label }: return ( + {childrenPre} { const { className = '', defaultValue, isDisabled, isError, label, name, onChange, onEnter, onEscape, type } = props; diff --git a/packages/react-params/src/Param/findComponent.ts b/packages/react-params/src/Param/findComponent.ts index 9ae04b2b75..bb3650e9f7 100644 --- a/packages/react-params/src/Param/findComponent.ts +++ b/packages/react-params/src/Param/findComponent.ts @@ -16,6 +16,7 @@ import Bytes from './Bytes'; import Call from './Call'; import Code from './Code'; import DispatchError from './DispatchError'; +import DispatchResult from './DispatchResult'; import Enum from './Enum'; import Hash160 from './Hash160'; import Hash256 from './Hash256'; @@ -52,6 +53,7 @@ const componentDef: TypeToComponent[] = [ { c: Call, t: ['Call', 'Proposal'] }, { c: Code, t: ['Code'] }, { c: DispatchError, t: ['DispatchError'] }, + { c: DispatchResult, t: ['DispatchResult'] }, { c: Raw, t: ['Raw', 'Keys'] }, { c: Enum, t: ['Enum'] }, { c: Hash256, t: ['Hash', 'H256'] },