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 { initialized, user } from '@/composables/useFirebase'
|
||||||
import shortid from 'shortid'
|
import shortid from 'shortid'
|
||||||
|
|
||||||
const notesSource = computed<'firebase' | 'local' | null>(() => {
|
const notesSources = computed(() => ({
|
||||||
if (!initialized.value) return null
|
local: true,
|
||||||
return user.value ? 'firebase' : 'local'
|
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 }>({})
|
const baseNotes = ref<{ [noteId: string]: BaseNote }>({})
|
||||||
watch(
|
watch(
|
||||||
baseNotes,
|
baseNotes,
|
||||||
() => {
|
() => {
|
||||||
console.log(`Sync base notes with ${notesSource.value}`, baseNotes.value)
|
console.log(`Sync base notes with ${activeNotesSource.value}`, baseNotes.value)
|
||||||
if (!notesSource.value) return
|
if (!activeNotesSource.value) return
|
||||||
if (notesSource.value === 'local') {
|
if (activeNotesSource.value === 'local') {
|
||||||
console.log('sync with local')
|
console.log('sync with local')
|
||||||
localStorage.setItem('notes', JSON.stringify(baseNotes.value))
|
localStorage.setItem('notes', JSON.stringify(baseNotes.value))
|
||||||
} else if (notesSource.value === 'firebase') {
|
} else if (activeNotesSource.value === 'firebase') {
|
||||||
console.log('sync with firebase')
|
console.log('sync with firebase')
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -177,14 +189,18 @@ export function getNoteReferences(note: Note) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
notesSource,
|
activeNotesSource,
|
||||||
() => {
|
() => {
|
||||||
if (!notesSource.value) return
|
if (!activeNotesSource.value) return
|
||||||
baseNotes.value = {}
|
baseNotes.value = {}
|
||||||
if (notesSource.value === 'local') {
|
if (activeNotesSource.value === 'local') {
|
||||||
const localNotes = JSON.parse(localStorage.getItem('notes') || '{}')
|
try {
|
||||||
baseNotes.value = localNotes
|
const localNotes = JSON.parse(localStorage.getItem('notes') || '{}')
|
||||||
} else if (notesSource.value === 'firebase') {
|
baseNotes.value = localNotes
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
}
|
||||||
|
} else if (activeNotesSource.value === 'firebase') {
|
||||||
console.log('get notes from firebase')
|
console.log('get notes from firebase')
|
||||||
}
|
}
|
||||||
if (Object.entries(baseNotes.value).length === 0) {
|
if (Object.entries(baseNotes.value).length === 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user