- 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
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import { GlobalStore, UserStore, Todos } from '@store'
|
||||
|
||||
const bcrypt = require('bcryptjs')
|
||||
|
||||
|
||||
// State
|
||||
const state: IUserState = {
|
||||
userId: '',
|
||||
@@ -136,7 +135,6 @@ namespace Actions {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function resetpwd (context, paramquery: IUserState) {
|
||||
let call = process.env.MONGODB_HOST + '/updatepwd'
|
||||
console.log('CALL ' + call)
|
||||
|
||||
@@ -5,8 +5,8 @@ export const rescodes = {
|
||||
DUPLICATE_EMAIL_ID: 11000,
|
||||
DUPLICATE_USERNAME_ID: 11100,
|
||||
|
||||
LIST_END: 10000000,
|
||||
LIST_START: 0,
|
||||
LIST_END: '10000000',
|
||||
LIST_START: '0',
|
||||
|
||||
localStorage: {
|
||||
verifiedEmail: 'vf',
|
||||
@@ -25,6 +25,13 @@ export const rescodes = {
|
||||
PRIORITY_LOW: 0
|
||||
},
|
||||
|
||||
DB: {
|
||||
CMD_SYNC_TODOS: 'sync-new-todos',
|
||||
CMD_DELETE_TODOS: 'sync-delete-todos',
|
||||
TABLE_SYNC_TODOS : 'sync_todos,',
|
||||
TABLE_DELETE_TODOS : 'delete_todos,'
|
||||
},
|
||||
|
||||
MenuAction: {
|
||||
DELETE: 100,
|
||||
TOGGLE_EXPIRING: 101,
|
||||
@@ -110,7 +117,7 @@ export const rescodes = {
|
||||
},
|
||||
{
|
||||
id: 50,
|
||||
label: 'Cancella',
|
||||
label: 'Elimina',
|
||||
value: 100, // DELETE
|
||||
icon: 'delete',
|
||||
checked: false
|
||||
|
||||
Reference in New Issue
Block a user