DispatchResult expansion (#5318)

This commit is contained in:
Jaco 2021-05-19 12:30:24 +03:00 committed by GitHub
parent 8839ec5bc1
commit 24fdf6df1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 3 deletions

View File

@ -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));
}

View 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);

View File

@ -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}

View File

@ -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;

View File

@ -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'] },