-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sort contacts alphabetically #11982
Merged
Merged
Sort contacts alphabetically #11982
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f9fe3b9
Sort contacts alphabetically
mcmire 2854002
Improve tests to be less brittle
mcmire 9a7f394
Remove this matcher
mcmire 0e572f8
Revert this file
mcmire 48967e4
Also don't need this change anymore
mcmire 5e93374
Don't need this data attribute either
mcmire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
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'; | ||
|
||
|
@@ -50,34 +51,36 @@ export default class ContactList extends PureComponent { | |
} | ||
|
||
renderAddressBook() { | ||
const contacts = this.props.searchForContacts(); | ||
const unsortedContactsByLetter = this.props | ||
.searchForContacts() | ||
.reduce((obj, contact) => { | ||
const firstLetter = contact.name[0].toUpperCase(); | ||
return { | ||
...obj, | ||
[firstLetter]: [...(obj[firstLetter] || []), contact], | ||
}; | ||
}, {}); | ||
|
||
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; | ||
}, {}); | ||
const letters = Object.keys(unsortedContactsByLetter).sort(); | ||
|
||
return Object.entries(contactGroups) | ||
.sort(([letter1], [letter2]) => { | ||
if (letter1 > letter2) { | ||
return 1; | ||
} else if (letter1 === letter2) { | ||
return 0; | ||
} | ||
return -1; | ||
}) | ||
.map(([letter, groupItems]) => ( | ||
<RecipientGroup | ||
key={`${letter}-contract-group`} | ||
label={letter} | ||
items={groupItems} | ||
onSelect={this.props.selectRecipient} | ||
selectedAddress={this.props.selectedAddress} | ||
/> | ||
)); | ||
const sortedContactGroups = letters.map((letter) => { | ||
return [ | ||
letter, | ||
sortBy(unsortedContactsByLetter[letter], (contact) => { | ||
return contact.name.toLowerCase(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that each contact is sorted case-insensitively. I thought this might provide a better experience — when I look through a list that I know is sorted, I don't tend to think about case. I'm happy to change this if we think otherwise, though. |
||
}), | ||
]; | ||
}); | ||
|
||
return sortedContactGroups.map(([letter, groupItems]) => ( | ||
<RecipientGroup | ||
key={`${letter}-contact-group`} | ||
label={letter} | ||
items={groupItems} | ||
onSelect={this.props.selectRecipient} | ||
selectedAddress={this.props.selectedAddress} | ||
/> | ||
)); | ||
} | ||
|
||
renderMyAccounts() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react'; | ||
import { within } from '@testing-library/react'; | ||
import configureMockStore from 'redux-mock-store'; | ||
import { renderWithProvider } from '../../../../test/jest/rendering'; | ||
import ContactList from '.'; | ||
|
||
describe('Contact List', () => { | ||
const store = configureMockStore([])({ metamask: {} }); | ||
|
||
describe('given searchForContacts', () => { | ||
const selectRecipient = () => null; | ||
const selectedAddress = null; | ||
|
||
it('sorts contacts by name within each letter group', () => { | ||
const { getAllByTestId } = renderWithProvider( | ||
<ContactList | ||
searchForContacts={() => { | ||
return [ | ||
{ | ||
name: 'Al', | ||
address: '0x0000000000000000000000000000000000000000', | ||
}, | ||
{ | ||
name: 'aa', | ||
address: '0x0000000000000000000000000000000000000001', | ||
}, | ||
{ | ||
name: 'Az', | ||
address: '0x0000000000000000000000000000000000000002', | ||
}, | ||
{ | ||
name: 'bbb', | ||
address: '0x0000000000000000000000000000000000000003', | ||
}, | ||
]; | ||
}} | ||
selectRecipient={selectRecipient} | ||
selectedAddress={selectedAddress} | ||
/>, | ||
store, | ||
); | ||
|
||
const recipientGroups = getAllByTestId('recipient-group'); | ||
expect(within(recipientGroups[0]).getByText('A')).toBeInTheDocument(); | ||
const recipientsInA = within(recipientGroups[0]).getAllByTestId( | ||
'recipient', | ||
); | ||
expect(recipientsInA[0]).toHaveTextContent('aa0x0000...0001'); | ||
expect(recipientsInA[1]).toHaveTextContent('Al0x0000...0000'); | ||
expect(recipientsInA[2]).toHaveTextContent('Az0x0000...0002'); | ||
expect(within(recipientGroups[1]).getByText('B')).toBeInTheDocument(); | ||
const recipientsInB = within(recipientGroups[1]).getAllByTestId( | ||
'recipient', | ||
); | ||
expect(recipientsInB[0]).toHaveTextContent('bbb0x0000...0003'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍