Be explicit in State returns (#58)

* Be explicit in State returns

* Cleanup Param properties

* Cleanup casting

* Cleanup type manipulations

* Don't allow null for InputAddress defaultValue
This commit is contained in:
Jaco Greeff 2018-05-25 17:57:51 +02:00 committed by GitHub
parent ccae56beca
commit 004824dbec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 41 additions and 38 deletions

View File

@ -13,9 +13,10 @@ import IdentityIcon from '@polkadot/ui-react/IdentityIcon';
import Balance from '@polkadot/ui-react-rx/Balance';
import Nonce from '@polkadot/ui-react-rx/Nonce';
import addressDecode from '@polkadot/util-keyring/address/decode';
import addressEncode from '@polkadot/util-keyring/address/encode';
type Props = BareProps & {
value: string | null;
value: string;
}
type State = {
@ -31,13 +32,10 @@ const DEFAULT_SHORT = `${DEFAULT_ADDR.slice(0, 7)}…${DEFAULT_ADDR.slice(-7)}`;
export default class Address extends React.PureComponent<Props, State> {
state: State = ({}: $Shape<State>);
static getDerivedStateFromProps ({ value }: Props, { publicKey, shortValue }: State): State {
// flowlint-next-line sketchy-null-string:off
const address = value || DEFAULT_ADDR;
static getDerivedStateFromProps ({ value }: Props, { address, publicKey, shortValue }: State): State {
try {
// $FlowFixMe yes, we expect a throw when invalid
publicKey = addressDecode(value);
address = addressEncode(publicKey);
shortValue = `${address.slice(0, 7)}…${address.slice(-7)}`;
} catch (error) {
publicKey = null;

View File

@ -76,7 +76,7 @@ class Creator extends React.PureComponent<Props, State> {
value={
isSeedValid
? address
: null
: ''
}
/>
{this.renderInput()}
@ -171,7 +171,7 @@ class Creator extends React.PureComponent<Props, State> {
nextState (newState: $Shape<State>): void {
this.setState(
(prevState: State, props: Props): $Shape<State> => {
(prevState: State, props: Props): State => {
const { name = prevState.name, password = prevState.password, seed = prevState.seed } = newState;
let address = prevState.address;
const isNameValid = !!name;

View File

@ -127,13 +127,14 @@ class Creator extends React.PureComponent<Props, State> {
nextState (newState: $Shape<State>): void {
this.setState(
(prevState: State, props: Props): $Shape<State> => {
(prevState: State, props: Props): State => {
const { address = prevState.address, name = prevState.name } = newState;
let isAddressValid = true;
let newAddress = address;
try {
addressEncode(
newAddress = addressEncode(
addressDecode(address)
);
} catch (error) {
@ -143,7 +144,7 @@ class Creator extends React.PureComponent<Props, State> {
const isNameValid = !!name;
return {
address,
address: newAddress,
isAddressValid,
isNameValid,
isValid: isAddressValid && isNameValid,

View File

@ -25,8 +25,7 @@ type State = {
currentAddress: KeyringAddress | null,
defaultPublicKey?: Uint8Array,
editedName: string,
isEdited: boolean,
info: KeyringAddress | null
isEdited: boolean
}
class Editor extends React.PureComponent<Props, State> {
@ -157,7 +156,7 @@ class Editor extends React.PureComponent<Props, State> {
nextState (newState?: $Shape<State> = {}): void {
this.setState(
(prevState: State): $Shape<State> => {
(prevState: State): State => {
let { currentAddress = prevState.currentAddress, editedName = prevState.editedName } = newState;
const previous = prevState.currentAddress || { address: () => null };
let isEdited = false;

View File

@ -32,15 +32,16 @@ type State = {
values: Array<RawParam>
};
export default class ExtrinsicDisplay extends React.PureComponent<Props, State> {
export default class Extrinsic extends React.PureComponent<Props, State> {
state: State;
constructor (props: Props) {
super(props);
this.state = ({
extrinsic: props.defaultValue
}: $Shape<State>);
this.state = {
extrinsic: props.defaultValue,
values: []
};
}
render (): React$Node {

View File

@ -38,6 +38,7 @@ class Query extends React.PureComponent<Props, State> {
if (!cache[id]) {
const values = params.map(({ value }) => value);
// $FlowFixMe we SHOULD be transforming these before passing
cache[id] = withStorageDiv(key, { params: values })(
(value) =>
valueToText(key.type, value),

View File

@ -4,10 +4,10 @@
// @flow
import type { Storage$Key } from '@polkadot/storage/types';
import type { RawParams } from '@polkadot/ui-app/Params/types';
export type StorageQuery = {
id: number,
key: Storage$Key,
// flowlint-next-line unclear-type:off
params: Array<any>
params: RawParams
}

View File

@ -41,7 +41,7 @@ class Unlock extends React.PureComponent<Props, State> {
unlockError: null
};
static getDerivedStateFromProps ({ pair }: Props, prevState: State): $Shape<State> {
static getDerivedStateFromProps ({ pair }: Props, prevState: State): State {
const address = pair
? pair.address()
: '';

View File

@ -151,7 +151,7 @@ class Verify extends React.PureComponent<Props, State> {
nextState (newState: $Shape<State>): void {
this.setState(
(prevState: State): $Shape<State> => {
(prevState: State): State => {
const { isHexData = prevState.isHexData, isValidAddress = prevState.isValidAddress, isValidSignature = prevState.isValidSignature, currentPublicKey = prevState.currentPublicKey, data = prevState.data, signature = prevState.signature } = newState;
let isValid = isValidAddress && isValidSignature;

View File

@ -25,8 +25,7 @@ type Props = BareProps & {
label?: React$Node,
max?: mixed,
min?: mixed,
// flowlint-next-line unclear-type:off
onChange: (value: any) => void,
onChange: (value: string) => void,
placeholder?: string,
type?: Input$Type,
value?: mixed,

View File

@ -6,7 +6,7 @@
import addressDecode from '@polkadot/util-keyring/address/decode';
import addressEncode from '@polkadot/util-keyring/address/encode';
export default function addressToAddress (value?: string | Uint8Array | null): ?string {
export default function addressToAddress (value?: string | Uint8Array): ?string {
// flowlint-next-line sketchy-null-string:off
if (value) {
try {

View File

@ -19,7 +19,7 @@ import classes from '../util/classes';
import addressToAddress from './addressToAddress';
type Props = BareProps & {
defaultValue?: string | Uint8Array | null,
defaultValue?: string | Uint8Array,
hideAddress?: boolean;
isError?: boolean,
isInput?: boolean,

View File

@ -37,7 +37,7 @@ export default class Amount extends React.PureComponent<Props> {
);
}
onChange = (value: number): void => {
onChange = (value: string): void => {
const { onChange } = this.props;
onChange({

View File

@ -28,6 +28,12 @@ type State = {
}
};
type LoadEvent = {
target: { // eslint-disable-line react/no-unused-prop-types
result: ArrayBuffer
}
};
class BytesFile extends React.PureComponent<Props, State> {
state: State = {};
@ -72,9 +78,8 @@ class BytesFile extends React.PureComponent<Props, State> {
reader.onabort = () => {};
reader.onerror = () => {};
reader.onload = () => {
// flowlint-next-line unclear-type:off
const data = new Uint8Array((((reader.result): any): ArrayBuffer));
reader.onload = ({ target: { result } }: LoadEvent) => {
const data = new Uint8Array(result);
onChange(data);

View File

@ -9,9 +9,10 @@ import React from 'react';
import BaseBytes from './BaseBytes';
export default function Hash ({ defaultValue, isDisabled, isError, label, name, onChange, withLabel }: Props): React$Node {
export default function Hash ({ className, defaultValue, isDisabled, isError, label, name, onChange, style, withLabel }: Props): React$Node {
return (
<BaseBytes
className={className}
defaultValue={defaultValue}
isDisabled={isDisabled}
isError={isError}
@ -20,6 +21,7 @@ export default function Hash ({ defaultValue, isDisabled, isError, label, name,
name={name}
onChange={onChange}
size='medium'
style={style}
withLabel={withLabel}
/>
);

View File

@ -15,7 +15,6 @@ import translate from '../../translate';
import findComponent from './findComponent';
type Props = I18nProps & BaseProps & {
index: number,
overrides?: ComponentMap
};
@ -43,15 +42,15 @@ class ParamComponent extends React.PureComponent<Props, State> {
return null;
}
const { className, defaultValue, index, name, onChange, style } = this.props;
const { className, defaultValue, name, onChange, style } = this.props;
const type = typeToString(defaultValue.type);
return (
<Component
className={classes('ui--Param', className)}
defaultValue={defaultValue}
key={`${name}:${type}:${index}}`}
label={`${name}: ${type} (${index})`}
key={`${name}:${type}`}
label={`${name}: ${type}`}
name={name}
onChange={onChange}
style={style}

View File

@ -89,12 +89,10 @@ class Params extends React.PureComponent<Props, State> {
{paramNames.map((paramName, index) => (
<Param
defaultValue={values[index]}
index={index}
key={`${name}:${paramName}:${index}`}
name={paramName}
onChange={handlers[index]}
overrides={overrides}
type={params[paramName].type}
/>
))}
</div>

View File

@ -31,7 +31,7 @@ type State = {
class Unlock extends React.PureComponent<Props, State> {
state: State = ({}: $Shape<State>);
static getDerivedStateFromProps ({ error, password, value }: Props): $Shape<State> {
static getDerivedStateFromProps ({ error, password, value }: Props): State {
const pair = keyring.getPair(value);
const isLocked = !pair.hasSecretKey();