From 8e129c6001e95005ff68f52ffcc2ed041eac04ec Mon Sep 17 00:00:00 2001 From: roschaefer Date: Thu, 31 Oct 2019 23:04:04 +0100 Subject: [PATCH] fix: update counter when notification is read @mattwr18 vue-apollo rocks! Taking the time to study the docs is a rewarding investment. My first idea was to cache the `unreadNotificationsCount` with Vuex. But the docs of apollo even suggest to use apollo's local state as a complete replacement of Vuex: https://vue-apollo.netlify.com/guide/local-state.html Then I further investigated why the updated `NOTIFIED` objects won't update the notification counter. Turns out: They don't have an ID and the computed property didn't fire when the notifications array would change. I fixed both in this commit and yes, it works as expected. No additional code required :muscle: --- backend/src/schema/resolvers/notifications.js | 6 +++ backend/src/schema/types/type/NOTIFIED.gql | 1 + .../NotificationMenu/NotificationMenu.spec.js | 4 +- .../NotificationMenu/NotificationMenu.vue | 46 +++++-------------- webapp/graphql/User.js | 2 + 5 files changed, 22 insertions(+), 37 deletions(-) diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index 93feb3781..8d0683258 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -78,4 +78,10 @@ export default { return notification }, }, + NOTIFIED: { + id: async (parent) => { + // serialize an ID to help the client update the cache + return `${parent.reason}/${parent.from.id}/${parent.to.id}` + } + } } diff --git a/backend/src/schema/types/type/NOTIFIED.gql b/backend/src/schema/types/type/NOTIFIED.gql index 42da6a39b..1182f2743 100644 --- a/backend/src/schema/types/type/NOTIFIED.gql +++ b/backend/src/schema/types/type/NOTIFIED.gql @@ -1,4 +1,5 @@ type NOTIFIED { + id: ID! from: NotificationSource to: User createdAt: String diff --git a/webapp/components/NotificationMenu/NotificationMenu.spec.js b/webapp/components/NotificationMenu/NotificationMenu.spec.js index 2145b7d6e..87576a5f3 100644 --- a/webapp/components/NotificationMenu/NotificationMenu.spec.js +++ b/webapp/components/NotificationMenu/NotificationMenu.spec.js @@ -50,7 +50,7 @@ describe('NotificationMenu.vue', () => { beforeEach(() => { data = () => { return { - displayedNotifications: [ + notifications: [ { id: 'notification-41', read: true, @@ -85,7 +85,7 @@ describe('NotificationMenu.vue', () => { beforeEach(() => { data = () => { return { - displayedNotifications: [ + notifications: [ { id: 'notification-41', read: false, diff --git a/webapp/components/NotificationMenu/NotificationMenu.vue b/webapp/components/NotificationMenu/NotificationMenu.vue index 79a16157c..ff50ea1e9 100644 --- a/webapp/components/NotificationMenu/NotificationMenu.vue +++ b/webapp/components/NotificationMenu/NotificationMenu.vue @@ -1,5 +1,5 @@