import React, { Component } from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import ContactList from '../../../components/app/contact-list'; import { CONTACT_ADD_ROUTE, CONTACT_LIST_ROUTE, CONTACT_VIEW_ROUTE, } from '../../../helpers/constants/routes'; import { getNumberOfSettingsInSection, handleSettingsRefs, } from '../../../helpers/utils/settings-search'; import { ButtonPrimary, Icon, IconName, IconSize, } from '../../../components/component-library'; import { IconColor, Size } from '../../../helpers/constants/design-system'; import EditContact from './edit-contact'; import AddContact from './add-contact'; import ViewContact from './view-contact'; export default class ContactListTab extends Component { static contextTypes = { t: PropTypes.func, }; static propTypes = { addressBook: PropTypes.array, history: PropTypes.object, selectedAddress: PropTypes.string, viewingContact: PropTypes.bool, editingContact: PropTypes.bool, addingContact: PropTypes.bool, hideAddressBook: PropTypes.bool, currentPath: PropTypes.string, }; settingsRefs = Array( getNumberOfSettingsInSection(this.context.t, this.context.t('contacts')), ) .fill(undefined) .map(() => { return React.createRef(); }); componentDidUpdate() { const { t } = this.context; handleSettingsRefs(t, t('contacts'), this.settingsRefs); } componentDidMount() { const { t } = this.context; handleSettingsRefs(t, t('contacts'), this.settingsRefs); } renderAddresses() { const { addressBook, history, selectedAddress } = this.props; const contacts = addressBook.filter(({ name }) => Boolean(name)); const nonContacts = addressBook.filter(({ name }) => !name); const { t } = this.context; if (addressBook.length) { return (
contacts} searchForRecents={() => nonContacts} selectRecipient={(address) => { history.push(`${CONTACT_VIEW_ROUTE}/${address}`); }} selectedAddress={selectedAddress} />
); } return (

{t('buildContactList')}

{t('addFriendsAndAddresses')}

); } renderAddButton() { const { history, viewingContact, editingContact } = this.props; return ( { history.push(CONTACT_ADD_ROUTE); }} margin={4} size={Size.LG} > {this.context.t('addContact')} ); } renderContactContent() { const { viewingContact, editingContact, addingContact } = this.props; let ContactContentComponent = null; if (viewingContact) { ContactContentComponent = ViewContact; } else if (editingContact) { ContactContentComponent = EditContact; } else if (addingContact) { ContactContentComponent = AddContact; } return ( ContactContentComponent && (
) ); } renderAddressBookContent() { const { hideAddressBook } = this.props; if (!hideAddressBook) { return (
{this.renderAddresses()}
); } return null; } render() { const { addingContact, addressBook, currentPath } = this.props; return (
{this.renderAddressBookContent()} {this.renderContactContent()} {currentPath === CONTACT_LIST_ROUTE && !addingContact && addressBook.length > 0 ? this.renderAddButton() : null}
); } }