Skip to content
Merged
Changes from all commits
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
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
}