From 30ba213f072a6ad342137f5ef4937d2a630590ae Mon Sep 17 00:00:00 2001 From: Fredrik Strand Oseberg Date: Mon, 29 Mar 2021 19:24:16 +0200 Subject: [PATCH] fix: add ascending sorting (#260) --- frontend/src/component/reporting/utils.js | 37 ++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/frontend/src/component/reporting/utils.js b/frontend/src/component/reporting/utils.js index 0c7abfbaa7..db969683d6 100644 --- a/frontend/src/component/reporting/utils.js +++ b/frontend/src/component/reporting/utils.js @@ -97,8 +97,39 @@ export const sortFeaturesByExpiredAtAscending = features => { const diffA = getDiffInDays(dateA, now); const diffB = getDiffInDays(dateB, now); - if (!expired(diffA, a.type)) return -1; - if (!expired(diffB, b.type)) return 1; + if (!expired(diffA, a.type) && expired(diffB, b.type)) { + return 1; + } + + if (expired(diffA, a.type) && !expired(diffB, b.type)) { + return -1; + } + + const expiredByA = diffA - toggleExpiryByTypeMap[a.type]; + const expiredByB = diffB - toggleExpiryByTypeMap[b.type]; + + return expiredByB - expiredByA; + }); + return sorted; +}; + +export const sortFeaturesByExpiredAtDescending = features => { + const sorted = [...features]; + const now = new Date(); + sorted.sort((a, b) => { + const dateA = parseISO(a.createdAt); + const dateB = parseISO(b.createdAt); + + const diffA = getDiffInDays(dateA, now); + const diffB = getDiffInDays(dateB, now); + + if (!expired(diffA, a.type) && expired(diffB, b.type)) { + return 1; + } + + if (expired(diffA, a.type) && !expired(diffB, b.type)) { + return -1; + } const expiredByA = diffA - toggleExpiryByTypeMap[a.type]; const expiredByB = diffB - toggleExpiryByTypeMap[b.type]; @@ -108,8 +139,6 @@ export const sortFeaturesByExpiredAtAscending = features => { return sorted; }; -export const sortFeaturesByExpiredAtDescending = features => sortFeaturesByExpiredAtAscending([...features]).reverse(); - export const sortFeaturesByStatusAscending = features => { const sorted = [...features]; sorted.sort((a, b) => {