diff --git a/src/components/todos/todo/todo.ts b/src/components/todos/todo/todo.ts index 7ddae5a..0639d65 100644 --- a/src/components/todos/todo/todo.ts +++ b/src/components/todos/todo/todo.ts @@ -363,21 +363,21 @@ export default class Todo extends Vue { this.todo = '' } - cmdToSyncAndDb(cmd, table, method, itemOrId, msg: String) { + async cmdToSyncAndDb(cmd, table, method, item: ITodo, id, msg: String) { // Send to Server to Sync - console.log('cmdToSyncAndDb', cmd, table, method, itemOrId, msg) + console.log('cmdToSyncAndDb', cmd, table, method, item, id, msg) const mythis = this if ('serviceWorker' in navigator && 'SyncManager' in window) { - navigator.serviceWorker.ready + await navigator.serviceWorker.ready .then(function (sw) { // _id: new Date().toISOString(), console.log('---------------------- navigator.serviceWorker.ready') // mythis.sendMessageToSW(item, method) - globalroutines(mythis, 'write', table, itemOrId) + globalroutines(mythis, 'write', table, item, id) .then(function (id) { console.log('id', id) const sep = '|' @@ -396,19 +396,19 @@ export default class Todo extends Vue { }) } else { if (cmd === rescodes.DB.CMD_SYNC_TODOS) - Todos.actions.dbSaveTodo(itemOrId) + Todos.actions.dbSaveTodo(item) else if (cmd === rescodes.DB.CMD_DELETE_TODOS) - Todos.actions.dbDeleteTodo(itemOrId) + Todos.actions.dbDeleteTodo(id) } } saveItemToSyncAndDb(table: String, method, item: ITodo) { - return this.cmdToSyncAndDb(rescodes.DB.CMD_SYNC_TODOS, table, method, item, 'Your Post was saved for syncing!') + return this.cmdToSyncAndDb(rescodes.DB.CMD_SYNC_TODOS, table, method, item, 0, 'Your Post was saved for syncing!') } - deleteItemToSyncAndDb(table: String, id: String) { - return this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', id, 'Your Post was canceled for syncing!') + deleteItemToSyncAndDb(table: String, id) { + return this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', null, id, 'Your Post was canceled for syncing!') } /* @@ -464,7 +464,7 @@ export default class Todo extends Vue { const mythis = this // Delete item - await globalroutines(this, 'delete', 'todos', id) + await globalroutines(this, 'delete', 'todos', null, id) .then((ris) => { console.log('UpdateTable', ris) mythis.updatetable() @@ -633,7 +633,7 @@ export default class Todo extends Vue { async modify(myobj: ITodo, update: boolean) { - await globalroutines(this, 'read', 'todos', myobj._id) + await globalroutines(this, 'read', 'todos', null, myobj._id) .then(miorec => { console.log('ArrTodos: ', myobj.descr, '[', myobj._id, ']') diff --git a/src/globalroutines/index.js b/src/globalroutines/index.js index 41568e9..7875b3b 100644 --- a/src/globalroutines/index.js +++ b/src/globalroutines/index.js @@ -1,6 +1,6 @@ import indexdb from './indexdb' -export default async (context, cmd, table, data = null) => { - console.log('globalroutines', cmd, table, data) - return await indexdb(context, cmd, table, data) +export default async (context, cmd, table, data, id = '') => { + console.log('globalroutines', cmd, table, data, id) + return await indexdb(context, cmd, table, data, id) } diff --git a/src/globalroutines/indexdb.js b/src/globalroutines/indexdb.js index cbda898..abb9766 100644 --- a/src/globalroutines/indexdb.js +++ b/src/globalroutines/indexdb.js @@ -39,7 +39,7 @@ async function readfromIndexDbToStateTodos(context) { } -export default async (context, cmd, table, datakey) => { +export default async (context, cmd, table, datakey, id) => { if (cmd === 'loadapp') { // ****** LOAD APP AL CARICAMENTO ! ******* return saveConfigIndexDb(context, datakey) @@ -56,8 +56,8 @@ export default async (context, cmd, table, datakey) => { } else if (cmd === 'readall') { return await storage.getalldata(table) } else if (cmd === 'read') { - return await storage.getdata(table, datakey) + return await storage.getdata(table, id) } else if (cmd === 'delete') { - return await storage.deletedata(table, datakey) + return await storage.deletedata(table, id) } } diff --git a/src/js/storage.js b/src/js/storage.js index ec42734..10509ef 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -1,4 +1,3 @@ -import { rescodes } from "../store/Modules/rescodes"; export let idbKeyval = (() => { let db; @@ -49,6 +48,7 @@ export let idbKeyval = (() => { }, async getdata(table, key) { let req; + await withStore('readonly', table, store => { console.log('store', store, 'key', key) req = store.get(key); @@ -70,9 +70,11 @@ export let idbKeyval = (() => { }, async setdata(table, valuekey) { + // set only the ID, because it need to delete it let value = [] - if (table === rescodes.DB.CMD_DELETE_TODOS) { + if (table === 'delete_todos') { value['_id'] = valuekey + value['value'] = valuekey }else { value = valuekey } diff --git a/src/statics/js/storage.js b/src/statics/js/storage.js index b99f862..53adccd 100644 --- a/src/statics/js/storage.js +++ b/src/statics/js/storage.js @@ -3,7 +3,7 @@ let idbKeyval = (() => { function getDB() { if (!db) { - console.log('CREO DB STATICS!') + console.log('CREO DB STORAGE JS !') db = new Promise((resolve, reject) => { const openreq = indexedDB.open('mydb3', 11); @@ -40,11 +40,20 @@ let idbKeyval = (() => { return { async get(key) { let req; - await withStore('readonly', store => { + await withStore('readonly', 'keyval', store => { req = store.get(key); }); return req.result; }, + async getdata(table, key) { + let req; + await withStore('readonly', table, store => { + console.log('store', store, 'key', key) + req = store.get(key); + }); + console.log('RISFINALE!', req.result) + return req.result; + }, async getalldata(table) { let req; await withStore('readonly', table, store => { @@ -52,18 +61,28 @@ let idbKeyval = (() => { }); return req.result; }, - set(key, value) { - return withStore('readwrite', 'keyval', store => { + async set(key, value) { + return await withStore('readwrite', 'keyval', store => { store.put(value, key); }); }, - setdata(table, value) { - return withStore('readwrite', table, store => { + async setdata(table, valuekey) { + + let value = [] + if (table === 'delete_todos') { + value['_id'] = valuekey + }else { + value = valuekey + } + + console.log('setdata', table, value) + + return await withStore('readwrite', table, store => { store.put(value); }); }, - delete(key) { - return withStore('readwrite', 'keyval', store => { + async delete(key) { + return await withStore('readwrite', 'keyval', store => { store.delete(key); }); }, diff --git a/src/store/Modules/rescodes.ts b/src/store/Modules/rescodes.ts index 76fcf80..36c4fa8 100644 --- a/src/store/Modules/rescodes.ts +++ b/src/store/Modules/rescodes.ts @@ -28,8 +28,8 @@ export const rescodes = { DB: { CMD_SYNC_TODOS: 'sync-new-todos', CMD_DELETE_TODOS: 'sync-delete-todos', - TABLE_SYNC_TODOS : 'sync_todos,', - TABLE_DELETE_TODOS : 'delete_todos,' + TABLE_SYNC_TODOS : 'sync_todos', + TABLE_DELETE_TODOS : 'delete_todos' }, MenuAction: { diff --git a/src/views/login/signin/signin.ts b/src/views/login/signin/signin.ts index 41d91c9..a356de5 100644 --- a/src/views/login/signin/signin.ts +++ b/src/views/login/signin/signin.ts @@ -119,7 +119,7 @@ export default class Signin extends Vue { .then((riscode) => { if (riscode === rescodes.OK) { router.push('/signin') - globalroutines(this, 'loadapp', null) + globalroutines(this, 'loadapp', '') } this.checkErrors(riscode) this.$q.loading.hide()