2019-02-09 18:04:49 +01:00
|
|
|
import { IGlobalState, StateConnection } from 'model'
|
2018-11-17 21:07:07 +01:00
|
|
|
import { storeBuilder } from './Store/Store'
|
|
|
|
|
|
2019-02-05 18:17:36 +01:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
|
2019-02-06 18:47:54 +01:00
|
|
|
import translate from './../../globalroutines/util'
|
|
|
|
|
|
2019-02-05 18:17:36 +01:00
|
|
|
import urlBase64ToUint8Array from '../../js/utility'
|
|
|
|
|
|
|
|
|
|
import messages from '../../statics/i18n'
|
2019-02-09 18:04:49 +01:00
|
|
|
import { UserStore } from '@store'
|
|
|
|
|
import globalroutines from './../../globalroutines/index'
|
|
|
|
|
|
|
|
|
|
const allTables = ['todos', 'sync_todos', 'sync_todos_patch', 'delete_todos', 'config', 'swmsg']
|
2019-02-12 12:06:01 +01:00
|
|
|
const allTablesAfterLogin = ['todos', 'sync_todos', 'sync_todos_patch', 'delete_todos', 'config', 'swmsg']
|
|
|
|
|
|
|
|
|
|
async function getstateConnSaved() {
|
|
|
|
|
const config = await globalroutines(null, 'readall', 'config', null)
|
|
|
|
|
if (config.length > 1) {
|
|
|
|
|
return config[1].stateconn
|
|
|
|
|
} else {
|
|
|
|
|
return 'online'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let stateConnDefault = 'online'
|
|
|
|
|
|
|
|
|
|
getstateConnSaved()
|
|
|
|
|
.then(conn => {
|
|
|
|
|
stateConnDefault = conn
|
|
|
|
|
})
|
2018-11-15 19:48:37 +01:00
|
|
|
|
|
|
|
|
const state: IGlobalState = {
|
|
|
|
|
conta: 0,
|
2019-02-05 18:17:36 +01:00
|
|
|
isSubscribed: false,
|
2018-11-15 19:48:37 +01:00
|
|
|
isLoginPage: false,
|
|
|
|
|
layoutNeeded: true,
|
|
|
|
|
mobileMode: false,
|
|
|
|
|
menuCollapse: true,
|
2019-01-02 18:01:36 +01:00
|
|
|
leftDrawerOpen: true,
|
2019-02-12 12:06:01 +01:00
|
|
|
stateConnection: stateConnDefault,
|
2019-01-30 01:05:31 +01:00
|
|
|
category: 'personal',
|
2019-01-29 23:13:28 +01:00
|
|
|
posts: [],
|
|
|
|
|
listatodo: [
|
2019-02-12 12:06:01 +01:00
|
|
|
{ namecat: 'personal', description: 'personal' },
|
|
|
|
|
{ namecat: 'work', description: 'work' },
|
|
|
|
|
{ namecat: 'shopping', description: 'shopping' }
|
|
|
|
|
]
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
const b = storeBuilder.module<IGlobalState>('GlobalModule', state)
|
|
|
|
|
|
|
|
|
|
// Getters
|
|
|
|
|
namespace Getters {
|
|
|
|
|
|
2018-12-26 21:02:16 +01:00
|
|
|
const conta = b.read(state => state.conta, 'conta')
|
2019-01-29 23:13:28 +01:00
|
|
|
const listatodo = b.read(state => state.listatodo, 'listatodo')
|
2019-01-30 01:05:31 +01:00
|
|
|
const category = b.read(state => state.category, 'category')
|
2018-12-22 18:42:00 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export const getters = {
|
2018-12-22 18:42:00 +01:00
|
|
|
get conta() {
|
|
|
|
|
return conta()
|
2019-01-29 23:13:28 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get listaTodo() {
|
|
|
|
|
return listatodo()
|
2019-01-30 01:05:31 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get category() {
|
|
|
|
|
return category()
|
2019-02-12 12:06:01 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get isOnline() {
|
|
|
|
|
return state.stateConnection === 'online'
|
2018-12-22 18:42:00 +01:00
|
|
|
}
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Mutations {
|
|
|
|
|
|
|
|
|
|
function setConta(state: IGlobalState, num: number) {
|
|
|
|
|
state.conta = num
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-02 18:01:36 +01:00
|
|
|
function setleftDrawerOpen(state: IGlobalState, bool: boolean) {
|
|
|
|
|
state.leftDrawerOpen = bool
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-30 01:05:31 +01:00
|
|
|
function setCategorySel(state: IGlobalState, cat: string) {
|
|
|
|
|
state.category = cat
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
function setStateConnection(state: IGlobalState, stateconn: StateConnection) {
|
|
|
|
|
if (state.stateConnection !== stateconn) {
|
|
|
|
|
console.log('INTERNET ', stateconn)
|
|
|
|
|
state.stateConnection = stateconn
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-30 01:05:31 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export const mutations = {
|
2019-01-02 18:01:36 +01:00
|
|
|
setConta: b.commit(setConta),
|
2019-01-30 01:05:31 +01:00
|
|
|
setleftDrawerOpen: b.commit(setleftDrawerOpen),
|
2019-02-09 18:04:49 +01:00
|
|
|
setCategorySel: b.commit(setCategorySel),
|
|
|
|
|
setStateConnection: b.commit(setStateConnection)
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Actions {
|
2018-11-17 20:32:28 +01:00
|
|
|
async function setConta(context, num: number) {
|
2018-11-15 19:48:37 +01:00
|
|
|
Mutations.mutations.setConta(num)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 18:17:36 +01:00
|
|
|
function createPushSubscription(context) {
|
|
|
|
|
if (!('serviceWorker' in navigator)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
if (!('PushManager' in window)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 18:17:36 +01:00
|
|
|
console.log('createPushSubscription')
|
|
|
|
|
|
|
|
|
|
let reg
|
|
|
|
|
const mykey = process.env.PUBLICKEY_PUSH
|
|
|
|
|
const mystate = state
|
|
|
|
|
navigator.serviceWorker.ready
|
|
|
|
|
.then(function (swreg) {
|
|
|
|
|
reg = swreg
|
|
|
|
|
return swreg.pushManager.getSubscription()
|
|
|
|
|
})
|
|
|
|
|
.then(function (subscription) {
|
|
|
|
|
mystate.isSubscribed = !(subscription === null)
|
|
|
|
|
|
|
|
|
|
if (mystate.isSubscribed) {
|
2019-02-12 19:09:43 +01:00
|
|
|
// console.log('User is already Subscribed!')
|
2019-02-05 18:17:36 +01:00
|
|
|
} else {
|
|
|
|
|
// Create a new subscription
|
|
|
|
|
let convertedVapidPublicKey = urlBase64ToUint8Array(mykey)
|
|
|
|
|
return reg.pushManager.subscribe({
|
|
|
|
|
userVisibleOnly: true,
|
|
|
|
|
applicationServerKey: convertedVapidPublicKey
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(function (newSub) {
|
2019-02-09 18:04:49 +01:00
|
|
|
// console.log('newSub', newSub)
|
2019-02-05 18:17:36 +01:00
|
|
|
if (newSub) {
|
|
|
|
|
saveNewSubscriptionToServer(context, newSub)
|
2019-02-12 12:06:01 +01:00
|
|
|
.then(ris => {
|
|
|
|
|
mystate.isSubscribed = true
|
|
|
|
|
})
|
|
|
|
|
.catch(e => {
|
|
|
|
|
console.log('Error during Subscription!', e)
|
|
|
|
|
})
|
2019-02-05 18:17:36 +01:00
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
})
|
|
|
|
|
.catch(function (err) {
|
|
|
|
|
console.log(err)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calling the Server to Save in the MongoDB the Subscriber
|
|
|
|
|
function saveNewSubscriptionToServer(context, newSub) {
|
|
|
|
|
console.log('saveSubscriptionToServer: ', newSub)
|
|
|
|
|
console.log('context', context)
|
|
|
|
|
|
|
|
|
|
const options = {
|
2019-02-06 18:47:54 +01:00
|
|
|
title: translate('notification.title_subscribed'),
|
|
|
|
|
content: translate('notification.subscribed'),
|
2019-02-05 18:17:36 +01:00
|
|
|
openUrl: '/'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let myres = {
|
|
|
|
|
options: { ...options },
|
2019-02-06 18:47:54 +01:00
|
|
|
subs: newSub,
|
|
|
|
|
others: {
|
2019-02-12 12:06:01 +01:00
|
|
|
userId: UserStore.state.userId,
|
|
|
|
|
access: UserStore.state.tokens[0].access
|
2019-02-06 18:47:54 +01:00
|
|
|
}
|
2019-02-05 18:17:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fetch(process.env.MONGODB_HOST + '/subscribe', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Accept': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(myres)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
async function deleteSubscriptionToServer(context) {
|
|
|
|
|
console.log('DeleteSubscriptionToServer: ')
|
|
|
|
|
|
|
|
|
|
return await fetch(process.env.MONGODB_HOST + '/subscribe/del', {
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Accept': 'application/json',
|
|
|
|
|
'x-auth': UserStore.state.x_auth_token
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 18:17:36 +01:00
|
|
|
function t(params) {
|
|
|
|
|
let msg = params.split('.')
|
|
|
|
|
let lang = UserStore.state.lang
|
|
|
|
|
|
|
|
|
|
let stringa = messages[lang]
|
|
|
|
|
|
|
|
|
|
let ris = stringa
|
|
|
|
|
msg.forEach(param => {
|
|
|
|
|
ris = ris[param]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ris
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prova(context) {
|
|
|
|
|
// console.log('prova')
|
|
|
|
|
|
|
|
|
|
// let msg = t('notification.title_subscribed')
|
|
|
|
|
|
|
|
|
|
// console.log('msg', msg)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
async function clearDataAfterLogout(context) {
|
|
|
|
|
console.log('clearDataAfterLogout')
|
2019-02-09 18:04:49 +01:00
|
|
|
|
|
|
|
|
// Clear all data from the IndexedDB
|
2019-02-12 12:06:01 +01:00
|
|
|
await allTables.forEach(table => {
|
2019-02-09 18:04:49 +01:00
|
|
|
globalroutines(null, 'clearalldata', table, null)
|
|
|
|
|
})
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
// REMOVE ALL SUBSCRIPTION
|
|
|
|
|
console.log('REMOVE ALL SUBSCRIPTION...')
|
|
|
|
|
await navigator.serviceWorker.ready.then(function(reg) {
|
|
|
|
|
console.log('... Ready')
|
|
|
|
|
reg.pushManager.getSubscription().then(function(subscription) {
|
|
|
|
|
console.log(' Found Subscription...')
|
|
|
|
|
subscription.unsubscribe().then(function(successful) {
|
|
|
|
|
// You've successfully unsubscribed
|
|
|
|
|
console.log('You\'ve successfully unsubscribed')
|
|
|
|
|
}).catch(function(e) {
|
|
|
|
|
// Unsubscription failed
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await deleteSubscriptionToServer(context)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function clearDataAfterLoginOnlyIfActiveConnection(context) {
|
|
|
|
|
|
|
|
|
|
// if (Getters.getters.isOnline) {
|
|
|
|
|
// console.log('clearDataAfterLoginOnlyIfActiveConnection')
|
|
|
|
|
// // Clear all data from the IndexedDB
|
|
|
|
|
// allTablesAfterLogin.forEach(table => {
|
|
|
|
|
// globalroutines(null, 'clearalldata', table, null)
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
}
|
2019-02-05 18:17:36 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
|
|
|
|
|
async function loadAfterLogin(context) {
|
|
|
|
|
actions.clearDataAfterLoginOnlyIfActiveConnection()
|
2019-02-05 18:17:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export const actions = {
|
2019-02-05 18:17:36 +01:00
|
|
|
setConta: b.dispatch(setConta),
|
|
|
|
|
createPushSubscription: b.dispatch(createPushSubscription),
|
|
|
|
|
loadAfterLogin: b.dispatch(loadAfterLogin),
|
2019-02-09 18:04:49 +01:00
|
|
|
clearDataAfterLogout: b.dispatch(clearDataAfterLogout),
|
2019-02-12 12:06:01 +01:00
|
|
|
clearDataAfterLoginOnlyIfActiveConnection: b.dispatch(clearDataAfterLoginOnlyIfActiveConnection),
|
2019-02-05 18:17:36 +01:00
|
|
|
prova: b.dispatch(prova)
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-22 23:30:02 +01:00
|
|
|
const stateGetter = b.state()
|
|
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
// Module
|
|
|
|
|
const GlobalModule = {
|
2018-12-26 21:02:16 +01:00
|
|
|
get state() {
|
|
|
|
|
return stateGetter()
|
|
|
|
|
},
|
2018-11-15 19:48:37 +01:00
|
|
|
getters: Getters.getters,
|
|
|
|
|
mutations: Mutations.mutations,
|
|
|
|
|
actions: Actions.actions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default GlobalModule
|