2020-09-06 16:20:48 +02:00
|
|
|
import ListService from '@/services/list'
|
2021-06-24 15:38:25 +02:00
|
|
|
import {setLoading} from '@/store/helper'
|
2021-07-20 18:03:38 +02:00
|
|
|
import {removeListFromHistory} from '@/modules/listHistory.ts'
|
2020-09-06 16:20:48 +02:00
|
|
|
|
|
|
|
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) {
|
2021-08-19 21:35:38 +02:00
|
|
|
state[list.id] = list
|
2020-05-11 16:52:58 +02:00
|
|
|
},
|
2021-05-26 16:46:16 +02:00
|
|
|
setLists(state, lists) {
|
2020-05-11 16:52:58 +02:00
|
|
|
lists.forEach(l => {
|
2021-08-19 21:35:38 +02:00
|
|
|
state[l.id] = l
|
2020-05-11 16:52:58 +02:00
|
|
|
})
|
|
|
|
},
|
2021-07-20 18:03:38 +02:00
|
|
|
removeListById(state, list) {
|
|
|
|
delete state[list.id]
|
|
|
|
},
|
2020-05-11 16:52:58 +02:00
|
|
|
},
|
|
|
|
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
|
|
|
|
},
|
2021-07-05 12:29:04 +02:00
|
|
|
findListByExactname: state => name => {
|
|
|
|
const list = Object.values(state).find(l => {
|
|
|
|
return l.title.toLowerCase() === name.toLowerCase()
|
|
|
|
})
|
|
|
|
return typeof list === 'undefined' ? null : list
|
|
|
|
},
|
2020-05-11 16:52:58 +02:00
|
|
|
},
|
2020-09-06 16:20:48 +02:00
|
|
|
actions: {
|
|
|
|
toggleListFavorite(ctx, list) {
|
2021-08-23 21:24:52 +02:00
|
|
|
return ctx.dispatch('updateList', {
|
|
|
|
...list,
|
|
|
|
isFavorite: !list.isFavorite,
|
|
|
|
})
|
2021-05-26 16:46:16 +02:00
|
|
|
},
|
|
|
|
createList(ctx, list) {
|
2021-06-24 15:38:25 +02:00
|
|
|
const cancel = setLoading(ctx, 'lists')
|
2021-05-26 16:46:16 +02:00
|
|
|
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)
|
|
|
|
})
|
2021-06-24 15:38:25 +02:00
|
|
|
.catch(e => Promise.reject(e))
|
|
|
|
.finally(() => cancel())
|
2021-05-26 16:46:16 +02:00
|
|
|
},
|
|
|
|
updateList(ctx, list) {
|
2021-06-24 15:38:25 +02:00
|
|
|
const cancel = setLoading(ctx, 'lists')
|
2020-09-06 16:20:48 +02:00
|
|
|
const listService = new ListService()
|
|
|
|
|
|
|
|
return listService.update(list)
|
2021-08-23 21:24:52 +02:00
|
|
|
.then(() => {
|
|
|
|
ctx.commit('setList', list)
|
|
|
|
ctx.commit('namespaces/setListInNamespaceById', list, {root: true})
|
|
|
|
|
|
|
|
// the returned list from listService.update is the same!
|
|
|
|
// in order to not validate vuex mutations we have to create a new copy
|
|
|
|
const newList = {
|
|
|
|
...list,
|
|
|
|
namespaceId: FavoriteListsNamespace,
|
|
|
|
}
|
|
|
|
if (list.isFavorite) {
|
|
|
|
ctx.commit('namespaces/addListToNamespace', newList, {root: true})
|
2020-09-06 16:20:48 +02:00
|
|
|
} else {
|
2021-08-23 21:24:52 +02:00
|
|
|
ctx.commit('namespaces/removeListFromNamespaceById', newList, {root: true})
|
2020-09-06 16:20:48 +02:00
|
|
|
}
|
|
|
|
ctx.dispatch('namespaces/loadNamespacesIfFavoritesDontExist', null, {root: true})
|
|
|
|
ctx.dispatch('namespaces/removeFavoritesNamespaceIfEmpty', null, {root: true})
|
2021-08-23 21:24:52 +02:00
|
|
|
return Promise.resolve(newList)
|
2020-09-06 16:20:48 +02:00
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
// Reset the list state to the initial one to avoid confusion for the user
|
2021-08-23 21:24:52 +02:00
|
|
|
ctx.commit('setList', {
|
|
|
|
...list,
|
|
|
|
isFavorite: !list.isFavorite,
|
|
|
|
})
|
2020-09-06 16:20:48 +02:00
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
2021-06-24 15:38:25 +02:00
|
|
|
.finally(() => cancel())
|
2021-07-06 22:22:57 +02:00
|
|
|
},
|
2021-07-20 18:03:38 +02:00
|
|
|
deleteList(ctx, list) {
|
|
|
|
const cancel = setLoading(ctx, 'lists')
|
|
|
|
const listService = new ListService()
|
|
|
|
|
|
|
|
return listService.delete(list)
|
|
|
|
.then(r => {
|
|
|
|
ctx.commit('removeListById', list)
|
|
|
|
ctx.commit('namespaces/removeListFromNamespaceById', list, {root: true})
|
|
|
|
removeListFromHistory({id: list.id})
|
|
|
|
return Promise.resolve(r)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
return Promise.reject(e)
|
|
|
|
})
|
|
|
|
.finally(() => cancel())
|
|
|
|
},
|
2020-09-06 16:20:48 +02:00
|
|
|
},
|
2020-05-11 16:52:58 +02:00
|
|
|
}
|