2022-07-21 18:45:58 +02:00
|
|
|
import type { Module } from 'vuex'
|
2022-07-21 00:42:36 +02:00
|
|
|
|
2022-09-02 10:20:25 +02:00
|
|
|
import NamespaceService from '../services/namespace'
|
2020-11-12 20:57:39 +01:00
|
|
|
import {setLoading} from '@/store/helper'
|
2021-11-14 21:49:52 +01:00
|
|
|
import {createNewIndexer} from '@/indexes'
|
2022-07-21 00:42:36 +02:00
|
|
|
import type {NamespaceState, RootStoreState} 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-07-21 18:45:58 +02:00
|
|
|
const namespacesStore : Module<NamespaceState, RootStoreState> = {
|
2020-05-08 20:43:51 +02:00
|
|
|
namespaced: true,
|
2022-07-21 18:45:58 +02:00
|
|
|
state: () => ({
|
2020-05-08 20:43:51 +02:00
|
|
|
namespaces: [],
|
|
|
|
}),
|
|
|
|
mutations: {
|
2022-07-21 18:45:58 +02:00
|
|
|
namespaces(state, namespaces: INamespace[]) {
|
2020-05-08 20:43:51 +02:00
|
|
|
state.namespaces = namespaces
|
2021-11-14 21:49:52 +01:00
|
|
|
namespaces.forEach(n => {
|
|
|
|
add(n)
|
|
|
|
})
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2022-07-21 18:45:58 +02:00
|
|
|
setNamespaceById(state, namespace: INamespace) {
|
2021-08-23 21:24:52 +02:00
|
|
|
const namespaceIndex = state.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) {
|
|
|
|
namespace.lists = state.namespaces[namespaceIndex].lists
|
|
|
|
}
|
2021-11-14 21:49:52 +01:00
|
|
|
|
2021-08-19 21:35:38 +02:00
|
|
|
state.namespaces[namespaceIndex] = namespace
|
2021-11-14 21:49:52 +01:00
|
|
|
update(namespace)
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2022-07-21 18:45:58 +02:00
|
|
|
setListInNamespaceById(state, list: IList) {
|
2020-05-08 20:43:51 +02:00
|
|
|
for (const n in state.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.
|
2020-09-06 16:20:48 +02:00
|
|
|
if (state.namespaces[n].id === list.namespaceId) {
|
|
|
|
for (const l in state.namespaces[n].lists) {
|
|
|
|
if (state.namespaces[n].lists[l].id === list.id) {
|
|
|
|
const namespace = state.namespaces[n]
|
|
|
|
namespace.lists[l] = list
|
2021-08-19 21:35:38 +02:00
|
|
|
state.namespaces[n] = namespace
|
2020-09-06 16:20:48 +02:00
|
|
|
return
|
|
|
|
}
|
2020-05-08 20:43:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-07-21 18:45:58 +02:00
|
|
|
addNamespace(state, namespace: INamespace) {
|
2020-05-08 20:43:51 +02:00
|
|
|
state.namespaces.push(namespace)
|
2021-11-14 21:49:52 +01:00
|
|
|
add(namespace)
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2022-07-21 18:45:58 +02:00
|
|
|
removeNamespaceById(state, namespaceId: INamespace['id']) {
|
2021-01-08 23:03:40 +01:00
|
|
|
for (const n in state.namespaces) {
|
|
|
|
if (state.namespaces[n].id === namespaceId) {
|
2021-11-14 21:49:52 +01:00
|
|
|
remove(state.namespaces[n])
|
2021-11-22 21:49:19 +01:00
|
|
|
state.namespaces.splice(n, 1)
|
2021-01-08 23:03:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-07-21 18:45:58 +02:00
|
|
|
addListToNamespace(state, list: IList) {
|
2020-05-08 20:43:51 +02:00
|
|
|
for (const n in state.namespaces) {
|
|
|
|
if (state.namespaces[n].id === list.namespaceId) {
|
|
|
|
state.namespaces[n].lists.push(list)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-07-21 18:45:58 +02:00
|
|
|
removeListFromNamespaceById(state, list: IList) {
|
2020-09-06 16:20:48 +02:00
|
|
|
for (const n in state.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.
|
|
|
|
if (state.namespaces[n].id === list.namespaceId) {
|
|
|
|
for (const l in state.namespaces[n].lists) {
|
|
|
|
if (state.namespaces[n].lists[l].id === list.id) {
|
|
|
|
state.namespaces[n].lists.splice(l, 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2020-05-09 19:33:37 +02:00
|
|
|
getters: {
|
2022-07-21 18:45:58 +02:00
|
|
|
getListAndNamespaceById: (state) => (listId: IList['id'], ignorePseudoNamespaces = false) => {
|
2020-05-09 23:28:54 +02:00
|
|
|
for (const n in state.namespaces) {
|
2021-11-14 21:49:52 +01:00
|
|
|
|
|
|
|
if (ignorePseudoNamespaces && state.namespaces[n].id < 0) {
|
2021-11-13 21:27:23 +01:00
|
|
|
continue
|
|
|
|
}
|
2021-11-14 21:49:52 +01:00
|
|
|
|
2020-05-09 23:28:54 +02:00
|
|
|
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
|
|
|
|
},
|
2022-07-21 18:45:58 +02:00
|
|
|
getNamespaceById: (state) => (namespaceId: INamespace['id']) => {
|
2021-10-02 22:19:55 +02:00
|
|
|
return state.namespaces.find(({id}) => id == namespaceId) || null
|
2020-06-27 23:12:33 +02:00
|
|
|
},
|
2022-07-21 18:45:58 +02:00
|
|
|
searchNamespace: (state, getters) => (query: string) => {
|
2021-11-14 21:49:52 +01:00
|
|
|
return search(query)
|
|
|
|
?.filter(value => value > 0)
|
|
|
|
.map(getters.getNamespaceById)
|
|
|
|
.filter(n => n !== null)
|
|
|
|
|| []
|
|
|
|
},
|
2020-05-09 19:33:37 +02:00
|
|
|
},
|
2020-05-08 20:43:51 +02:00
|
|
|
actions: {
|
2022-07-21 18:45:58 +02:00
|
|
|
async loadNamespaces(ctx) {
|
2021-01-09 15:24:06 +01:00
|
|
|
const cancel = setLoading(ctx, 'namespaces')
|
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
|
|
|
|
const namespaces = await namespaceService.getAll({}, {is_archived: true})
|
|
|
|
ctx.commit('namespaces', 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-07-21 18:45:58 +02:00
|
|
|
loadNamespacesIfFavoritesDontExist(ctx) {
|
2022-01-29 22:12:46 +01:00
|
|
|
// The first or second namespace should be the one holding all favorites
|
|
|
|
if (ctx.state.namespaces[0].id !== -2 && ctx.state.namespaces[1]?.id !== -2) {
|
2020-09-05 22:16:17 +02:00
|
|
|
return ctx.dispatch('loadNamespaces')
|
|
|
|
}
|
|
|
|
},
|
2021-10-11 19:37:20 +02:00
|
|
|
|
2022-07-21 18:45:58 +02:00
|
|
|
removeFavoritesNamespaceIfEmpty(ctx) {
|
2020-09-06 16:20:48 +02:00
|
|
|
if (ctx.state.namespaces[0].id === -2 && ctx.state.namespaces[0].lists.length === 0) {
|
|
|
|
ctx.state.namespaces.splice(0, 1)
|
|
|
|
}
|
|
|
|
},
|
2021-10-11 19:37:20 +02:00
|
|
|
|
2022-07-21 18:45:58 +02:00
|
|
|
async deleteNamespace(ctx, namespace: INamespace) {
|
2021-06-24 15:38:25 +02:00
|
|
|
const cancel = setLoading(ctx, 'namespaces')
|
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)
|
|
|
|
ctx.commit('removeNamespaceById', namespace.id)
|
|
|
|
return response
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
2021-06-24 15:38:25 +02:00
|
|
|
},
|
2021-10-11 19:37:20 +02:00
|
|
|
|
2022-07-21 18:45:58 +02:00
|
|
|
async createNamespace(ctx, namespace: INamespace) {
|
2021-06-24 15:38:25 +02:00
|
|
|
const cancel = setLoading(ctx, 'namespaces')
|
|
|
|
const namespaceService = new NamespaceService()
|
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
try {
|
|
|
|
const createdNamespace = await namespaceService.create(namespace)
|
|
|
|
ctx.commit('addNamespace', createdNamespace)
|
|
|
|
return createdNamespace
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
2021-05-26 17:39:57 +02:00
|
|
|
},
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2022-07-21 18:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default namespacesStore
|