Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash upon removing contact #9031

Merged
merged 1 commit into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function RecipientGroup ({ label, items, onSelect, selectedAddres
RecipientGroup.propTypes = {
label: PropTypes.string,
items: PropTypes.arrayOf(PropTypes.shape({
address: PropTypes.string,
address: PropTypes.string.isRequired,
name: PropTypes.string,
})),
onSelect: PropTypes.func.isRequired,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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'
Expand Down Expand Up @@ -28,7 +29,6 @@ export default class EditContact extends PureComponent {

static defaultProps = {
name: '',
address: '',
memo: '',
}

Expand All @@ -55,6 +55,10 @@ export default class EditContact extends PureComponent {
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">
Expand All @@ -68,7 +72,6 @@ export default class EditContact extends PureComponent {
className="settings-page__address-book-button"
onClick={async () => {
await removeFromAddressBook(chainId, address)
history.push(listRoute)
}}
>
{t('deleteAccount')}
Expand Down Expand Up @@ -136,15 +139,19 @@ export default class EditContact extends PureComponent {
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)
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)
setAccountLabel(address, this.state.newName || name)
if (showingMyAccounts) {
setAccountLabel(address, this.state.newName || name)
}
history.push(listRoute)
}
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ const mapStateToProps = (state, ownProps) => {
const pathNameTailIsAddress = pathNameTail.includes('0x')
const address = pathNameTailIsAddress ? pathNameTail.toLowerCase() : ownProps.match.params.id

const { memo, name } = getAddressBookEntry(state, address) || state.metamask.identities[address]
const contact = getAddressBookEntry(state, address) || state.metamask.identities[address]
const { memo, name } = contact || {}

const chainId = state.metamask.network

const showingMyAccounts = Boolean(pathname.match(CONTACT_MY_ACCOUNTS_EDIT_ROUTE))

return {
address,
address: contact ? address : null,
chainId,
name,
memo,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { Redirect } from 'react-router-dom'

import Identicon from '../../../../components/ui/identicon'
import Copy from '../../../../components/ui/icon/copy-icon.component'

import Button from '../../../../components/ui/button/button.component'
import copyToClipboard from 'copy-to-clipboard'

Expand All @@ -23,11 +24,16 @@ export default class ViewContact extends PureComponent {
checkSummedAddress: PropTypes.string,
memo: PropTypes.string,
editRoute: PropTypes.string,
listRoute: PropTypes.string.isRequired,
}

render () {
const { t } = this.context
const { history, name, address, checkSummedAddress, memo, editRoute } = this.props
const { history, name, address, checkSummedAddress, memo, editRoute, listRoute } = this.props

if (!address) {
return <Redirect to={{ pathname: listRoute }} />
}

return (
<div className="settings-page__content-row">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { getAddressBookEntry } from '../../../../selectors'
import { checksumAddress } from '../../../../helpers/utils/util'
import {
CONTACT_EDIT_ROUTE,
CONTACT_LIST_ROUTE,
CONTACT_MY_ACCOUNTS_EDIT_ROUTE,
CONTACT_MY_ACCOUNTS_ROUTE,
CONTACT_MY_ACCOUNTS_VIEW_ROUTE,
} from '../../../../helpers/constants/routes'

Expand All @@ -17,16 +19,18 @@ const mapStateToProps = (state, ownProps) => {
const pathNameTailIsAddress = pathNameTail.includes('0x')
const address = pathNameTailIsAddress ? pathNameTail.toLowerCase() : ownProps.match.params.id

const { memo, name } = getAddressBookEntry(state, address) || state.metamask.identities[address]
const contact = getAddressBookEntry(state, address) || state.metamask.identities[address]
const { memo, name } = contact || {}

const showingMyAccounts = Boolean(pathname.match(CONTACT_MY_ACCOUNTS_VIEW_ROUTE))

return {
name,
address,
address: contact ? address : null,
checkSummedAddress: checksumAddress(address),
memo,
editRoute: showingMyAccounts ? CONTACT_MY_ACCOUNTS_EDIT_ROUTE : CONTACT_EDIT_ROUTE,
listRoute: showingMyAccounts ? CONTACT_MY_ACCOUNTS_ROUTE : CONTACT_LIST_ROUTE,
}
}

Expand Down