opnform-host-nginx/client/stores/workspaces.js

56 lines
1.2 KiB
JavaScript
Raw Normal View History

import { defineStore } from "pinia"
import { useContentStore } from "~/composables/stores/useContentStore.js"
2023-12-09 15:47:03 +01:00
export const workspaceEndpoint = "open/workspaces/"
2023-12-14 16:53:05 +01:00
export const useWorkspacesStore = defineStore("workspaces", () => {
const storedWorkspaceId = useCookie("currentWorkspace")
2023-12-19 18:57:31 +01:00
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 18:57:31 +01:00
const save = (items) => {
contentStore.save(items)
if (getCurrent.value == null && contentStore.length.value) {
2023-12-19 18:57:31 +01:00
setCurrentId(items[0].id)
2023-12-09 15:47:03 +01:00
}
2023-12-19 18:57:31 +01: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 15:47:03 +01:00
}
}
2023-12-19 18:57:31 +01:00
return {
...contentStore,
currentId,
getCurrent,
setCurrentId,
set,
2023-12-19 18:57:31 +01:00
save,
remove,
2023-12-19 18:57:31 +01:00
}
2023-12-09 15:47:03 +01:00
})
2023-12-19 18:57:31 +01:00
export const fetchAllWorkspaces = (options = {}) => {
return useOpnApi(workspaceEndpoint, options)
}