|
@@ -1,6 +1,5 @@
|
|
|
import { ref } from 'vue'
|
|
|
-import { createTemploraryID } from './sys'
|
|
|
-import { autoSetModeCallback } from './sys'
|
|
|
+import { autoSetModeCallback, createTemploraryID } from './sys'
|
|
|
import {
|
|
|
fetchGuides,
|
|
|
postAddGuide,
|
|
@@ -9,14 +8,23 @@ import {
|
|
|
uploadFile,
|
|
|
} from '@/api'
|
|
|
import {
|
|
|
+ togetherCallback,
|
|
|
deleteStoreItem,
|
|
|
addStoreItem,
|
|
|
updateStoreItem,
|
|
|
- fetchStoreItems,
|
|
|
saveStoreItems,
|
|
|
recoverStoreItems
|
|
|
} from '@/utils'
|
|
|
|
|
|
+import {
|
|
|
+ getGuidePaths,
|
|
|
+ guidePaths,
|
|
|
+ recoverGuidePaths,
|
|
|
+ backupGuidePaths,
|
|
|
+ saveGuidePaths,
|
|
|
+ initialGuidePathsByGuide
|
|
|
+} from './guide-path'
|
|
|
+
|
|
|
import type { Guide as SGuide } from '@/api'
|
|
|
|
|
|
export type Guide = LocalMode<SGuide, 'cover'>
|
|
@@ -42,11 +50,27 @@ export const transformGuide = async (guide: Guide): Promise<SGuide> => {
|
|
|
return { ...guide, cover: guideCover }
|
|
|
}
|
|
|
|
|
|
+export const addGuide = addStoreItem(guides, async (guide) => {
|
|
|
+ const paths = getGuidePaths(guide)
|
|
|
+ const newGuide = await postAddGuide(guide)
|
|
|
+ paths.forEach(path => path.guideId = newGuide.id)
|
|
|
+ return newGuide
|
|
|
+}, transformGuide)
|
|
|
+
|
|
|
+export const deleteGuide = deleteStoreItem(guides, async guide => {
|
|
|
+ const paths = getGuidePaths(guide)
|
|
|
+ await postDeleteGuide(guide.id)
|
|
|
+ guidePaths.value = guidePaths.value.filter(path => !paths.includes(path))
|
|
|
+})
|
|
|
+
|
|
|
+export const initialGuides = async () => {
|
|
|
+ guides.value = await fetchGuides()
|
|
|
+ await Promise.all(guides.value.map(initialGuidePathsByGuide))
|
|
|
+ backupGuides()
|
|
|
+}
|
|
|
+
|
|
|
export const recoverGuides = recoverStoreItems(guides, getBackupGuides)
|
|
|
-export const addGuide = addStoreItem(guides, postAddGuide, transformGuide)
|
|
|
export const updateGuide = updateStoreItem(guides, postUpdateGuide, transformGuide)
|
|
|
-export const deleteGuide = deleteStoreItem(guides, guide => postDeleteGuide(guide.id))
|
|
|
-export const initialGuides = fetchStoreItems(guides, fetchGuides, backupGuides)
|
|
|
export const saveGuides = saveStoreItems(
|
|
|
guides,
|
|
|
getBackupGuides,
|
|
@@ -56,8 +80,11 @@ export const saveGuides = saveStoreItems(
|
|
|
delete: deleteGuide,
|
|
|
}
|
|
|
)
|
|
|
-export const autoSaveGuides = autoSetModeCallback(guides, {
|
|
|
- backup: backupGuides,
|
|
|
- recovery: recoverGuides,
|
|
|
- save: saveGuides,
|
|
|
+export const autoSaveGuides = autoSetModeCallback([guides, guidePaths], {
|
|
|
+ backup: togetherCallback([backupGuides, backupGuidePaths]),
|
|
|
+ recovery: togetherCallback([recoverGuides, recoverGuidePaths]),
|
|
|
+ save: async () => {
|
|
|
+ await saveGuides()
|
|
|
+ await saveGuidePaths()
|
|
|
+ },
|
|
|
})
|