- cleaned some code.

- routing offline
- pushNotification
This commit is contained in:
Paolo Arena
2019-02-12 12:06:01 +01:00
parent e755ada1ff
commit a024b78e95
21 changed files with 560 additions and 247 deletions

View File

@@ -5,8 +5,8 @@ import Api from '@api'
import { rescodes } from './rescodes'
import { GlobalStore, Todos, UserStore } from '@store'
import globalroutines from './../../globalroutines/index'
import { Mutation } from "vuex-module-decorators"
import { serv_constants } from "@src/store/Modules/serv_constants"
import { Mutation } from 'vuex-module-decorators'
import { serv_constants } from '@src/store/Modules/serv_constants'
const state: ITodosState = {
@@ -74,7 +74,7 @@ namespace Actions {
let something = false
if ('serviceWorker' in navigator) {
// console.log(' -------- sendSwMsgIfAvailable')
console.log(' -------- sendSwMsgIfAvailable')
let count = await checkPendingMsg(null)
if (count > 0) {
@@ -86,19 +86,19 @@ namespace Actions {
// let recclone = [...arr_recmsg]
if (arr_recmsg.length > 0) {
console.log(' TROVATI MSG PENDENTI ! ORA LI MANDO: ', arr_recmsg)
// console.log(' TROVATI MSG PENDENTI ! ORA LI MANDO: ', arr_recmsg)
// console.log('---------------------- 2) navigator (2) .serviceWorker.ready')
something = true
for (let rec of arr_recmsg) {
console.log(' .... sw.sync.register ( ', rec._id)
if ('SyncManager' in window) {
sw.sync.register(rec._id)
} else {
// console.log(' .... sw.sync.register ( ', rec._id)
// if ('SyncManager' in window) {
// sw.sync.register(rec._id)
// } else {
// #Todo ++ Alternative to SyncManager
Api.syncAlternative(rec._id)
}
// }
}
return something
}
@@ -115,12 +115,10 @@ namespace Actions {
await aspettansec(1000)
// console.log('waitAndcheckPendingMsg')
return await checkPendingMsg(context)
.then(ris => {
if (ris) {
console.log('ris = ', ris)
console.log('risPending = ', ris)
const result = sendSwMsgIfAvailable()
.then(something => {
if (something) {
@@ -136,14 +134,30 @@ namespace Actions {
async function waitAndRefreshData(context) {
await aspettansec(3000)
// console.log('waitAndRefreshData')
return await dbLoadTodo(context, false)
}
async function checkPendingMsg(context) {
// console.log('checkPendingMsg')
const config = await globalroutines(null, 'readall', 'config', null)
// console.log('config', config)
try {
if (config) {
if (config[1].stateconn !== undefined) {
// console.log('config.stateconn', config[1].stateconn)
if (config[1].stateconn !== GlobalStore.state.stateConnection) {
GlobalStore.mutations.setStateConnection(config[1].stateconn)
}
}
}
} catch (e) {
}
return new Promise(function (resolve, reject) {
/*
@@ -173,45 +187,49 @@ namespace Actions {
async function dbLoadTodo(context, checkPending: boolean = false) {
console.log('dbLoadTodo', checkPending)
const token = UserStore.state.idToken
if (UserStore.state.userId === '')
return false // Login not made
let call = process.env.MONGODB_HOST + '/todos/' + UserStore.state.userId
state.networkDataReceived = false
let ris = await Api.SendReq(call, UserStore.state.lang, token, 'GET', null)
.then(({ resData, body }) => {
let ris = await Api.SendReq(call, 'GET', null)
.then(({ resData, body, status }) => {
state.networkDataReceived = true
// console.log('******* UPDATE TODOS.STATE.TODOS !:', resData.todos)
state.todos = [...body.todos]
Todos.mutations.setTodos_changed()
if (body.todos) {
state.todos = [...body.todos]
Todos.mutations.setTodos_changed()
}
console.log('********** resData', resData, 'state.todos', state.todos, 'checkPending', checkPending)
// After Login will store into the indexedDb...
return rescodes.OK
return { status }
})
.catch((error) => {
.catch(error => {
console.log('error=', error)
UserStore.mutations.setErrorCatch(error)
return UserStore.getters.getServerCode
return { status }
})
console.log('ris FUNZ: ', ris.code, 'status', ris.status)
console.log('ris : ', ris)
console.log('ris STATUS: ', ris.status)
if (!Todos.state.networkDataReceived) {
if (ris.status === serv_constants.RIS_CODE__HTTP_FORBIDDEN_INVALID_TOKEN) {
consolelogpao('UNAUTHORIZING... TOKEN EXPIRED... !! ')
} else {
consolelogpao('NETWORK UNREACHABLE ! (Error in fetch)', UserStore.getters.getServerCode, ris.code)
consolelogpao('NETWORK UNREACHABLE ! (Error in fetch)', UserStore.getters.getServerCode, ris.status)
}
// Read all data from IndexedDB Store into Memory
await updateArrayInMemory(context)
} else {
if (ris.code === rescodes.OK && checkPending) {
if (ris.status === rescodes.OK && checkPending) {
waitAndcheckPendingMsg(context)
}
}
@@ -273,14 +291,15 @@ namespace Actions {
console.log('dbInsertSaveTodo', itemtodo, method)
let call = process.env.MONGODB_HOST + '/todos'
if (UserStore.state.userId === '')
return false // Login not made
if (method !== 'POST')
call += '/' + itemtodo._id
const token = UserStore.state.idToken
console.log('TODO TO SAVE: ', itemtodo)
let res = await Api.SendReq(call, UserStore.state.lang, token, method, itemtodo)
let res = await Api.SendReq(call, method, itemtodo)
.then(({ res, newItem }) => {
console.log('dbInsertSaveTodo RIS =', newItem)
if (newItem) {
@@ -301,9 +320,10 @@ namespace Actions {
// console.log('dbDeleteTodo', item)
let call = process.env.MONGODB_HOST + '/todos/' + item._id
const token = UserStore.state.idToken
if (UserStore.state.userId === '')
return false // Login not made
let res = await Api.SendReq(call, UserStore.state.lang, token, 'DELETE', item)
let res = await Api.SendReq(call, 'DELETE', item)
.then(function ({ res, itemris }) {
if (res.status === 200) {