import axios from axios import fetchNotificationsUr saveNotificationsU

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
import axios from 'axios'
import { fetchNotificationsUrl, saveNotificationsUrl } from '../../config/APIUtils'
let defaultState = {
notifications_preference: {
preference_list: [],
loaders: {
preference: false,
save: false
},
browser_alert: false
}
}
const state = defaultState
// getters
const getters = {
getNotificationsPreference: state => {
return state.notifications_preference.preference_list
},
getNotificationsPreferenceLoader: state => {
return state.notifications_preference.loaders.preference
},
getNotificationsPreferenceBrowserAlert: state => {
return state.notifications_preference.browser_alert
}
}
// actions
const actions = {
fetchNotificationsPreference ({ commit, getters, dispatch, rootGetters, state }) {
commit('SET_PREFERENCE_LOADER', true)
axios.post(fetchNotificationsUrl, {
}, { headers: { Authorization: 'Bearer ' + getters.getJWTToken } }).then(
resp => {
if (resp.data.status) {
commit('SET_PREFERENCE_LIST', resp.data.preference)
commit('SET_PREFERENCE_LOADER', false)
dispatch('isNoAccessForBrowserNotification')
}
}
).catch(error => {
console.log(error)
})
},
saveNotificationsPreference ({ commit, getters }) {
axios.post(saveNotificationsUrl, { payload: getters.getNotificationsPreference }, { headers: { Authorization: 'Bearer ' + getters.getJWTToken } }).then(
resp => {
}
).catch(error => {
console.log(error)
})
},
isNoAccessForBrowserNotification ({ commit }) {
console.debug('Method:checkBrowserNotificationEnabled')
if (Notification.permission === 'default' || Notification.permission === 'denied') {
// no access given, show alert message
commit('SET_BROWSER_ALERT', true)
return true
}
// access given
commit('SET_BROWSER_ALERT', false)
return true
},
// enable notifications for the user.
enableNotifications ({ commit }) {
if (Notification.permission === 'denied') {
window.open('https://docs.contentstudio.io/article/493-enabling-disabling-browser-notifications')
} else {
Notification.requestPermission()
commit('SET_BROWSER_ALERT', false)
}
},
resetNotificationsPreference ({ commit }) {
commit('RESET_NOTIFICATIONS_PREFERENCE')
}
}
// mutations
const mutations = {
SET_PREFERENCE_LIST (state, preference) {
state.notifications_preference.preference_list = preference
},
SET_PREFERENCE_LOADER (state, value) {
state.notifications_preference.loaders.preference = value
},
SET_BROWSER_ALERT (state, value) {
state.notifications_preference.browser_alert = value
},
RESET_NOTIFICATIONS_PREFERENCE (state) {
state = JSON.parse(JSON.stringify(defaultState))
}
}
export default {
state,
getters,
actions,
mutations
}