Reduce complexity of notifications watcher

- Co-authored-by: @Tirokk <wolle.huss@pjannto.com>
This commit is contained in:
mattwr18 2019-09-24 09:55:07 +02:00 committed by Wolfgang Huß
parent 7727707d49
commit 99dafcda64
2 changed files with 7 additions and 19 deletions

View File

@ -84,7 +84,7 @@ describe('NotificationMenu.vue', () => {
read: true,
post: {
id: 'post-3',
title: 'red post title',
title: 'read post title',
contentExcerpt: 'this is yet another post content',
author: {
id: 'john-1',
@ -98,7 +98,7 @@ describe('NotificationMenu.vue', () => {
}
})
it('displays the unread number of notifications', () => {
it('displays the number of unread notifications', () => {
wrapper = Wrapper()
expect(wrapper.find('ds-button-stub').text()).toEqual('2')
})

View File

@ -50,32 +50,20 @@ export default {
},
watch: {
notifications: {
immediate: true,
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)
const sameNotification = this.displayedNotifications.find(oldListNotification => {
return this.equalNotification(oldListNotification, updatedListNotification)
})
if (sameNotification === undefined) {
oldNotifications.unshift(updatedListNotification)
}
if (!sameNotification) this.displayedNotifications.unshift(updatedListNotification)
})
this.displayedNotifications = oldNotifications
}
},
deep: true,
immediate: true,
},
},
methods: {
async updateNotifications() {
try {