Files

172 lines
4.9 KiB
TypeScript
Raw Permalink Normal View History

2017-12-18 16:23:31 -05:00
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { addHexPrefix } from 'ethereumjs-util';
import translate, { translateRaw } from 'translations';
import { isValidENSAddress } from 'libs/validators';
2018-03-22 14:30:51 -04:00
import { Address } from 'libs/units';
import { ICurrentTo } from 'features/types';
import { AppState } from 'features/reducers';
import * as selectors from 'features/selectors';
import { walletSelectors } from 'features/wallet';
import { ensSelectors } from 'features/ens';
import { Identicon, Spinner } from 'components/ui';
import { Query } from 'components/renderCbs';
import { CallbackProps } from 'components/AddressFieldFactory';
2018-05-21 18:10:51 -05:00
import AddressFieldDropdown from './AddressFieldDropdown';
2018-03-22 14:30:51 -04:00
import './AddressInputFactory.scss';
2017-12-18 16:23:31 -05:00
interface StateProps {
currentTo: ICurrentTo;
2018-05-21 18:10:51 -05:00
label: string | null;
2017-12-18 16:23:31 -05:00
isValid: boolean;
2018-05-21 18:10:51 -05:00
isLabelEntry: boolean;
isResolving: boolean;
2017-12-18 16:23:31 -05:00
}
2017-12-18 16:23:31 -05:00
interface OwnProps {
2018-03-22 14:30:51 -04:00
isSelfAddress?: boolean;
2018-05-21 18:10:51 -05:00
showLabelMatch?: boolean;
2018-07-12 12:49:21 -05:00
showIdenticon?: boolean;
2018-05-21 18:10:51 -05:00
isFocused?: boolean;
2018-07-12 12:49:21 -05:00
className?: string;
value?: string;
dropdownThreshold?: number;
2017-12-18 16:23:31 -05:00
onChange(ev: React.FormEvent<HTMLInputElement>): void;
2018-07-12 13:04:22 -05:00
onChangeOverride?(ev: React.FormEvent<HTMLInputElement>): void;
2018-05-21 18:10:51 -05:00
onFocus(ev: React.FormEvent<HTMLInputElement>): void;
onBlur(ev: React.FormEvent<HTMLInputElement>): void;
2018-01-02 12:04:50 -06:00
withProps(props: CallbackProps): React.ReactElement<any> | null;
2017-12-18 16:23:31 -05:00
}
const ENSStatus: React.SFC<{ isLoading: boolean; ensAddress: string; rawAddress: string }> = ({
isLoading,
ensAddress,
rawAddress
}) => {
const isENS = isValidENSAddress(ensAddress);
const text = translate('LOADING_ENS_ADDRESS');
if (isLoading) {
return (
2018-03-22 14:30:51 -04:00
<React.Fragment>
<Spinner /> {text}
2018-03-22 14:30:51 -04:00
</React.Fragment>
);
} else {
2018-03-22 14:30:51 -04:00
return isENS ? <React.Fragment>{`Resolved Address: ${rawAddress}`}</React.Fragment> : null;
}
};
2017-12-18 16:23:31 -05:00
type Props = OwnProps & StateProps;
2018-01-02 12:04:50 -06:00
class AddressInputFactoryClass extends Component<Props> {
2017-12-18 16:23:31 -05:00
public render() {
2018-05-21 18:10:51 -05:00
const {
label,
currentTo,
onChange,
onFocus,
onBlur,
isValid,
isLabelEntry,
withProps,
showLabelMatch,
isSelfAddress,
isResolving,
2018-07-12 12:49:21 -05:00
isFocused,
showIdenticon = true,
onChangeOverride,
value,
dropdownThreshold
2018-05-21 18:10:51 -05:00
} = this.props;
const inputClassName = `AddressInput-input ${label ? 'AddressInput-input-with-label' : ''}`;
const sendingTo = label
? translateRaw('SENDING_TO', {
$label: label
})
: '';
2018-05-21 18:10:51 -05:00
const isENSAddress = currentTo.raw.includes('.eth');
2018-07-12 12:49:21 -05:00
/**
* @desc Initially set the address to the passed value.
* If there wasn't a value passed, use the value from the redux store.
*/
let addr = value;
if (addr == null) {
addr = addHexPrefix(currentTo.value ? currentTo.value.toString('hex') : '0');
}
2018-07-12 15:23:23 -05:00
/**
* @desc If passed a value and an onChangeOverride function,
* infer that the dropdown should be uncontrolled.
*/
const controlled = value == null && !onChangeOverride;
2017-12-18 16:23:31 -05:00
return (
2018-03-22 14:30:51 -04:00
<div className="AddressInput form-group">
2018-05-21 18:10:51 -05:00
<div className={inputClassName}>
2017-12-18 16:23:31 -05:00
<Query
params={['readOnly']}
2018-01-02 12:04:50 -06:00
withQuery={({ readOnly }) =>
2018-01-08 02:12:16 -05:00
withProps({
currentTo,
isValid,
2018-05-21 18:10:51 -05:00
isLabelEntry,
2018-01-08 02:12:16 -05:00
onChange,
2018-05-21 18:10:51 -05:00
onFocus,
onBlur,
2018-03-22 14:30:51 -04:00
readOnly: !!(readOnly || this.props.isResolving || isSelfAddress)
2018-01-08 02:12:16 -05:00
})
2018-01-02 12:04:50 -06:00
}
2017-12-18 16:23:31 -05:00
/>
<ENSStatus ensAddress={currentTo.raw} isLoading={isResolving} rawAddress={addr} />
2018-07-12 12:49:21 -05:00
{isFocused &&
!isENSAddress && (
<AddressFieldDropdown
2018-07-12 15:23:23 -05:00
controlled={controlled}
2018-07-12 12:49:21 -05:00
value={value}
2018-07-12 15:23:23 -05:00
onChangeOverride={onChangeOverride}
2018-07-12 12:49:21 -05:00
dropdownThreshold={dropdownThreshold}
/>
)}
2018-05-21 18:10:51 -05:00
{showLabelMatch &&
label && (
<div title={sendingTo} className="AddressInput-input-label">
<i className="fa fa-check" /> {sendingTo}
</div>
)}
2017-12-18 16:23:31 -05:00
</div>
2018-07-12 12:49:21 -05:00
{showIdenticon && (
<div className="AddressInput-identicon">
<Identicon address={addr} />
</div>
)}
2017-12-18 16:23:31 -05:00
</div>
);
}
}
2018-03-22 14:30:51 -04:00
export const AddressInputFactory = connect((state: AppState, ownProps: OwnProps) => {
let currentTo: ICurrentTo;
if (ownProps.isSelfAddress) {
const wallet = walletSelectors.getWalletInst(state);
2018-03-22 14:30:51 -04:00
const addr = wallet ? wallet.getAddressString() : '';
currentTo = {
raw: addr,
value: Address(addr)
};
} else {
currentTo = selectors.getCurrentTo(state);
2018-03-22 14:30:51 -04:00
}
return {
currentTo,
label: selectors.getCurrentToLabel(state),
isResolving: ensSelectors.getResolvingDomain(state),
isValid: selectors.isValidCurrentTo(state),
isLabelEntry: selectors.isCurrentToLabelEntry(state)
2018-03-22 14:30:51 -04:00
};
})(AddressInputFactoryClass);