2019-03-13 01:53:53 +01:00
|
|
|
import { ICfgServer, IConfig, IGlobalState, ITodoList, 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'
|
|
|
|
|
|
2019-02-22 10:23:00 +01:00
|
|
|
import Api from '@api'
|
2019-03-04 17:28:29 +01:00
|
|
|
import * as Types from '@src/store/Api/ApiTypes'
|
2019-03-13 01:53:53 +01:00
|
|
|
import { costanti } from '@src/store/Modules/costanti'
|
|
|
|
|
import { tools } from '@src/store/Modules/tools'
|
|
|
|
|
import { GlobalStore, Todos, UserStore } from '@store'
|
|
|
|
|
import messages from '../../statics/i18n'
|
|
|
|
|
import globalroutines from './../../globalroutines/index'
|
2019-02-09 18:04:49 +01:00
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
let stateConnDefault = 'online'
|
|
|
|
|
|
|
|
|
|
getstateConnSaved()
|
2019-03-13 01:53:53 +01:00
|
|
|
.then((conn) => {
|
2019-02-12 12:06:01 +01:00
|
|
|
stateConnDefault = conn
|
|
|
|
|
})
|
2018-11-15 19:48:37 +01:00
|
|
|
|
|
|
|
|
const state: IGlobalState = {
|
|
|
|
|
conta: 0,
|
2019-02-13 18:48:30 +01:00
|
|
|
wasAlreadySubscribed: false,
|
|
|
|
|
wasAlreadySubOnDb: 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-02-22 10:23:00 +01:00
|
|
|
networkDataReceived: false,
|
|
|
|
|
cfgServer: [],
|
2019-02-27 02:58:41 +01:00
|
|
|
testp1: { contatore: 0, mioarray: [] },
|
2019-01-30 01:05:31 +01:00
|
|
|
category: 'personal',
|
2019-01-29 23:13:28 +01:00
|
|
|
posts: [],
|
2019-03-13 01:53:53 +01:00
|
|
|
menulinks: {},
|
2019-01-29 23:13:28 +01:00
|
|
|
listatodo: [
|
2019-02-12 12:06:01 +01:00
|
|
|
{ namecat: 'personal', description: 'personal' },
|
|
|
|
|
{ namecat: 'work', description: 'work' },
|
|
|
|
|
{ namecat: 'shopping', description: 'shopping' }
|
2019-02-22 10:23:00 +01:00
|
|
|
],
|
|
|
|
|
connData: {
|
|
|
|
|
uploading_server: 0,
|
|
|
|
|
uploading_indexeddb: 0,
|
|
|
|
|
downloading_server: 0,
|
|
|
|
|
downloading_indexeddb: 0
|
2019-03-04 17:28:29 +01:00
|
|
|
},
|
|
|
|
|
arrConfig: []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getConfig(id) {
|
|
|
|
|
return await globalroutines(null, 'read', 'config', null, id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getstateConnSaved() {
|
|
|
|
|
const config = await getConfig(costanti.CONFIG_ID_CFG)
|
2019-03-14 21:09:41 +01:00
|
|
|
// console.log('config', config)
|
2019-03-05 23:44:48 +01:00
|
|
|
if (config) {
|
|
|
|
|
if (config.length > 1) {
|
|
|
|
|
const cfgstateconn = config[1]
|
|
|
|
|
return cfgstateconn.stateconn
|
|
|
|
|
} else {
|
|
|
|
|
return 'online'
|
|
|
|
|
}
|
2019-03-04 17:28:29 +01:00
|
|
|
} else {
|
2019-03-05 23:44:48 +01:00
|
|
|
return 'offline'
|
2019-02-22 10:23:00 +01:00
|
|
|
}
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const b = storeBuilder.module<IGlobalState>('GlobalModule', state)
|
|
|
|
|
|
|
|
|
|
// Getters
|
|
|
|
|
namespace Getters {
|
|
|
|
|
|
2019-03-13 01:53:53 +01:00
|
|
|
const conta = b.read((state) => state.conta, 'conta')
|
|
|
|
|
const listatodo = b.read((state) => state.listatodo, 'listatodo')
|
|
|
|
|
const category = b.read((state) => state.category, 'category')
|
2018-12-22 18:42:00 +01:00
|
|
|
|
2019-03-13 01:53:53 +01:00
|
|
|
const testpao1_getter_contatore = b.read((state) => (param1) => state.testp1.contatore + 100 + param1, 'testpao1_getter_contatore')
|
|
|
|
|
const testpao1_getter_array = b.read((state) => (param1) => state.testp1.mioarray.filter((item) => item).map((item) => item.valore), 'testpao1_getter_array')
|
2019-03-04 17:28:29 +01:00
|
|
|
|
2019-03-13 01:53:53 +01:00
|
|
|
const getConfigbyId = b.read((state) => (id) => state.arrConfig.find((item) => item._id === id), 'getConfigbyId')
|
|
|
|
|
const getConfigStringbyId = b.read((state) => (params) => {
|
|
|
|
|
const config = state.arrConfig.find((item) => item._id === params.id)
|
2019-03-04 17:28:29 +01:00
|
|
|
if (config) {
|
|
|
|
|
return config.value
|
|
|
|
|
} else {
|
2019-03-05 23:44:48 +01:00
|
|
|
return params.default
|
2019-03-04 17:28:29 +01:00
|
|
|
}
|
|
|
|
|
}, 'getConfigStringbyId')
|
|
|
|
|
|
2019-03-13 01:53:53 +01:00
|
|
|
const showtype = b.read((state) => {
|
2019-03-04 17:28:29 +01:00
|
|
|
// const config = state.arrConfig.find(item => item._id === cat + costanti.CONFIG_ID_SHOW_TYPE_TODOS)
|
2019-03-13 01:53:53 +01:00
|
|
|
const config = state.arrConfig.find((item) => item._id === costanti.CONFIG_ID_SHOW_TYPE_TODOS)
|
|
|
|
|
if (config) {
|
2019-03-04 17:28:29 +01:00
|
|
|
return config.value
|
2019-03-13 01:53:53 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2019-03-04 17:28:29 +01:00
|
|
|
return ''
|
2019-03-13 01:53:53 +01:00
|
|
|
}
|
2019-03-04 17:28:29 +01:00
|
|
|
|
|
|
|
|
}, 'showtype')
|
2019-02-27 02:58:41 +01:00
|
|
|
|
2019-03-13 01:53:53 +01:00
|
|
|
const getmenu = b.read((state) => {
|
|
|
|
|
|
|
|
|
|
const arrlista = GlobalStore.state.listatodo
|
|
|
|
|
let listatodo = []
|
|
|
|
|
|
|
|
|
|
arrlista.forEach((elem: ITodoList) => {
|
|
|
|
|
const item = {
|
|
|
|
|
faIcon: 'fa fa-list-alt',
|
|
|
|
|
materialIcon: 'todo',
|
|
|
|
|
name: 'pages.' + elem.description,
|
|
|
|
|
route: '/todo/' + elem.namecat
|
|
|
|
|
}
|
|
|
|
|
listatodo.push(item)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (UserStore.state.isAdmin) {
|
|
|
|
|
state.menulinks = {
|
|
|
|
|
Dashboard: {
|
|
|
|
|
routes: [
|
|
|
|
|
{ route: '/', faIcon: 'fa fa-home', materialIcon: 'home', name: 'pages.home' },
|
2019-03-15 02:01:03 +01:00
|
|
|
{
|
|
|
|
|
route: '/todo', faIcon: 'fa fa-list-alt', materialIcon: 'format_list_numbered', name: 'pages.Todo',
|
2019-03-13 01:53:53 +01:00
|
|
|
routes2: listatodo
|
|
|
|
|
},
|
2019-03-21 20:43:15 +01:00
|
|
|
{ route: '/projects', faIcon: 'fa fa-list-alt', materialIcon: 'next_week', name: 'pages.Projects' },
|
|
|
|
|
|
2019-03-13 01:53:53 +01:00
|
|
|
{ route: '/category', faIcon: 'fa fa-list-alt', materialIcon: 'category', name: 'pages.Category' },
|
|
|
|
|
{ route: '/admin/cfgserv', faIcon: 'fa fa-database', materialIcon: 'event_seat', name: 'pages.Admin' },
|
|
|
|
|
{ route: '/admin/testp1/par1', faIcon: 'fa fa-database', materialIcon: 'restore', name: 'pages.Test1' },
|
|
|
|
|
{ route: '/admin/testp1/par2', faIcon: 'fa fa-database', materialIcon: 'restore', name: 'pages.Test2' }
|
|
|
|
|
/* {route: '/vreg?idlink=aaa', faIcon: 'fa fa-login', materialIcon: 'login', name: 'pages.vreg'},*/
|
|
|
|
|
],
|
|
|
|
|
show: true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// PRODUCTION USER:
|
|
|
|
|
if (process.env.PROD) {
|
|
|
|
|
state.menulinks = {
|
|
|
|
|
Dashboard: {
|
|
|
|
|
routes: [
|
|
|
|
|
{ route: '/', faIcon: 'fa fa-home', materialIcon: 'home', name: 'pages.home' }
|
|
|
|
|
],
|
|
|
|
|
show: true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// SERVER TEST
|
|
|
|
|
state.menulinks = {
|
|
|
|
|
Dashboard: {
|
|
|
|
|
routes: [
|
|
|
|
|
{ route: '/', faIcon: 'fa fa-home', materialIcon: 'home', name: 'pages.home' },
|
|
|
|
|
{
|
2019-03-13 18:11:26 +01:00
|
|
|
route: '/todo', faIcon: 'fa fa-list-alt', materialIcon: 'format_list_numbered', name: 'pages.Todo',
|
2019-03-13 01:53:53 +01:00
|
|
|
routes2: listatodo
|
|
|
|
|
},
|
|
|
|
|
{ route: '/category', faIcon: 'fa fa-list-alt', materialIcon: 'category', name: 'pages.Category' }
|
|
|
|
|
// { route: '/signup', faIcon: 'fa fa-registered', materialIcon: 'home', name: 'pages.SignUp' },
|
|
|
|
|
// { route: '/signin', faIcon: 'fa fa-anchor', materialIcon: 'home', name: 'pages.SignIn' },
|
|
|
|
|
/* {route: '/vreg?idlink=aaa', faIcon: 'fa fa-login', materialIcon: 'login', name: 'pages.vreg'},*/
|
|
|
|
|
],
|
|
|
|
|
show: true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return state.menulinks
|
|
|
|
|
|
|
|
|
|
}, 'getmenu')
|
2019-02-27 02:58:41 +01:00
|
|
|
|
2018-11-15 19:48:37 +01:00
|
|
|
export const getters = {
|
2019-02-27 02:58:41 +01:00
|
|
|
get testpao1_getter_contatore() {
|
|
|
|
|
return testpao1_getter_contatore()
|
|
|
|
|
},
|
|
|
|
|
get testpao1_getter_array() {
|
|
|
|
|
return testpao1_getter_array()
|
|
|
|
|
},
|
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
|
|
|
},
|
|
|
|
|
|
2019-03-04 17:28:29 +01:00
|
|
|
get getConfigbyId() {
|
|
|
|
|
return getConfigbyId()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get getConfigStringbyId() {
|
|
|
|
|
return getConfigStringbyId()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get showtype() {
|
|
|
|
|
return showtype()
|
|
|
|
|
},
|
|
|
|
|
|
2019-03-13 01:53:53 +01:00
|
|
|
get getmenu() {
|
|
|
|
|
return getmenu()
|
|
|
|
|
},
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
get isOnline() {
|
2019-02-22 10:23:00 +01:00
|
|
|
console.log('*********************** isOnline')
|
2019-02-12 12:06:01 +01:00
|
|
|
return state.stateConnection === 'online'
|
2019-02-22 10:23:00 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
get isNewVersionAvailable() {
|
2019-03-14 21:09:41 +01:00
|
|
|
// console.log('state.cfgServer', state.cfgServer)
|
2019-03-13 01:53:53 +01:00
|
|
|
const serversrec = state.cfgServer.find((x) => x.chiave === tools.SERVKEY_VERS)
|
2019-03-14 21:09:41 +01:00
|
|
|
// console.log('Record ', serversrec)
|
2019-02-22 10:23:00 +01:00
|
|
|
if (serversrec) {
|
|
|
|
|
console.log('Vers Server ', serversrec.valore, 'Vers locale:', process.env.APP_VERSION)
|
|
|
|
|
return serversrec.valore !== process.env.APP_VERSION
|
2019-03-13 01:53:53 +01:00
|
|
|
} else {
|
2019-02-22 10:23:00 +01:00
|
|
|
return false
|
2019-03-13 01:53:53 +01:00
|
|
|
}
|
2018-12-22 18:42:00 +01:00
|
|
|
}
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Mutations {
|
2019-02-27 02:58:41 +01:00
|
|
|
function setPaoArray(state: IGlobalState, miorec: ICfgServer) {
|
|
|
|
|
state.testp1.mioarray[state.testp1.mioarray.length - 1] = miorec
|
|
|
|
|
|
|
|
|
|
tools.notifyarraychanged(state.testp1.mioarray)
|
|
|
|
|
console.log('last elem = ', state.testp1.mioarray[state.testp1.mioarray.length - 1])
|
|
|
|
|
}
|
2019-03-04 17:28:29 +01:00
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
function NewArray(state: IGlobalState, newarr: ICfgServer[]) {
|
|
|
|
|
state.testp1.mioarray = newarr
|
|
|
|
|
}
|
2019-03-04 17:28:29 +01:00
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
function setPaoArray_Delete(state: IGlobalState) {
|
|
|
|
|
state.testp1.mioarray.pop()
|
|
|
|
|
}
|
2018-11-15 19:48:37 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-03-04 17:28:29 +01:00
|
|
|
function saveConfig(state: IGlobalState, data: IConfig) {
|
|
|
|
|
let dataout
|
|
|
|
|
// this.$set(dataout, data.value, {'value': 'default value'})
|
|
|
|
|
return globalroutines(null, 'write', 'config', { _id: data._id, value: data.value })
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
function SetwasAlreadySubOnDb(state: IGlobalState, subscrib: boolean) {
|
|
|
|
|
state.wasAlreadySubOnDb = subscrib
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 17:28:29 +01:00
|
|
|
function setShowType(state: IGlobalState, showtype: number) {
|
2019-03-05 23:44:48 +01:00
|
|
|
console.log('setShowType', showtype)
|
2019-03-04 17:28:29 +01:00
|
|
|
const config = Getters.getters.getConfigbyId(costanti.CONFIG_ID_SHOW_TYPE_TODOS)
|
2019-03-05 23:44:48 +01:00
|
|
|
console.log('config', config)
|
2019-03-04 17:28:29 +01:00
|
|
|
if (config) {
|
|
|
|
|
config.value = String(showtype)
|
|
|
|
|
Todos.state.showtype = parseInt(config.value)
|
2019-03-05 23:44:48 +01:00
|
|
|
} else {
|
|
|
|
|
Todos.state.showtype = showtype
|
2019-03-04 17:28:29 +01:00
|
|
|
}
|
2019-03-05 23:44:48 +01:00
|
|
|
console.log('Todos.state.showtype', Todos.state.showtype)
|
|
|
|
|
GlobalStore.mutations.saveConfig({ _id: costanti.CONFIG_ID_SHOW_TYPE_TODOS, value: String(showtype) })
|
|
|
|
|
|
2019-03-04 17:28:29 +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),
|
2019-02-13 18:48:30 +01:00
|
|
|
setStateConnection: b.commit(setStateConnection),
|
2019-02-27 02:58:41 +01:00
|
|
|
SetwasAlreadySubOnDb: b.commit(SetwasAlreadySubOnDb),
|
2019-03-04 17:28:29 +01:00
|
|
|
saveConfig: b.commit(saveConfig),
|
2019-02-27 02:58:41 +01:00
|
|
|
setPaoArray: b.commit(setPaoArray),
|
|
|
|
|
setPaoArray_Delete: b.commit(setPaoArray_Delete),
|
2019-03-04 17:28:29 +01:00
|
|
|
NewArray: b.commit(NewArray),
|
|
|
|
|
setShowType: b.commit(setShowType)
|
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) {
|
2019-02-13 18:48:30 +01:00
|
|
|
|
|
|
|
|
// If Already subscribed, don't send to the Server DB
|
2019-02-14 18:38:23 +01:00
|
|
|
// if (state.wasAlreadySubOnDb) {
|
|
|
|
|
// // console.log('wasAlreadySubOnDb!')
|
|
|
|
|
// return
|
|
|
|
|
// }
|
2019-02-13 18:48:30 +01:00
|
|
|
|
2019-02-05 18:17:36 +01:00
|
|
|
if (!('serviceWorker' in navigator)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
if (!('PushManager' in window)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 18:48:07 +01:00
|
|
|
// console.log('createPushSubscription')
|
2019-02-05 18:17:36 +01:00
|
|
|
|
|
|
|
|
let reg
|
|
|
|
|
const mykey = process.env.PUBLICKEY_PUSH
|
|
|
|
|
const mystate = state
|
2019-02-14 18:38:23 +01:00
|
|
|
return navigator.serviceWorker.ready
|
2019-03-15 02:01:03 +01:00
|
|
|
.then(function (swreg) {
|
2019-02-05 18:17:36 +01:00
|
|
|
reg = swreg
|
|
|
|
|
return swreg.pushManager.getSubscription()
|
|
|
|
|
})
|
2019-03-15 02:01:03 +01:00
|
|
|
.then(function (subscription) {
|
2019-02-13 18:48:30 +01:00
|
|
|
mystate.wasAlreadySubscribed = !(subscription === null)
|
2019-02-05 18:17:36 +01:00
|
|
|
|
2019-02-14 18:38:23 +01:00
|
|
|
if (mystate.wasAlreadySubscribed) {
|
2019-02-27 02:58:41 +01:00
|
|
|
// console.log('User is already SAVED Subscribe on DB!')
|
2019-02-14 18:38:23 +01:00
|
|
|
// return null
|
|
|
|
|
return subscription
|
2019-02-05 18:17:36 +01:00
|
|
|
} else {
|
|
|
|
|
// Create a new subscription
|
2019-02-14 18:38:23 +01:00
|
|
|
console.log('Create a new subscription')
|
2019-03-13 01:53:53 +01:00
|
|
|
const convertedVapidPublicKey = urlBase64ToUint8Array(mykey)
|
2019-02-05 18:17:36 +01:00
|
|
|
return reg.pushManager.subscribe({
|
|
|
|
|
userVisibleOnly: true,
|
|
|
|
|
applicationServerKey: convertedVapidPublicKey
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
2019-03-15 02:01:03 +01:00
|
|
|
.then(function (newSub) {
|
2019-02-13 18:48:30 +01:00
|
|
|
saveNewSubscriptionToServer(context, newSub)
|
2019-02-05 18:17:36 +01:00
|
|
|
})
|
2019-03-15 02:01:03 +01:00
|
|
|
.catch(function (err) {
|
2019-02-14 18:38:23 +01:00
|
|
|
console.log('ERR createPushSubscription:', err)
|
2019-02-05 18:17:36 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calling the Server to Save in the MongoDB the Subscriber
|
|
|
|
|
function saveNewSubscriptionToServer(context, newSub) {
|
2019-02-13 18:48:30 +01:00
|
|
|
// If already subscribed, exit
|
2019-03-13 01:53:53 +01:00
|
|
|
if (!newSub) {
|
2019-02-13 18:48:30 +01:00
|
|
|
return
|
2019-03-13 01:53:53 +01:00
|
|
|
}
|
2019-02-13 18:48:30 +01:00
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
// console.log('saveSubscriptionToServer: ', newSub)
|
2019-02-13 18:48:30 +01:00
|
|
|
// console.log('context', context)
|
2019-02-05 18:17:36 +01:00
|
|
|
|
2019-02-14 18:38:23 +01:00
|
|
|
let options = null
|
|
|
|
|
|
|
|
|
|
// If is not already stored in DB, then show the message to the user.
|
2019-02-14 19:01:32 +01:00
|
|
|
if (!state.wasAlreadySubscribed) {
|
2019-02-14 18:38:23 +01:00
|
|
|
options = {
|
|
|
|
|
title: translate('notification.title_subscribed'),
|
|
|
|
|
content: translate('notification.subscribed'),
|
|
|
|
|
openUrl: '/'
|
|
|
|
|
}
|
2019-02-14 19:01:32 +01:00
|
|
|
}
|
2019-02-05 18:17:36 +01:00
|
|
|
|
2019-03-13 01:53:53 +01:00
|
|
|
const myres = {
|
2019-02-14 19:01:32 +01:00
|
|
|
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-22 10:23:00 +01:00
|
|
|
}
|
2019-02-05 18:17:36 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-19 02:33:02 +01:00
|
|
|
return Api.SendReq('/subscribe', 'POST', myres)
|
2019-03-13 01:53:53 +01:00
|
|
|
.then((res) => {
|
2019-02-13 18:48:30 +01:00
|
|
|
state.wasAlreadySubscribed = true
|
|
|
|
|
state.wasAlreadySubOnDb = true
|
2019-02-05 18:17:36 +01:00
|
|
|
|
2019-02-27 02:58:41 +01:00
|
|
|
localStorage.setItem(tools.localStorage.wasAlreadySubOnDb, String(state.wasAlreadySubOnDb))
|
2019-02-13 18:48:30 +01:00
|
|
|
})
|
2019-03-13 01:53:53 +01:00
|
|
|
.catch((e) => {
|
2019-02-13 18:48:30 +01:00
|
|
|
console.log('Error during Subscription!', e)
|
|
|
|
|
})
|
2019-02-05 18:17:36 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
async function deleteSubscriptionToServer(context) {
|
|
|
|
|
console.log('DeleteSubscriptionToServer: ')
|
|
|
|
|
|
2019-03-15 02:01:03 +01:00
|
|
|
return Api.SendReq('/subscribe/del', 'DELETE', null)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
|
|
|
|
|
})
|
2019-02-12 12:06:01 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 18:17:36 +01:00
|
|
|
function t(params) {
|
2019-03-13 01:53:53 +01:00
|
|
|
const msg = params.split('.')
|
|
|
|
|
const lang = UserStore.state.lang
|
2019-02-05 18:17:36 +01:00
|
|
|
|
2019-03-13 01:53:53 +01:00
|
|
|
const stringa = messages[lang]
|
2019-02-05 18:17:36 +01:00
|
|
|
|
|
|
|
|
let ris = stringa
|
2019-03-13 01:53:53 +01:00
|
|
|
msg.forEach((param) => {
|
2019-02-05 18:17:36 +01:00
|
|
|
ris = ris[param]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ris
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prova(context) {
|
|
|
|
|
// console.log('prova')
|
2019-02-27 02:58:41 +01:00
|
|
|
// state.testp1.mioarray[state.testp1.mioarray.length - 1].valore = 'VALMODIF';
|
2019-02-05 18:17:36 +01:00
|
|
|
|
|
|
|
|
// 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-03-21 20:43:15 +01:00
|
|
|
for (const table of tools.allTables) {
|
2019-02-20 19:17:29 +01:00
|
|
|
await globalroutines(null, 'clearalldata', table, null)
|
|
|
|
|
}
|
2019-02-09 18:04:49 +01:00
|
|
|
|
2019-02-14 18:38:23 +01:00
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
|
|
// REMOVE ALL SUBSCRIPTION
|
|
|
|
|
console.log('REMOVE ALL SUBSCRIPTION...')
|
2019-03-21 20:43:15 +01:00
|
|
|
await navigator.serviceWorker.ready.then((reg) => {
|
2019-02-14 18:38:23 +01:00
|
|
|
console.log('... Ready')
|
2019-03-15 02:01:03 +01:00
|
|
|
reg.pushManager.getSubscription().then((subscription) => {
|
2019-02-14 18:38:23 +01:00
|
|
|
console.log(' Found Subscription...')
|
2019-03-15 02:01:03 +01:00
|
|
|
if (subscription) {
|
2019-03-21 20:43:15 +01:00
|
|
|
subscription.unsubscribe().then((successful) => {
|
2019-03-15 02:01:03 +01:00
|
|
|
// You've successfully unsubscribed
|
|
|
|
|
console.log('You\'ve successfully unsubscribed')
|
2019-03-21 20:43:15 +01:00
|
|
|
}).catch( (e) => {
|
2019-03-15 02:01:03 +01:00
|
|
|
// Unsubscription failed
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-02-12 12:06:01 +01:00
|
|
|
})
|
|
|
|
|
})
|
2019-02-14 18:38:23 +01:00
|
|
|
}
|
2019-02-12 12:06:01 +01:00
|
|
|
|
|
|
|
|
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) {
|
2019-03-04 17:28:29 +01:00
|
|
|
console.log('loadAfterLogin')
|
2019-02-12 12:06:01 +01:00
|
|
|
actions.clearDataAfterLoginOnlyIfActiveConnection()
|
2019-03-04 17:28:29 +01:00
|
|
|
|
|
|
|
|
state.arrConfig = await globalroutines(null, 'readall', 'config', null)
|
2019-02-05 18:17:36 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-22 10:23:00 +01:00
|
|
|
async function saveCfgServerKey(context, dataval: ICfgServer) {
|
|
|
|
|
console.log('saveCfgServerKey dataval', dataval)
|
|
|
|
|
|
2019-03-13 01:53:53 +01:00
|
|
|
const ris = await Api.SendReq('/admin/updateval', 'POST', { pairval: dataval })
|
|
|
|
|
.then((res) => {
|
2019-02-22 10:23:00 +01:00
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function checkUpdates(context) {
|
|
|
|
|
console.log('checkUpdates')
|
|
|
|
|
|
|
|
|
|
// if (UserStore.state.userId === '')
|
|
|
|
|
// return false // Login not made
|
|
|
|
|
|
|
|
|
|
state.networkDataReceived = false
|
|
|
|
|
|
2019-03-13 01:53:53 +01:00
|
|
|
const ris = await Api.SendReq('/checkupdates', 'GET', null)
|
|
|
|
|
.then((res) => {
|
2019-02-22 10:23:00 +01:00
|
|
|
state.networkDataReceived = true
|
|
|
|
|
|
|
|
|
|
console.log('******* checkUpdates RES :', res.data.cfgServer)
|
|
|
|
|
if (res.data.cfgServer) {
|
|
|
|
|
state.cfgServer = [...res.data.cfgServer]
|
|
|
|
|
console.log('res.data.cfgServer', res.data.cfgServer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// console.log('********** res', 'state.todos', state.todos, 'checkPending', checkPending)
|
|
|
|
|
// After Login will store into the indexedDb...
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
})
|
2019-03-13 01:53:53 +01:00
|
|
|
.catch((error) => {
|
2019-02-22 10:23:00 +01:00
|
|
|
console.log('error checkUpdates', error)
|
|
|
|
|
UserStore.mutations.setErrorCatch(error)
|
|
|
|
|
return error
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
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-22 10:23:00 +01:00
|
|
|
prova: b.dispatch(prova),
|
|
|
|
|
saveCfgServerKey: b.dispatch(saveCfgServerKey),
|
|
|
|
|
checkUpdates: b.dispatch(checkUpdates)
|
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()
|
|
|
|
|
},
|
2019-03-13 01:53:53 +01:00
|
|
|
actions: Actions.actions,
|
2018-11-15 19:48:37 +01:00
|
|
|
getters: Getters.getters,
|
2019-03-13 01:53:53 +01:00
|
|
|
mutations: Mutations.mutations
|
2018-11-15 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default GlobalModule
|