opnform/client/stores/workspaces.js

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-12-14 15:53:05 +00:00
import {defineStore} from 'pinia'
import {useStorage} from "@vueuse/core"
2023-12-19 17:57:31 +00:00
import {useContentStore} from "~/composables/stores/useContentStore.js";
2023-12-09 14:47:03 +00:00
2023-12-14 15:53:05 +00:00
export const workspaceEndpoint = 'open/workspaces/'
2023-12-19 17:57:31 +00:00
export const useWorkspacesStore = defineStore('workspaces', () => {
2023-12-09 14:47:03 +00:00
2023-12-19 17:57:31 +00:00
const storedWorkspaceId = useCookie('currentWorkspace')
const contentStore = useContentStore()
const currentId = ref(storedWorkspaceId)
const getCurrent = computed(() => {
return contentStore.getByKey(currentId.value)
})
const setCurrentId = (id) => {
currentId.value = id
storedWorkspaceId.value = id
}
const set = (items) => {
contentStore.content.value = new Map
save(items)
}
2023-12-19 17:57:31 +00:00
const save = (items) => {
contentStore.save(items)
2023-12-24 08:51:22 +00:00
if ((getCurrent.value == null) && contentStore.length.value) {
2023-12-19 17:57:31 +00:00
setCurrentId(items[0].id)
2023-12-09 14:47:03 +00:00
}
2023-12-19 17:57:31 +00:00
}
const remove = (itemId) => {
contentStore.remove(itemId)
if (currentId.value === itemId) {
setCurrentId(contentStore.length.value > 0 ? contentStore.getAll.value[0].id : null)
2023-12-09 14:47:03 +00:00
}
}
2023-12-19 17:57:31 +00:00
return {
...contentStore,
currentId,
getCurrent,
setCurrentId,
set,
2023-12-19 17:57:31 +00:00
save,
remove
}
2023-12-09 14:47:03 +00:00
})
2023-12-19 17:57:31 +00:00
export const fetchAllWorkspaces = (options = {}) => {
return useOpnApi(workspaceEndpoint, options)
}