2019-07-31 21:56:44 +02:00
|
|
|
import React, { PureComponent } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
2020-08-18 21:18:25 +02:00
|
|
|
import { debounce } from 'lodash'
|
2019-07-31 21:56:44 +02:00
|
|
|
import Identicon from '../../../../components/ui/identicon'
|
|
|
|
import TextField from '../../../../components/ui/text-field'
|
|
|
|
import { CONTACT_LIST_ROUTE } from '../../../../helpers/constants/routes'
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
isValidAddress,
|
|
|
|
isValidDomainName,
|
|
|
|
} from '../../../../helpers/utils/util'
|
2020-01-09 04:34:58 +01:00
|
|
|
import EnsInput from '../../../send/send-content/add-recipient/ens-input'
|
2019-07-31 21:56:44 +02:00
|
|
|
import PageContainerFooter from '../../../../components/ui/page-container/page-container-footer'
|
|
|
|
|
|
|
|
export default class AddContact extends PureComponent {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
2019-12-03 16:30:03 +01:00
|
|
|
newName: '',
|
2019-07-31 21:56:44 +02:00
|
|
|
ethAddress: '',
|
|
|
|
ensAddress: '',
|
|
|
|
error: '',
|
|
|
|
ensError: '',
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
constructor(props) {
|
2019-07-31 21:56:44 +02:00
|
|
|
super(props)
|
|
|
|
this.dValidate = debounce(this.validate, 1000)
|
|
|
|
}
|
|
|
|
|
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') {
|
|
|
|
const scannedAddress = nextProps.qrCodeData.values.address.toLowerCase()
|
|
|
|
const currentAddress = this.state.ensAddress || this.state.ethAddress
|
|
|
|
if (currentAddress.toLowerCase() !== scannedAddress) {
|
|
|
|
this.setState({ ethAddress: scannedAddress, ensAddress: '' })
|
|
|
|
// Clean up QR code data after handling
|
|
|
|
this.props.qrCodeDetected(null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
validate = (address) => {
|
2019-07-31 21:56:44 +02:00
|
|
|
const valid = isValidAddress(address)
|
2020-02-18 02:20:01 +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 === '') {
|
|
|
|
this.setState({ error: '', ethAddress: address })
|
|
|
|
} else {
|
|
|
|
this.setState({ error: 'Invalid Address' })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderInput() {
|
2019-07-31 21:56:44 +02:00
|
|
|
return (
|
|
|
|
<EnsInput
|
|
|
|
className="send__to-row"
|
2020-02-15 21:34:12 +01:00
|
|
|
scanQrCode={(_) => {
|
2019-11-20 01:03:20 +01:00
|
|
|
this.props.scanQrCode()
|
|
|
|
}}
|
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) => {
|
2019-07-31 21:56:44 +02:00
|
|
|
this.setState({ ensAddress: address, error: '', ensError: '' })
|
|
|
|
}}
|
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
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2019-07-31 21:56:44 +02:00
|
|
|
const { t } = this.context
|
|
|
|
const { history, addToAddressBook } = this.props
|
|
|
|
|
|
|
|
const errorToRender = this.state.ensError || this.state.error
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
2019-07-31 21:56:44 +02:00
|
|
|
history.push(CONTACT_LIST_ROUTE)
|
|
|
|
}}
|
|
|
|
onCancel={() => {
|
|
|
|
history.push(CONTACT_LIST_ROUTE)
|
|
|
|
}}
|
|
|
|
submitText={this.context.t('save')}
|
2019-11-18 16:08:47 +01:00
|
|
|
submitButtonType="confirm"
|
2019-07-31 21:56:44 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|