From 7727707d4983c3b673241d6b2571c6ab318ac978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Mon, 23 Sep 2019 18:00:35 +0200 Subject: [PATCH] Followed some of Roberts suggestions and a bit more --- .../NotificationMenu/NotificationMenu.spec.js | 2 +- .../NotificationMenu/NotificationMenu.vue | 71 +++++++++++-------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/webapp/components/notifications/NotificationMenu/NotificationMenu.spec.js b/webapp/components/notifications/NotificationMenu/NotificationMenu.spec.js index 51b74b7f2..85384977e 100644 --- a/webapp/components/notifications/NotificationMenu/NotificationMenu.spec.js +++ b/webapp/components/notifications/NotificationMenu/NotificationMenu.spec.js @@ -50,7 +50,7 @@ describe('NotificationMenu.vue', () => { beforeEach(() => { data = () => { return { - notifications: [ + displayedNotifications: [ { id: 'notification-41', read: false, diff --git a/webapp/components/notifications/NotificationMenu/NotificationMenu.vue b/webapp/components/notifications/NotificationMenu/NotificationMenu.vue index f72658c15..ecc89ff41 100644 --- a/webapp/components/notifications/NotificationMenu/NotificationMenu.vue +++ b/webapp/components/notifications/NotificationMenu/NotificationMenu.vue @@ -21,7 +21,7 @@ @@ -41,42 +41,45 @@ export default { }, data() { return { + displayedNotifications: [], notifications: [], } }, props: { placement: { type: String }, }, - created() { - setInterval(this.updateNotifications, REFRESH_MILLISEC) - }, - destroyed() { - clearInterval(this.updateNotifications) + watch: { + notifications: { + handler(lastQueriedNotifications) { + if (lastQueriedNotifications && lastQueriedNotifications.length > 0) { + // set this to be empty to get always called if a query comes with results from the backend + // has the sideeffect the handler is encouraged to be called again, but only once with no effect, because of the if above + this.notifications = [] + + let oldNotifications = this.displayedNotifications + const equalNotification = this.equalNotification // because we can not use 'this.equalNotification' in callback + + // add all the new notifications to the oldNotifications at top of the list + lastQueriedNotifications.forEach(updatedListNotification => { + const sameNotification = oldNotifications.find(function(oldListNotification) { + return equalNotification(oldListNotification, updatedListNotification) + }) + if (sameNotification === undefined) { + oldNotifications.unshift(updatedListNotification) + } + }) + + this.displayedNotifications = oldNotifications + } + }, + deep: true, + immediate: true, + }, }, methods: { async updateNotifications() { try { - let oldNotifications = this.notifications this.$apollo.queries.notifications.refetch() - const udatedNotifications = this.notifications - - // add all the new notifications to the old notifications at top of the list - if (udatedNotifications) { - udatedNotifications.forEach(udatedListNotification => { - const sameNotification = oldNotifications.find(function(oldListNotification) { - return ( - oldListNotification.from.id === udatedListNotification.from.id && - oldListNotification.createdAt === udatedListNotification.createdAt && - oldListNotification.reason === udatedListNotification.reason - ) - }) - if (sameNotification === undefined) { - oldNotifications.unshift(udatedListNotification) - } - }) - - this.notifications = oldNotifications - } } catch (err) { throw new Error(err) } @@ -91,22 +94,25 @@ export default { variables, }) if (!(markAsRead && markAsRead.read === true)) return - this.notifications = this.notifications.map(n => { - return n.from.id === markAsRead.from.id ? markAsRead : n + this.displayedNotifications = this.displayedNotifications.map(n => { + return this.equalNotification(n, markAsRead) ? markAsRead : n }) } catch (err) { throw new Error(err) } }, + equalNotification(a, b) { + return a.from.id === b.from.id && a.createdAt === b.createdAt && a.reason === b.reason + }, }, computed: { totalNotifications() { - return (this.notifications || []).length + return (this.displayedNotifications || []).length }, unreadNotifications() { let countUnread = 0 - if (this.notifications) { - this.notifications.forEach(notification => { + if (this.displayedNotifications) { + this.displayedNotifications.forEach(notification => { if (!notification.read) countUnread++ }) } @@ -118,6 +124,9 @@ export default { query() { return notificationQuery(this.$i18n) }, + pollInterval() { + return REFRESH_MILLISEC + }, }, }, }