Intermediate store/notifications

This commit is contained in:
Robert Schäfer 2019-04-09 15:09:38 +02:00
parent 512835f202
commit 3559cfd9a3

View File

@ -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)
}
}