2021-02-04 19:15:23 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
import Identicon from '../../../../components/ui/identicon';
|
|
|
|
import TextField from '../../../../components/ui/text-field';
|
|
|
|
import { CONTACT_LIST_ROUTE } from '../../../../helpers/constants/routes';
|
2021-05-17 21:00:59 +02:00
|
|
|
import { isValidDomainName } from '../../../../helpers/utils/util';
|
2021-02-04 19:15:23 +01:00
|
|
|
import EnsInput from '../../../send/send-content/add-recipient/ens-input';
|
|
|
|
import PageContainerFooter from '../../../../components/ui/page-container/page-container-footer';
|
2021-05-17 21:00:59 +02:00
|
|
|
import {
|
|
|
|
isBurnAddress,
|
|
|
|
isValidHexAddress,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from '../../../../../shared/modules/hexstring-utils';
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
export default class AddContact extends PureComponent {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
addToAddressBook: PropTypes.func,
|
|
|
|
history: PropTypes.object,
|
|
|
|
scanQrCode: PropTypes.func,
|
2020-11-03 00:41:28 +01:00
|
|
|
qrCodeData:
|
|
|
|
PropTypes.object /* eslint-disable-line react/no-unused-prop-types */,
|
2019-07-31 21:56:44 +02:00
|
|
|
qrCodeDetected: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
state = {
|
2019-12-03 16:30:03 +01:00
|
|
|
newName: '',
|
2019-07-31 21:56:44 +02:00
|
|
|
ethAddress: '',
|
|
|
|
ensAddress: '',
|
|
|
|
error: '',
|
|
|
|
ensError: '',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(props) {
|
2021-02-04 19:15:23 +01:00
|
|
|
super(props);
|
|
|
|
this.dValidate = debounce(this.validate, 1000);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2019-07-31 21:56:44 +02:00
|
|
|
if (nextProps.qrCodeData) {
|
|
|
|
if (nextProps.qrCodeData.type === 'address') {
|
2021-02-04 19:15:23 +01:00
|
|
|
const scannedAddress = nextProps.qrCodeData.values.address.toLowerCase();
|
|
|
|
const currentAddress = this.state.ensAddress || this.state.ethAddress;
|
2019-07-31 21:56:44 +02:00
|
|
|
if (currentAddress.toLowerCase() !== scannedAddress) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ ethAddress: scannedAddress, ensAddress: '' });
|
2019-07-31 21:56:44 +02:00
|
|
|
// Clean up QR code data after handling
|
2021-02-04 19:15:23 +01:00
|
|
|
this.props.qrCodeDetected(null);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
validate = (address) => {
|
2021-05-17 21:00:59 +02:00
|
|
|
const valid =
|
|
|
|
!isBurnAddress(address) &&
|
|
|
|
isValidHexAddress(address, { mixedCaseUseChecksum: true });
|
2021-02-04 19:15:23 +01:00
|
|
|
const validEnsAddress = isValidDomainName(address);
|
2020-10-21 18:35:56 +02:00
|
|
|
|
2019-07-31 21:56:44 +02:00
|
|
|
if (valid || validEnsAddress || address === '') {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ error: '', ethAddress: address });
|
2019-07-31 21:56:44 +02:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ error: 'Invalid Address' });
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderInput() {
|
2019-07-31 21:56:44 +02:00
|
|
|
return (
|
|
|
|
<EnsInput
|
2020-02-15 21:34:12 +01:00
|
|
|
scanQrCode={(_) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.props.scanQrCode();
|
2019-11-20 01:03:20 +01:00
|
|
|
}}
|
2019-07-31 21:56:44 +02:00
|
|
|
onChange={this.dValidate}
|
2020-02-15 21:34:12 +01:00
|
|
|
onPaste={(text) => this.setState({ ethAddress: text })}
|
2019-07-31 21:56:44 +02:00
|
|
|
onReset={() => this.setState({ ethAddress: '', ensAddress: '' })}
|
2020-02-15 21:34:12 +01:00
|
|
|
updateEnsResolution={(address) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ ensAddress: address, error: '', ensError: '' });
|
2019-07-31 21:56:44 +02:00
|
|
|
}}
|
2020-11-03 00:41:28 +01:00
|
|
|
updateEnsResolutionError={(message) =>
|
|
|
|
this.setState({ ensError: message })
|
|
|
|
}
|
2020-10-21 18:35:56 +02:00
|
|
|
value={this.state.ethAddress || ''}
|
2019-07-31 21:56:44 +02:00
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
|
|
|
const { history, addToAddressBook } = this.props;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const errorToRender = this.state.ensError || this.state.error;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="settings-page__content-row address-book__add-contact">
|
2019-12-03 17:35:44 +01:00
|
|
|
{this.state.ensAddress && (
|
|
|
|
<div className="address-book__view-contact__group">
|
|
|
|
<Identicon address={this.state.ensAddress} diameter={60} />
|
|
|
|
<div className="address-book__view-contact__group__value">
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.state.ensAddress}
|
2019-12-03 17:35:44 +01:00
|
|
|
</div>
|
2019-07-31 22:17:11 +02:00
|
|
|
</div>
|
2019-12-03 17:35:44 +01:00
|
|
|
)}
|
2019-07-31 21:56:44 +02:00
|
|
|
<div className="address-book__add-contact__content">
|
2019-07-31 22:17:11 +02:00
|
|
|
<div className="address-book__view-contact__group">
|
2019-07-31 21:56:44 +02:00
|
|
|
<div className="address-book__view-contact__group__label">
|
2020-11-03 00:41:28 +01:00
|
|
|
{t('userName')}
|
2019-07-31 21:56:44 +02:00
|
|
|
</div>
|
|
|
|
<TextField
|
|
|
|
type="text"
|
|
|
|
id="nickname"
|
|
|
|
value={this.state.newName}
|
2020-02-15 21:34:12 +01:00
|
|
|
onChange={(e) => this.setState({ newName: e.target.value })}
|
2019-07-31 21:56:44 +02:00
|
|
|
fullWidth
|
|
|
|
margin="dense"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="address-book__view-contact__group">
|
|
|
|
<div className="address-book__view-contact__group__label">
|
2020-11-03 00:41:28 +01:00
|
|
|
{t('ethereumPublicAddress')}
|
2019-07-31 21:56:44 +02:00
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.renderInput()}
|
|
|
|
{errorToRender && (
|
|
|
|
<div className="address-book__add-contact__error">
|
|
|
|
{errorToRender}
|
|
|
|
</div>
|
|
|
|
)}
|
2019-07-31 21:56:44 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<PageContainerFooter
|
|
|
|
cancelText={this.context.t('cancel')}
|
|
|
|
disabled={Boolean(this.state.error)}
|
2020-04-29 06:59:49 +02:00
|
|
|
onSubmit={async () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
await addToAddressBook(
|
|
|
|
this.state.ensAddress || this.state.ethAddress,
|
|
|
|
this.state.newName,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
history.push(CONTACT_LIST_ROUTE);
|
2019-07-31 21:56:44 +02:00
|
|
|
}}
|
|
|
|
onCancel={() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(CONTACT_LIST_ROUTE);
|
2019-07-31 21:56:44 +02:00
|
|
|
}}
|
|
|
|
submitText={this.context.t('save')}
|
2019-11-18 16:08:47 +01:00
|
|
|
submitButtonType="confirm"
|
2019-07-31 21:56:44 +02:00
|
|
|
/>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
}
|