Skip to content
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

fix(archive): Fix blank screen when sorting #1153

Merged
merged 4 commits into from
Jan 17, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/lib/viewers/archive/ArchiveExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,27 @@ class ArchiveExplorer extends React.Component {
*/
sortItemList(itemList) {
const { sortBy, sortDirection } = this.state;
let sortedItems;
mickr marked this conversation as resolved.
Show resolved Hide resolved

if (!sortBy.length) {
return itemList;
}

const sortedItems = itemList.sort((a, b) => {
if (typeof a[sortBy] === 'number' && typeof b[sortBy] === 'number') {
return a[sortBy] - b[sortBy];
}
try {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we should be able to guard against pretty much any issues without resorting to try/catch here. I think we should add a componentDidCatch to the parent component, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are safe as well, but I through it in for good measure to ensure we always get an itemList returned.

Copy link
Collaborator

@jstoffan jstoffan Jan 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, try/catch should be reserved for exception handling rather than for flow control. I don't think it hurts in this case, but it can get overused.

sortedItems = itemList.sort((a, b) => {
if (!a[sortBy] || !b[sortBy]) {
mickr marked this conversation as resolved.
Show resolved Hide resolved
return -1;
}

return a[sortBy].localeCompare(b[sortBy]);
});
if (typeof a[sortBy] === 'number' && typeof b[sortBy] === 'number') {
return a[sortBy] - b[sortBy];
}

return a[sortBy].localeCompare(b[sortBy]);
});
} catch {
sortedItems = itemList;
}

return sortDirection === SortDirection.ASC ? sortedItems : sortedItems.reverse();
}
Expand Down