2020-05-11 16:52:58 +02:00
|
|
|
import Vue from 'vue'
|
2020-09-06 16:20:48 +02:00
|
|
|
import ListService from '@/services/list'
|
|
|
|
|
|
|
|
const FavoriteListsNamespace = -2
|
2020-05-11 16:52:58 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
// The state is an object which has the list ids as keys.
|
|
|
|
state: () => ({}),
|
|
|
|
mutations: {
|
2021-05-26 16:46:16 +02:00
|
|
|
setList(state, list) {
|
2020-05-11 16:52:58 +02:00
|
|
|
Vue.set(state, list.id, list)
|
|
|
|
},
|
2021-05-26 16:46:16 +02:00
|
|
|
setLists(state, lists) {
|
2020-05-11 16:52:58 +02:00
|
|
|
lists.forEach(l => {
|
|
|
|
Vue.set(state, l.id, l)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
getListById: state => id => {
|
2020-09-05 22:35:52 +02:00
|
|
|
if (typeof state[id] !== 'undefined') {
|
2020-05-11 16:52:58 +02:00
|
|
|
return state[id]
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
},
|
|
|
|
},
|
2020-09-06 16:20:48 +02:00
|
|
|
actions: {
|
|
|
|
toggleListFavorite(ctx, list) {
|
|
|
|
list.isFavorite = !list.isFavorite
|
2021-05-26 16:46:16 +02:00
|
|
|
|
|
|
|
return ctx.dispatch('updateList', list)
|
|
|
|
},
|
|
|
|
createList(ctx, list) {
|
|
|
|
const listService = new ListService()
|
|
|
|
|
|
|
|
return listService.create(list)
|
|
|
|
.then(r => {
|
|
|
|
r.namespaceId = list.namespaceId
|
|
|
|
ctx.commit('namespaces/addListToNamespace', r, {root: true})
|
|
|
|
ctx.commit('setList', r)
|
|
|
|
return Promise.resolve(r)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
updateList(ctx, list) {
|
2020-09-06 16:20:48 +02:00
|
|
|
const listService = new ListService()
|
|
|
|
|
|
|
|
return listService.update(list)
|
|
|
|
.then(r => {
|
2021-05-26 16:46:16 +02:00
|
|
|
ctx.commit('setList', r)
|
|
|
|
ctx.commit('namespaces/setListInNamespaceById', r, {root: true})
|
2020-09-06 16:20:48 +02:00
|
|
|
if (r.isFavorite) {
|
|
|
|
r.namespaceId = FavoriteListsNamespace
|
|
|
|
ctx.commit('namespaces/addListToNamespace', r, {root: true})
|
|
|
|
} else {
|
|
|
|
r.namespaceId = FavoriteListsNamespace
|
|
|
|
ctx.commit('namespaces/removeListFromNamespaceById', r, {root: true})
|
|
|
|
}
|
|
|
|
ctx.dispatch('namespaces/loadNamespacesIfFavoritesDontExist', null, {root: true})
|
|
|
|
ctx.dispatch('namespaces/removeFavoritesNamespaceIfEmpty', null, {root: true})
|
|
|
|
return Promise.resolve(r)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
// Reset the list state to the initial one to avoid confusion for the user
|
|
|
|
list.isFavorite = !list.isFavorite
|
2021-05-26 16:46:16 +02:00
|
|
|
ctx.commit('setList', list)
|
2020-09-06 16:20:48 +02:00
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-05-26 16:46:16 +02:00
|
|
|
}
|
2020-09-06 16:20:48 +02:00
|
|
|
},
|
2020-05-11 16:52:58 +02:00
|
|
|
}
|