Cleaning Code, moving functions...
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
import Api from '@api'
|
||||
import { ITodo } from '@src/model'
|
||||
import { GlobalStore, Todos, UserStore } from '@store'
|
||||
import { UserStore } from '@store'
|
||||
import globalroutines from './../../globalroutines/index'
|
||||
import { costanti } from './costanti'
|
||||
import Quasar from 'quasar'
|
||||
@@ -12,7 +10,6 @@ export interface INotify {
|
||||
}
|
||||
|
||||
export const tools = {
|
||||
allTables: ['todos', 'categories', 'sync_post_todos', 'sync_patch_todos', 'delete_todos', 'config', 'swmsg'],
|
||||
EMPTY: 0,
|
||||
CALLING: 10,
|
||||
OK: 20,
|
||||
@@ -24,9 +21,6 @@ export const tools = {
|
||||
|
||||
arrLangUsed: ['enUs', 'it', 'es'],
|
||||
|
||||
LIST_END: '10000000',
|
||||
LIST_START: '0',
|
||||
|
||||
SERVKEY_VERS: 'vers',
|
||||
|
||||
localStorage: {
|
||||
@@ -48,15 +42,6 @@ export const tools = {
|
||||
PRIORITY_LOW: 0
|
||||
},
|
||||
|
||||
DB: {
|
||||
CMD_SYNC: 'sync-',
|
||||
CMD_SYNC_NEW: 'sync-new-',
|
||||
CMD_DELETE: 'sync-delete-',
|
||||
TABLE_SYNC_POST: 'sync_post_',
|
||||
TABLE_SYNC_PATCH: 'sync_patch_',
|
||||
TABLE_DELETE: 'delete_'
|
||||
},
|
||||
|
||||
MenuAction: {
|
||||
DELETE: 100,
|
||||
TOGGLE_EXPIRING: 101,
|
||||
@@ -393,145 +378,6 @@ export const tools = {
|
||||
return result
|
||||
},
|
||||
|
||||
async cmdToSyncAndDb(cmd, table, method, item: ITodo, id, msg: String) {
|
||||
// Send to Server to Sync
|
||||
|
||||
// console.log('cmdToSyncAndDb', cmd, table, method, item.descr, id, msg)
|
||||
|
||||
let cmdSw = cmd
|
||||
if ((cmd === tools.DB.CMD_SYNC_NEW) || (cmd === tools.DB.CMD_DELETE)) {
|
||||
cmdSw = tools.DB.CMD_SYNC
|
||||
}
|
||||
|
||||
if ('serviceWorker' in navigator) {
|
||||
return await navigator.serviceWorker.ready
|
||||
.then((sw) => {
|
||||
// console.log('---------------------- navigator.serviceWorker.ready')
|
||||
|
||||
return globalroutines(null, 'write', table, item, id)
|
||||
.then((id) => {
|
||||
// console.log('id', id)
|
||||
const sep = '|'
|
||||
|
||||
const multiparams = cmdSw + sep + table + sep + method + sep + UserStore.state.x_auth_token + sep + UserStore.state.lang
|
||||
const mymsgkey = {
|
||||
_id: multiparams,
|
||||
value: multiparams
|
||||
}
|
||||
return globalroutines(null, 'write', 'swmsg', mymsgkey, multiparams)
|
||||
.then((ris) => {
|
||||
// if ('SyncManager' in window) {
|
||||
// console.log(' SENDING... sw.sync.register', multiparams)
|
||||
// return sw.sync.register(multiparams)
|
||||
// } else {
|
||||
// #Todo ++ Alternative 2 to SyncManager
|
||||
return Api.syncAlternative(multiparams)
|
||||
// }
|
||||
})
|
||||
.then(() => {
|
||||
let data = null
|
||||
if (msg !== '') {
|
||||
data = { message: msg, position: 'bottom', timeout: 3000 }
|
||||
}
|
||||
return data
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Errore in globalroutines', table, err)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
async dbInsertSave(call, item, method) {
|
||||
|
||||
let ret = true
|
||||
if (!('serviceWorker' in navigator)) {
|
||||
|
||||
console.log('dbInsertSave', item, method)
|
||||
|
||||
if (UserStore.state.userId === '') {
|
||||
return false
|
||||
} // Login not made
|
||||
|
||||
call = '/' + call
|
||||
if (method !== 'POST') {
|
||||
call += '/' + item._id
|
||||
}
|
||||
|
||||
console.log('SAVE: ', item)
|
||||
|
||||
ret = await Api.SendReq(call, method, item)
|
||||
.then((res) => {
|
||||
console.log('dbInsertSave ', call, 'to the Server', res.data)
|
||||
|
||||
return (res.status === 200)
|
||||
})
|
||||
.catch((error) => {
|
||||
UserStore.mutations.setErrorCatch(error)
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
return ret
|
||||
},
|
||||
|
||||
async dbdeleteItem(call, item) {
|
||||
|
||||
if (!('serviceWorker' in navigator)) {
|
||||
// console.log('dbdeleteItem', item)
|
||||
if (UserStore.state.userId === '') {
|
||||
return false
|
||||
} // Login not made
|
||||
|
||||
call = '/' + call
|
||||
|
||||
const res = await Api.SendReq(call + item._id, 'DELETE', item)
|
||||
.then((res) => {
|
||||
console.log('dbdeleteItem to the Server')
|
||||
return res
|
||||
})
|
||||
.catch((error) => {
|
||||
UserStore.mutations.setErrorCatch(error)
|
||||
return UserStore.getters.getServerCode
|
||||
})
|
||||
|
||||
return res
|
||||
}
|
||||
},
|
||||
|
||||
async cmdToSyncAndDbTable(cmd, nametab: string, table, method, item: ITodo, id, msg: String) {
|
||||
// Send to Server to Sync
|
||||
|
||||
console.log('cmdToSyncAndDb', cmd, table, method, item.descr, id, msg)
|
||||
|
||||
const risdata = await tools.cmdToSyncAndDb(cmd, table, method, item, id, msg)
|
||||
|
||||
if (cmd === tools.DB.CMD_SYNC_NEW) {
|
||||
if ((method === 'POST') || (method === 'PATCH')) {
|
||||
await tools.dbInsertSave(nametab, item, method)
|
||||
}
|
||||
} else if (cmd === tools.DB.CMD_DELETE) {
|
||||
await tools.dbdeleteItem(nametab, item)
|
||||
}
|
||||
|
||||
return risdata
|
||||
},
|
||||
|
||||
deleteItemToSyncAndDb(nametab: string, item, id) {
|
||||
tools.cmdToSyncAndDbTable(tools.DB.CMD_DELETE, nametab, tools.DB.TABLE_DELETE + nametab, 'DELETE', item, id, '')
|
||||
},
|
||||
|
||||
async saveItemToSyncAndDb(nametab: string, method, item) {
|
||||
let table = ''
|
||||
if (method === 'POST')
|
||||
table = tools.DB.TABLE_SYNC_POST
|
||||
else if (method === 'PATCH')
|
||||
table = tools.DB.TABLE_SYNC_PATCH
|
||||
|
||||
return await tools.cmdToSyncAndDbTable(tools.DB.CMD_SYNC_NEW, nametab, table + nametab, method, item, 0, '')
|
||||
},
|
||||
|
||||
showNotif(q: any, msg, data?: INotify | null) {
|
||||
let myicon = data ? data.icon : 'ion-add'
|
||||
if (!myicon) {
|
||||
@@ -562,6 +408,7 @@ export const tools = {
|
||||
mylang = 'es'
|
||||
}
|
||||
|
||||
|
||||
if (!(tools.arrLangUsed.includes(mylang))) {
|
||||
console.log('non incluso ', mylang)
|
||||
mylang = tools.arrLangUsed[0]
|
||||
@@ -587,124 +434,12 @@ export const tools = {
|
||||
globalroutines(null, 'log', strlog + ' ' + strlog2 + ' ' + strlog3, null)
|
||||
},
|
||||
|
||||
async checkPendingMsg() {
|
||||
// console.log('checkPendingMsg')
|
||||
|
||||
const config = await globalroutines(null, 'read', 'config', null, '1')
|
||||
// console.log('config', config)
|
||||
|
||||
try {
|
||||
if (config) {
|
||||
if (!!config[1].stateconn) {
|
||||
// console.log('config.stateconn', config[1].stateconn)
|
||||
|
||||
if (config[1].stateconn !== GlobalStore.state.stateConnection) {
|
||||
GlobalStore.mutations.setStateConnection(config[1].stateconn)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
|
||||
aspettansec(numsec) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Check if there is something
|
||||
return globalroutines(null, 'count', 'swmsg')
|
||||
.then((count) => {
|
||||
if (count > 0) {
|
||||
// console.log('count = ', count)
|
||||
return resolve(true)
|
||||
} else {
|
||||
return resolve(false)
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
return reject()
|
||||
})
|
||||
setTimeout(() => {
|
||||
resolve('anything')
|
||||
}, numsec)
|
||||
})
|
||||
|
||||
},
|
||||
|
||||
// If something in the call of Service Worker went wrong (Network or Server Down), then retry !
|
||||
async sendSwMsgIfAvailable() {
|
||||
let something = false
|
||||
|
||||
if ('serviceWorker' in navigator) {
|
||||
console.log(' -------- sendSwMsgIfAvailable')
|
||||
|
||||
const count = await tools.checkPendingMsg()
|
||||
if (count > 0) {
|
||||
return await navigator.serviceWorker.ready
|
||||
.then((sw) => {
|
||||
|
||||
return globalroutines(null, 'readall', 'swmsg')
|
||||
.then((arr_recmsg) => {
|
||||
if (arr_recmsg.length > 0) {
|
||||
|
||||
// console.log('---------------------- 2) navigator (2) .serviceWorker.ready')
|
||||
let promiseChain = Promise.resolve()
|
||||
|
||||
for (const rec of arr_recmsg) {
|
||||
// console.log(' .... sw.sync.register ( ', rec._id)
|
||||
// if ('SyncManager' in window) {
|
||||
// sw.sync.register(rec._id)
|
||||
// } else {
|
||||
|
||||
// #Alternative to SyncManager
|
||||
promiseChain = promiseChain.then(() => {
|
||||
return Api.syncAlternative(rec._id)
|
||||
.then(() => {
|
||||
something = true
|
||||
})
|
||||
})
|
||||
|
||||
// }
|
||||
}
|
||||
return promiseChain
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve(something)
|
||||
})
|
||||
},
|
||||
|
||||
async waitAndRefreshData() {
|
||||
return await Todos.actions.dbLoadTodo({ checkPending: false })
|
||||
},
|
||||
|
||||
async waitAndcheckPendingMsg() {
|
||||
|
||||
// await aspettansec(1000)
|
||||
|
||||
return await tools.checkPendingMsg()
|
||||
.then((ris) => {
|
||||
if (ris) {
|
||||
// console.log('risPending = ', ris)
|
||||
return tools.sendSwMsgIfAvailable()
|
||||
.then((something) => {
|
||||
if (something) {
|
||||
if (process.env.DEBUG === '1') {
|
||||
console.log('something')
|
||||
}
|
||||
// Refresh data
|
||||
return tools.waitAndRefreshData()
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
async updatefromIndexedDbToStateTodo(nametab) {
|
||||
await globalroutines(null, 'updatefromIndexedDbToStateTodo', nametab, null)
|
||||
.then(() => {
|
||||
console.log('updatefromIndexedDbToStateTodo! ')
|
||||
return true
|
||||
})
|
||||
},
|
||||
|
||||
isLoggedToSystem() {
|
||||
|
||||
Reference in New Issue
Block a user