2020-05-08 20:43:51 +02:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
|
|
|
import NamespaceService from '../../services/namespace'
|
2020-11-12 20:57:39 +01:00
|
|
|
import {setLoading} from '@/store/helper'
|
2020-05-08 20:43:51 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state: () => ({
|
|
|
|
namespaces: [],
|
|
|
|
}),
|
|
|
|
mutations: {
|
|
|
|
namespaces(state, namespaces) {
|
|
|
|
state.namespaces = namespaces
|
|
|
|
},
|
|
|
|
setNamespaceById(state, namespace) {
|
2021-08-23 21:24:52 +02:00
|
|
|
const namespaceIndex = state.namespaces.findIndex(n => n.id === namespace.id)
|
|
|
|
|
|
|
|
if (namespaceIndex === -1) {
|
|
|
|
return
|
2020-05-08 20:43:51 +02:00
|
|
|
}
|
2021-08-23 21:24:52 +02:00
|
|
|
|
|
|
|
Vue.set(state.namespaces, namespaceIndex, 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
|
|
|
|
Vue.set(state.namespaces, n, namespace)
|
|
|
|
return
|
|
|
|
}
|
2020-05-08 20:43:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addNamespace(state, namespace) {
|
|
|
|
state.namespaces.push(namespace)
|
|
|
|
},
|
2021-01-08 23:03:40 +01:00
|
|
|
removeNamespaceById(state, namespaceId) {
|
|
|
|
for (const n in state.namespaces) {
|
|
|
|
if (state.namespaces[n].id === namespaceId) {
|
|
|
|
state.namespaces.splice(n, 1)
|
|
|
|
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: {
|
2020-05-09 23:28:54 +02:00
|
|
|
getListAndNamespaceById: state => listId => {
|
|
|
|
for (const n in state.namespaces) {
|
|
|
|
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 => {
|
|
|
|
for (const n in state.namespaces) {
|
|
|
|
if (state.namespaces[n].id === namespaceId) {
|
|
|
|
return state.namespaces[n]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
},
|
2020-05-09 19:33:37 +02:00
|
|
|
},
|
2020-05-08 20:43:51 +02:00
|
|
|
actions: {
|
|
|
|
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()
|
|
|
|
// We always load all namespaces and filter them on the frontend
|
|
|
|
return namespaceService.getAll({}, {is_archived: true})
|
|
|
|
.then(r => {
|
|
|
|
ctx.commit('namespaces', r)
|
2020-05-11 16:52:58 +02:00
|
|
|
|
|
|
|
// Put all lists in the list state
|
|
|
|
const lists = []
|
|
|
|
r.forEach(n => {
|
|
|
|
n.lists.forEach(l => {
|
|
|
|
lists.push(l)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-05-26 16:46:16 +02:00
|
|
|
ctx.commit('lists/setLists', lists, {root: true})
|
2020-05-11 16:52:58 +02:00
|
|
|
|
2021-02-20 16:28:45 +01:00
|
|
|
return Promise.resolve(r)
|
2020-05-08 20:43:51 +02:00
|
|
|
})
|
2021-06-24 15:38:25 +02:00
|
|
|
.catch(e => Promise.reject(e))
|
2020-11-12 20:57:39 +01:00
|
|
|
.finally(() => {
|
|
|
|
cancel()
|
|
|
|
})
|
2020-05-08 20:43:51 +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')
|
|
|
|
}
|
|
|
|
},
|
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)
|
|
|
|
return Promise.resolve()
|
|
|
|
}
|
|
|
|
},
|
2021-05-26 17:39:57 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
return namespaceService.delete(namespace)
|
|
|
|
.then(r => {
|
|
|
|
ctx.commit('removeNamespaceById', namespace.id)
|
|
|
|
return Promise.resolve(r)
|
|
|
|
})
|
2021-06-24 15:38:25 +02:00
|
|
|
.catch(e => Promise.reject(e))
|
|
|
|
.finally(() => cancel())
|
|
|
|
},
|
|
|
|
createNamespace(ctx, namespace) {
|
|
|
|
const cancel = setLoading(ctx, 'namespaces')
|
|
|
|
const namespaceService = new NamespaceService()
|
|
|
|
|
|
|
|
return namespaceService.create(namespace)
|
|
|
|
.then(r => {
|
|
|
|
ctx.commit('addNamespace', r)
|
|
|
|
return Promise.resolve(r)
|
|
|
|
})
|
|
|
|
.catch(e => Promise.reject(e))
|
|
|
|
.finally(() => cancel())
|
2021-05-26 17:39:57 +02:00
|
|
|
},
|
2020-05-08 20:43:51 +02:00
|
|
|
},
|
|
|
|
}
|