- fix Indexdb:

1) ReadAllData 'GET'  : OK
 2) InsertTodo 'POST'  : OK
 3) DeleteItem 'DELETE': OK
 4) Modify     'PATCH' : OK
This commit is contained in:
Paolo Arena
2019-02-03 00:51:58 +01:00
parent 185bd2657d
commit 5db5fb7dd0
5 changed files with 77 additions and 65 deletions

View File

@@ -333,13 +333,15 @@ export default class Todo extends Vue {
await globalroutines(this, 'write', 'todos', objtodo) await globalroutines(this, 'write', 'todos', objtodo)
.then((id) => { .then((id) => {
console.log('*** IDNEW (2) = ', id) console.log('*** IDNEW (3) = ', id)
// update also the last elem // update also the last elem
if (lastelem !== null) { if (lastelem !== null) {
lastelem.id_next = id lastelem.id_next = id
lastelem.modified = true lastelem.modified = true
console.log('calling MODIFY 4', lastelem) console.log('calling MODIFY 4', lastelem)
}
this.modify(lastelem, false) this.modify(lastelem, false)
.then(ris => { .then(ris => {
console.log('END calling MODIFY 4') console.log('END calling MODIFY 4')
@@ -348,10 +350,7 @@ export default class Todo extends Vue {
this.updatetable(false) this.updatetable(false)
}) })
} else {
this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS, 'POST', objtodo)
this.updatetable(false)
}
}).catch(err => { }).catch(err => {
console.log('Errore: ' + err.message) console.log('Errore: ' + err.message)
@@ -369,7 +368,7 @@ export default class Todo extends Vue {
console.log('cmdToSyncAndDb', cmd, table, method, item, id, msg) console.log('cmdToSyncAndDb', cmd, table, method, item, id, msg)
const mythis = this const mythis = this
if ('serviceWorker' in navigator && 'SyncManager' in window) { if (false && ('serviceWorker' in navigator && 'SyncManager' in window)) {
await navigator.serviceWorker.ready await navigator.serviceWorker.ready
.then(function (sw) { .then(function (sw) {
// _id: new Date().toISOString(), // _id: new Date().toISOString(),
@@ -395,9 +394,12 @@ export default class Todo extends Vue {
}) })
}) })
} else { } else {
if (cmd === rescodes.DB.CMD_SYNC_TODOS) if (cmd === rescodes.DB.CMD_SYNC_TODOS) {
if (method === 'POST')
Todos.actions.dbInsertTodo(item)
else if (method === 'PATCH')
Todos.actions.dbSaveTodo(item) Todos.actions.dbSaveTodo(item)
else if (cmd === rescodes.DB.CMD_DELETE_TODOS) } else if (cmd === rescodes.DB.CMD_DELETE_TODOS)
Todos.actions.dbDeleteTodo(id) Todos.actions.dbDeleteTodo(id)
} }
} }
@@ -411,7 +413,7 @@ export default class Todo extends Vue {
return this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', null, id, 'Your Post was canceled for syncing!') 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({ navigator.serviceWorker.controller.postMessage({
@@ -423,7 +425,7 @@ export default class Todo extends Vue {
lang: UserStore.state.lang lang: UserStore.state.lang
}) })
} }
*/ */
getElemById(id, lista = this.todos_arr) { getElemById(id, lista = this.todos_arr) {
@@ -633,6 +635,10 @@ export default class Todo extends Vue {
async modify(myobj: ITodo, update: boolean) { async modify(myobj: ITodo, update: boolean) {
if (myobj === null)
return new Promise(function (resolve, reject) {
resolve()
})
await globalroutines(this, 'read', 'todos', null, myobj._id) await globalroutines(this, 'read', 'todos', null, myobj._id)
.then(miorec => { .then(miorec => {
console.log('ArrTodos: ', myobj.descr, '[', myobj._id, ']') console.log('ArrTodos: ', myobj.descr, '[', myobj._id, ']')

View File

@@ -64,26 +64,20 @@ export let idbKeyval = (() => {
return req.result; return req.result;
}, },
async set(key, value) { async set(key, value) {
return await withStore('readwrite', 'keyval', store => { let req;
store.put(value, key); await withStore('readwrite', 'keyval', store => {
req = store.put(value, key);
}); });
return req.result;
}, },
async setdata(table, valuekey) { async setdata(table, value) {
let req;
// 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
}
console.log('setdata', table, value) console.log('setdata', table, value)
return await withStore('readwrite', table, store => { await withStore('readwrite', table, store => {
store.put(value); req = store.put(value);
}); });
return req.result;
}, },
async delete(key) { async delete(key) {
return await withStore('readwrite', 'keyval', store => { return await withStore('readwrite', 'keyval', store => {

View File

@@ -1,4 +1,4 @@
let idbKeyval = (() => { let let idbKeyval = (() => {
let db; let db;
function getDB() { function getDB() {
@@ -47,6 +47,7 @@ let idbKeyval = (() => {
}, },
async getdata(table, key) { async getdata(table, key) {
let req; let req;
await withStore('readonly', table, store => { await withStore('readonly', table, store => {
console.log('store', store, 'key', key) console.log('store', store, 'key', key)
req = store.get(key); req = store.get(key);
@@ -62,24 +63,20 @@ let idbKeyval = (() => {
return req.result; return req.result;
}, },
async set(key, value) { async set(key, value) {
return await withStore('readwrite', 'keyval', store => { let req;
store.put(value, key); await withStore('readwrite', 'keyval', store => {
req = store.put(value, key);
}); });
return req.result;
}, },
async setdata(table, valuekey) { async setdata(table, value) {
let req;
let value = []
if (table === 'delete_todos') {
value['_id'] = valuekey
}else {
value = valuekey
}
console.log('setdata', table, value) console.log('setdata', table, value)
return await withStore('readwrite', table, store => { await withStore('readwrite', table, store => {
store.put(value); req = store.put(value);
}); });
return req.result;
}, },
async delete(key) { async delete(key) {
return await withStore('readwrite', 'keyval', store => { return await withStore('readwrite', 'keyval', store => {

View File

@@ -1,6 +1,6 @@
import axios, { AxiosInstance, AxiosPromise, AxiosResponse, AxiosInterceptorManager } from 'axios' 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, ']') console.log('sendRequest', method, url, '[', lang, ']')
@@ -18,6 +18,12 @@ async function sendRequest (url: string, lang: string, mytok: string, method: st
cache: 'no-cache', cache: 'no-cache',
headers: authHeader headers: authHeader
} }
} else if (method === 'DELETE') {
configInit = {
method: method,
cache: 'no-cache',
headers: authHeader
}
} else { } else {
configInit = { configInit = {
method: method, method: method,

View File

@@ -77,12 +77,20 @@ namespace Actions {
} }
async function dbSaveTodo(context, itemtodo: ITodo) { 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 let call = process.env.MONGODB_HOST + '/todos/' + itemtodo._id
const token = UserStore.state.idToken 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) { .then(function (res) {
return rescodes.OK return rescodes.OK
}) })
@@ -127,6 +135,7 @@ namespace Actions {
} }
export const actions = { export const actions = {
dbInsertTodo: b.dispatch(dbInsertTodo),
dbSaveTodo: b.dispatch(dbSaveTodo), dbSaveTodo: b.dispatch(dbSaveTodo),
dbLoadTodo: b.dispatch(dbLoadTodo), dbLoadTodo: b.dispatch(dbLoadTodo),
dbDeleteTodo: b.dispatch(dbDeleteTodo), dbDeleteTodo: b.dispatch(dbDeleteTodo),