try catch json parse
This commit is contained in:
@@ -6,21 +6,33 @@ import { getAllMatches } from '@/utils/helpers'
|
||||
import { initialized, user } from '@/composables/useFirebase'
|
||||
import shortid from 'shortid'
|
||||
|
||||
const notesSource = computed<'firebase' | 'local' | null>(() => {
|
||||
const notesSources = computed(() => ({
|
||||
local: true,
|
||||
firebase: initialized.value && user.value
|
||||
}))
|
||||
|
||||
type notesSourceValues = keyof typeof notesSources.value | null
|
||||
|
||||
const activeNotesSource = ref<notesSourceValues>(null)
|
||||
|
||||
watchEffect(() => {
|
||||
const getSource = (): notesSourceValues => {
|
||||
if (!initialized.value) return null
|
||||
return user.value ? 'firebase' : 'local'
|
||||
}
|
||||
activeNotesSource.value = getSource()
|
||||
})
|
||||
|
||||
const baseNotes = ref<{ [noteId: string]: BaseNote }>({})
|
||||
watch(
|
||||
baseNotes,
|
||||
() => {
|
||||
console.log(`Sync base notes with ${notesSource.value}`, baseNotes.value)
|
||||
if (!notesSource.value) return
|
||||
if (notesSource.value === 'local') {
|
||||
console.log(`Sync base notes with ${activeNotesSource.value}`, baseNotes.value)
|
||||
if (!activeNotesSource.value) return
|
||||
if (activeNotesSource.value === 'local') {
|
||||
console.log('sync with local')
|
||||
localStorage.setItem('notes', JSON.stringify(baseNotes.value))
|
||||
} else if (notesSource.value === 'firebase') {
|
||||
} else if (activeNotesSource.value === 'firebase') {
|
||||
console.log('sync with firebase')
|
||||
}
|
||||
},
|
||||
@@ -177,14 +189,18 @@ export function getNoteReferences(note: Note) {
|
||||
}
|
||||
|
||||
watch(
|
||||
notesSource,
|
||||
activeNotesSource,
|
||||
() => {
|
||||
if (!notesSource.value) return
|
||||
if (!activeNotesSource.value) return
|
||||
baseNotes.value = {}
|
||||
if (notesSource.value === 'local') {
|
||||
if (activeNotesSource.value === 'local') {
|
||||
try {
|
||||
const localNotes = JSON.parse(localStorage.getItem('notes') || '{}')
|
||||
baseNotes.value = localNotes
|
||||
} else if (notesSource.value === 'firebase') {
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
} else if (activeNotesSource.value === 'firebase') {
|
||||
console.log('get notes from firebase')
|
||||
}
|
||||
if (Object.entries(baseNotes.value).length === 0) {
|
||||
|
||||
Reference in New Issue
Block a user