You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
4.6 KiB
149 lines
4.6 KiB
4 years ago
|
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';
|
||
4 years ago
|
import {
|
||
|
isValidAddress,
|
||
|
isValidDomainName,
|
||
4 years ago
|
} from '../../../../helpers/utils/util';
|
||
|
import EnsInput from '../../../send/send-content/add-recipient/ens-input';
|
||
|
import PageContainerFooter from '../../../../components/ui/page-container/page-container-footer';
|
||
5 years ago
|
|
||
|
export default class AddContact extends PureComponent {
|
||
|
static contextTypes = {
|
||
|
t: PropTypes.func,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
|
static propTypes = {
|
||
|
addToAddressBook: PropTypes.func,
|
||
|
history: PropTypes.object,
|
||
|
scanQrCode: PropTypes.func,
|
||
4 years ago
|
qrCodeData:
|
||
|
PropTypes.object /* eslint-disable-line react/no-unused-prop-types */,
|
||
5 years ago
|
qrCodeDetected: PropTypes.func,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
|
state = {
|
||
5 years ago
|
newName: '',
|
||
5 years ago
|
ethAddress: '',
|
||
|
ensAddress: '',
|
||
|
error: '',
|
||
|
ensError: '',
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
constructor(props) {
|
||
4 years ago
|
super(props);
|
||
|
this.dValidate = debounce(this.validate, 1000);
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
||
5 years ago
|
if (nextProps.qrCodeData) {
|
||
|
if (nextProps.qrCodeData.type === 'address') {
|
||
4 years ago
|
const scannedAddress = nextProps.qrCodeData.values.address.toLowerCase();
|
||
|
const currentAddress = this.state.ensAddress || this.state.ethAddress;
|
||
5 years ago
|
if (currentAddress.toLowerCase() !== scannedAddress) {
|
||
4 years ago
|
this.setState({ ethAddress: scannedAddress, ensAddress: '' });
|
||
5 years ago
|
// Clean up QR code data after handling
|
||
4 years ago
|
this.props.qrCodeDetected(null);
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
validate = (address) => {
|
||
4 years ago
|
const valid = isValidAddress(address);
|
||
|
const validEnsAddress = isValidDomainName(address);
|
||
4 years ago
|
|
||
5 years ago
|
if (valid || validEnsAddress || address === '') {
|
||
4 years ago
|
this.setState({ error: '', ethAddress: address });
|
||
5 years ago
|
} else {
|
||
4 years ago
|
this.setState({ error: 'Invalid Address' });
|
||
5 years ago
|
}
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
renderInput() {
|
||
5 years ago
|
return (
|
||
|
<EnsInput
|
||
5 years ago
|
scanQrCode={(_) => {
|
||
4 years ago
|
this.props.scanQrCode();
|
||
5 years ago
|
}}
|
||
5 years ago
|
onChange={this.dValidate}
|
||
5 years ago
|
onPaste={(text) => this.setState({ ethAddress: text })}
|
||
5 years ago
|
onReset={() => this.setState({ ethAddress: '', ensAddress: '' })}
|
||
5 years ago
|
updateEnsResolution={(address) => {
|
||
4 years ago
|
this.setState({ ensAddress: address, error: '', ensError: '' });
|
||
5 years ago
|
}}
|
||
4 years ago
|
updateEnsResolutionError={(message) =>
|
||
|
this.setState({ ensError: message })
|
||
|
}
|
||
4 years ago
|
value={this.state.ethAddress || ''}
|
||
5 years ago
|
/>
|
||
4 years ago
|
);
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
render() {
|
||
4 years ago
|
const { t } = this.context;
|
||
|
const { history, addToAddressBook } = this.props;
|
||
5 years ago
|
|
||
4 years ago
|
const errorToRender = this.state.ensError || this.state.error;
|
||
5 years ago
|
|
||
|
return (
|
||
|
<div className="settings-page__content-row address-book__add-contact">
|
||
5 years ago
|
{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">
|
||
4 years ago
|
{this.state.ensAddress}
|
||
5 years ago
|
</div>
|
||
5 years ago
|
</div>
|
||
5 years ago
|
)}
|
||
5 years ago
|
<div className="address-book__add-contact__content">
|
||
5 years ago
|
<div className="address-book__view-contact__group">
|
||
5 years ago
|
<div className="address-book__view-contact__group__label">
|
||
4 years ago
|
{t('userName')}
|
||
5 years ago
|
</div>
|
||
|
<TextField
|
||
|
type="text"
|
||
|
id="nickname"
|
||
|
value={this.state.newName}
|
||
5 years ago
|
onChange={(e) => this.setState({ newName: e.target.value })}
|
||
5 years ago
|
fullWidth
|
||
|
margin="dense"
|
||
|
/>
|
||
|
</div>
|
||
|
|
||
|
<div className="address-book__view-contact__group">
|
||
|
<div className="address-book__view-contact__group__label">
|
||
4 years ago
|
{t('ethereumPublicAddress')}
|
||
5 years ago
|
</div>
|
||
4 years ago
|
{this.renderInput()}
|
||
|
{errorToRender && (
|
||
|
<div className="address-book__add-contact__error">
|
||
|
{errorToRender}
|
||
|
</div>
|
||
|
)}
|
||
5 years ago
|
</div>
|
||
|
</div>
|
||
|
<PageContainerFooter
|
||
|
cancelText={this.context.t('cancel')}
|
||
|
disabled={Boolean(this.state.error)}
|
||
5 years ago
|
onSubmit={async () => {
|
||
4 years ago
|
await addToAddressBook(
|
||
|
this.state.ensAddress || this.state.ethAddress,
|
||
|
this.state.newName,
|
||
4 years ago
|
);
|
||
|
history.push(CONTACT_LIST_ROUTE);
|
||
5 years ago
|
}}
|
||
|
onCancel={() => {
|
||
4 years ago
|
history.push(CONTACT_LIST_ROUTE);
|
||
5 years ago
|
}}
|
||
|
submitText={this.context.t('save')}
|
||
5 years ago
|
submitButtonType="confirm"
|
||
5 years ago
|
/>
|
||
|
</div>
|
||
4 years ago
|
);
|
||
5 years ago
|
}
|
||
|
}
|