Support Double Map on app-storage (#1457)

* DoubleMap support

* InputStorage support

* fixes

* merge props and created values

* rv console
This commit is contained in:
kwingram25 2019-07-30 15:34:00 +01:00 committed by GitHub
parent 16d5e1a35e
commit f3d158662a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 88 additions and 28 deletions

View File

@ -23,6 +23,7 @@ type Props = ComponentProps & ApiProps & I18nProps;
interface State {
isValid: boolean;
key: StorageEntryPromise;
defaultValues?: RawParams | null;
values: RawParams;
params: { type: TypeDef }[];
}
@ -48,7 +49,7 @@ class Modules extends TxComponent<Props, State> {
public render (): React.ReactNode {
const { t } = this.props;
const { isValid, key: { creator: { method, section, meta } }, params } = this.state;
const { isValid, key: { creator: { method, section, meta } }, defaultValues, params } = this.state;
return (
<section className='storage--actionrow'>
@ -64,6 +65,7 @@ class Modules extends TxComponent<Props, State> {
onChange={this.onChangeParams}
onEnter={this.submit}
params={params}
values={defaultValues}
/>
</div>
<div className='storage--actionrow-buttons'>
@ -83,29 +85,65 @@ class Modules extends TxComponent<Props, State> {
this.setState(
(prevState: State): Pick<State, never> => {
const { key = prevState.key, values = prevState.values } = newState;
const hasParam = key.creator.meta.type.isMap;
const isValid = values.length === (hasParam ? 1 : 0) &&
values.reduce(
(isValid, value): boolean =>
const areParamsValid = (): boolean => {
return values.reduce(
(isValid: boolean, value): boolean => (
isValid &&
!isUndefined(value) &&
!isUndefined(value.value) &&
value.isValid,
value.isValid),
true
);
};
if (key.creator.meta.type.isDoubleMap) {
const key1 = key.creator.meta.type.asDoubleMap.key1.toString();
const key2 = key.creator.meta.type.asDoubleMap.key2.toString();
return {
defaultValues: this.getDefaultValues(),
isValid: values.length === 2 && areParamsValid(),
key,
params: [
{ type: getTypeDef(key1) },
{ type: getTypeDef(key2) }
],
values
};
}
const hasParam = key.creator.meta.type.isMap;
const isValid = values.length === (hasParam ? 1 : 0) && areParamsValid();
return {
defaultValues: null,
isValid,
key,
values,
params: hasParam
? [{ type: getTypeDef(key.creator.meta.type.asMap.key.toString()) }]
: []
: [],
values
};
}
);
}
private getDefaultValues = (): RawParams | null => {
const { api } = this.props;
const { key } = this.state;
if (key.creator.section === 'session') {
return [
{
isValid: true,
value: api.consts.session.dedupKeyPrefix.toHex()
}
];
}
return null;
}
private onAdd = (): void => {
const { onAdd } = this.props;
const { key, values } = this.state;

View File

@ -21,9 +21,23 @@ export default function createOptions (api: ApiPromise, sectionName: string): Dr
.map((value): DropdownOption => {
const method = section[value] as unknown as StorageEntry;
const type = method.meta.type;
const input = type.isMap
? type.asMap.key.toString()
: '';
let input = '';
if (type.isMap) {
input = type.asMap.key.toString();
} else if (type.isDoubleMap) {
input = type.asDoubleMap.key1.toString() + ', ' + type.asDoubleMap.key2.toString();
}
let output = type.toString();
if (type.isDoubleMap) {
output = `DoubleMap<${type.asDoubleMap.value.toString()}>`;
}
if (method.meta.modifier.isOptional) {
output = `Option<${output}>`;
}
return {
className: 'ui--DropdownLinked-Item',
@ -33,11 +47,7 @@ export default function createOptions (api: ApiPromise, sectionName: string): Dr
className='ui--DropdownLinked-Item-call'
key={`${sectionName}_${value}:call`}
>
{value}({input}): {
method.meta.modifier.isOptional
? `Option<${type}>`
: type.toString()
}
{value}({input}): {output}
</div>,
<div
className='ui--DropdownLinked-Item-text'

View File

@ -35,7 +35,7 @@ interface TypeToComponent {
}
const components: ComponentMap = ([
{ c: Account, t: ['AccountId', 'AccountIdOf', 'Address', 'AuthorityId', 'SessionKey'] },
{ c: Account, t: ['AccountId', 'AccountIdOf', 'Address', 'AuthorityId', 'SessionKey', 'ValidatorId'] },
{ c: Amount, t: ['AccountIndex', 'BlockNumber', 'Gas', 'Index', 'Nonce', 'ParaId', 'ProposalIndex', 'PropIndex', 'ReferendumIndex', 'u16', 'u32', 'u64', 'u128', 'u256', 'VoteIndex'] },
{ c: Balance, t: ['Amount', 'AssetOf', 'Balance', 'BalanceOf'] },
{ c: Bool, t: ['bool'] },

View File

@ -13,7 +13,7 @@ import { classes } from '@polkadot/ui-app/util';
import translate from '@polkadot/ui-app/translate';
import Param from './Param';
import createValues from './values';
import { createValue } from './values';
interface Param {
name?: string;
@ -26,7 +26,7 @@ interface Props extends I18nProps {
onEnter?: () => void;
overrides?: ComponentMap;
params: Param[];
values?: RawParams;
values?: RawParams | null;
}
interface State {
@ -54,7 +54,16 @@ class Params extends React.PureComponent<Props, State> {
return null;
}
const values = createValues(props.params);
const values = props.params.reduce(
(result: RawParams, param, index): RawParams => [
...result,
props.values && props.values[index]
? props.values[index]
: createValue(param)
],
[]
);
const handlers = values.map(
(_, index): RawParamOnChange =>
(value: RawParamOnChangeValue): void =>

View File

@ -88,6 +88,7 @@ export default function getInitValue (def: TypeDef): RawParamValue | RawParamVal
case 'Signature':
case 'SessionKey':
case 'StorageKey':
case 'ValidatorId':
return void 0;
case 'Extrinsic':

View File

@ -9,13 +9,15 @@ import { isUndefined } from '@polkadot/util';
import getInitValue from './initValue';
export default function values (params: { type: TypeDef }[]): RawParam[] {
return params.map(({ type }): RawParam => {
const value = getInitValue(type);
export function createValue (param: { type: TypeDef }): RawParam {
const value = getInitValue(param.type);
return {
isValid: !isUndefined(value),
value
};
});
return {
isValid: !isUndefined(value),
value
};
}
export default function createValues (params: { type: TypeDef }[]): RawParam[] {
return params.map(createValue);
}