DispatchResult expansion (#5318)
This commit is contained in:
parent
8839ec5bc1
commit
24fdf6df1e
@ -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));
|
||||
}
|
||||
|
||||
56
packages/react-params/src/Param/DispatchResult.tsx
Normal file
56
packages/react-params/src/Param/DispatchResult.tsx
Normal file
@ -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<Props> {
|
||||
const { defaultValue, isDisabled, label } = props;
|
||||
const dispatchError = useMemo(
|
||||
() => defaultValue && isDispatchResultErr(defaultValue.value)
|
||||
? { isValid: true, value: defaultValue.value.asErr }
|
||||
: null,
|
||||
[defaultValue]
|
||||
);
|
||||
|
||||
if (!isDisabled) {
|
||||
return <Unknown {...props} />;
|
||||
} else if (!dispatchError) {
|
||||
return (
|
||||
<Static
|
||||
{...props}
|
||||
defaultValue={{ isValid: true, value: 'Ok' }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<DispatchError
|
||||
{...props}
|
||||
childrenPre={
|
||||
<Input
|
||||
className='full'
|
||||
isDisabled
|
||||
label={label}
|
||||
value='Err'
|
||||
/>
|
||||
}
|
||||
defaultValue={dispatchError}
|
||||
label='DispatchError'
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(DispatchResultDisplay);
|
||||
@ -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<Props> {
|
||||
function StaticParam ({ asHex, children, childrenPre, className = '', defaultValue, label }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const value = defaultValue && (defaultValue.value as string) && (
|
||||
asHex
|
||||
@ -36,6 +37,7 @@ function StaticParam ({ asHex, children, className = '', defaultValue, label }:
|
||||
|
||||
return (
|
||||
<Bare className={className}>
|
||||
{childrenPre}
|
||||
<Static
|
||||
className='full'
|
||||
label={label}
|
||||
|
||||
@ -1,13 +1,17 @@
|
||||
// Copyright 2017-2021 @polkadot/react-params authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Props } from '../types';
|
||||
import type { Props as BaseProps } from '../types';
|
||||
|
||||
import React from 'react';
|
||||
|
||||
import BaseBytes from './BaseBytes';
|
||||
import Static from './Static';
|
||||
|
||||
interface Props extends BaseProps {
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
|
||||
function Unknown (props: Props): React.ReactElement<Props> {
|
||||
const { className = '', defaultValue, isDisabled, isError, label, name, onChange, onEnter, onEscape, type } = props;
|
||||
|
||||
|
||||
@ -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'] },
|
||||
|
||||
Loading…
Reference in New Issue
Block a user