Reworked workspaces store

This commit is contained in:
Julien Nahum
2023-12-19 18:57:31 +01:00
parent aac4d1da04
commit 5640f43b9d
17 changed files with 210 additions and 282 deletions

View File

@@ -36,26 +36,29 @@ export const useTemplatesStore = defineStore('templates', () => {
...contentStore,
industries,
types,
allLoaded,
getTemplateTypes,
getTemplateIndustries,
initTypesAndIndustries
}
})
export const fetchTemplate = (slug) => {
return useOpnApi(templatesEndpoint + '/' + slug)
export const fetchTemplate = (slug, options = {}) => {
return useOpnApi(templatesEndpoint + '/' + slug, options)
}
export const fetchAllTemplates = () => {
return useOpnApi(templatesEndpoint)
export const fetchAllTemplates = (options = {}) => {
return useOpnApi(templatesEndpoint, options)
}
export const loadAllTemplates = async (store) => {
export const loadAllTemplates = async (store, options={}) => {
console.log('in------',store, store.allLoaded)
if (!store.allLoaded) {
store.startLoading()
store.initTypesAndIndustries()
const {data} = await fetchAllTemplates()
const {data} = await fetchAllTemplates(options)
store.set(data.value)
store.stopLoading()
store.allLoaded = true
}
}

View File

@@ -1,93 +1,50 @@
import {defineStore} from 'pinia'
import {useStorage} from "@vueuse/core"
import {useContentStore} from "~/composables/stores/useContentStore.js";
export const workspaceEndpoint = 'open/workspaces/'
const storedWorkspaceId = useStorage('currentWorkspace', 0)
export const useWorkspacesStore = defineStore('workspaces', () => {
export const useWorkspacesStore = defineStore('workspaces', {
state: () => ({
content: new Map,
currentId: null,
loading: false
}),
getters: {
getById: (state) => (id) => {
if (state.content.length === 0) return null
return state.content.find(item => item.id === id)
},
getCurrent (){
if (this.content.length === 0 || this.currentId === null) return null
return this.content.find(item => item.id === this.currentId)
}
},
actions: {
set(items) {
this.content = items
if (this.currentId == null && this.content.length > 0) {
// If one only, set it
if (this.content.length === 1) {
this.setCurrentId(items[0].id)
} else if (storedWorkspaceId && this.content.find(item => item.id === parseInt(storedWorkspaceId.value))) {
// Check local storage for current workspace, or take first
this.setCurrentId(parseInt(storedWorkspaceId.value))
} else {
// Else, take first
this.setCurrentId(items[0].id)
}
} else {
this.setCurrentId(null)
}
},
setCurrentId(id) {
this.currentId = id
storedWorkspaceId.value = id
},
addOrUpdate(item) {
this.content = this.content.filter((val) => val.id !== item.id)
this.content.push(item)
if (this.currentId == null) {
this.currentId = item.id
storedWorkspaceId.value = this.currentId
}
},
remove(itemId) {
this.content = this.content.filter((val) => val.id !== itemId)
if (this.currentId === itemId) {
this.setCurrentId(this.content.length > 0 ? this.content[0].id : null)
}
},
startLoading() {
this.loading = true
},
stopLoading() {
this.loading = false
},
resetState() {
this.set([])
this.stopLoading()
},
load() {
this.set([])
this.startLoading()
console.log('loaindgworkspaces')
return useOpnApi(workspaceEndpoint).then(({data, error}) => {
this.set(data.value)
this.stopLoading()
})
},
loadIfEmpty() {
if (this.content.length === 0) {
return this.load()
}
return Promise.resolve()
},
delete(id) {
this.startLoading()
return useOpnApi(workspaceEndpoint + id, {method: 'DELETE'}).then(({data}) => {
this.remove(data.value.workspace_id)
this.stopLoading()
})
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 save = (items) => {
contentStore.save(items)
console.log('cookie issi', currentId.value, contentStore.length.value)
if (currentId.value == null && contentStore.length.value) {
setCurrentId(items[0].id)
}
}
const remove = (itemId) => {
contentStore.remove(itemId)
if (currentId.value === itemId) {
setCurrentId(contentStore.length.value > 0 ? contentStore.getAll[0].id : null)
}
}
return {
...contentStore,
currentId,
getCurrent,
setCurrentId,
save,
remove
}
})
export const fetchAllWorkspaces = (options = {}) => {
return useOpnApi(workspaceEndpoint, options)
}