From 990955b6c269f1b1ae18e14c4480c53c12b31eee Mon Sep 17 00:00:00 2001 From: Paolo Arena Date: Sun, 3 Feb 2019 03:44:25 +0100 Subject: [PATCH] - fix: objectId in javascript, and not created by Mongodb --- src/components/todos/todo/todo.ts | 19 +++++++++++------- src/js/objectId.js | 10 ++++++++++ src/model/Todos.ts | 2 +- src/store/Modules/Todos.ts | 32 ++++++++++++++++++------------- src/store/Modules/rescodes.ts | 3 ++- 5 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 src/js/objectId.js diff --git a/src/components/todos/todo/todo.ts b/src/components/todos/todo/todo.ts index 41e1c20..ccb4c9a 100644 --- a/src/components/todos/todo/todo.ts +++ b/src/components/todos/todo/todo.ts @@ -9,6 +9,9 @@ import { rescodes } from '../../../store/Modules/rescodes' import { Todos } from '@store' import { UserStore } from '@store' +import objectId from '../../../js/objectId.js' + + import _ from 'lodash' import draggable from 'vuedraggable' @@ -283,7 +286,7 @@ export default class Todo extends Vue { } copy(o) { - var output, v, key + let output, v, key output = Array.isArray(o) ? [] : {} for (key in o) { v = o[key] @@ -292,6 +295,7 @@ export default class Todo extends Vue { return output } + initcat() { @@ -301,7 +305,8 @@ export default class Todo extends Vue { console.log('User:' + UserStore.state.userId) const objtodo: ITodo = { - _id: new Date().toISOString(), // Create NEW + // _id: new Date().toISOString(), // Create NEW + _id: objectId(), userId: UserStore.state.userId, descr: '', priority: rescodes.Todos.PRIORITY_NORMAL, @@ -389,7 +394,7 @@ export default class Todo extends Vue { console.log('cmdToSyncAndDb', cmd, table, method, item, id, msg) const mythis = this - if (false && ('serviceWorker' in navigator && 'SyncManager' in window)) { + if (('serviceWorker' in navigator && 'SyncManager' in window)) { await navigator.serviceWorker.ready .then(function (sw) { // _id: new Date().toISOString(), @@ -421,7 +426,7 @@ export default class Todo extends Vue { else if (method === 'PATCH') Todos.actions.dbSaveTodo(item) } else if (cmd === rescodes.DB.CMD_DELETE_TODOS) - Todos.actions.dbDeleteTodo(id) + Todos.actions.dbDeleteTodo(item) } } @@ -430,8 +435,8 @@ export default class Todo extends Vue { } - deleteItemToSyncAndDb(table: String, id) { - return this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', null, id, 'Your Post was canceled for syncing!') + deleteItemToSyncAndDb(table: String, item: ITodo, id) { + return this.cmdToSyncAndDb(rescodes.DB.CMD_DELETE_TODOS, table, 'DELETE', item, id, 'Your Post was canceled for syncing!') } /* @@ -483,7 +488,7 @@ export default class Todo extends Vue { this.modify(myobjnext, false) } - this.deleteItemToSyncAndDb(rescodes.DB.TABLE_DELETE_TODOS, id) + this.deleteItemToSyncAndDb(rescodes.DB.TABLE_DELETE_TODOS, myobjtrov, id) const mythis = this // Delete item diff --git a/src/js/objectId.js b/src/js/objectId.js new file mode 100644 index 0000000..d4b36d8 --- /dev/null +++ b/src/js/objectId.js @@ -0,0 +1,10 @@ +function objectId () { + return hex(Date.now() / 1000) + + ' '.repeat(16).replace(/./g, () => hex(Math.random() * 16)) +} + +function hex (value) { + return Math.floor(value).toString(16) +} + +export default objectId diff --git a/src/model/Todos.ts b/src/model/Todos.ts index 6904c38..9868c8b 100644 --- a/src/model/Todos.ts +++ b/src/model/Todos.ts @@ -1,5 +1,5 @@ export interface ITodo { - _id?: string, + _id?: any, userId: string category?: string descr?: string, diff --git a/src/store/Modules/Todos.ts b/src/store/Modules/Todos.ts index 4c6b46d..f118f93 100644 --- a/src/store/Modules/Todos.ts +++ b/src/store/Modules/Todos.ts @@ -90,13 +90,15 @@ namespace Actions { console.log('ITEM', newItem) if (method === 'POST') { state.todos.push(newItem) - } else if (method === 'PATCH') { - state.todos.map(item => { - if (item._id === newItem._id) { - return newItem - } - }) + // } else if (method === 'PATCH') { + // state.todos.map(item => { + // if (item._id === newItem._id) { + // return newItem + // } + // }) } + + console.log('DOPO state.todos', state.todos) } @@ -117,9 +119,9 @@ namespace Actions { if (newItem) { const newId = newItem._id - if (method === 'PATCH') { - newItem = newItem.todo - } + // if (method === 'PATCH') { + // newItem = newItem.todo + // } // Update ID on local UpdateNewIdFromDB(itemtodo, newItem, method) @@ -136,14 +138,18 @@ namespace Actions { return res } - async function dbDeleteTodo(context, id: String) { - console.log('dbDeleteTodo', id) - let call = process.env.MONGODB_HOST + '/todos/' + id + async function dbDeleteTodo(context, item: ITodo) { + console.log('dbDeleteTodo', item) + let call = process.env.MONGODB_HOST + '/todos/' + item._id const token = UserStore.state.idToken - let res = await Api.SendReq(call, UserStore.state.lang, token, 'DELETE', id) + let res = await Api.SendReq(call, UserStore.state.lang, token, 'DELETE', item) .then(function (res) { + + // Delete Item in to Array + state.todos.splice(state.todos.indexOf(item), 1) + return rescodes.OK }) .catch((error) => { diff --git a/src/store/Modules/rescodes.ts b/src/store/Modules/rescodes.ts index 36c4fa8..1549108 100644 --- a/src/store/Modules/rescodes.ts +++ b/src/store/Modules/rescodes.ts @@ -160,7 +160,8 @@ export const rescodes = { checked: false } ] - } + }, + }