vikunja-frontend/src/stores/namespaces.ts

207 lines
5.3 KiB
TypeScript
Raw Normal View History

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-30 12:52:21 +02:00
import {setModuleLoading} from '@/stores/helper'
import {createNewIndexer} from '@/indexes'
2022-08-04 20:57:43 +02:00
import type {INamespace} from '@/modelTypes/INamespace'
import type {IList} from '@/modelTypes/IList'
import {useListStore} from '@/stores/lists'
const {add, remove, search, update} = createNewIndexer('namespaces', ['title', 'description'])
2022-09-24 15:20:40 +02:00
export interface NamespaceState {
namespaces: INamespace[]
isLoading: boolean,
}
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
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
namespaces.forEach(n => {
add(n)
})
},
2022-09-02 11:15:29 +02:00
setNamespaceById(namespace: INamespace) {
const namespaceIndex = this.namespaces.findIndex(n => n.id === namespace.id)
if (namespaceIndex === -1) {
return
}
if (!namespace.lists || namespace.lists.length === 0) {
2022-09-02 11:15:29 +02:00
namespace.lists = this.namespaces[namespaceIndex].lists
}
2022-09-02 11:15:29 +02:00
this.namespaces[namespaceIndex] = namespace
update(namespace)
},
2022-09-02 11:15:29 +02:00
setListInNamespaceById(list: IList) {
for (const n in this.namespaces) {
// 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]
namespace.lists[l] = list
2022-09-02 11:15:29 +02:00
this.namespaces[n] = namespace
return
}
}
}
}
},
2022-09-02 11:15:29 +02:00
addNamespace(namespace: INamespace) {
this.namespaces.push(namespace)
add(namespace)
},
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)
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)
return
}
}
},
2022-09-02 11:15:29 +02:00
removeListFromNamespaceById(list: IList) {
for (const n in this.namespaces) {
// 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)
return
}
}
}
}
},
2022-09-02 11:15:29 +02:00
async loadNamespaces() {
2022-09-30 12:52:21 +02:00
const cancel = setModuleLoading(this)
const namespaceService = new NamespaceService()
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)
// Put all lists in the list state
const lists = namespaces.flatMap(({lists}) => lists)
const listStore = useListStore()
listStore.setLists(lists)
return namespaces
} finally {
cancel()
}
},
2022-09-02 11:15:29 +02:00
loadNamespacesIfFavoritesDontExist() {
// 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
}
2022-09-02 11:15:29 +02:00
return this.loadNamespaces()
},
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)
}
},
2022-09-02 11:15:29 +02:00
async deleteNamespace(namespace: INamespace) {
2022-09-30 12:52:21 +02:00
const cancel = setModuleLoading(this)
const namespaceService = new NamespaceService()
try {
const response = await namespaceService.delete(namespace)
2022-09-02 11:15:29 +02:00
this.removeNamespaceById(namespace.id)
return response
} finally {
cancel()
}
},
2022-09-02 11:15:29 +02:00
async createNamespace(namespace: INamespace) {
2022-09-30 12:52:21 +02:00
const cancel = setModuleLoading(this)
const namespaceService = new NamespaceService()
try {
const createdNamespace = await namespaceService.create(namespace)
2022-09-02 11:15:29 +02:00
this.addNamespace(createdNamespace)
return createdNamespace
} finally {
cancel()
}
},
},
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))
}