- fix: objectId in javascript, and not created by Mongodb

This commit is contained in:
Paolo Arena
2019-02-03 03:44:25 +01:00
parent 5d987013c5
commit 990955b6c2
5 changed files with 44 additions and 22 deletions

View File

@@ -9,6 +9,9 @@ import { rescodes } from '../../../store/Modules/rescodes'
import { Todos } from '@store' import { Todos } from '@store'
import { UserStore } from '@store' import { UserStore } from '@store'
import objectId from '../../../js/objectId.js'
import _ from 'lodash' import _ from 'lodash'
import draggable from 'vuedraggable' import draggable from 'vuedraggable'
@@ -283,7 +286,7 @@ export default class Todo extends Vue {
} }
copy(o) { copy(o) {
var output, v, key let output, v, key
output = Array.isArray(o) ? [] : {} output = Array.isArray(o) ? [] : {}
for (key in o) { for (key in o) {
v = o[key] v = o[key]
@@ -292,6 +295,7 @@ export default class Todo extends Vue {
return output return output
} }
initcat() { initcat() {
@@ -301,7 +305,8 @@ export default class Todo extends Vue {
console.log('User:' + UserStore.state.userId) console.log('User:' + UserStore.state.userId)
const objtodo: ITodo = { const objtodo: ITodo = {
_id: new Date().toISOString(), // Create NEW // _id: new Date().toISOString(), // Create NEW
_id: objectId(),
userId: UserStore.state.userId, userId: UserStore.state.userId,
descr: '', descr: '',
priority: rescodes.Todos.PRIORITY_NORMAL, priority: rescodes.Todos.PRIORITY_NORMAL,
@@ -389,7 +394,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 (false && ('serviceWorker' in navigator && 'SyncManager' in window)) { if (('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(),
@@ -421,7 +426,7 @@ export default class Todo extends Vue {
else if (method === 'PATCH') 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(item)
} }
} }
@@ -430,8 +435,8 @@ export default class Todo extends Vue {
} }
deleteItemToSyncAndDb(table: String, id) { deleteItemToSyncAndDb(table: String, item: ITodo, id) {
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', item, id, 'Your Post was canceled for syncing!')
} }
/* /*
@@ -483,7 +488,7 @@ export default class Todo extends Vue {
this.modify(myobjnext, false) this.modify(myobjnext, false)
} }
this.deleteItemToSyncAndDb(rescodes.DB.TABLE_DELETE_TODOS, id) this.deleteItemToSyncAndDb(rescodes.DB.TABLE_DELETE_TODOS, myobjtrov, id)
const mythis = this const mythis = this
// Delete item // Delete item

10
src/js/objectId.js Normal file
View File

@@ -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

View File

@@ -1,5 +1,5 @@
export interface ITodo { export interface ITodo {
_id?: string, _id?: any,
userId: string userId: string
category?: string category?: string
descr?: string, descr?: string,

View File

@@ -90,13 +90,15 @@ namespace Actions {
console.log('ITEM', newItem) console.log('ITEM', newItem)
if (method === 'POST') { if (method === 'POST') {
state.todos.push(newItem) state.todos.push(newItem)
} else if (method === 'PATCH') { // } else if (method === 'PATCH') {
state.todos.map(item => { // state.todos.map(item => {
if (item._id === newItem._id) { // if (item._id === newItem._id) {
return newItem // return newItem
} // }
}) // })
} }
console.log('DOPO state.todos', state.todos) console.log('DOPO state.todos', state.todos)
} }
@@ -117,9 +119,9 @@ namespace Actions {
if (newItem) { if (newItem) {
const newId = newItem._id const newId = newItem._id
if (method === 'PATCH') { // if (method === 'PATCH') {
newItem = newItem.todo // newItem = newItem.todo
} // }
// Update ID on local // Update ID on local
UpdateNewIdFromDB(itemtodo, newItem, method) UpdateNewIdFromDB(itemtodo, newItem, method)
@@ -136,14 +138,18 @@ namespace Actions {
return res return res
} }
async function dbDeleteTodo(context, id: String) { async function dbDeleteTodo(context, item: ITodo) {
console.log('dbDeleteTodo', id) console.log('dbDeleteTodo', item)
let call = process.env.MONGODB_HOST + '/todos/' + id let call = process.env.MONGODB_HOST + '/todos/' + item._id
const token = UserStore.state.idToken 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) { .then(function (res) {
// Delete Item in to Array
state.todos.splice(state.todos.indexOf(item), 1)
return rescodes.OK return rescodes.OK
}) })
.catch((error) => { .catch((error) => {

View File

@@ -160,7 +160,8 @@ export const rescodes = {
checked: false checked: false
} }
] ]
} },
} }