@@ -26,6 +26,7 @@ import Dropdown from '~/components/Dropdown'
import { NOTIFICATIONS_POLL_INTERVAL } from '~/constants/notifications'
import { notificationQuery, markAsReadMutation } from '~/graphql/User'
import NotificationList from '../NotificationList/NotificationList'
+import unionBy from 'lodash/unionBy'
export default {
name: 'NotificationMenu',
@@ -35,7 +36,6 @@ export default {
},
data() {
return {
- displayedNotifications: [],
notifications: [],
}
},
@@ -46,39 +46,21 @@ export default {
async markAsRead(notificationSourceId) {
const variables = { id: notificationSourceId }
try {
- const {
- data: { markAsRead },
- } = await this.$apollo.mutate({
+ await this.$apollo.mutate({
mutation: markAsReadMutation(this.$i18n),
variables,
})
- if (!(markAsRead && markAsRead.read === true)) return
- this.displayedNotifications = this.displayedNotifications.map(n => {
- return this.equalNotification(n, markAsRead) ? markAsRead : n
- })
} catch (err) {
this.$toast.error(err.message)
}
},
- equalNotification(a, b) {
- return a.from.id === b.from.id && a.createdAt === b.createdAt && a.reason === b.reason
- },
},
computed: {
- notificationsCount() {
- return (this.displayedNotifications || []).length
- },
unreadNotificationsCount() {
- let countUnread = 0
- if (this.displayedNotifications) {
- this.displayedNotifications.forEach(notification => {
- if (!notification.read) countUnread++
- })
- }
- return countUnread
- },
- updateNotifications() {
- return this.notificationRead
+ const result = this.notifications.reduce((count, n) => {
+ return n.read ? count : count + 1
+ }, 0)
+ return result
},
},
apollo: {
@@ -94,15 +76,9 @@ export default {
},
pollInterval: NOTIFICATIONS_POLL_INTERVAL,
update({ notifications }) {
- const newNotifications = notifications.filter(newN => {
- return !this.displayedNotifications.find(oldN => this.equalNotification(newN, oldN))
- })
- this.displayedNotifications = newNotifications
- .concat(this.displayedNotifications)
- .sort((a, b) => {
- return new Date(b.createdAt) - new Date(a.createdAt)
- })
- return notifications
+ return unionBy(notifications, this.notifications, n => n.id).sort(
+ (a, b) => new Date(b.createdAt) - new Date(a.createdAt),
+ )
},
error(error) {
this.$toast.error(error.message)
diff --git a/webapp/graphql/User.js b/webapp/graphql/User.js
index 7b2a012a4..fda023d06 100644
--- a/webapp/graphql/User.js
+++ b/webapp/graphql/User.js
@@ -53,6 +53,7 @@ export const notificationQuery = i18n => {
query($read: Boolean, $orderBy: NotificationOrdering, $first: Int, $offset: Int) {
notifications(read: $read, orderBy: $orderBy, first: $first, offset: $offset) {
+ id
read
reason
createdAt
@@ -81,6 +82,7 @@ export const markAsReadMutation = i18n => {
mutation($id: ID!) {
markAsRead(id: $id) {
+ id
read
reason
createdAt