import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import Button from '../../ui/button';
import RecipientGroup from './recipient-group/recipient-group.component';
export default class ContactList extends PureComponent {
static propTypes = {
searchForContacts: PropTypes.func,
searchForRecents: PropTypes.func,
searchForMyAccounts: PropTypes.func,
selectRecipient: PropTypes.func,
children: PropTypes.node,
selectedAddress: PropTypes.string,
};
static contextTypes = {
t: PropTypes.func,
};
state = {
isShowingAllRecent: false,
};
renderRecents() {
const { t } = this.context;
const { isShowingAllRecent } = this.state;
const nonContacts = this.props.searchForRecents();
const showLoadMore = !isShowingAllRecent && nonContacts.length > 2;
return (
{showLoadMore && (
)}
);
}
renderAddressBook() {
const contacts = this.props.searchForContacts();
const contactGroups = contacts.reduce((acc, contact) => {
const firstLetter = contact.name.slice(0, 1).toUpperCase();
acc[firstLetter] = acc[firstLetter] || [];
const bucket = acc[firstLetter];
bucket.push(contact);
return acc;
}, {});
return Object.entries(contactGroups)
.sort(([letter1], [letter2]) => {
if (letter1 > letter2) {
return 1;
} else if (letter1 === letter2) {
return 0;
}
return -1;
})
.map(([letter, groupItems]) => (
));
}
renderMyAccounts() {
const myAccounts = this.props.searchForMyAccounts();
return (
);
}
render() {
const {
children,
searchForRecents,
searchForContacts,
searchForMyAccounts,
} = this.props;
return (
{children || null}
{searchForRecents && this.renderRecents()}
{searchForContacts && this.renderAddressBook()}
{searchForMyAccounts && this.renderMyAccounts()}
);
}
}