From 3559cfd9a3a07cbbc97b8462d8172ecec39ab878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Tue, 9 Apr 2019 15:09:38 +0200 Subject: [PATCH] Intermediate store/notifications --- webapp/store/notifications.js | 71 +++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/webapp/store/notifications.js b/webapp/store/notifications.js index ba9fdc14b..cfb41e333 100644 --- a/webapp/store/notifications.js +++ b/webapp/store/notifications.js @@ -1,5 +1,75 @@ import gql from 'graphql-tag' + +export const state = () => { + return { + notifications: null, + pending: false + } +} + +export const mutations = { + SET_NOTIFICATIONS(state, notifications) { + state.notifications = notifications + }, + SET_PENDING(state, pending) { + state.pending = pending + }, + UPDATE_NOTIFICATIONS(state, notification) { + const notifications = state.notifications + const toBeUpdated = notifications.find(n => { + return n.id === notification.id + }) + toBeUpdated = { ...toBeUpdated, ...notification } + } +} +export const getters = { + notifications(state) { + return !!state.notifications + } +} + export const actions = { + async init({ getters, commit }) { + if (getters.notifications) return + commit('SET_PENDING', true) + const client = this.app.apolloProvider.defaultClient + let notifications + try { + const { + data: { currentUser } + } = await client.query({ + query: gql(`{ + currentUser { + id + notifications(orderBy: createdAt_desc) { + id + read + createdAt + post { + author { + id + slug + name + disabled + deleted + } + title + contentExcerpt + slug + } + } + } + }`) + }) + notifications = currentUser.notifications + console.log(notifications) + commit('SET_NOTIFICATIONS', notifications) + } finally { + commit('SET_PENDING', false) + } + return notifications + }, + async markAsRead({ commit, rootGetters }, notificationId) { const client = this.app.apolloProvider.defaultClient const mutation = gql(` @@ -14,5 +84,6 @@ export const actions = { const { data: { UpdateNotification } } = await client.mutate({ mutation, variables }) + commit('UPDATE_NOTIFICATIONS', UpdateNotification) } }