1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 19:10:22 +01:00
metamask-extension/ui/app/pages/settings/contact-list-tab/edit-contact/edit-contact.component.js
Mark Stacey 7cd609b86b
Fix crash upon removing contact (#9031)
The UI would crash upon deleting a contact from the contact list. This
happened for two reasons: the deletion could result in a re-render
before the `history.push` finished navigating back to the contact list
(it was a race condition), and the contact entry left behind an invalid
`identities` entry when it was removed.

The first problem was fixed by making the container components for view
and edit contact more tolerant of being passed an `address` that
doesn't correspond to a contact. If they are given an address without a
contact, `null` is passed to the component via the `address` prop. The
component will redirect back to the list when this happens instead
rendering. This is more awkward than I'd like, but it was the most
sensible way of handling this I could think of without making much more
drastic changes to how we're handling routing here.

The second problem was caused by the `setAccountLabel` call, which was
used to ensure the contact entry for any wallet accounts was kept
in-sync with the account label. This was being called even for non-
wallet accounts though, which is where this problem arose. This step is
now skipped for non-wallet accounts.

Fixes #9019
2020-07-20 11:55:47 -03:00

168 lines
5.3 KiB
JavaScript

import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { Redirect } from 'react-router-dom'
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: '',
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
if (!address) {
return <Redirect to={{ pathname: listRoute }} />
}
return (
<div className="settings-page__content-row address-book__edit-contact">
<div className="settings-page__header address-book__header--edit">
<Identicon address={address} diameter={60} />
{
showingMyAccounts
? null
: (
<Button
type="link"
className="settings-page__address-book-button"
onClick={async () => {
await removeFromAddressBook(chainId, address)
}}
>
{t('deleteAccount')}
</Button>
)
}
</div>
<div className="address-book__edit-contact__content">
<div className="address-book__view-contact__group">
<div className="address-book__view-contact__group__label">
{ t('userName') }
</div>
<TextField
type="text"
id="nickname"
placeholder={this.context.t('addAlias')}
value={this.state.newName}
onChange={(e) => this.setState({ newName: e.target.value })}
fullWidth
margin="dense"
/>
</div>
<div className="address-book__view-contact__group">
<div className="address-book__view-contact__group__label">
{ t('ethereumPublicAddress') }
</div>
<TextField
type="text"
id="address"
value={this.state.newAddress}
error={this.state.error}
onChange={(e) => this.setState({ newAddress: e.target.value })}
fullWidth
margin="dense"
/>
</div>
<div className="address-book__view-contact__group">
<div className="address-book__view-contact__group__label--capitalized">
{ t('memo') }
</div>
<TextField
type="text"
id="memo"
placeholder={memo}
value={this.state.newMemo}
onChange={(e) => 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',
}}
/>
</div>
</div>
<PageContainerFooter
cancelText={this.context.t('cancel')}
onSubmit={async () => {
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)
if (showingMyAccounts) {
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)
if (showingMyAccounts) {
setAccountLabel(address, this.state.newName || name)
}
history.push(listRoute)
}
}}
onCancel={() => {
history.push(`${viewRoute}/${address}`)
}}
submitText={this.context.t('save')}
submitButtonType="confirm"
/>
</div>
)
}
}