From 5db5fb7dd03cdc30f16f555e289e4db1a6bf3875 Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Sun, 3 Feb 2019 00:51:58 +0100 Subject: [PATCH] - fix Indexdb: 1) ReadAllData 'GET' : OK 2) InsertTodo 'POST' : OK 3) DeleteItem 'DELETE': OK 4) Modify 'PATCH' : OK --- src/components/todos/todo/todo.ts | 72 +++++++++++++++++-------------- src/js/storage.js | 24 ++++------- src/statics/js/storage.js | 25 +++++------ src/store/Api/Inst-Pao.ts | 8 +++- src/store/Modules/Todos.ts | 13 +++++- 5 files changed, 77 insertions(+), 65 deletions(-) diff --git a/src/components/todos/todo/todo.ts b/src/components/todos/todo/todo.ts index 0639d65..a52f5b6 100644 --- a/src/components/todos/todo/todo.ts +++ b/src/components/todos/todo/todo.ts @@ -333,29 +333,28 @@ export default class Todo extends Vue { await globalroutines(this, 'write', 'todos', objtodo) .then((id) => { - console.log('*** IDNEW (2) = ', id) + console.log('*** IDNEW (3) = ', id) // update also the last elem if (lastelem !== null) { lastelem.id_next = id lastelem.modified = true console.log('calling MODIFY 4', lastelem) - this.modify(lastelem, false) - .then(ris => { - console.log('END calling MODIFY 4') - - this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS, 'POST', objtodo) - this.updatetable(false) - - }) - } else { - this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS, 'POST', objtodo) - this.updatetable(false) } + this.modify(lastelem, false) + .then(ris => { + console.log('END calling MODIFY 4') + + this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS, 'POST', objtodo) + this.updatetable(false) + + }) + + }).catch(err => { - console.log('Errore: ' + err.message) - }) + console.log('Errore: ' + err.message) + }) console.log('ESCO.........') @@ -369,7 +368,7 @@ export default class Todo extends Vue { console.log('cmdToSyncAndDb', cmd, table, method, item, id, msg) const mythis = this - if ('serviceWorker' in navigator && 'SyncManager' in window) { + if (false && ('serviceWorker' in navigator && 'SyncManager' in window)) { await navigator.serviceWorker.ready .then(function (sw) { // _id: new Date().toISOString(), @@ -395,15 +394,18 @@ export default class Todo extends Vue { }) }) } else { - if (cmd === rescodes.DB.CMD_SYNC_TODOS) - Todos.actions.dbSaveTodo(item) - else if (cmd === rescodes.DB.CMD_DELETE_TODOS) + if (cmd === rescodes.DB.CMD_SYNC_TODOS) { + if (method === 'POST') + Todos.actions.dbInsertTodo(item) + else if (method === 'PATCH') + Todos.actions.dbSaveTodo(item) + } else if (cmd === rescodes.DB.CMD_DELETE_TODOS) Todos.actions.dbDeleteTodo(id) } } saveItemToSyncAndDb(table: String, method, item: ITodo) { - return this.cmdToSyncAndDb(rescodes.DB.CMD_SYNC_TODOS, table, method, item, 0, 'Your Post was saved for syncing!') + return this.cmdToSyncAndDb(rescodes.DB.CMD_SYNC_TODOS, table, method, item, 0, 'Your Post was saved for syncing!') } @@ -411,19 +413,19 @@ export default class Todo extends Vue { return this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', null, id, 'Your Post was canceled for syncing!') } -/* - sendMessageToSW(recdata, method) { + /* + sendMessageToSW(recdata, method) { - navigator.serviceWorker.controller.postMessage({ - type: 'sync', - recdata, - method, - cmd: 'sync-new-todos', - token: UserStore.state.idToken, - lang: UserStore.state.lang - }) - } -*/ + navigator.serviceWorker.controller.postMessage({ + type: 'sync', + recdata, + method, + cmd: 'sync-new-todos', + token: UserStore.state.idToken, + lang: UserStore.state.lang + }) + } + */ getElemById(id, lista = this.todos_arr) { @@ -469,8 +471,8 @@ export default class Todo extends Vue { console.log('UpdateTable', ris) mythis.updatetable() }).catch((error) => { - console.log('err: ', error) - }) + console.log('err: ', error) + }) } console.log('FINE deleteitem') @@ -633,6 +635,10 @@ export default class Todo extends Vue { async modify(myobj: ITodo, update: boolean) { + if (myobj === null) + return new Promise(function (resolve, reject) { + resolve() + }) await globalroutines(this, 'read', 'todos', null, myobj._id) .then(miorec => { console.log('ArrTodos: ', myobj.descr, '[', myobj._id, ']') diff --git a/src/js/storage.js b/src/js/storage.js index 10509ef..bb68813 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -64,26 +64,20 @@ export let idbKeyval = (() => { return req.result; }, async set(key, value) { - return await withStore('readwrite', 'keyval', store => { - store.put(value, key); + let req; + await withStore('readwrite', 'keyval', store => { + req = store.put(value, key); }); + return req.result; }, - async setdata(table, valuekey) { - - // set only the ID, because it need to delete it - let value = [] - if (table === 'delete_todos') { - value['_id'] = valuekey - value['value'] = valuekey - }else { - value = valuekey - } - + async setdata(table, value) { + let req; console.log('setdata', table, value) - return await withStore('readwrite', table, store => { - store.put(value); + await withStore('readwrite', table, store => { + req = store.put(value); }); + return req.result; }, async delete(key) { return await withStore('readwrite', 'keyval', store => { diff --git a/src/statics/js/storage.js b/src/statics/js/storage.js index 53adccd..4bff620 100644 --- a/src/statics/js/storage.js +++ b/src/statics/js/storage.js @@ -1,4 +1,4 @@ -let idbKeyval = (() => { +let let idbKeyval = (() => { let db; function getDB() { @@ -47,6 +47,7 @@ let idbKeyval = (() => { }, async getdata(table, key) { let req; + await withStore('readonly', table, store => { console.log('store', store, 'key', key) req = store.get(key); @@ -62,24 +63,20 @@ let idbKeyval = (() => { return req.result; }, async set(key, value) { - return await withStore('readwrite', 'keyval', store => { - store.put(value, key); + let req; + await withStore('readwrite', 'keyval', store => { + req = store.put(value, key); }); + return req.result; }, - async setdata(table, valuekey) { - - let value = [] - if (table === 'delete_todos') { - value['_id'] = valuekey - }else { - value = valuekey - } - + async setdata(table, value) { + let req; console.log('setdata', table, value) - return await withStore('readwrite', table, store => { - store.put(value); + await withStore('readwrite', table, store => { + req = store.put(value); }); + return req.result; }, async delete(key) { return await withStore('readwrite', 'keyval', store => { diff --git a/src/store/Api/Inst-Pao.ts b/src/store/Api/Inst-Pao.ts index be79110..83d049d 100644 --- a/src/store/Api/Inst-Pao.ts +++ b/src/store/Api/Inst-Pao.ts @@ -1,6 +1,6 @@ import axios, { AxiosInstance, AxiosPromise, AxiosResponse, AxiosInterceptorManager } from 'axios' -async function sendRequest (url: string, lang: string, mytok: string, method: string, mydata: any) { +async function sendRequest(url: string, lang: string, mytok: string, method: string, mydata: any) { console.log('sendRequest', method, url, '[', lang, ']') @@ -18,6 +18,12 @@ async function sendRequest (url: string, lang: string, mytok: string, method: st cache: 'no-cache', headers: authHeader } + } else if (method === 'DELETE') { + configInit = { + method: method, + cache: 'no-cache', + headers: authHeader + } } else { configInit = { method: method, diff --git a/src/store/Modules/Todos.ts b/src/store/Modules/Todos.ts index 726be5b..2720a5d 100644 --- a/src/store/Modules/Todos.ts +++ b/src/store/Modules/Todos.ts @@ -77,12 +77,20 @@ namespace Actions { } async function dbSaveTodo(context, itemtodo: ITodo) { - console.log('dbSaveTodo', itemtodo) + return await dbInsertSaveTodo(context, itemtodo, 'PATCH') + } + + async function dbInsertTodo(context, itemtodo: ITodo) { + return await dbInsertSaveTodo(context, itemtodo, 'POST') + } + + async function dbInsertSaveTodo(context, itemtodo: ITodo, method) { + console.log('dbInsertSaveTodo', itemtodo, method) let call = process.env.MONGODB_HOST + '/todos/' + itemtodo._id const token = UserStore.state.idToken - let res = await Api.SendReq(call, UserStore.state.lang, token, 'PATCH', itemtodo) + let res = await Api.SendReq(call, UserStore.state.lang, token, method, itemtodo) .then(function (res) { return rescodes.OK }) @@ -127,6 +135,7 @@ namespace Actions { } export const actions = { + dbInsertTodo: b.dispatch(dbInsertTodo), dbSaveTodo: b.dispatch(dbSaveTodo), dbLoadTodo: b.dispatch(dbLoadTodo), dbDeleteTodo: b.dispatch(dbDeleteTodo),