You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.0 KiB
113 lines
3.0 KiB
4 years ago
|
import React, { PureComponent } from 'react';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import Button from '../../ui/button';
|
||
|
import RecipientGroup from './recipient-group/recipient-group.component';
|
||
5 years ago
|
|
||
|
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,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
|
static contextTypes = {
|
||
|
t: PropTypes.func,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
|
state = {
|
||
|
isShowingAllRecent: false,
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
4 years ago
|
renderRecents() {
|
||
4 years ago
|
const { t } = this.context;
|
||
|
const { isShowingAllRecent } = this.state;
|
||
|
const nonContacts = this.props.searchForRecents();
|
||
5 years ago
|
|
||
4 years ago
|
const showLoadMore = !isShowingAllRecent && nonContacts.length > 2;
|
||
5 years ago
|
|
||
|
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}
|
||
|
/>
|
||
4 years ago
|
{showLoadMore && (
|
||
|
<Button
|
||
|
type="link"
|
||
|
className="send__select-recipient-wrapper__recent-group-wrapper__load-more"
|
||
|
onClick={() => this.setState({ isShowingAllRecent: true })}
|
||
|
>
|
||
|
{t('loadMore')}
|
||
|
</Button>
|
||
|
)}
|
||
5 years ago
|
</div>
|
||
4 years ago
|
);
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
renderAddressBook() {
|
||
4 years ago
|
const contacts = this.props.searchForContacts();
|
||
5 years ago
|
|
||
|
const contactGroups = contacts.reduce((acc, contact) => {
|
||
4 years ago
|
const firstLetter = contact.name.slice(0, 1).toUpperCase();
|
||
|
acc[firstLetter] = acc[firstLetter] || [];
|
||
|
const bucket = acc[firstLetter];
|
||
|
bucket.push(contact);
|
||
|
return acc;
|
||
|
}, {});
|
||
5 years ago
|
|
||
4 years ago
|
return Object.entries(contactGroups)
|
||
5 years ago
|
.sort(([letter1], [letter2]) => {
|
||
|
if (letter1 > letter2) {
|
||
4 years ago
|
return 1;
|
||
5 years ago
|
} else if (letter1 === letter2) {
|
||
4 years ago
|
return 0;
|
||
5 years ago
|
}
|
||
4 years ago
|
return -1;
|
||
5 years ago
|
})
|
||
|
.map(([letter, groupItems]) => (
|
||
|
<RecipientGroup
|
||
|
key={`${letter}-contract-group`}
|
||
|
label={letter}
|
||
|
items={groupItems}
|
||
|
onSelect={this.props.selectRecipient}
|
||
|
selectedAddress={this.props.selectedAddress}
|
||
|
/>
|
||
4 years ago
|
));
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
renderMyAccounts() {
|
||
4 years ago
|
const myAccounts = this.props.searchForMyAccounts();
|
||
5 years ago
|
|
||
|
return (
|
||
|
<RecipientGroup
|
||
|
items={myAccounts}
|
||
|
onSelect={this.props.selectRecipient}
|
||
|
selectedAddress={this.props.selectedAddress}
|
||
|
/>
|
||
4 years ago
|
);
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
render() {
|
||
5 years ago
|
const {
|
||
|
children,
|
||
|
searchForRecents,
|
||
|
searchForContacts,
|
||
|
searchForMyAccounts,
|
||
4 years ago
|
} = this.props;
|
||
5 years ago
|
|
||
|
return (
|
||
|
<div className="send__select-recipient-wrapper__list">
|
||
4 years ago
|
{children || null}
|
||
|
{searchForRecents && this.renderRecents()}
|
||
|
{searchForContacts && this.renderAddressBook()}
|
||
|
{searchForMyAccounts && this.renderMyAccounts()}
|
||
5 years ago
|
</div>
|
||
4 years ago
|
);
|
||
5 years ago
|
}
|
||
|
}
|