- Service Worker: Save on IndexDb and Sync to send after to the DB.
- Insert record on MongoDb, POST, GET.
This commit is contained in:
@@ -1,12 +1,22 @@
|
||||
import { ITodosState } from 'model'
|
||||
import VueIdb from 'vue-idb'
|
||||
import { ISigninOptions, 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 = {
|
||||
visuOnlyUncompleted: false
|
||||
visuOnlyUncompleted: false,
|
||||
networkDataReceived: false,
|
||||
todos: []
|
||||
}
|
||||
|
||||
const b = storeBuilder.module<ITodosState>('TodosModule', state)
|
||||
const stateGetter = b.state()
|
||||
|
||||
// Getters
|
||||
namespace Getters {
|
||||
@@ -27,8 +37,42 @@ namespace Mutations {
|
||||
// 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)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -38,13 +82,95 @@ namespace Actions {
|
||||
Mutations.mutations.deleteItem(num)
|
||||
}
|
||||
|
||||
function json2array(json){
|
||||
var result = []
|
||||
var keys = Object.keys(json);
|
||||
keys.forEach(function(key){
|
||||
result.push(json[key])
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
async function dbLoadTodo(context) {
|
||||
|
||||
console.log('dbLoadTodo')
|
||||
|
||||
const token = localStorage.getItem(rescodes.localStorage.token)
|
||||
|
||||
let call = process.env.MONGODB_HOST + '/todos/' + UserStore.state.userId
|
||||
|
||||
return await Api.SendReq(call, UserStore.state.lang, token, 'GET', null)
|
||||
.then((res) => {
|
||||
return res.json()
|
||||
}).then((resData) => {
|
||||
// console.log('res.todos:', res.todos)
|
||||
|
||||
state.todos = []
|
||||
for(var i in resData.todos)
|
||||
state.todos.push(resData.todos [i])
|
||||
|
||||
// state.todos = Object.keys(resData.todos).map((key) => {
|
||||
// return resData.todos[key]
|
||||
// })
|
||||
|
||||
console.log('state.todos', state.todos)
|
||||
return rescodes.OK
|
||||
})
|
||||
.catch((error) => {
|
||||
if (process.env.DEV) {
|
||||
console.log('ERROREEEEEEEEE')
|
||||
console.log(error)
|
||||
}
|
||||
return rescodes.ERR_GENERICO
|
||||
})
|
||||
}
|
||||
|
||||
async function dbSaveTodo(context, itemtodo: ITodo) {
|
||||
let call = process.env.MONGODB_HOST + '/todos/' + itemtodo._id
|
||||
|
||||
const token = localStorage.getItem(rescodes.localStorage.token)
|
||||
|
||||
let res = await Api.SendReq(call, UserStore.state.lang, token, 'PATCH', itemtodo)
|
||||
.then(function (res) {
|
||||
state.networkDataReceived = true
|
||||
return rescodes.OK
|
||||
})
|
||||
.catch((error) => {
|
||||
if (process.env.DEV) {
|
||||
console.log('ERROREEEEEEEEE')
|
||||
console.log(error)
|
||||
}
|
||||
return rescodes.ERR_GENERICO
|
||||
})
|
||||
|
||||
|
||||
if ('indexedDB' in window) {
|
||||
await Mutations.mutations.readdbTodoData()
|
||||
if (!state.networkDataReceived) {
|
||||
console.log('From cache', state.todos)
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
async function getTodosByCategory(context, category: string) {
|
||||
let myarr = state.todos.filter((p) => {
|
||||
return p.category === category
|
||||
})
|
||||
|
||||
return myarr
|
||||
}
|
||||
|
||||
export const actions = {
|
||||
setConta: b.dispatch(deleteItem)
|
||||
setConta: b.dispatch(deleteItem),
|
||||
dbSaveTodo: b.dispatch(dbSaveTodo),
|
||||
dbLoadTodo: b.dispatch(dbLoadTodo),
|
||||
getTodosByCategory: b.dispatch(getTodosByCategory)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const stateGetter = b.state()
|
||||
|
||||
// Module
|
||||
const TodosModule = {
|
||||
@@ -56,6 +182,4 @@ const TodosModule = {
|
||||
actions: Actions.actions
|
||||
}
|
||||
|
||||
|
||||
export default ITodosState
|
||||
|
||||
export default TodosModule
|
||||
|
||||
@@ -6,7 +6,7 @@ import router from '@router'
|
||||
|
||||
import { serv_constants } from '../Modules/serv_constants'
|
||||
import { rescodes } from '../Modules/rescodes'
|
||||
import { GlobalStore, UserStore } from '@store'
|
||||
import { GlobalStore, UserStore, Todos } from '@store'
|
||||
|
||||
const bcrypt = require('bcryptjs')
|
||||
|
||||
@@ -156,7 +156,7 @@ namespace Actions {
|
||||
|
||||
let x_auth_token: string = ''
|
||||
|
||||
return Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
||||
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
||||
.then((res) => {
|
||||
console.log(res)
|
||||
myres = res
|
||||
@@ -197,7 +197,7 @@ namespace Actions {
|
||||
|
||||
let myres
|
||||
|
||||
return Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
||||
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
||||
.then((res) => {
|
||||
console.log(res)
|
||||
myres = res
|
||||
@@ -381,7 +381,7 @@ namespace Actions {
|
||||
|
||||
let x_auth_token: string = ''
|
||||
|
||||
return Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
||||
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'POST', usertosend)
|
||||
.then((res) => {
|
||||
myres = res
|
||||
x_auth_token = String(res.headers.get('x-auth'))
|
||||
@@ -433,6 +433,8 @@ namespace Actions {
|
||||
localStorage.setItem(rescodes.localStorage.isLogged, String(true))
|
||||
localStorage.setItem(rescodes.localStorage.verifiedEmail, Number(verifiedEmail).toString())
|
||||
|
||||
setGlobal()
|
||||
|
||||
// dispatch('storeUser', authData);
|
||||
// dispatch('setLogoutTimer', myres.data.expiresIn);
|
||||
return rescodes.OK
|
||||
@@ -469,7 +471,7 @@ namespace Actions {
|
||||
}
|
||||
|
||||
console.log(usertosend)
|
||||
Api.SendReq(call, state.lang, Getters.getters.tok, 'DELETE', usertosend)
|
||||
return await Api.SendReq(call, state.lang, Getters.getters.tok, 'DELETE', usertosend)
|
||||
.then(
|
||||
(res) => {
|
||||
console.log(res)
|
||||
@@ -495,10 +497,13 @@ namespace Actions {
|
||||
function setGlobal() {
|
||||
GlobalStore.mutations.setleftDrawerOpen(localStorage.getItem(rescodes.localStorage.leftDrawerOpen) === 'true')
|
||||
GlobalStore.mutations.setCategorySel(localStorage.getItem(rescodes.localStorage.categorySel))
|
||||
|
||||
Todos.actions.dbLoadTodo()
|
||||
}
|
||||
|
||||
async function autologin (context) {
|
||||
try {
|
||||
console.log('*** Autologin ***')
|
||||
// INIT
|
||||
UserStore.mutations.setlang(process.env.LANG_DEFAULT)
|
||||
// ++Todo: Estrai la Lang dal Localstorage
|
||||
@@ -521,7 +526,7 @@ namespace Actions {
|
||||
const username = String(localStorage.getItem(rescodes.localStorage.username))
|
||||
const verifiedEmail = localStorage.getItem(rescodes.localStorage.verifiedEmail) === '1'
|
||||
|
||||
setGlobal()
|
||||
console.log('autologin userId', userId)
|
||||
|
||||
Mutations.mutations.authUser({
|
||||
userId: userId,
|
||||
@@ -529,6 +534,11 @@ namespace Actions {
|
||||
idToken: token,
|
||||
verifiedEmail: verifiedEmail
|
||||
})
|
||||
|
||||
setGlobal()
|
||||
|
||||
console.log('autologin userId STATE ', state.userId)
|
||||
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('ERR autologin ', e.message)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export {storeBuilder} from './Store/Store'
|
||||
export {default as GlobalStore} from './GlobalStore'
|
||||
export {default as UserStore} from './UserStore'
|
||||
export {default as Todos} from './Todos'
|
||||
|
||||
Reference in New Issue
Block a user