delete notes & sync to firebase

This commit is contained in:
2023-05-25 22:29:40 +02:00
parent 5a4bba2dcd
commit f6e5d5ca4f
10 changed files with 64 additions and 66 deletions

View File

@@ -1,9 +1,9 @@
import shortid from 'shortid'
import { useTitle } from '@vueuse/core'
import { doc, getDoc } from 'firebase/firestore'
import { doc, getDoc, updateDoc, deleteField } from 'firebase/firestore'
import { viewModes, activeViewMode } from '@/composables/useViewMode'
import { initialized, user, db } from '@/composables/useFirebase'
import { decryptNotes, encryptionKey } from '@/composables/useEncryption'
import { decryptNotes, encryptNotes, encryptionKey } from '@/composables/useEncryption'
import { defaultNotes } from '@/utils/defaultNotes'
import { mdToHtml } from '@/utils/markdown'
import { getAllMatches } from '@/utils/helpers'
@@ -39,18 +39,37 @@ watchEffect(() => {
activeNotesSource.value = getSource()
})
const baseNotes = ref<{ [noteId: string]: BaseNote }>({})
const baseNotes = ref<BaseNotes>({})
watch(
baseNotes,
() => {
async (newBaseNotes, oldBaseNotes) => {
if (!activeNotesSource.value || Object.keys(baseNotes.value).length === 0) return
console.log()
if (activeNotesSource.value === 'local') {
console.log('sync with local')
localStorage.setItem('notes', JSON.stringify(baseNotes.value))
} else if (activeNotesSource.value === 'firebase') {
console.log('sync with firebase')
if (!db.value) throw Error("Database undefined, can't sync to Firebase")
if (!user.value) throw Error("User undefined, can't sync to Firebase")
const notes = encryptionKey.value
? encryptNotes(baseNotes.value, encryptionKey.value)
: baseNotes.value
const notesToDelete = Object.keys(oldBaseNotes).filter(
(x) => !Object.keys(newBaseNotes).includes(x)
)
try {
const docRef = doc(db.value, 'pages', user.value.uid)
await Promise.all(
notesToDelete.map((noteId: string) => {
return updateDoc(docRef, { [noteId]: deleteField() })
})
)
await updateDoc(docRef, notes)
} catch (error: any) {
console.error(error)
}
}
console.log(`Sync base notes with ${activeNotesSource.value}`, baseNotes.value)
},
{ deep: true }
)
@@ -144,6 +163,7 @@ export const addNote = (title: string, content: string, goToNote: boolean = fals
id,
title,
content,
isRoot: false,
created: new Date().getTime(),
modified: new Date().getTime()
}
@@ -153,7 +173,9 @@ export const addNote = (title: string, content: string, goToNote: boolean = fals
}
export const deleteNote = (noteId: string) => {
delete baseNotes.value[noteId]
const baseNotesClone: BaseNotes = structuredClone(toRaw(baseNotes.value))
delete baseNotesClone[noteId]
baseNotes.value = baseNotesClone
}
const getNoteLinksByNoteId = (noteId: string): string[] => {
@@ -213,9 +235,9 @@ const parseBaseNotes = (notes: BaseNotes): BaseNotes => {
id: noteId,
title: note.title,
content: note.content,
created: note.created,
modified: note.modified,
isRoot: note.isRoot
isRoot: Boolean(note.isRoot),
created: note.created || note.modified || new Date().getTime(),
modified: note.modified || note.created || new Date().getTime()
}
]
})
@@ -235,10 +257,10 @@ watch(
console.log(error)
}
} else if (activeNotesSource.value === 'firebase' && db.value) {
if (encryptionKey.value === undefined) return
const firebaseNotes = (
await getDoc(doc(db.value, 'pages', user.value?.uid || ''))
).data() as { [noteId: string]: any }
if (encryptionKey.value === undefined || !user.value) return
const firebaseNotes = (await getDoc(doc(db.value, 'pages', user.value.uid))).data() as {
[noteId: string]: any
}
notes = encryptionKey.value ? decryptNotes(firebaseNotes, encryptionKey.value) : firebaseNotes
console.log('get notes from firebase', notes)
}