Skip to content

Commit

Permalink
Merge pull request #1988 from appirio-tech/hotfix/notifications-sorting
Browse files Browse the repository at this point in the history
fix issue #1980 - Fix sorting for notifications
  • Loading branch information
gondzo authored May 2, 2018
2 parents f4c9605 + 1b935ca commit 7b018e4
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/routes/notifications/helpers/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ export const getNotificationsFilters = (sources) => {
filterSections.push([{ title: 'Global', value: 'global', quantity: globalNotificationsQuantity }])
}
const filtersBySource = []
const sortedSources = [...sources].sort(compareSourcesByLastNotificationDate)

sources.forEach(source => {
sortedSources.forEach(source => {
if (source.id !== 'global') {
filtersBySource.push({
title: source.title,
Expand All @@ -93,6 +94,20 @@ export const getNotificationsFilters = (sources) => {
return filterSections
}

/**
* Compare two sources by the first's (latest) notification date
* If source doesn't have notifications, such source is "less" than another
*
* @param {Object} s1 source object
* @param {Object} s2 source object
*/
const compareSourcesByLastNotificationDate = (s1, s2) => {
const date1 = s1.notifications && s1.notifications.length ? new Date(s1.notifications[0].date).getTime() : 0
const date2 = s2.notifications && s2.notifications.length ? new Date(s2.notifications[0].date).getTime() : 0

return date2 - date1
}

/**
* Split notifications by sources
*
Expand All @@ -105,13 +120,13 @@ export const splitNotificationsBySources = (sources, notifications) => {
const notificationsBySources = []

sources.filter(source => source.total > 0).forEach(source => {
source.notifications = _.filter(notifications, n => {
if (n.sourceId !== source.id) return false
return true
})
source.notifications = _.filter(notifications, n => n.sourceId === source.id)
notificationsBySources.push(source)
})

// source that has the most recent notification should be on top
notificationsBySources.sort(compareSourcesByLastNotificationDate)

return notificationsBySources
}

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

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

// sort notifications by date (newer first)
notifications.sort((n1, n2) => {
const date1 = new Date(n1.date).getTime()
const date2 = new Date(n2.date).getTime()

return date2 - date1
})

return notifications
}

0 comments on commit 7b018e4

Please sign in to comment.