import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { sortBy } from 'lodash';
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 unsortedContactsByLetter = this.props
.searchForContacts()
.reduce((obj, contact) => {
const firstLetter = contact.name[0].toUpperCase();
return {
...obj,
[firstLetter]: [...(obj[firstLetter] || []), contact],
};
}, {});
const letters = Object.keys(unsortedContactsByLetter).sort();
const sortedContactGroups = letters.map((letter) => {
return [
letter,
sortBy(unsortedContactsByLetter[letter], (contact) => {
return contact.name.toLowerCase();
}),
];
});
return sortedContactGroups.map(([letter, groupItems]) => (
));
}
renderMyAccounts() {
const myAccounts = this.props.searchForMyAccounts();
return (
);
}
render() {
const {
children,
searchForRecents,
searchForContacts,
searchForMyAccounts,
} = this.props;
return (
{children || null}
{searchForRecents ? this.renderRecents() : null}
{searchForContacts ? this.renderAddressBook() : null}
{searchForMyAccounts ? this.renderMyAccounts() : null}
);
}
}