Skip to content

Commit 62b76ea

Browse files
committed
issue #1985 - fixed sources generation and total calculation after notifications store refactoring
1 parent 2b6d018 commit 62b76ea

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

src/routes/notifications/containers/NotificationsContainer.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const NotificationsContainerView = (props) => {
115115
<aside className="filters">
116116
<Sticky top={90}>
117117
<SideFilter
118-
filterSections={getNotificationsFilters(sources)}
118+
filterSections={getNotificationsFilters(allNotificationsBySources)}
119119
selectedFilter={filterBy}
120120
onFilterItemClick={setNotificationsFilterBy}
121121
>
@@ -148,12 +148,12 @@ class NotificationsContainer extends Component {
148148
render() {
149149
const { notifications, ...restProps } = this.props
150150
const preRenderedNotifications = preRenderNotifications(notifications)
151-
151+
152152
return (
153153
<NotificationsContainerView
154154
{...{
155155
...restProps,
156-
notifications: preRenderedNotifications
156+
notifications: preRenderedNotifications,
157157
}}
158158
/>
159159
)

src/routes/notifications/helpers/notifications.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,14 @@ const compareSourcesByLastNotificationDate = (s1, s2) => {
158158
* @return {Array} list of sources with related notifications
159159
*/
160160
export const splitNotificationsBySources = (sources, notifications) => {
161-
const notificationsBySources = []
162-
163-
sources.filter(source => source.total > 0).forEach(source => {
164-
source.notifications = _.filter(notifications, n => n.sourceId === source.id)
165-
notificationsBySources.push(source)
161+
const notificationsBySources = sources.map(source => {
162+
const sourceNotifications = _.filter(notifications, { sourceId: source.id })
163+
164+
return ({
165+
...source,
166+
notifications: sourceNotifications,
167+
total: sourceNotifications.length,
168+
})
166169
})
167170

168171
// source that has the most recent notification should be on top

src/routes/notifications/reducers/index.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const initialState = {
2626
isLoading: false,
2727
initialized: false,
2828
filterBy: '',
29-
sources: [{ id: 'global', title: 'Global', total: 0 }],
29+
sources: [{ id: 'global', title: 'Global' }],
3030
notifications: [],
3131
// ids of sources that will also show old notifications
3232
oldSourceIds: [],
@@ -40,17 +40,16 @@ const initialState = {
4040

4141
// get sources from notifications
4242
const getSources = (notifications) => {
43-
const sources = [{ id: 'global', title: 'Global', total: 0 }]
44-
_.each(notifications, notification => {
45-
if (!notification.isRead) {
46-
const source = _.find(sources, s => s.id === notification.sourceId)
47-
if (source) {
48-
source.total++
49-
} else {
50-
sources.push({ id: notification.sourceId, title: notification.sourceName, total: 1 })
51-
}
52-
}
53-
})
43+
const sources = [
44+
...initialState.sources,
45+
..._.uniqBy(
46+
notifications.map((notification) => ({
47+
id: notification.sourceId,
48+
title: notification.sourceName
49+
})),
50+
'id'
51+
)
52+
]
5453
return sources
5554
}
5655

@@ -75,7 +74,6 @@ export default (state = initialState, action) => {
7574
ids.indexOf(n.id) >= 0 ? { ...n, seen: true } : n
7675
))
7776
}
78-
newState.sources = getSources(newState.notifications)
7977
return newState
8078
}
8179

@@ -97,7 +95,6 @@ export default (state = initialState, action) => {
9795
!action.payload || n.sourceId === action.payload ? { ...n, isRead: true } : n
9896
))
9997
}
100-
newState.sources = getSources(newState.notifications)
10198
return newState
10299
}
103100

@@ -109,7 +106,6 @@ export default (state = initialState, action) => {
109106
n.id === action.payload ? { ...n, isRead: true } : n
110107
))
111108
}
112-
newState.sources = getSources(newState.notifications)
113109
return newState
114110
}
115111

@@ -121,7 +117,6 @@ export default (state = initialState, action) => {
121117
_.includes(action.payload, n.id) ? { ...n, isRead: true } : n
122118
))
123119
}
124-
newState.sources = getSources(newState.notifications)
125120
return newState
126121
}
127122

0 commit comments

Comments
 (0)