2023-12-14 15:53:05 +00:00
|
|
|
import {defineStore} from 'pinia'
|
|
|
|
import {useStorage} from "@vueuse/core"
|
2023-12-09 14:47:03 +00:00
|
|
|
|
2023-12-14 15:53:05 +00:00
|
|
|
export const workspaceEndpoint = 'open/workspaces/'
|
|
|
|
|
|
|
|
const storedWorkspaceId = useStorage('currentWorkspace', 0)
|
2023-12-09 14:47:03 +00:00
|
|
|
|
|
|
|
export const useWorkspacesStore = defineStore('workspaces', {
|
|
|
|
state: () => ({
|
2023-12-19 12:46:55 +00:00
|
|
|
content: new Map,
|
2023-12-09 14:47:03 +00:00
|
|
|
currentId: null,
|
|
|
|
loading: false
|
|
|
|
}),
|
|
|
|
getters: {
|
|
|
|
getById: (state) => (id) => {
|
|
|
|
if (state.content.length === 0) return null
|
|
|
|
return state.content.find(item => item.id === id)
|
|
|
|
},
|
2023-12-16 18:21:03 +00:00
|
|
|
getCurrent (){
|
|
|
|
if (this.content.length === 0 || this.currentId === null) return null
|
|
|
|
return this.content.find(item => item.id === this.currentId)
|
2023-12-09 14:47:03 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
2023-12-14 15:53:05 +00:00
|
|
|
set(items) {
|
2023-12-09 14:47:03 +00:00
|
|
|
this.content = items
|
|
|
|
if (this.currentId == null && this.content.length > 0) {
|
|
|
|
// If one only, set it
|
|
|
|
if (this.content.length === 1) {
|
2023-12-14 15:53:05 +00:00
|
|
|
this.setCurrentId(items[0].id)
|
|
|
|
} else if (storedWorkspaceId && this.content.find(item => item.id === parseInt(storedWorkspaceId.value))) {
|
2023-12-09 14:47:03 +00:00
|
|
|
// Check local storage for current workspace, or take first
|
2023-12-14 15:53:05 +00:00
|
|
|
this.setCurrentId(parseInt(storedWorkspaceId.value))
|
2023-12-09 14:47:03 +00:00
|
|
|
} else {
|
|
|
|
// Else, take first
|
2023-12-14 15:53:05 +00:00
|
|
|
this.setCurrentId(items[0].id)
|
2023-12-09 14:47:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
2023-12-14 15:53:05 +00:00
|
|
|
this.setCurrentId(null)
|
2023-12-09 14:47:03 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-14 15:53:05 +00:00
|
|
|
setCurrentId(id) {
|
2023-12-09 14:47:03 +00:00
|
|
|
this.currentId = id
|
2023-12-14 15:53:05 +00:00
|
|
|
storedWorkspaceId.value = id
|
2023-12-09 14:47:03 +00:00
|
|
|
},
|
2023-12-14 15:53:05 +00:00
|
|
|
addOrUpdate(item) {
|
2023-12-09 14:47:03 +00:00
|
|
|
this.content = this.content.filter((val) => val.id !== item.id)
|
|
|
|
this.content.push(item)
|
|
|
|
if (this.currentId == null) {
|
|
|
|
this.currentId = item.id
|
2023-12-14 15:53:05 +00:00
|
|
|
storedWorkspaceId.value = this.currentId
|
2023-12-09 14:47:03 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-14 15:53:05 +00:00
|
|
|
remove(itemId) {
|
2023-12-09 14:47:03 +00:00
|
|
|
this.content = this.content.filter((val) => val.id !== itemId)
|
|
|
|
if (this.currentId === itemId) {
|
2023-12-14 15:53:05 +00:00
|
|
|
this.setCurrentId(this.content.length > 0 ? this.content[0].id : null)
|
2023-12-09 14:47:03 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-14 15:53:05 +00:00
|
|
|
startLoading() {
|
2023-12-09 14:47:03 +00:00
|
|
|
this.loading = true
|
|
|
|
},
|
2023-12-14 15:53:05 +00:00
|
|
|
stopLoading() {
|
2023-12-09 14:47:03 +00:00
|
|
|
this.loading = false
|
|
|
|
},
|
2023-12-14 15:53:05 +00:00
|
|
|
resetState() {
|
2023-12-09 14:47:03 +00:00
|
|
|
this.set([])
|
|
|
|
this.stopLoading()
|
|
|
|
},
|
2023-12-14 15:53:05 +00:00
|
|
|
load() {
|
2023-12-09 14:47:03 +00:00
|
|
|
this.set([])
|
|
|
|
this.startLoading()
|
2023-12-19 12:46:55 +00:00
|
|
|
console.log('loaindgworkspaces')
|
2023-12-16 18:21:03 +00:00
|
|
|
return useOpnApi(workspaceEndpoint).then(({data, error}) => {
|
|
|
|
this.set(data.value)
|
2023-12-09 14:47:03 +00:00
|
|
|
this.stopLoading()
|
|
|
|
})
|
|
|
|
},
|
2023-12-14 15:53:05 +00:00
|
|
|
loadIfEmpty() {
|
2023-12-09 14:47:03 +00:00
|
|
|
if (this.content.length === 0) {
|
|
|
|
return this.load()
|
|
|
|
}
|
|
|
|
return Promise.resolve()
|
|
|
|
},
|
2023-12-14 15:53:05 +00:00
|
|
|
delete(id) {
|
2023-12-09 14:47:03 +00:00
|
|
|
this.startLoading()
|
2023-12-16 18:21:03 +00:00
|
|
|
return useOpnApi(workspaceEndpoint + id, {method: 'DELETE'}).then(({data}) => {
|
|
|
|
this.remove(data.value.workspace_id)
|
2023-12-09 14:47:03 +00:00
|
|
|
this.stopLoading()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|