2019-02-03 14:40:20 +01:00
|
|
|
import { IGlobalState, ITodo, ITodosState } from 'model'
|
2019-01-14 22:40:30 +01:00
|
|
|
import { storeBuilder } from './Store/Store'
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
import Api from '@api'
|
|
|
|
|
import { rescodes } from './rescodes'
|
2019-02-03 14:40:20 +01:00
|
|
|
import { Todos, UserStore } from '@store'
|
|
|
|
|
import globalroutines from './../../globalroutines/index'
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-01-14 22:40:30 +01:00
|
|
|
|
|
|
|
|
const state: ITodosState = {
|
2019-02-01 04:10:31 +01:00
|
|
|
visuOnlyUncompleted: false,
|
|
|
|
|
networkDataReceived: false,
|
2019-02-03 14:40:20 +01:00
|
|
|
todos: [],
|
|
|
|
|
todos_changed: 1,
|
2019-02-04 03:09:15 +01:00
|
|
|
reload_fromServer: false,
|
|
|
|
|
testpao: 'Test',
|
|
|
|
|
insidePending: false
|
2019-01-14 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const b = storeBuilder.module<ITodosState>('TodosModule', state)
|
2019-02-01 04:10:31 +01:00
|
|
|
const stateGetter = b.state()
|
2019-01-14 22:40:30 +01:00
|
|
|
|
|
|
|
|
// Getters
|
|
|
|
|
namespace Getters {
|
|
|
|
|
|
|
|
|
|
const visuOnlyUncompleted = b.read(state => state.visuOnlyUncompleted, 'visuOnlyUncompleted')
|
|
|
|
|
|
|
|
|
|
export const getters = {
|
|
|
|
|
get visuOnlyUncompleted() {
|
|
|
|
|
return visuOnlyUncompleted
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Mutations {
|
|
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
function setTestpao(state: ITodosState, testpao: String) {
|
|
|
|
|
state.testpao = testpao
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setTodos_changed(state: ITodosState) {
|
|
|
|
|
state.todos_changed++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const mutations = {
|
|
|
|
|
setTestpao: b.commit(setTestpao),
|
2019-02-03 19:28:06 +01:00
|
|
|
setTodos_changed: b.commit(setTodos_changed)
|
2019-02-03 14:40:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function consolelogpao(strlog, strlog2 = '', strlog3 = '') {
|
|
|
|
|
globalroutines(null, 'log', strlog + strlog2 + strlog3, null)
|
2019-01-14 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
|
2019-01-14 22:40:30 +01:00
|
|
|
namespace Actions {
|
|
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
function json2array(json) {
|
|
|
|
|
let result = []
|
|
|
|
|
let keys = Object.keys(json)
|
|
|
|
|
keys.forEach(function (key) {
|
2019-02-01 04:10:31 +01:00
|
|
|
result.push(json[key])
|
2019-02-02 20:13:06 +01:00
|
|
|
})
|
|
|
|
|
return result
|
2019-02-01 04:10:31 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-04 03:09:15 +01:00
|
|
|
// If something in the call of Service Worker went wrong (Network or Server Down), then retry !
|
|
|
|
|
async function sendSwMsgIfAvailable() {
|
|
|
|
|
|
|
|
|
|
console.log(' -------- sendSwMsgIfAvailable')
|
|
|
|
|
|
|
|
|
|
let something = false
|
|
|
|
|
|
|
|
|
|
let count = await checkPendingMsg(null)
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
if (('serviceWorker' in navigator && 'SyncManager' in window)) {
|
|
|
|
|
return navigator.serviceWorker.ready
|
|
|
|
|
.then(function (sw) {
|
|
|
|
|
|
|
|
|
|
globalroutines(null, 'readall', 'swmsg')
|
|
|
|
|
.then(function (arr_recmsg) {
|
|
|
|
|
let recclone = [...arr_recmsg]
|
|
|
|
|
if (arr_recmsg.length > 0) {
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
sw.sync.register(rec._id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return something
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return something
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function waitAndcheckPendingMsg(context) {
|
|
|
|
|
|
|
|
|
|
await aspettansec(1000)
|
|
|
|
|
|
2019-02-04 04:17:50 +01:00
|
|
|
// console.log('waitAndcheckPendingMsg')
|
2019-02-04 03:09:15 +01:00
|
|
|
|
|
|
|
|
return await checkPendingMsg(context)
|
|
|
|
|
.then(ris => {
|
|
|
|
|
if (ris) {
|
2019-02-04 04:17:50 +01:00
|
|
|
console.log('ris = ', ris)
|
2019-02-04 03:09:15 +01:00
|
|
|
const result = sendSwMsgIfAvailable()
|
|
|
|
|
.then(something => {
|
|
|
|
|
if (something) {
|
|
|
|
|
// Refresh data
|
|
|
|
|
waitAndRefreshData(context)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function waitAndRefreshData(context) {
|
|
|
|
|
await aspettansec(3000)
|
|
|
|
|
|
|
|
|
|
console.log('waitAndRefreshData')
|
|
|
|
|
|
|
|
|
|
return await dbLoadTodo(context, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function checkPendingMsg(context) {
|
2019-02-04 04:17:50 +01:00
|
|
|
// console.log('checkPendingMsg')
|
2019-02-04 03:09:15 +01:00
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
globalroutines(null, 'readall', 'swmsg')
|
|
|
|
|
.then(function (arr_recmsg) {
|
|
|
|
|
if (arr_recmsg.length > 0) {
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Check if there is something
|
|
|
|
|
globalroutines(null, 'count', 'swmsg')
|
|
|
|
|
.then(function (count) {
|
|
|
|
|
if (count > 0) {
|
2019-02-04 04:17:50 +01:00
|
|
|
console.log('count = ', count)
|
2019-02-04 03:09:15 +01:00
|
|
|
return resolve(true)
|
|
|
|
|
} else {
|
|
|
|
|
return resolve(false)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(e => {
|
|
|
|
|
return reject()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function dbLoadTodo(context, checkPending: boolean = false) {
|
|
|
|
|
console.log('dbLoadTodo', checkPending)
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
const token = UserStore.state.idToken
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
let call = process.env.MONGODB_HOST + '/todos/' + UserStore.state.userId
|
|
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
state.networkDataReceived = false
|
|
|
|
|
|
|
|
|
|
let ris = await Api.SendReq(call, UserStore.state.lang, token, 'GET', null)
|
2019-02-01 04:10:31 +01:00
|
|
|
.then((res) => {
|
|
|
|
|
return res.json()
|
|
|
|
|
}).then((resData) => {
|
2019-02-02 20:13:06 +01:00
|
|
|
state.networkDataReceived = true
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-04 03:09:15 +01:00
|
|
|
// console.log('******* UPDATE TODOS.STATE.TODOS !:', resData.todos)
|
2019-02-03 02:40:24 +01:00
|
|
|
state.todos = [...resData.todos]
|
2019-02-04 03:09:15 +01:00
|
|
|
Todos.state.todos_changed++
|
|
|
|
|
|
|
|
|
|
console.log('state.todos', state.todos, 'checkPending', checkPending)
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
// After Login will store into the indexedDb...
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
return rescodes.OK
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
if (process.env.DEV) {
|
2019-02-04 03:09:15 +01:00
|
|
|
// console.log('dbLoadTodo ERRORE', error)
|
2019-02-01 04:10:31 +01:00
|
|
|
}
|
2019-02-03 14:40:20 +01:00
|
|
|
// If error network connection, take the data from IndexedDb
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
return rescodes.ERR_GENERICO
|
|
|
|
|
})
|
2019-02-02 20:13:06 +01:00
|
|
|
|
2019-02-04 03:09:15 +01:00
|
|
|
console.log('fine della funz...')
|
|
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
if (!Todos.state.networkDataReceived) {
|
|
|
|
|
console.log('NETWORK UNREACHABLE ! (Error in fetch)')
|
|
|
|
|
consolelogpao('NETWORK UNREACHABLE ! (Error in fetch)')
|
|
|
|
|
// Read all data from IndexedDB Store into Memory
|
2019-02-03 19:28:06 +01:00
|
|
|
await updateArrayInMemory(context)
|
2019-02-04 03:09:15 +01:00
|
|
|
} else {
|
|
|
|
|
if (ris === rescodes.OK && checkPending) {
|
|
|
|
|
waitAndcheckPendingMsg(context)
|
|
|
|
|
}
|
2019-02-03 14:40:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-02 20:13:06 +01:00
|
|
|
|
2019-02-03 19:28:06 +01:00
|
|
|
async function updateArrayInMemory(context) {
|
|
|
|
|
console.log('Update the array in memory, from todos table from IndexedDb')
|
|
|
|
|
await globalroutines(null, 'updateinMemory', 'todos', null)
|
|
|
|
|
.then(() => {
|
2019-02-04 03:09:15 +01:00
|
|
|
// console.log('updateArrayInMemory! ')
|
2019-02-03 19:28:06 +01:00
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
function aspettansec(numsec) {
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
resolve('anything')
|
|
|
|
|
}, numsec)
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-02-02 20:13:06 +01:00
|
|
|
|
2019-02-03 14:40:20 +01:00
|
|
|
async function testfunc() {
|
|
|
|
|
while (true) {
|
|
|
|
|
consolelogpao('testfunc')
|
|
|
|
|
Todos.state.todos_changed++
|
2019-02-03 19:28:06 +01:00
|
|
|
// console.log('Todos.state.todos_changed:', Todos.state.todos_changed)
|
2019-02-03 14:40:20 +01:00
|
|
|
await aspettansec(5000)
|
|
|
|
|
}
|
2019-02-01 04:10:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function dbSaveTodo(context, itemtodo: ITodo) {
|
2019-02-03 00:51:58 +01:00
|
|
|
return await dbInsertSaveTodo(context, itemtodo, 'PATCH')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function dbInsertTodo(context, itemtodo: ITodo) {
|
|
|
|
|
return await dbInsertSaveTodo(context, itemtodo, 'POST')
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 02:40:24 +01:00
|
|
|
function UpdateNewIdFromDB(oldItem, newItem, method) {
|
|
|
|
|
console.log('PRIMA state.todos', state.todos)
|
|
|
|
|
console.log('ITEM', newItem)
|
|
|
|
|
if (method === 'POST') {
|
|
|
|
|
state.todos.push(newItem)
|
2019-02-03 14:40:20 +01:00
|
|
|
// } else if (method === 'PATCH') {
|
|
|
|
|
// state.todos.map(item => {
|
|
|
|
|
// if (item._id === newItem._id) {
|
|
|
|
|
// return newItem
|
|
|
|
|
// }
|
|
|
|
|
// })
|
2019-02-03 02:40:24 +01:00
|
|
|
}
|
2019-02-03 03:44:25 +01:00
|
|
|
|
|
|
|
|
|
2019-02-03 02:40:24 +01:00
|
|
|
console.log('DOPO state.todos', state.todos)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 00:51:58 +01:00
|
|
|
async function dbInsertSaveTodo(context, itemtodo: ITodo, method) {
|
|
|
|
|
console.log('dbInsertSaveTodo', itemtodo, method)
|
2019-02-01 04:10:31 +01:00
|
|
|
let call = process.env.MONGODB_HOST + '/todos/' + itemtodo._id
|
|
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
const token = UserStore.state.idToken
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2019-02-03 00:51:58 +01:00
|
|
|
let res = await Api.SendReq(call, UserStore.state.lang, token, method, itemtodo)
|
2019-02-03 14:40:20 +01:00
|
|
|
.then(function (response) {
|
2019-02-03 02:40:24 +01:00
|
|
|
if (response)
|
|
|
|
|
return response.json()
|
|
|
|
|
else
|
|
|
|
|
return null
|
|
|
|
|
}).then(newItem => {
|
|
|
|
|
console.log('RESDATA =', newItem)
|
|
|
|
|
if (newItem) {
|
|
|
|
|
const newId = newItem._id
|
|
|
|
|
|
2019-02-03 03:44:25 +01:00
|
|
|
// if (method === 'PATCH') {
|
|
|
|
|
// newItem = newItem.todo
|
|
|
|
|
// }
|
2019-02-03 02:40:24 +01:00
|
|
|
|
|
|
|
|
// Update ID on local
|
|
|
|
|
UpdateNewIdFromDB(itemtodo, newItem, method)
|
|
|
|
|
}
|
2019-02-01 04:10:31 +01:00
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
if (process.env.DEV) {
|
2019-02-03 14:40:20 +01:00
|
|
|
console.log('ERRORE FETCH', 'dbInsertSaveTodo', method)
|
2019-02-01 04:10:31 +01:00
|
|
|
console.log(error)
|
|
|
|
|
}
|
|
|
|
|
return rescodes.ERR_GENERICO
|
|
|
|
|
})
|
|
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-03 03:44:25 +01:00
|
|
|
async function dbDeleteTodo(context, item: ITodo) {
|
|
|
|
|
console.log('dbDeleteTodo', item)
|
|
|
|
|
let call = process.env.MONGODB_HOST + '/todos/' + item._id
|
2019-02-02 20:13:06 +01:00
|
|
|
|
|
|
|
|
const token = UserStore.state.idToken
|
|
|
|
|
|
2019-02-03 03:44:25 +01:00
|
|
|
let res = await Api.SendReq(call, UserStore.state.lang, token, 'DELETE', item)
|
2019-02-02 20:13:06 +01:00
|
|
|
.then(function (res) {
|
2019-02-03 03:44:25 +01:00
|
|
|
|
|
|
|
|
// Delete Item in to Array
|
|
|
|
|
state.todos.splice(state.todos.indexOf(item), 1)
|
|
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
return rescodes.OK
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
if (process.env.DEV) {
|
2019-02-03 14:40:20 +01:00
|
|
|
console.log('ERRORE FETCH', 'dbDeleteTodo')
|
2019-02-02 20:13:06 +01:00
|
|
|
}
|
|
|
|
|
return rescodes.ERR_GENERICO
|
|
|
|
|
})
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getTodosByCategory(context, category: string) {
|
|
|
|
|
let myarr = state.todos.filter((p) => {
|
|
|
|
|
return p.category === category
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return myarr
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 22:40:30 +01:00
|
|
|
export const actions = {
|
2019-02-03 00:51:58 +01:00
|
|
|
dbInsertTodo: b.dispatch(dbInsertTodo),
|
2019-02-01 04:10:31 +01:00
|
|
|
dbSaveTodo: b.dispatch(dbSaveTodo),
|
|
|
|
|
dbLoadTodo: b.dispatch(dbLoadTodo),
|
2019-02-02 20:13:06 +01:00
|
|
|
dbDeleteTodo: b.dispatch(dbDeleteTodo),
|
2019-02-03 19:28:06 +01:00
|
|
|
updateArrayInMemory: b.dispatch(updateArrayInMemory),
|
2019-02-04 03:09:15 +01:00
|
|
|
getTodosByCategory: b.dispatch(getTodosByCategory),
|
|
|
|
|
checkPendingMsg: b.dispatch(checkPendingMsg),
|
|
|
|
|
waitAndcheckPendingMsg: b.dispatch(waitAndcheckPendingMsg)
|
2019-01-14 22:40:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Module
|
|
|
|
|
const TodosModule = {
|
|
|
|
|
get state() {
|
|
|
|
|
return stateGetter()
|
|
|
|
|
},
|
|
|
|
|
getters: Getters.getters,
|
2019-02-03 14:40:20 +01:00
|
|
|
mutations: Mutations.mutations,
|
2019-01-14 22:40:30 +01:00
|
|
|
actions: Actions.actions
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
export default TodosModule
|