- Service Worker
- Indexdb
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
import VueIdb from 'vue-idb'
|
||||
import { ISigninOptions, ITodo, ITodosState } from 'model'
|
||||
import { ITodo, ITodosState } from 'model'
|
||||
import { storeBuilder } from './Store/Store'
|
||||
|
||||
import Api from '@api'
|
||||
import { serv_constants } from '../Modules/serv_constants'
|
||||
import { rescodes } from './rescodes'
|
||||
import { UserStore } from '@store'
|
||||
import { IResult } from 'model/other'
|
||||
|
||||
|
||||
const state: ITodosState = {
|
||||
@@ -32,86 +29,37 @@ namespace Getters {
|
||||
|
||||
namespace Mutations {
|
||||
|
||||
function deleteItem(state: ITodosState, num: number) {
|
||||
// state.conta = num
|
||||
// Cancella Item
|
||||
}
|
||||
|
||||
async function clearAllData(state: ITodosState) {
|
||||
// Delete item
|
||||
|
||||
VueIdb.$db.todos
|
||||
.where('userId').equals(UserStore.state.userId)
|
||||
.delete()
|
||||
.then(() => {
|
||||
console.log('Todo clearAllData !')
|
||||
|
||||
// state
|
||||
|
||||
}).catch((error) => {
|
||||
console.log('err: ', error)
|
||||
})
|
||||
}
|
||||
|
||||
async function readdbTodoData(state: ITodosState) {
|
||||
// Delete item
|
||||
|
||||
VueIdb.$db.todos
|
||||
.where('userId').equals(UserStore.state.userId)
|
||||
// .and(todo => todo.category === this.getCategory())
|
||||
.toArray()
|
||||
.then(ristodos => {
|
||||
console.log('readdbTodoData OK !')
|
||||
state.todos = ristodos
|
||||
}).catch((error) => {
|
||||
console.log('err: ', error)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
export const mutations = {
|
||||
deleteItem: b.commit(deleteItem),
|
||||
clearAllData: b.commit(clearAllData),
|
||||
readdbTodoData: b.commit(readdbTodoData)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace Actions {
|
||||
async function deleteItem(context, num: number) {
|
||||
Mutations.mutations.deleteItem(num)
|
||||
}
|
||||
|
||||
function json2array(json){
|
||||
var result = []
|
||||
var keys = Object.keys(json);
|
||||
keys.forEach(function(key){
|
||||
function json2array(json) {
|
||||
let result = []
|
||||
let keys = Object.keys(json)
|
||||
keys.forEach(function (key) {
|
||||
result.push(json[key])
|
||||
});
|
||||
return result;
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
async function dbLoadTodo(context) {
|
||||
|
||||
console.log('dbLoadTodo')
|
||||
|
||||
const token = localStorage.getItem(rescodes.localStorage.token)
|
||||
const token = UserStore.state.idToken
|
||||
|
||||
let call = process.env.MONGODB_HOST + '/todos/' + UserStore.state.userId
|
||||
|
||||
return await Api.SendReq(call, UserStore.state.lang, token, 'GET', null)
|
||||
state.networkDataReceived = false
|
||||
|
||||
let ris = await Api.SendReq(call, UserStore.state.lang, token, 'GET', null)
|
||||
.then((res) => {
|
||||
return res.json()
|
||||
}).then((resData) => {
|
||||
// console.log('res.todos:', res.todos)
|
||||
state.networkDataReceived = true
|
||||
|
||||
state.todos = []
|
||||
for(var i in resData.todos)
|
||||
state.todos.push(resData.todos [i])
|
||||
state.todos = resData.todos
|
||||
|
||||
// state.todos = Object.keys(resData.todos).map((key) => {
|
||||
// return resData.todos[key]
|
||||
// })
|
||||
// After Login will store into the indexedDb...
|
||||
|
||||
console.log('state.todos', state.todos)
|
||||
return rescodes.OK
|
||||
@@ -123,16 +71,19 @@ namespace Actions {
|
||||
}
|
||||
return rescodes.ERR_GENERICO
|
||||
})
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
async function dbSaveTodo(context, itemtodo: ITodo) {
|
||||
console.log('dbSaveTodo', itemtodo)
|
||||
let call = process.env.MONGODB_HOST + '/todos/' + itemtodo._id
|
||||
|
||||
const token = localStorage.getItem(rescodes.localStorage.token)
|
||||
const token = UserStore.state.idToken
|
||||
|
||||
let res = await Api.SendReq(call, UserStore.state.lang, token, 'PATCH', itemtodo)
|
||||
.then(function (res) {
|
||||
state.networkDataReceived = true
|
||||
return rescodes.OK
|
||||
})
|
||||
.catch((error) => {
|
||||
@@ -144,12 +95,25 @@ namespace Actions {
|
||||
})
|
||||
|
||||
|
||||
if ('indexedDB' in window) {
|
||||
await Mutations.mutations.readdbTodoData()
|
||||
if (!state.networkDataReceived) {
|
||||
console.log('From cache', state.todos)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
async function dbDeleteTodo(context, id: String) {
|
||||
console.log('dbDeleteTodo', id)
|
||||
let call = process.env.MONGODB_HOST + '/todos/' + id
|
||||
|
||||
const token = UserStore.state.idToken
|
||||
|
||||
let res = await Api.SendReq(call, UserStore.state.lang, token, 'DELETE', id)
|
||||
.then(function (res) {
|
||||
return rescodes.OK
|
||||
})
|
||||
.catch((error) => {
|
||||
if (process.env.DEV) {
|
||||
console.log('ERROREEEEEEEEE', error)
|
||||
}
|
||||
return rescodes.ERR_GENERICO
|
||||
})
|
||||
|
||||
return res
|
||||
}
|
||||
@@ -163,9 +127,9 @@ namespace Actions {
|
||||
}
|
||||
|
||||
export const actions = {
|
||||
setConta: b.dispatch(deleteItem),
|
||||
dbSaveTodo: b.dispatch(dbSaveTodo),
|
||||
dbLoadTodo: b.dispatch(dbLoadTodo),
|
||||
dbDeleteTodo: b.dispatch(dbDeleteTodo),
|
||||
getTodosByCategory: b.dispatch(getTodosByCategory)
|
||||
}
|
||||
|
||||
@@ -178,7 +142,7 @@ const TodosModule = {
|
||||
return stateGetter()
|
||||
},
|
||||
getters: Getters.getters,
|
||||
mutations: Mutations.mutations,
|
||||
// mutations: Mutations.mutations,
|
||||
actions: Actions.actions
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user