add path to new inbox file in pinia store

Not ideal to have data in multiple store (pinia and PouchDB) but it work this way. May need a refactoring.
This commit is contained in:
Julien Calixte
2023-11-26 10:35:38 +01:00
parent 086f7664df
commit c7ea52c7f8
6 changed files with 33 additions and 6 deletions

View File

@@ -91,6 +91,12 @@ watch(mode, async (newMode) => {
if (newMode === 'read' && rawContent.value !== initialRawContent.value) { if (newMode === 'read' && rawContent.value !== initialRawContent.value) {
const editedSha = (await getEditedSha()) ?? sha.value const editedSha = (await getEditedSha()) ?? sha.value
if (!path.value) {
console.warn('no path found for this file')
return
}
const newSha = await updateFile({ const newSha = await updateFile({
content: rawContent.value, content: rawContent.value,
path: path.value, path: path.value,
@@ -98,10 +104,14 @@ watch(mode, async (newMode) => {
}) })
if (!newSha) { if (!newSha) {
console.warn('no new SHA found for this file')
return return
} }
await saveCacheNote(encodeUTF8ToBase64(rawContent.value), newSha) await saveCacheNote(encodeUTF8ToBase64(rawContent.value), {
editedSha: newSha
})
initialRawContent.value = rawContent.value initialRawContent.value = rawContent.value
} }
}) })

View File

@@ -10,7 +10,7 @@ export const useFile = (sha: Ref<string> | string, retrieveContent = true) => {
const path = computed(() => { const path = computed(() => {
const file = store.files.find((file) => file.sha === toValue(sha)) const file = store.files.find((file) => file.sha === toValue(sha))
return file?.path ?? '' return file?.path
}) })
const { const {

View File

@@ -1,19 +1,30 @@
import { data } from '@/data/data' import { data } from '@/data/data'
import { DataType } from '@/data/DataType.enum' import { DataType } from '@/data/DataType.enum'
import { Note } from '@/modules/note/models/Note' import { Note } from '@/modules/note/models/Note'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
export const prepareNoteCache = (sha: string) => { export const prepareNoteCache = (sha: string) => {
const store = useUserRepoStore()
const noteId = data.generateId(DataType.Note, sha) const noteId = data.generateId(DataType.Note, sha)
const getCachedNote = async () => data.get<DataType.Note, Note>(noteId) const getCachedNote = async () => data.get<DataType.Note, Note>(noteId)
const saveCacheNote = async (content: string, editedSha?: string) => { const saveCacheNote = async (
content: string,
params?: { editedSha?: string; path?: string }
) => {
const newNote: Note = { const newNote: Note = {
_id: noteId, _id: noteId,
$type: DataType.Note, $type: DataType.Note,
content, content,
editedSha editedSha: params?.editedSha
} }
store.addFile({
path: params?.path,
sha: params?.editedSha
})
await data.update(newNote) await data.update(newNote)
} }

View File

@@ -86,6 +86,9 @@ export const useUserRepoStore = defineStore({
}, 350) }, 350)
}) })
}, },
addFile(file: RepoFile) {
this.files = [...this.files.filter((f) => f.sha !== file.sha), file]
},
resetUserRepo() { resetUserRepo() {
this.user = '' this.user = ''
this.repo = '' this.repo = ''

View File

@@ -50,7 +50,10 @@ watch(mode, async (newMode) => {
newContent.value = initialContent newContent.value = initialContent
const { saveCacheNote } = prepareNoteCache(newSha) const { saveCacheNote } = prepareNoteCache(newSha)
await saveCacheNote(encodeUTF8ToBase64(content)) await saveCacheNote(encodeUTF8ToBase64(content), {
editedSha: newSha,
path: newContentPath
})
addStackedNote('', newSha) addStackedNote('', newSha)
} }