2022-09-02 11:15:29 +02:00
|
|
|
import {defineStore, acceptHMRUpdate} from 'pinia'
|
2022-07-21 00:42:36 +02:00
|
|
|
|
2022-09-02 10:20:25 +02:00
|
|
|
import NamespaceService from '../services/namespace'
|
2022-09-02 11:15:29 +02:00
|
|
|
import {setLoadingPinia} from '@/store/helper'
|
2021-11-14 21:49:52 +01:00
|
|
|
import {createNewIndexer} from '@/indexes'
|
2022-09-02 11:15:29 +02:00
|
|
|
import type {NamespaceState} from '@/store/types'
|
2022-08-04 20:57:43 +02:00
|
|
|
import type {INamespace} from '@/modelTypes/INamespace'
|
|
|
|
import type {IList} from '@/modelTypes/IList'
|
2022-09-21 18:21:25 +02:00
|
|
|
import {useListStore} from '@/stores/lists'
|
2021-11-14 21:49:52 +01:00
|
|
|
|
|
|
|
const {add, remove, search, update} = createNewIndexer('namespaces', ['title', 'description'])
|
2020-05-08 20:43:51 +02:00
|
|
|
|
2022-09-02 11:15:29 +02:00
|
|
|
export const useNamespaceStore = defineStore('namespace', {
|
|
|
|
state: (): NamespaceState => ({
|
|
|
|
isLoading: false,
|
|
|
|
// FIXME: should be object with id as key
|
2020-05-08 20:43:51 +02:00
|
|
|
namespaces: [],
|
|
|
|
}),
|
2022-09-02 11:15:29 +02:00
|
|
|
getters: {
|
|
|
|
getListAndNamespaceById: (state) => (listId: IList['id'], ignorePseudoNamespaces = false) => {
|
|
|
|
for (const n in state.namespaces) {
|
|
|
|
|
|
|
|
if (ignorePseudoNamespaces && state.namespaces[n].id < 0) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const l in state.namespaces[n].lists) {
|
|
|
|
if (state.namespaces[n].lists[l].id === listId) {
|
|
|
|
return {
|
|
|
|
list: state.namespaces[n].lists[l],
|
|
|
|
namespace: state.namespaces[n],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
},
|
|
|
|
|
|
|
|
getNamespaceById: state => (namespaceId: INamespace['id']) => {
|
|
|
|
return state.namespaces.find(({id}) => id == namespaceId) || null
|
|
|
|
},
|
|
|
|
|
|
|
|
searchNamespace() {
|
|
|
|
return (query: string) => (
|
|
|
|
search(query)
|
|
|
|
?.filter(value => value > 0)
|
|
|
|
.map(this.getNamespaceById)
|
|
|
|
.filter(n => n !== null)
|
|
|
|
|| []
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
setIsLoading(isLoading: boolean) {
|
|
|
|
this.isLoading = isLoading
|
|
|
|
},
|
|
|
|
|
|
|
|
setNamespaces(namespaces: INamespace[]) {
|
|
|
|
this.namespaces = namespaces
|
2021-11-14 21:49:52 +01:00
|
|
|
namespaces.forEach(n => {
|
|
|
|
add(n)
|
|
|
|
})
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2022-09-02 11:15:29 +02:00
|
|
|
|
|
|
|
setNamespaceById(namespace: INamespace) {
|
|
|
|
const namespaceIndex = this.namespaces.findIndex(n => n.id === namespace.id)
|
2021-09-24 20:51:43 +02:00
|
|
|
|
2021-08-23 21:24:52 +02:00
|
|
|
if (namespaceIndex === -1) {
|
|
|
|
return
|
2020-05-08 20:43:51 +02:00
|
|
|
}
|
2021-09-24 20:51:43 +02:00
|
|
|
|
|
|
|
if (!namespace.lists || namespace.lists.length === 0) {
|
2022-09-02 11:15:29 +02:00
|
|
|
namespace.lists = this.namespaces[namespaceIndex].lists
|
2021-09-24 20:51:43 +02:00
|
|
|
}
|
2021-11-14 21:49:52 +01:00
|
|
|
|
2022-09-02 11:15:29 +02:00
|
|
|
this.namespaces[namespaceIndex] = namespace
|
2021-11-14 21:49:52 +01:00
|
|
|
update(namespace)
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2022-09-02 11:15:29 +02:00
|
|
|
|
|
|
|
setListInNamespaceById(list: IList) {
|
|
|
|
for (const n in this.namespaces) {
|
2020-05-08 20:43:51 +02:00
|
|
|
// We don't have the namespace id on the list which means we need to loop over all lists until we find it.
|
|
|
|
// FIXME: Not ideal at all - we should fix that at the api level.
|
2022-09-02 11:15:29 +02:00
|
|
|
if (this.namespaces[n].id === list.namespaceId) {
|
|
|
|
for (const l in this.namespaces[n].lists) {
|
|
|
|
if (this.namespaces[n].lists[l].id === list.id) {
|
|
|
|
const namespace = this.namespaces[n]
|
2020-09-06 16:20:48 +02:00
|
|
|
namespace.lists[l] = list
|
2022-09-02 11:15:29 +02:00
|
|
|
this.namespaces[n] = namespace
|
2020-09-06 16:20:48 +02:00
|
|
|
return
|
|
|
|
}
|
2020-05-08 20:43:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-09-02 11:15:29 +02:00
|
|
|
|
|
|
|
addNamespace(namespace: INamespace) {
|
|
|
|
this.namespaces.push(namespace)
|
2021-11-14 21:49:52 +01:00
|
|
|
add(namespace)
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2022-09-02 11:15:29 +02:00
|
|
|
|
|
|
|
removeNamespaceById(namespaceId: INamespace['id']) {
|
|
|
|
for (const n in this.namespaces) {
|
|
|
|
if (this.namespaces[n].id === namespaceId) {
|
|
|
|
remove(this.namespaces[n])
|
|
|
|
this.namespaces.splice(n, 1)
|
2021-01-08 23:03:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-09-02 11:15:29 +02:00
|
|
|
|
|
|
|
addListToNamespace(list: IList) {
|
|
|
|
for (const n in this.namespaces) {
|
|
|
|
if (this.namespaces[n].id === list.namespaceId) {
|
|
|
|
this.namespaces[n].lists.push(list)
|
2020-05-08 20:43:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-09-02 11:15:29 +02:00
|
|
|
|
|
|
|
removeListFromNamespaceById(list: IList) {
|
|
|
|
for (const n in this.namespaces) {
|
2020-09-06 16:20:48 +02:00
|
|
|
// We don't have the namespace id on the list which means we need to loop over all lists until we find it.
|
|
|
|
// FIXME: Not ideal at all - we should fix that at the api level.
|
2022-09-02 11:15:29 +02:00
|
|
|
if (this.namespaces[n].id === list.namespaceId) {
|
|
|
|
for (const l in this.namespaces[n].lists) {
|
|
|
|
if (this.namespaces[n].lists[l].id === list.id) {
|
|
|
|
this.namespaces[n].lists.splice(l, 1)
|
2020-09-06 16:20:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-11-14 21:49:52 +01:00
|
|
|
|
2022-09-02 11:15:29 +02:00
|
|
|
async loadNamespaces() {
|
|
|
|
const cancel = setLoadingPinia(this)
|
2020-11-12 20:57:39 +01:00
|
|
|
|
2020-05-08 20:43:51 +02:00
|
|
|
const namespaceService = new NamespaceService()
|
2021-10-11 19:37:20 +02:00
|
|
|
try {
|
|
|
|
// We always load all namespaces and filter them on the frontend
|
2022-09-02 11:15:29 +02:00
|
|
|
const namespaces = await namespaceService.getAll({}, {is_archived: true}) as INamespace[]
|
|
|
|
this.setNamespaces(namespaces)
|
2021-11-14 21:49:52 +01:00
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
// Put all lists in the list state
|
|
|
|
const lists = namespaces.flatMap(({lists}) => lists)
|
2021-11-14 21:49:52 +01:00
|
|
|
|
2022-09-21 18:21:25 +02:00
|
|
|
const listStore = useListStore()
|
|
|
|
listStore.setLists(lists)
|
2021-11-14 21:49:52 +01:00
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
return namespaces
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2021-10-11 19:37:20 +02:00
|
|
|
|
2022-09-02 11:15:29 +02:00
|
|
|
loadNamespacesIfFavoritesDontExist() {
|
2022-01-29 22:12:46 +01:00
|
|
|
// The first or second namespace should be the one holding all favorites
|
2022-09-02 11:15:29 +02:00
|
|
|
if (this.namespaces[0].id === -2 || this.namespaces[1]?.id === -2) {
|
|
|
|
return
|
2020-09-05 22:16:17 +02:00
|
|
|
}
|
2022-09-02 11:15:29 +02:00
|
|
|
return this.loadNamespaces()
|
2020-09-05 22:16:17 +02:00
|
|
|
},
|
2021-10-11 19:37:20 +02:00
|
|
|
|
2022-09-02 11:15:29 +02:00
|
|
|
removeFavoritesNamespaceIfEmpty() {
|
|
|
|
if (this.namespaces[0].id === -2 && this.namespaces[0].lists.length === 0) {
|
|
|
|
this.namespaces.splice(0, 1)
|
2020-09-06 16:20:48 +02:00
|
|
|
}
|
|
|
|
},
|
2021-10-11 19:37:20 +02:00
|
|
|
|
2022-09-02 11:15:29 +02:00
|
|
|
async deleteNamespace(namespace: INamespace) {
|
|
|
|
const cancel = setLoadingPinia(this)
|
2021-05-26 17:39:57 +02:00
|
|
|
const namespaceService = new NamespaceService()
|
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
try {
|
|
|
|
const response = await namespaceService.delete(namespace)
|
2022-09-02 11:15:29 +02:00
|
|
|
this.removeNamespaceById(namespace.id)
|
2021-10-11 19:37:20 +02:00
|
|
|
return response
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
2021-06-24 15:38:25 +02:00
|
|
|
},
|
2021-10-11 19:37:20 +02:00
|
|
|
|
2022-09-02 11:15:29 +02:00
|
|
|
async createNamespace(namespace: INamespace) {
|
|
|
|
const cancel = setLoadingPinia(this)
|
2021-06-24 15:38:25 +02:00
|
|
|
const namespaceService = new NamespaceService()
|
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
try {
|
|
|
|
const createdNamespace = await namespaceService.create(namespace)
|
2022-09-02 11:15:29 +02:00
|
|
|
this.addNamespace(createdNamespace)
|
2021-10-11 19:37:20 +02:00
|
|
|
return createdNamespace
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
2021-05-26 17:39:57 +02:00
|
|
|
},
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2022-09-02 11:15:29 +02:00
|
|
|
})
|
2022-07-21 18:45:58 +02:00
|
|
|
|
2022-09-02 11:15:29 +02:00
|
|
|
// support hot reloading
|
|
|
|
if (import.meta.hot) {
|
|
|
|
import.meta.hot.accept(acceptHMRUpdate(useNamespaceStore, import.meta.hot))
|
|
|
|
}
|