delete account
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import shortid from 'shortid'
|
||||
import { useTitle } from '@vueuse/core'
|
||||
import { doc, getDoc, updateDoc, deleteField } from 'firebase/firestore'
|
||||
import { doc, getDoc, setDoc } from 'firebase/firestore'
|
||||
import { viewModes, activeViewMode } from '@/composables/useViewMode'
|
||||
import { initialized, user, db } from '@/composables/useFirebase'
|
||||
import { decryptNotes, encryptNotes, encryptionKey } from '@/composables/useEncryption'
|
||||
@@ -45,23 +45,22 @@ const syncNotesLocal = (notes: BaseNotes) => {
|
||||
localStorage.setItem('notes', JSON.stringify(notes))
|
||||
}
|
||||
|
||||
export const syncNotesToFirebase = async (newNotes: BaseNotes, oldNotes?: BaseNotes) => {
|
||||
// export const syncNotesToFirebase = async (newNotes: BaseNotes, oldNotes?: BaseNotes) => {
|
||||
export const syncNotesToFirebase = async (baseNotes: BaseNotes) => {
|
||||
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 notes = encryptionKey.value ? encryptNotes(baseNotes, encryptionKey.value) : baseNotes
|
||||
try {
|
||||
const docRef = doc(db.value, 'pages', user.value.uid)
|
||||
if (oldNotes) {
|
||||
const notesToDelete = Object.keys(oldNotes).filter((x) => !Object.keys(newNotes).includes(x))
|
||||
await Promise.all(
|
||||
notesToDelete.map((noteId: string) => {
|
||||
return updateDoc(docRef, { [noteId]: deleteField() })
|
||||
})
|
||||
)
|
||||
}
|
||||
await updateDoc(docRef, notes)
|
||||
// if (oldNotes) {
|
||||
// const notesToDelete = Object.keys(oldNotes).filter((x) => !Object.keys(newNotes).includes(x))
|
||||
// await Promise.all(
|
||||
// notesToDelete.map((noteId: string) => {
|
||||
// return updateDoc(docRef, { [noteId]: deleteField() })
|
||||
// })
|
||||
// )
|
||||
// }
|
||||
await setDoc(docRef, notes)
|
||||
} catch (error: any) {
|
||||
console.error(error)
|
||||
}
|
||||
@@ -69,12 +68,12 @@ export const syncNotesToFirebase = async (newNotes: BaseNotes, oldNotes?: BaseNo
|
||||
|
||||
watch(
|
||||
baseNotes,
|
||||
async (newBaseNotes, oldBaseNotes) => {
|
||||
async () => {
|
||||
if (!activeNotesSource.value || Object.keys(baseNotes.value).length === 0) return
|
||||
if (activeNotesSource.value === 'local') {
|
||||
syncNotesLocal(baseNotes.value)
|
||||
} else if (activeNotesSource.value === 'firebase') {
|
||||
syncNotesToFirebase(newBaseNotes, oldBaseNotes)
|
||||
syncNotesToFirebase(baseNotes.value)
|
||||
}
|
||||
},
|
||||
{ deep: true }
|
||||
@@ -262,8 +261,9 @@ export const getNotes = async () => {
|
||||
} else if (activeNotesSource.value === 'firebase') {
|
||||
if (encryptionKey.value === undefined || !user.value || !db.value) return
|
||||
const firebaseNotes = (await getDoc(doc(db.value, 'pages', user.value.uid))).data() as BaseNotes
|
||||
notes = encryptionKey.value ? decryptNotes(firebaseNotes, encryptionKey.value) : firebaseNotes
|
||||
console.log(encryptionKey.value)
|
||||
notes = encryptionKey.value
|
||||
? decryptNotes(firebaseNotes, encryptionKey.value)
|
||||
: firebaseNotes || {}
|
||||
console.log('get notes from firebase', notes)
|
||||
}
|
||||
baseNotes.value = parseBaseNotes(notes)
|
||||
|
||||
Reference in New Issue
Block a user