2020-05-08 20:43:51 +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'
|
|
|
|
|
|
|
|
const {add, remove, search, update} = createNewIndexer('namespaces', ['title', 'description'])
|
2020-05-08 20:43:51 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state: () => ({
|
|
|
|
namespaces: [],
|
|
|
|
}),
|
|
|
|
mutations: {
|
|
|
|
namespaces(state, namespaces) {
|
|
|
|
state.namespaces = namespaces
|
2021-11-14 21:49:52 +01:00
|
|
|
namespaces.forEach(n => {
|
|
|
|
add(n)
|
|
|
|
})
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
|
|
|
setNamespaceById(state, namespace) {
|
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
|
|
|
},
|
|
|
|
setListInNamespaceById(state, list) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addNamespace(state, namespace) {
|
|
|
|
state.namespaces.push(namespace)
|
2021-11-14 21:49:52 +01:00
|
|
|
add(namespace)
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
2021-01-08 23:03:40 +01:00
|
|
|
removeNamespaceById(state, namespaceId) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-05-08 20:43:51 +02:00
|
|
|
addListToNamespace(state, list) {
|
|
|
|
for (const n in state.namespaces) {
|
|
|
|
if (state.namespaces[n].id === list.namespaceId) {
|
|
|
|
state.namespaces[n].lists.push(list)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-09-06 16:20:48 +02:00
|
|
|
removeListFromNamespaceById(state, list) {
|
|
|
|
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: {
|
2021-11-13 21:27:23 +01:00
|
|
|
getListAndNamespaceById: state => (listId, 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
|
|
|
|
},
|
2020-06-27 23:12:33 +02:00
|
|
|
getNamespaceById: state => namespaceId => {
|
2021-10-02 22:19:55 +02:00
|
|
|
return state.namespaces.find(({id}) => id == namespaceId) || null
|
2020-06-27 23:12:33 +02:00
|
|
|
},
|
2021-11-14 21:49:52 +01:00
|
|
|
searchNamespace: (state, getters) => query => {
|
|
|
|
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: {
|
2021-10-11 19:37:20 +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
|
|
|
|
2021-10-11 19:37:20 +02:00
|
|
|
ctx.commit('lists/setLists', lists, {root: true})
|
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
|
|
|
|
2020-09-05 22:16:17 +02:00
|
|
|
loadNamespacesIfFavoritesDontExist(ctx) {
|
|
|
|
// The first namespace should be the one holding all favorites
|
2020-09-05 22:35:52 +02:00
|
|
|
if (ctx.state.namespaces[0].id !== -2) {
|
2020-09-05 22:16:17 +02:00
|
|
|
return ctx.dispatch('loadNamespaces')
|
|
|
|
}
|
|
|
|
},
|
2021-10-11 19:37:20 +02:00
|
|
|
|
2020-09-06 16:20:48 +02:00
|
|
|
removeFavoritesNamespaceIfEmpty(ctx) {
|
|
|
|
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
|
|
|
|
|
|
|
async deleteNamespace(ctx, namespace) {
|
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
|
|
|
|
|
|
|
async createNamespace(ctx, namespace) {
|
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
|
|
|
},
|
|
|
|
}
|