- fix: objectId in javascript, and not created by Mongodb
This commit is contained in:
@@ -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
|
||||
|
||||
10
src/js/objectId.js
Normal file
10
src/js/objectId.js
Normal 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
|
||||
@@ -1,5 +1,5 @@
|
||||
export interface ITodo {
|
||||
_id?: string,
|
||||
_id?: any,
|
||||
userId: string
|
||||
category?: string
|
||||
descr?: string,
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -160,7 +160,8 @@ export const rescodes = {
|
||||
checked: false
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user