2021-02-04 19:15:23 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2021-09-03 21:31:12 +02:00
|
|
|
import { sortBy } from 'lodash';
|
2021-02-04 19:15:23 +01:00
|
|
|
import Button from '../../ui/button';
|
|
|
|
import RecipientGroup from './recipient-group/recipient-group.component';
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
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,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
state = {
|
|
|
|
isShowingAllRecent: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderRecents() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
|
|
|
const { isShowingAllRecent } = this.state;
|
|
|
|
const nonContacts = this.props.searchForRecents();
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const showLoadMore = !isShowingAllRecent && nonContacts.length > 2;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="send__select-recipient-wrapper__recent-group-wrapper">
|
|
|
|
<RecipientGroup
|
|
|
|
label={t('recents')}
|
|
|
|
items={showLoadMore ? nonContacts.slice(0, 2) : nonContacts}
|
|
|
|
onSelect={this.props.selectRecipient}
|
|
|
|
selectedAddress={this.props.selectedAddress}
|
|
|
|
/>
|
2020-11-03 00:41:28 +01:00
|
|
|
{showLoadMore && (
|
|
|
|
<Button
|
|
|
|
type="link"
|
|
|
|
className="send__select-recipient-wrapper__recent-group-wrapper__load-more"
|
|
|
|
onClick={() => this.setState({ isShowingAllRecent: true })}
|
|
|
|
>
|
|
|
|
{t('loadMore')}
|
|
|
|
</Button>
|
|
|
|
)}
|
2019-07-31 21:56:44 +02:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderAddressBook() {
|
2021-09-03 21:31:12 +02:00
|
|
|
const unsortedContactsByLetter = this.props
|
|
|
|
.searchForContacts()
|
|
|
|
.reduce((obj, contact) => {
|
|
|
|
const firstLetter = contact.name[0].toUpperCase();
|
|
|
|
return {
|
|
|
|
...obj,
|
|
|
|
[firstLetter]: [...(obj[firstLetter] || []), contact],
|
|
|
|
};
|
|
|
|
}, {});
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-09-03 21:31:12 +02:00
|
|
|
const letters = Object.keys(unsortedContactsByLetter).sort();
|
2019-07-31 21:56:44 +02:00
|
|
|
|
2021-09-03 21:31:12 +02:00
|
|
|
const sortedContactGroups = letters.map((letter) => {
|
|
|
|
return [
|
|
|
|
letter,
|
|
|
|
sortBy(unsortedContactsByLetter[letter], (contact) => {
|
|
|
|
return contact.name.toLowerCase();
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
return sortedContactGroups.map(([letter, groupItems]) => (
|
|
|
|
<RecipientGroup
|
|
|
|
key={`${letter}-contact-group`}
|
|
|
|
label={letter}
|
|
|
|
items={groupItems}
|
|
|
|
onSelect={this.props.selectRecipient}
|
|
|
|
selectedAddress={this.props.selectedAddress}
|
|
|
|
/>
|
|
|
|
));
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderMyAccounts() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const myAccounts = this.props.searchForMyAccounts();
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<RecipientGroup
|
|
|
|
items={myAccounts}
|
|
|
|
onSelect={this.props.selectRecipient}
|
|
|
|
selectedAddress={this.props.selectedAddress}
|
|
|
|
/>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2019-07-31 21:56:44 +02:00
|
|
|
const {
|
|
|
|
children,
|
|
|
|
searchForRecents,
|
|
|
|
searchForContacts,
|
|
|
|
searchForMyAccounts,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
2019-07-31 21:56:44 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="send__select-recipient-wrapper__list">
|
2020-11-03 00:41:28 +01:00
|
|
|
{children || null}
|
2021-10-21 18:11:31 +02:00
|
|
|
{searchForRecents ? this.renderRecents() : null}
|
|
|
|
{searchForContacts ? this.renderAddressBook() : null}
|
|
|
|
{searchForMyAccounts ? this.renderMyAccounts() : null}
|
2019-07-31 21:56:44 +02:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-07-31 21:56:44 +02:00
|
|
|
}
|
|
|
|
}
|