From 1f830b3e98368432da9163b2ca8618956e094e49 Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Thu, 14 Feb 2019 20:08:22 +0100 Subject: [PATCH 01/22] cleaned some code Promise... --- src/components/todos/todo/todo.ts | 126 ++++++++++++++--------------- src/components/todos/todo/todo.vue | 14 ++-- src/store/Modules/Todos.ts | 26 +++--- src/store/Modules/UserStore.ts | 2 +- 4 files changed, 83 insertions(+), 85 deletions(-) diff --git a/src/components/todos/todo/todo.ts b/src/components/todos/todo/todo.ts index 36e1a2e..2f4b568 100644 --- a/src/components/todos/todo/todo.ts +++ b/src/components/todos/todo/todo.ts @@ -104,8 +104,9 @@ export default class Todo extends Vue { // console.log('reload_fromServer_changed!', value, oldValue) // if (value) { Todos.actions.dbLoadTodo(false) - - Todos.actions.updateArrayInMemory() + .then(() => { + return Todos.actions.updateArrayInMemory() + }) } // } } @@ -127,7 +128,7 @@ export default class Todo extends Vue { let mythis = this mythis.tmpstrTodos = '' - globalroutines(null, 'readall', 'todos', null) + return globalroutines(null, 'readall', 'todos', null) .then(function (alldata) { const myrecs = [...alldata] @@ -145,7 +146,7 @@ export default class Todo extends Vue { let mythis = this mythis.tmpstrTodos = '' - globalroutines(null, 'write', 'todos', this.todos_arr[0]) + return globalroutines(null, 'write', 'todos', this.todos_arr[0]) .then(function (alldata) { mythis.getArrTodos() }) @@ -190,7 +191,7 @@ export default class Todo extends Vue { this.itemDragStart = null } - async updateLinkedList(init: boolean, arr: ITodo[] = this.todos_arr) { + updateLinkedList(init: boolean, arr: ITodo[] = this.todos_arr) { // console.log('updateLinkedList', this.todos_arr) @@ -201,7 +202,7 @@ export default class Todo extends Vue { idprev = arr[0].id_prev idnext = arr[0].id_next } - await arr.forEach((elem, index) => { + arr.forEach((elem, index) => { if (index === 0) { idprev = rescodes.LIST_START } else { @@ -318,33 +319,36 @@ export default class Todo extends Vue { } } - await this.updateLinkedList(false) + this.updateLinkedList(false) // Updated only elements modified await this.updateModifyRecords(true) - this.updatetable(false, 'onEnd') + await this.updatetable(false, 'onEnd') } async updateModifyRecords(refresh: boolean = false) { let update = false + await this.todos_arr.forEach((elem: ITodo) => { if (elem.modified) { console.log('calling MODIFY 3') - this.modify(elem, false) - update = true - elem.modified = false + return this.modify(elem, false) + .then(() => { + update = true + elem.modified = false + }) } }) if (update) - await this.updatetable(refresh, 'updateModifyRecords') + return await this.updatetable(refresh, 'updateModifyRecords') } - created() { - this.load() + async created() { + await this.load() } setarrPriority() { @@ -373,15 +377,13 @@ export default class Todo extends Vue { } // console.log('Priority:' + this.listPriorityLabel) this.setarrPriority() - this.clearArr() await this.updatetable(false, 'load') + this.loadDone = true this.checkUpdate_everytime() - - this.loadDone = true } // Call to check if need to refresh @@ -393,7 +395,7 @@ export default class Todo extends Vue { initcat() { - let tomorrow = new Date(); + let tomorrow = new Date() tomorrow.setDate(tomorrow.getDate() + 1) @@ -448,6 +450,7 @@ export default class Todo extends Vue { objtodo.descr = this.todo objtodo.category = this.getCategory() + const lastelem: ITodo = this.getLastList() objtodo.id_prev = (lastelem !== null) ? lastelem._id : rescodes.LIST_START objtodo.id_next = rescodes.LIST_END @@ -467,10 +470,10 @@ export default class Todo extends Vue { // console.log('calling MODIFY 4', lastelem) } - const rismod = await this.modify(lastelem, false) + await this.modify(lastelem, false) - this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS, 'POST', objtodo, true) - this.updatetable(false, 'insertTodo') + await this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS, 'POST', objtodo, true) + await this.updatetable(false, 'insertTodo') // console.log('ESCO.........') @@ -496,49 +499,44 @@ export default class Todo extends Vue { const mythis = this if ('serviceWorker' in navigator) { - await navigator.serviceWorker.ready + const 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, item, id) + return globalroutines(mythis, 'write', table, item, id) .then(function (id) { // console.log('id', id) + const sep = '|' - }) - const sep = '|' - - let multiparams = cmdSw + sep + table + sep + method + sep + UserStore.state.x_auth_token + sep + UserStore.state.lang - let mymsgkey = { - _id: multiparams, - value: multiparams - } - globalroutines(mythis, 'write', 'swmsg', mymsgkey, multiparams) - .then(ris => { - // if ('SyncManager' in window) { - // console.log(' SENDING... sw.sync.register', multiparams) - // return sw.sync.register(multiparams) - // } else { - // #Todo ++ Alternative 2 to SyncManager - Api.syncAlternative(multiparams) - // } - }) - .then(function () { - - let snackbarContainer = document.querySelector('#confirmation-toast') - let data = { message: msg } - // snackbarContainer.MaterialSnackbar.showSnackbar(data) - }) - .catch(function (err) { - console.error('Errore in globalroutines', table, err) + let multiparams = cmdSw + sep + table + sep + method + sep + UserStore.state.x_auth_token + sep + UserStore.state.lang + let mymsgkey = { + _id: multiparams, + value: multiparams + } + return globalroutines(mythis, 'write', 'swmsg', mymsgkey, multiparams) + .then(ris => { + // if ('SyncManager' in window) { + // console.log(' SENDING... sw.sync.register', multiparams) + // return sw.sync.register(multiparams) + // } else { + // #Todo ++ Alternative 2 to SyncManager + return Api.syncAlternative(multiparams) + // } + }) + .then(function () { + let data = { message: msg, position: 'bottom', timeout: 3000 } + mythis.$q.notify(data) + }) + .catch(function (err) { + console.error('Errore in globalroutines', table, err) + }) }) }) if (update) { // // Update the array in memory, from todos table from IndexedDb - Todos.actions.updateArrayInMemory() + await Todos.actions.updateArrayInMemory() .then((ris) => { return ris }) @@ -547,11 +545,11 @@ export default class Todo extends Vue { } else { if (cmd === rescodes.DB.CMD_SYNC_NEW_TODOS) { if (method === 'POST') - Todos.actions.dbInsertTodo(item) + await Todos.actions.dbInsertTodo(item) else if (method === 'PATCH') - Todos.actions.dbSaveTodo(item) + await Todos.actions.dbSaveTodo(item) } else if (cmd === rescodes.DB.CMD_DELETE_TODOS) - Todos.actions.dbDeleteTodo(item) + await Todos.actions.dbDeleteTodo(item) } } @@ -560,8 +558,8 @@ export default class Todo extends Vue { } - deleteItemToSyncAndDb(table: String, item: ITodo, id, update: boolean) { - return this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', item, id, '', update) + async deleteItemToSyncAndDb(table: String, item: ITodo, id, update: boolean) { + return await this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', item, id, '', update) } /* @@ -603,17 +601,17 @@ export default class Todo extends Vue { myobjprev.id_next = myobjtrov.id_next myobjprev.modified = true console.log('calling MODIFY 2') - this.modify(myobjprev, false) + await this.modify(myobjprev, false) } if (myobjnext !== null) { myobjnext.id_prev = myobjtrov.id_prev myobjnext.modified = true console.log('calling MODIFY 1') - this.modify(myobjnext, false) + await this.modify(myobjnext, false) } - this.deleteItemToSyncAndDb(rescodes.DB.TABLE_DELETE_TODOS, myobjtrov, id, true) + await this.deleteItemToSyncAndDb(rescodes.DB.TABLE_DELETE_TODOS, myobjtrov, id, true) const mythis = this // Delete item @@ -815,7 +813,7 @@ export default class Todo extends Vue { return new Promise(function (resolve, reject) { resolve() }) - await globalroutines(this, 'read', 'todos', null, myobj._id) + return await globalroutines(this, 'read', 'todos', null, myobj._id) .then(miorec => { if (miorec === undefined) { console.log('~~~~~~~~~~~~~~~~~~~~ !!!!!!!!!!!!!!!!!! Record not Found !!!!!! id=', myobj._id) @@ -837,15 +835,15 @@ export default class Todo extends Vue { // this.logelem('modify', miorec) - globalroutines(this, 'write', 'todos', miorec) + return globalroutines(this, 'write', 'todos', miorec) .then(ris => { - this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS_PATCH, 'PATCH', miorec, update) + return this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS_PATCH, 'PATCH', miorec, update) .then(() => { // console.log('SET MODIFIED FALSE') if (update) - this.updatetable(false, 'modify') + return this.updatetable(false, 'modify') }) }) diff --git a/src/components/todos/todo/todo.vue b/src/components/todos/todo/todo.vue index 0eedb26..8fa0c4f 100644 --- a/src/components/todos/todo/todo.vue +++ b/src/components/todos/todo/todo.vue @@ -37,16 +37,16 @@ - - - - - +
+ Get Todo + Set Todo + Reload +
- + - + diff --git a/src/store/Modules/Todos.ts b/src/store/Modules/Todos.ts index 7c26958..fbf0827 100644 --- a/src/store/Modules/Todos.ts +++ b/src/store/Modules/Todos.ts @@ -90,17 +90,23 @@ namespace Actions { // console.log('---------------------- 2) navigator (2) .serviceWorker.ready') + let promiseChain = Promise.resolve() + something = true for (let rec of arr_recmsg) { // console.log(' .... sw.sync.register ( ', rec._id) // if ('SyncManager' in window) { // sw.sync.register(rec._id) // } else { - // #Todo ++ Alternative to SyncManager - Api.syncAlternative(rec._id) + + // #Alternative to SyncManager + promiseChain = promiseChain.then(() => { + return Api.syncAlternative(rec._id) + }) + // } } - return something + return promiseChain } }) @@ -108,7 +114,9 @@ namespace Actions { } } - return something + return new Promise(function (resolve, reject) { + resolve(something) + }) } async function waitAndcheckPendingMsg(context) { @@ -155,19 +163,11 @@ namespace Actions { } } } catch (e) { - } return new Promise(function (resolve, reject) { - - /* - globalroutines(null, 'readall', 'swmsg') - .then(function (arr_recmsg) { - if (arr_recmsg.length > 0) { - */ - // Check if there is something - globalroutines(null, 'count', 'swmsg') + return globalroutines(null, 'count', 'swmsg') .then(function (count) { if (count > 0) { console.log('count = ', count) diff --git a/src/store/Modules/UserStore.ts b/src/store/Modules/UserStore.ts index 8930196..9830162 100644 --- a/src/store/Modules/UserStore.ts +++ b/src/store/Modules/UserStore.ts @@ -371,7 +371,7 @@ namespace Actions { if ('serviceWorker' in navigator) { sub = await navigator.serviceWorker.ready .then(function (swreg) { - const sub = swreg.pushManager.getSubscription() + let sub = swreg.pushManager.getSubscription() return sub }) .catch(e => { From 1af2c86067ab1f42814547f70e24bd70530e1f8b Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Thu, 14 Feb 2019 21:36:16 +0100 Subject: [PATCH 02/22] cleaned some code Promise... --- .env.development | 2 +- src/components/todos/todo/todo.vue | 2 +- src/store/Modules/Todos.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.env.development b/.env.development index e032ebc..3cb9400 100644 --- a/.env.development +++ b/.env.development @@ -1,4 +1,4 @@ -APP_VERSION="DEV 0.0.27" +APP_VERSION="DEV 0.0.30" SERVICE_WORKER_FILE='service-worker.js' APP_ID='1' APP_URL='https://freeplanet.app' diff --git a/src/components/todos/todo/todo.vue b/src/components/todos/todo/todo.vue index 8fa0c4f..843f20b 100644 --- a/src/components/todos/todo/todo.vue +++ b/src/components/todos/todo/todo.vue @@ -35,7 +35,7 @@ :after="[{icon: 'arrow_forward', content: true, handler () {}}]" v-on:keyup.enter="insertTodo"/> - + {{ tmpstrTodos }}
Get Todo diff --git a/src/store/Modules/Todos.ts b/src/store/Modules/Todos.ts index fbf0827..9293477 100644 --- a/src/store/Modules/Todos.ts +++ b/src/store/Modules/Todos.ts @@ -201,7 +201,7 @@ namespace Actions { // console.log('******* UPDATE TODOS.STATE.TODOS !:', res.todos) if (body.todos) { state.todos = [...body.todos] - // Todos.mutations.setTodos_changed() + Todos.mutations.setTodos_changed() } console.log('********** res', 'state.todos', state.todos, 'checkPending', checkPending) From cd0ec4044140ebdb36998abcaefcdaddebca86ad Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Fri, 15 Feb 2019 01:25:44 +0100 Subject: [PATCH 03/22] Corrected some logic problems... 4 storing: - Array in Memory Global state.todos - Array temporary filtered arr_todos - IndexedDb - Database --- src-pwa/custom-service-worker.js | 6 +- .../todos/SingleTodo/SingleTodo.scss | 7 + src/components/todos/SingleTodo/SingleTodo.ts | 12 +- .../todos/SingleTodo/SingleTodo.vue | 8 +- src/components/todos/todo/todo.ts | 113 ++++++++----- src/globalroutines/indexdb.js | 2 +- src/root/home/home.ts | 10 +- src/statics/i18n.js | 6 +- src/store/Api/index.ts | 2 +- src/store/Modules/Todos.ts | 157 ++++++++++-------- src/store/Modules/rescodes.ts | 7 +- 11 files changed, 198 insertions(+), 132 deletions(-) diff --git a/src-pwa/custom-service-worker.js b/src-pwa/custom-service-worker.js index 43ee1de..0dc793e 100644 --- a/src-pwa/custom-service-worker.js +++ b/src-pwa/custom-service-worker.js @@ -137,7 +137,7 @@ if (workbox) { if (data) { if (data.todos) { console.log('***********************+++++++++++++++++++++++++++++++++++++++++++++++++++********** Records TODOS Received from Server [', data.todos.length, 'record]', data.todos) - for (const key in data.todos) { + for (let key in data.todos) { await writeData('todos', data.todos[key]) } } @@ -506,7 +506,9 @@ self.addEventListener('push', function (event) { badge: '/statics/icons/android-chrome-192x192.png', data: { url: data.url - } + }, + tag: 'received', + renitify: true, // vibrate also with others messages. }; event.waitUntil( diff --git a/src/components/todos/SingleTodo/SingleTodo.scss b/src/components/todos/SingleTodo/SingleTodo.scss index 4eebdb8..1721016 100644 --- a/src/components/todos/SingleTodo/SingleTodo.scss +++ b/src/components/todos/SingleTodo/SingleTodo.scss @@ -26,6 +26,10 @@ $heightitem: 19px; display: inline-block; } +.comp_selected { + display: inline-block !important; +} + .flex-container2:hover{ background-color: rgba(230, 230, 230, 0.8); } @@ -87,6 +91,9 @@ $heightitem: 19px; //visibility: hidden; } +//.q-popover.animate-scale { +// animation: none; +//} .pos-item:hover, .pos-item-popover:hover { cursor: grab; diff --git a/src/components/todos/SingleTodo/SingleTodo.ts b/src/components/todos/SingleTodo/SingleTodo.ts index 813ab95..788900b 100644 --- a/src/components/todos/SingleTodo/SingleTodo.ts +++ b/src/components/todos/SingleTodo/SingleTodo.ts @@ -40,6 +40,8 @@ export default class SingleTodo extends Vue { public togglemenu: boolean = false public percentageProgress: number = 0 public itemtodoPrec: ITodo + public clButtPopover: string = 'pos-item-popover' + $q: any @Prop({ required: true }) itemtodo: ITodo @@ -128,6 +130,9 @@ export default class SingleTodo extends Vue { this.menuProgress += mycolcl this.percProgress += mycolcl + this.clButtPopover = this.sel ? 'pos-item-popover comp_selected' : 'pos-item-popover' + + // if (this.inEdit) { // this.classDescr += ' hide' // this.classDescrEdit += ' show' @@ -264,6 +269,7 @@ export default class SingleTodo extends Vue { } } */ + // Delete Key or Backspage if (((e.keyCode === 8) || (e.keyCode === 46)) && (this.precDescr === '') && !e.shiftKey) { e.preventDefault() this.deselectRiga() @@ -382,11 +388,13 @@ export default class SingleTodo extends Vue { const deletestr = this.$t('dialog.delete') const cancelstr = this.$t('dialog.cancel') - await askConfirm(this.$q, this.$t('dialog.msg.titledeleteTask'), this.$t('dialog.msg.deleteTask').toString(), deletestr, cancelstr) + let msg = this.$t('dialog.msg.deleteTask', {'mytodo' : this.itemtodo.descr }) + await askConfirm(this.$q, this.$t('dialog.msg.titledeleteTask'), msg, deletestr, cancelstr) .then(ris => { console.log('ris', ris) - if (ris) + if (ris) { this.removeitem(this.itemtodo._id) + } }).catch(err => { }) diff --git a/src/components/todos/SingleTodo/SingleTodo.vue b/src/components/todos/SingleTodo/SingleTodo.vue index 68ffee7..73ae24b 100644 --- a/src/components/todos/SingleTodo/SingleTodo.vue +++ b/src/components/todos/SingleTodo/SingleTodo.vue @@ -13,7 +13,7 @@
- @@ -50,8 +50,8 @@
- @@ -59,6 +59,8 @@
+ + diff --git a/src/components/todos/todo/todo.ts b/src/components/todos/todo/todo.ts index 2f4b568..2ebb42d 100644 --- a/src/components/todos/todo/todo.ts +++ b/src/components/todos/todo/todo.ts @@ -105,7 +105,7 @@ export default class Todo extends Vue { // if (value) { Todos.actions.dbLoadTodo(false) .then(() => { - return Todos.actions.updateArrayInMemory() + // return Todos.actions.updatefromIndexedDbToStateTodo() }) } // } @@ -462,6 +462,7 @@ export default class Todo extends Vue { return } + // 1) Insert into the IndexedDb const id = await globalroutines(this, 'write', 'todos', objtodo) // update also the last elem if (lastelem !== null) { @@ -470,15 +471,21 @@ export default class Todo extends Vue { // console.log('calling MODIFY 4', lastelem) } + // Create record in Memory + Todos.mutations.createNewItem(objtodo) + + // Modify the record above to the new last await this.modify(lastelem, false) - await this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS, 'POST', objtodo, true) - await this.updatetable(false, 'insertTodo') - - // console.log('ESCO.........') - // empty the field this.todo = '' + + this.updatetable(false, 'insertTodo') + + this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS, 'POST', objtodo, true) + + // console.log('ESCO.........') + } @@ -525,8 +532,10 @@ export default class Todo extends Vue { // } }) .then(function () { - let data = { message: msg, position: 'bottom', timeout: 3000 } - mythis.$q.notify(data) + if (msg !== '') { + let data = { message: msg, position: 'bottom', timeout: 3000 } + mythis.$q.notify(data) + } }) .catch(function (err) { console.error('Errore in globalroutines', table, err) @@ -534,32 +543,36 @@ export default class Todo extends Vue { }) }) - if (update) { - // // Update the array in memory, from todos table from IndexedDb - await Todos.actions.updateArrayInMemory() - .then((ris) => { - return ris - }) - } + // if (update) { + // // // Update the array in memory, from IndexedDb update the todos array + // await Todos.actions.updatefromIndexedDbToStateTodo() + // .then((ris) => { + // return ris + // }) + // } - } else { - if (cmd === rescodes.DB.CMD_SYNC_NEW_TODOS) { - if (method === 'POST') - await Todos.actions.dbInsertTodo(item) - else if (method === 'PATCH') - await Todos.actions.dbSaveTodo(item) - } else if (cmd === rescodes.DB.CMD_DELETE_TODOS) - await Todos.actions.dbDeleteTodo(item) + } + + if (cmd === rescodes.DB.CMD_SYNC_NEW_TODOS) { + if (method === 'POST') + await Todos.actions.dbInsertTodo(item) + else if (method === 'PATCH') + await Todos.actions.dbSaveTodo(item) + } else if (cmd === rescodes.DB.CMD_DELETE_TODOS) { + await Todos.actions.dbDeleteTodo(item) } } async saveItemToSyncAndDb(table: String, method, item: ITodo, update: boolean) { + // let msg = (method === 'PATCH') ? 'Modif: ' : '++Create: ' + // msg = msg + item.descr return await this.cmdToSyncAndDb(rescodes.DB.CMD_SYNC_NEW_TODOS, table, method, item, 0, '', update) } - async deleteItemToSyncAndDb(table: String, item: ITodo, id, update: boolean) { - return await this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', item, id, '', update) + deleteItemToSyncAndDb(table: String, item: ITodo, id, update: boolean) { + // let msg = 'Delete: ' + item.descr + this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', item, id, '', update) } /* @@ -591,6 +604,8 @@ export default class Todo extends Vue { async deleteitem(id) { console.log('deleteitem: KEY = ', id) + + let myobjtrov = this.getElemById(id) if (myobjtrov !== null) { @@ -611,16 +626,23 @@ export default class Todo extends Vue { await this.modify(myobjnext, false) } - await this.deleteItemToSyncAndDb(rescodes.DB.TABLE_DELETE_TODOS, myobjtrov, id, true) + // 1) Delete from the Todos Array + Todos.mutations.deletemyitem(myobjtrov) + Todos.mutations.setTodos_changed() - const mythis = this - // Delete item - await globalroutines(this, 'delete', 'todos', null, id) + // 2) Delete from the IndexedDb + globalroutines(this, 'delete', 'todos', null, id) .then((ris) => { - mythis.updatetable(false, 'deleteitem') + // Update in to the UI + this.updatetable(true, 'deleteitem') + }).catch((error) => { console.log('err: ', error) }) + + // 3) Delete from the Server (call) + this.deleteItemToSyncAndDb(rescodes.DB.TABLE_DELETE_TODOS, myobjtrov, id, true) + } // console.log('FINE deleteitem') @@ -646,7 +668,7 @@ export default class Todo extends Vue { return await Todos.actions.getTodosByCategory(this.getCategory()) .then(arrris => { - this.todos_arr = [] + // this.todos_arr = [] let arrtemp = [...arrris] @@ -663,7 +685,7 @@ export default class Todo extends Vue { this.todos_arr = [...arrtemp] // make copy - console.log('AGGIORNA todos_arr') + console.log('AGGIORNA todos_arr [', this.todos_arr.length, ']') }) } @@ -797,9 +819,9 @@ export default class Todo extends Vue { modifyField(recOut, recIn, field) { if (String(recOut[field]) !== String(recIn[field])) { - console.log('*************** CAMPO ', field, 'MODIFICATO!') - console.log(recOut[field]) - console.log(recIn[field]) + // console.log('*************** CAMPO ', field, 'MODIFICATO!') + // console.log(recOut[field]) + // console.log(recIn[field]) recOut.modified = true recOut[field] = recIn[field] return true @@ -833,19 +855,22 @@ export default class Todo extends Vue { miorec.modify_at = new Date().getDate() miorec.modified = false - // this.logelem('modify', miorec) + // 1) Modify on Global Memory + Todos.mutations.modifymyItem(miorec) - return globalroutines(this, 'write', 'todos', miorec) + if (update) { + // 4) Update the filter in Memory + this.updatetable(false, 'modify') + } + + // this.logelem('modify', miorec) + // 2) Modify on IndexedDb + globalroutines(this, 'write', 'todos', miorec) .then(ris => { - return this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS_PATCH, 'PATCH', miorec, update) - .then(() => { - // console.log('SET MODIFIED FALSE') + // 3) Modify on the Server (call) + this.saveItemToSyncAndDb(rescodes.DB.TABLE_SYNC_TODOS_PATCH, 'PATCH', miorec, update) - if (update) - return this.updatetable(false, 'modify') - - }) }) } }) diff --git a/src/globalroutines/indexdb.js b/src/globalroutines/indexdb.js index e647dce..b0b6423 100644 --- a/src/globalroutines/indexdb.js +++ b/src/globalroutines/indexdb.js @@ -63,7 +63,7 @@ export default async (context, cmd, table, datakey = null, id = '') => { } else if (cmd === 'write') { return await storage.setdata(table, datakey) - } else if (cmd === 'updateinMemory') { + } else if (cmd === 'updatefromIndexedDbToStateTodo') { return await readfromIndexDbToStateTodos(context, table) } else if (cmd === 'readall') { return await storage.getalldata(table) diff --git a/src/root/home/home.ts b/src/root/home/home.ts index 8f62da5..2a3ab52 100644 --- a/src/root/home/home.ts +++ b/src/root/home/home.ts @@ -43,10 +43,10 @@ export default class Home extends Vue { set conta(valore) { GlobalStore.actions.setConta(valore) let my = this.$q.i18n.lang - this.showNotification(String(my)) + this.showNotif(String(my)) } - showNotification(message: string, color = 'primary', icon = '') { + showNotif(message: string, color = 'primary', icon = '') { this.$q.notify({ color, icon, @@ -158,15 +158,15 @@ export default class Home extends Vue { } askfornotification() { - this.showNotification(this.$t('notification.waitingconfirm'), 'positive', 'notifications') + this.showNotif(this.$t('notification.waitingconfirm'), 'positive', 'notifications') let mythis = this Notification.requestPermission(function (result) { console.log('User Choice', result) if (result === 'granted') { - mythis.showNotification(mythis.$t('notification.confirmed'), 'positive', 'notifications') + mythis.showNotif(mythis.$t('notification.confirmed'), 'positive', 'notifications') } else { - mythis.showNotification(mythis.$t('notification.denied'), 'negative', 'notifications') + mythis.showNotif(mythis.$t('notification.denied'), 'negative', 'notifications') // displayConfirmNotification(); } diff --git a/src/statics/i18n.js b/src/statics/i18n.js index e10e65c..8652842 100644 --- a/src/statics/i18n.js +++ b/src/statics/i18n.js @@ -8,7 +8,7 @@ const messages = { cancel: 'Annulla', msg: { titledeleteTask: 'Elimina Task', - deleteTask: 'Vuoi Eliminare questo Task?' + deleteTask: "Vuoi Eliminare {mytodo}?" } }, comp:{ @@ -134,7 +134,7 @@ const messages = { cancel: 'Cancelar', msg: { titledeleteTask: 'Borrar Tarea', - deleteTask: 'Quieres borrar este tarea?' + deleteTask: 'Quieres borrar %s?' } }, comp:{ @@ -260,7 +260,7 @@ const messages = { cancel: 'Cancel', msg: { titledeleteTask: 'Delete Task', - deleteTask: 'Delete this Task?' + deleteTask: 'Delete %s?' } }, comp:{ diff --git a/src/store/Api/index.ts b/src/store/Api/index.ts index 0274374..867784c 100644 --- a/src/store/Api/index.ts +++ b/src/store/Api/index.ts @@ -132,7 +132,7 @@ export namespace ApiTool { } export async function syncAlternative(mystrparam) { - console.log('[ALTERNATIVE Background syncing', mystrparam) + // console.log('[ALTERNATIVE Background syncing', mystrparam) let multiparams = mystrparam.split('|') if (multiparams) { diff --git a/src/store/Modules/Todos.ts b/src/store/Modules/Todos.ts index 9293477..89dd7bc 100644 --- a/src/store/Modules/Todos.ts +++ b/src/store/Modules/Todos.ts @@ -46,9 +46,45 @@ namespace Mutations { // console.log('******* state.todos_changed', state.todos_changed) } + function findTodoById(state: ITodosState, id: string) { + for (let i = 0; i < state.todos.length; i++) { + if (state.todos[i]._id === id) + return i + } + return -1 + } + + function createNewItem(state: ITodosState, myitem: ITodo) { + state.todos.push(myitem) + Todos.mutations.setTodos_changed() + } + + + function modifymyItem(state: ITodosState, myitem: ITodo) { + // Find record + const ind = findTodoById(state, myitem._id) + if (ind >= 0) + state.todos[ind] = rescodes.jsonCopy(myitem) + + } + + function deletemyitem(state: ITodosState, myitem: ITodo) { + // Find record + const ind = findTodoById(state, myitem._id) + + // Delete Item in to Array + if (ind >= 0) + state.todos.splice(ind, 1) + + } + + export const mutations = { setTestpao: b.commit(setTestpao), - setTodos_changed: b.commit(setTodos_changed) + setTodos_changed: b.commit(setTodos_changed), + modifymyItem: b.commit(modifymyItem), + deletemyitem: b.commit(deletemyitem), + createNewItem: b.commit(createNewItem) } } @@ -226,8 +262,10 @@ namespace Actions { } else { consolelogpao('NETWORK UNREACHABLE ! (Error in fetch)', UserStore.getters.getServerCode, ris.status) } - // Read all data from IndexedDB Store into Memory - await updateArrayInMemory(context) + if ('serviceWorker' in navigator) { + // Read all data from IndexedDB Store into Memory + await updatefromIndexedDbToStateTodo(context) + } } else { if (ris.status === rescodes.OK && checkPending) { waitAndcheckPendingMsg(context) @@ -235,11 +273,11 @@ namespace Actions { } } - async function updateArrayInMemory(context) { + async function updatefromIndexedDbToStateTodo(context) { // console.log('Update the array in memory, from todos table from IndexedDb') - await globalroutines(null, 'updateinMemory', 'todos', null) + await globalroutines(null, 'updatefromIndexedDbToStateTodo', 'todos', null) .then(() => { - // console.log('updateArrayInMemory! ') + console.log('updatefromIndexedDbToStateTodo! ') return true }) } @@ -269,78 +307,57 @@ namespace Actions { return await dbInsertSaveTodo(context, itemtodo, 'POST') } - function UpdateNewIdFromDB(oldItem, newItem, method) { - // console.log('PRIMA state.todos', state.todos) - // console.log('ITEM', newItem) - if (method === 'POST') { - state.todos.push(newItem) - Todos.mutations.setTodos_changed() - // } else if (method === 'PATCH') { - // state.todos.map(item => { - // if (item._id === newItem._id) { - // return newItem - // } - // }) + async function dbInsertSaveTodo(context, itemtodo: ITodo, method) { + + if (!('serviceWorker' in navigator)) { + + console.log('dbInsertSaveTodo', itemtodo, method) + let call = process.env.MONGODB_HOST + '/todos' + + if (UserStore.state.userId === '') + return false // Login not made + + if (method !== 'POST') + call += '/' + itemtodo._id + + console.log('TODO TO SAVE: ', itemtodo) + + let res = await Api.SendReq(call, method, itemtodo) + .then(({ res, newItem }) => { + console.log('dbInsertSaveTodo to the Server', newItem) + + return (res.status === 200) + }) + .catch((error) => { + UserStore.mutations.setErrorCatch(error) + // return UserStore.getters.getServerCode + return false + }) } - - // console.log('DOPO state.todos', state.todos) - } - - async function dbInsertSaveTodo(context, itemtodo: ITodo, method) { - console.log('dbInsertSaveTodo', itemtodo, method) - let call = process.env.MONGODB_HOST + '/todos' - - if (UserStore.state.userId === '') - return false // Login not made - - if (method !== 'POST') - call += '/' + itemtodo._id - - console.log('TODO TO SAVE: ', itemtodo) - - let res = await Api.SendReq(call, method, itemtodo) - .then(({ res, newItem }) => { - console.log('dbInsertSaveTodo RIS =', newItem) - if (newItem) { - - // Update ID on local - UpdateNewIdFromDB(itemtodo, newItem, method) - } - }) - .catch((error) => { - UserStore.mutations.setErrorCatch(error) - return UserStore.getters.getServerCode - }) - - return res + return true } async function dbDeleteTodo(context, item: ITodo) { - // console.log('dbDeleteTodo', item) - let call = process.env.MONGODB_HOST + '/todos/' + item._id - if (UserStore.state.userId === '') - return false // Login not made + if (!('serviceWorker' in navigator)) { + // console.log('dbDeleteTodo', item) + let call = process.env.MONGODB_HOST + '/todos/' + item._id - let res = await Api.SendReq(call, 'DELETE', item) - .then(function ({ res, itemris }) { + if (UserStore.state.userId === '') + return false // Login not made - if (res.status === 200) { - // Delete Item in to Array - state.todos.splice(state.todos.indexOf(item), 1) + let res = await Api.SendReq(call, 'DELETE', item) + .then(function ({ res, itemris }) { + console.log('dbDeleteTodo to the Server') + }) + .catch((error) => { + UserStore.mutations.setErrorCatch(error) + return UserStore.getters.getServerCode + }) - Todos.mutations.setTodos_changed() - } - - return rescodes.OK - }) - .catch((error) => { - UserStore.mutations.setErrorCatch(error) - return UserStore.getters.getServerCode - }) - - return res + return res + } } async function getTodosByCategory(context, category: string) { @@ -356,7 +373,7 @@ namespace Actions { dbSaveTodo: b.dispatch(dbSaveTodo), dbLoadTodo: b.dispatch(dbLoadTodo), dbDeleteTodo: b.dispatch(dbDeleteTodo), - updateArrayInMemory: b.dispatch(updateArrayInMemory), + updatefromIndexedDbToStateTodo: b.dispatch(updatefromIndexedDbToStateTodo), getTodosByCategory: b.dispatch(getTodosByCategory), checkPendingMsg: b.dispatch(checkPendingMsg), waitAndcheckPendingMsg: b.dispatch(waitAndcheckPendingMsg) diff --git a/src/store/Modules/rescodes.ts b/src/store/Modules/rescodes.ts index 13ef138..f2f4c6a 100644 --- a/src/store/Modules/rescodes.ts +++ b/src/store/Modules/rescodes.ts @@ -89,7 +89,7 @@ export const rescodes = { }, - INDEX_MENU_DELETE: 3, + INDEX_MENU_DELETE: 4, menuPopupTodo: { 'it': [ @@ -168,6 +168,11 @@ export const rescodes = { ] }, + jsonCopy(src) { + return JSON.parse(JSON.stringify(src)) + } + + } From fac16d2d5279c963feec5b6326ec0786af5fbcb1 Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Fri, 15 Feb 2019 01:37:42 +0100 Subject: [PATCH 04/22] Corrected some logic problems... 4 storing: - Array in Memory Global state.todos - Array temporary filtered arr_todos - IndexedDb - Database --- .env.development | 2 +- src/store/Modules/UserStore.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.env.development b/.env.development index 3cb9400..19ae2ec 100644 --- a/.env.development +++ b/.env.development @@ -1,4 +1,4 @@ -APP_VERSION="DEV 0.0.30" +APP_VERSION="DEV 0.0.31" SERVICE_WORKER_FILE='service-worker.js' APP_ID='1' APP_URL='https://freeplanet.app' diff --git a/src/store/Modules/UserStore.ts b/src/store/Modules/UserStore.ts index 9830162..96a02da 100644 --- a/src/store/Modules/UserStore.ts +++ b/src/store/Modules/UserStore.ts @@ -569,9 +569,9 @@ namespace Actions { UserStore.mutations.setAuth(token) Mutations.mutations.authUser({ - userId: userId, - username: username, - verified_email: verified_email, + userId, + username, + verified_email }) await setGlobal(false) From ffe3fce52b47c7de0f9b166225eafe4961542ba9 Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Fri, 15 Feb 2019 14:54:56 +0100 Subject: [PATCH 05/22] fix: ask updated dbLoadTodo (if something to process) only when is connected... --- src/components/todos/SingleTodo/SingleTodo.vue | 9 +++------ src/components/todos/todo/todo.vue | 8 ++++++-- src/store/Modules/Todos.ts | 5 ++++- src/store/Modules/UserStore.ts | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/components/todos/SingleTodo/SingleTodo.vue b/src/components/todos/SingleTodo/SingleTodo.vue index 73ae24b..b10b532 100644 --- a/src/components/todos/SingleTodo/SingleTodo.vue +++ b/src/components/todos/SingleTodo/SingleTodo.vue @@ -1,8 +1,5 @@