import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import Identicon from '../../../../components/ui/identicon'
import Button from '../../../../components/ui/button/button.component'
import TextField from '../../../../components/ui/text-field'
import { isValidAddress } from '../../../../helpers/utils/util'
import PageContainerFooter from '../../../../components/ui/page-container/page-container-footer'
export default class EditContact extends PureComponent {
static contextTypes = {
t: PropTypes.func,
}
static propTypes = {
addToAddressBook: PropTypes.func,
removeFromAddressBook: PropTypes.func,
history: PropTypes.object,
name: PropTypes.string,
address: PropTypes.string,
chainId: PropTypes.string,
memo: PropTypes.string,
viewRoute: PropTypes.string,
listRoute: PropTypes.string,
setAccountLabel: PropTypes.func,
showingMyAccounts: PropTypes.bool.isRequired,
}
static defaultProps = {
name: '',
address: '',
memo: '',
}
state = {
newName: this.props.name,
newAddress: this.props.address,
newMemo: this.props.memo,
error: '',
}
render () {
const { t } = this.context
const {
address,
addToAddressBook,
chainId,
history,
listRoute,
memo,
name,
removeFromAddressBook,
setAccountLabel,
showingMyAccounts,
viewRoute,
} = this.props
return (
{
showingMyAccounts
? null
: (
)
}
{ t('userName') }
this.setState({ newName: e.target.value })}
fullWidth
margin="dense"
/>
{ t('ethereumPublicAddress') }
this.setState({ newAddress: e.target.value })}
fullWidth
margin="dense"
/>
{ t('memo') }
this.setState({ newMemo: e.target.value })}
fullWidth
margin="dense"
multiline
rows={3}
classes={{
inputMultiline: 'address-book__view-contact__text-area',
inputRoot: 'address-book__view-contact__text-area-wrapper',
}}
/>
{
if (this.state.newAddress !== '' && this.state.newAddress !== address) {
// if the user makes a valid change to the address field, remove the original address
if (isValidAddress(this.state.newAddress)) {
await removeFromAddressBook(chainId, address)
await addToAddressBook(this.state.newAddress, this.state.newName || name, this.state.newMemo || memo)
setAccountLabel(this.state.newAddress, this.state.newName || name)
history.push(listRoute)
} else {
this.setState({ error: this.context.t('invalidAddress') })
}
} else {
// update name
await addToAddressBook(address, this.state.newName || name, this.state.newMemo || memo)
setAccountLabel(address, this.state.newName || name)
history.push(listRoute)
}
}}
onCancel={() => {
history.push(`${viewRoute}/${address}`)
}}
submitText={this.context.t('save')}
submitButtonType="confirm"
/>
)
}
}