Skip to content

Commit 1b935ca

Browse files
committed
fix issue #1980 - Fix sorting for notifications
1 parent 85ef36d commit 1b935ca

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/routes/notifications/helpers/notifications.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ export const getNotificationsFilters = (sources) => {
7575
filterSections.push([{ title: 'Global', value: 'global', quantity: globalNotificationsQuantity }])
7676
}
7777
const filtersBySource = []
78+
const sortedSources = [...sources].sort(compareSourcesByLastNotificationDate)
7879

79-
sources.forEach(source => {
80+
sortedSources.forEach(source => {
8081
if (source.id !== 'global') {
8182
filtersBySource.push({
8283
title: source.title,
@@ -93,6 +94,20 @@ export const getNotificationsFilters = (sources) => {
9394
return filterSections
9495
}
9596

97+
/**
98+
* Compare two sources by the first's (latest) notification date
99+
* If source doesn't have notifications, such source is "less" than another
100+
*
101+
* @param {Object} s1 source object
102+
* @param {Object} s2 source object
103+
*/
104+
const compareSourcesByLastNotificationDate = (s1, s2) => {
105+
const date1 = s1.notifications && s1.notifications.length ? new Date(s1.notifications[0].date).getTime() : 0
106+
const date2 = s2.notifications && s2.notifications.length ? new Date(s2.notifications[0].date).getTime() : 0
107+
108+
return date2 - date1
109+
}
110+
96111
/**
97112
* Split notifications by sources
98113
*
@@ -105,13 +120,13 @@ export const splitNotificationsBySources = (sources, notifications) => {
105120
const notificationsBySources = []
106121

107122
sources.filter(source => source.total > 0).forEach(source => {
108-
source.notifications = _.filter(notifications, n => {
109-
if (n.sourceId !== source.id) return false
110-
return true
111-
})
123+
source.notifications = _.filter(notifications, n => n.sourceId === source.id)
112124
notificationsBySources.push(source)
113125
})
114126

127+
// source that has the most recent notification should be on top
128+
notificationsBySources.sort(compareSourcesByLastNotificationDate)
129+
115130
return notificationsBySources
116131
}
117132

@@ -370,5 +385,13 @@ export const prepareNotifications = (rawNotifications) => {
370385

371386
const notifications = _.map(bundledNotificationsWithRules, 'notification')
372387

388+
// sort notifications by date (newer first)
389+
notifications.sort((n1, n2) => {
390+
const date1 = new Date(n1.date).getTime()
391+
const date2 = new Date(n2.date).getTime()
392+
393+
return date2 - date1
394+
})
395+
373396
return notifications
374397
}

0 commit comments

Comments
 (0)