From 3e0a5ec3a66c858bba3a8b75fdedb530b52ef64d Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Thu, 16 Jan 2020 14:07:55 -0800 Subject: [PATCH 1/3] fix(archive): Fix blank screen when sorting * Add try/catch handler for exceptions * Handle case when sorting data is falsy --- src/lib/viewers/archive/ArchiveExplorer.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/lib/viewers/archive/ArchiveExplorer.js b/src/lib/viewers/archive/ArchiveExplorer.js index 8541ac219..20f9978d3 100644 --- a/src/lib/viewers/archive/ArchiveExplorer.js +++ b/src/lib/viewers/archive/ArchiveExplorer.js @@ -162,18 +162,27 @@ class ArchiveExplorer extends React.Component { */ sortItemList(itemList) { const { sortBy, sortDirection } = this.state; + let sortedItems; 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 { + sortedItems = itemList.sort((a, b) => { + if (!a[sortBy] || !b[sortBy]) { + 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(); } From e8755dbc0eeea70079c76d5daa8c35cec700707d Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Thu, 16 Jan 2020 14:25:24 -0800 Subject: [PATCH 2/3] fix(archive): PR Feedback --- src/lib/viewers/archive/ArchiveExplorer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/viewers/archive/ArchiveExplorer.js b/src/lib/viewers/archive/ArchiveExplorer.js index 20f9978d3..baa6e010e 100644 --- a/src/lib/viewers/archive/ArchiveExplorer.js +++ b/src/lib/viewers/archive/ArchiveExplorer.js @@ -162,14 +162,15 @@ class ArchiveExplorer extends React.Component { */ sortItemList(itemList) { const { sortBy, sortDirection } = this.state; - let sortedItems; if (!sortBy.length) { return itemList; } + let sortedItems; + try { - sortedItems = itemList.sort((a, b) => { + sortedItems = itemList.sort((a = {}, b = {}) => { if (!a[sortBy] || !b[sortBy]) { return -1; } From 113ff01cea2a5ce19ed8e8847a92468feef89661 Mon Sep 17 00:00:00 2001 From: Mick Ryan Date: Thu, 16 Jan 2020 16:51:11 -0800 Subject: [PATCH 3/3] fix(archive): Addressing PR feedback * Remove try/catch * Add test for comparing to null values in the sort --- src/lib/viewers/archive/ArchiveExplorer.js | 24 +++++++------------ .../__tests__/ArchiveExplorer-test-react.js | 15 ++++++++++++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/lib/viewers/archive/ArchiveExplorer.js b/src/lib/viewers/archive/ArchiveExplorer.js index baa6e010e..eb4a1e9e0 100644 --- a/src/lib/viewers/archive/ArchiveExplorer.js +++ b/src/lib/viewers/archive/ArchiveExplorer.js @@ -167,23 +167,17 @@ class ArchiveExplorer extends React.Component { return itemList; } - let sortedItems; + const sortedItems = itemList.sort((a = {}, b = {}) => { + if (!a[sortBy] || !b[sortBy]) { + return -1; + } - try { - sortedItems = itemList.sort((a = {}, b = {}) => { - if (!a[sortBy] || !b[sortBy]) { - return -1; - } + if (typeof a[sortBy] === 'number' && typeof b[sortBy] === 'number') { + return a[sortBy] - 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 a[sortBy].localeCompare(b[sortBy]); + }); return sortDirection === SortDirection.ASC ? sortedItems : sortedItems.reverse(); } diff --git a/src/lib/viewers/archive/__tests__/ArchiveExplorer-test-react.js b/src/lib/viewers/archive/__tests__/ArchiveExplorer-test-react.js index 2e53cf4b5..f502bbe88 100644 --- a/src/lib/viewers/archive/__tests__/ArchiveExplorer-test-react.js +++ b/src/lib/viewers/archive/__tests__/ArchiveExplorer-test-react.js @@ -219,5 +219,20 @@ describe('lib/viewers/archive/ArchiveExplorer', () => { expect(sortedList[0]).to.equal(itemList[0]); }); + + it('should sort item list with null values', () => { + data[1].modified_at = null; + data[2].modified_at = null; + + const component = getComponent({ filename, itemCollection: data }); + const instance = component.instance(); + const itemList = instance.getItemList(data, 'test/'); + + instance.handleSort({ sortBy: 'modified_at' }); + + const sortedList = instance.sortItemList(itemList); + + expect(sortedList[0]).to.equal(itemList[0]); + }); }); });