Provide fallback for tx defaults (Extrinsic, Proposal) (#576)

This commit is contained in:
Jaco Greeff 2019-01-10 10:00:05 +01:00 committed by GitHub
parent 274c9f32cc
commit 312f57f302
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 10 deletions

View File

@ -14,12 +14,19 @@ type Props = ApiProps & BaseProps;
class Call extends React.PureComponent<Props> {
render () {
const { apiPromise, className, isDisabled, isError, label, onChange, style, withLabel } = this.props;
const { apiDefaultTx, apiPromise, className, isDisabled, isError, label, onChange, style, withLabel } = this.props;
const defaultValue = (() => {
try {
return apiPromise.tx.balances.transfer;
} catch (error) {
return apiDefaultTx;
}
})();
return (
<Extrinsic
className={className}
defaultValue={apiPromise.tx.balances.transfer}
defaultValue={defaultValue}
isDisabled={isDisabled}
isError={isError}
isPrivate={false}

View File

@ -39,7 +39,7 @@ class ExtrinsicDisplay extends React.PureComponent<Props> {
label
}
})}
labelSection={t('proposal.method', {
labelSection={t('proposal.section', {
defaultValue: '{{label}} (section)',
replace: {
label

View File

@ -15,12 +15,19 @@ type Props = ApiProps & BaseProps;
class ProposalDisplay extends React.PureComponent<Props> {
render () {
const { apiPromise, className, isDisabled, isError, label, style, withLabel } = this.props;
const { apiDefaultTx, apiPromise, className, isDisabled, isError, label, style, withLabel } = this.props;
const defaultValue = (() => {
try {
return apiPromise.tx.consensus.setCode;
} catch (error) {
return apiDefaultTx;
}
})();
return (
<ExtrinsicDisplay
className={className}
defaultValue={apiPromise.tx.consensus.setCode}
defaultValue={defaultValue}
isDisabled={isDisabled}
isError={isError}
isPrivate

View File

@ -36,8 +36,15 @@ class Selection extends React.PureComponent<Props, State> {
} as State;
render () {
const { apiPromise, t } = this.props;
const { apiDefaultTx, apiPromise, t } = this.props;
const { isValid, accountId } = this.state;
const defaultExtrinsic = (() => {
try {
return apiPromise.tx.balances.transfer;
} catch (error) {
return apiDefaultTx;
}
})();
return (
<div className='extrinsics--Selection'>
@ -50,7 +57,7 @@ class Selection extends React.PureComponent<Props, State> {
type='account'
/>
<ExtrinsicDisplay
defaultValue={apiPromise.tx.balances.transfer}
defaultValue={defaultExtrinsic}
labelMethod={t('display.method', {
defaultValue: 'submit the following extrinsic'
})}

View File

@ -50,7 +50,7 @@ export default class ApiWrapper extends React.PureComponent<Props, State> {
isApiReady: false,
apiPromise: new ApiPromise(provider),
setApiUrl
};
} as State;
}
componentDidMount () {
@ -116,16 +116,23 @@ export default class ApiWrapper extends React.PureComponent<Props, State> {
private subscribeIsReady = (api: ApiPromise) => {
api.on('ready', () => {
this.setState({ isApiReady: true });
const section = Object.keys(api.tx)[0];
const method = Object.keys(api.tx[section])[0];
this.setState({
isApiReady: true,
apiDefaultTx: api.tx[section][method]
});
});
}
render () {
const { apiPromise, chain, isApiConnected, isApiReady, setApiUrl } = this.state;
const { apiDefaultTx, apiPromise, chain, isApiConnected, isApiReady, setApiUrl } = this.state;
return (
<ApiContext.Provider
value={{
apiDefaultTx,
apiPromise,
isApiConnected,
isApiReady: isApiReady && !!chain,

View File

@ -2,6 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SubmittableExtrinsicFunction } from '@polkadot/api/promise/types';
import ApiPromise from '@polkadot/api/promise';
export type BareProps = {
@ -12,6 +13,7 @@ export type BareProps = {
export type ApiProps = {
isApiConnected: boolean,
isApiReady: boolean,
apiDefaultTx: SubmittableExtrinsicFunction,
apiPromise: ApiPromise,
setApiUrl: (url?: string) => void
};