2022-07-21 16:45:58 +00:00
|
|
|
import type { Module } from 'vuex'
|
|
|
|
|
2020-09-06 14:20:48 +00:00
|
|
|
import ListService from '@/services/list'
|
2021-06-24 13:38:25 +00:00
|
|
|
import {setLoading} from '@/store/helper'
|
2022-02-15 12:07:59 +00:00
|
|
|
import {removeListFromHistory} from '@/modules/listHistory'
|
2021-11-14 20:49:52 +00:00
|
|
|
import {createNewIndexer} from '@/indexes'
|
2022-07-20 22:42:36 +00:00
|
|
|
import type {ListState, RootStoreState} from '@/store/types'
|
2022-08-04 18:57:43 +00:00
|
|
|
import type {IList} from '@/modelTypes/IList'
|
2021-11-14 20:49:52 +00:00
|
|
|
|
|
|
|
const {add, remove, search, update} = createNewIndexer('lists', ['title', 'description'])
|
2020-09-06 14:20:48 +00:00
|
|
|
|
|
|
|
const FavoriteListsNamespace = -2
|
2020-05-11 14:52:58 +00:00
|
|
|
|
2022-07-21 16:45:58 +00:00
|
|
|
const listsStore : Module<ListState, RootStoreState>= {
|
2020-05-11 14:52:58 +00:00
|
|
|
namespaced: true,
|
|
|
|
// The state is an object which has the list ids as keys.
|
2022-07-21 16:45:58 +00:00
|
|
|
state: () => ({}),
|
2020-05-11 14:52:58 +00:00
|
|
|
mutations: {
|
2022-07-21 16:45:58 +00:00
|
|
|
setList(state, list: IList) {
|
2021-08-19 19:35:38 +00:00
|
|
|
state[list.id] = list
|
2021-11-14 20:49:52 +00:00
|
|
|
update(list)
|
2020-05-11 14:52:58 +00:00
|
|
|
},
|
2022-07-21 16:45:58 +00:00
|
|
|
setLists(state, lists: IList[]) {
|
2020-05-11 14:52:58 +00:00
|
|
|
lists.forEach(l => {
|
2021-08-19 19:35:38 +00:00
|
|
|
state[l.id] = l
|
2021-11-14 20:49:52 +00:00
|
|
|
add(l)
|
2020-05-11 14:52:58 +00:00
|
|
|
})
|
|
|
|
},
|
2022-07-21 16:45:58 +00:00
|
|
|
removeListById(state, list: IList) {
|
2021-11-14 20:49:52 +00:00
|
|
|
remove(list)
|
2021-11-22 20:49:19 +00:00
|
|
|
delete state[list.id]
|
2021-07-20 16:03:38 +00:00
|
|
|
},
|
2020-05-11 14:52:58 +00:00
|
|
|
},
|
|
|
|
getters: {
|
2022-07-21 16:45:58 +00:00
|
|
|
getListById: (state) => (id: IList['id']) => {
|
2020-09-05 20:35:52 +00:00
|
|
|
if (typeof state[id] !== 'undefined') {
|
2020-05-11 14:52:58 +00:00
|
|
|
return state[id]
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
},
|
2022-07-21 16:45:58 +00:00
|
|
|
findListByExactname: (state) => (name: string) => {
|
2021-07-05 10:29:04 +00:00
|
|
|
const list = Object.values(state).find(l => {
|
|
|
|
return l.title.toLowerCase() === name.toLowerCase()
|
|
|
|
})
|
|
|
|
return typeof list === 'undefined' ? null : list
|
|
|
|
},
|
2022-07-21 16:45:58 +00:00
|
|
|
searchList: (state) => (query: string, includeArchived = false) => {
|
2021-11-14 20:49:52 +00:00
|
|
|
return search(query)
|
|
|
|
?.filter(value => value > 0)
|
|
|
|
.map(id => state[id])
|
|
|
|
.filter(list => list.isArchived === includeArchived)
|
|
|
|
|| []
|
|
|
|
},
|
2020-05-11 14:52:58 +00:00
|
|
|
},
|
2020-09-06 14:20:48 +00:00
|
|
|
actions: {
|
2022-07-21 16:45:58 +00:00
|
|
|
toggleListFavorite(ctx, list: IList) {
|
2021-08-23 19:24:52 +00:00
|
|
|
return ctx.dispatch('updateList', {
|
|
|
|
...list,
|
|
|
|
isFavorite: !list.isFavorite,
|
|
|
|
})
|
2021-05-26 14:46:16 +00:00
|
|
|
},
|
2021-10-11 17:37:20 +00:00
|
|
|
|
2022-07-21 16:45:58 +00:00
|
|
|
async createList(ctx, list: IList) {
|
2021-06-24 13:38:25 +00:00
|
|
|
const cancel = setLoading(ctx, 'lists')
|
2021-05-26 14:46:16 +00:00
|
|
|
const listService = new ListService()
|
|
|
|
|
2021-10-11 17:37:20 +00:00
|
|
|
try {
|
|
|
|
const createdList = await listService.create(list)
|
|
|
|
createdList.namespaceId = list.namespaceId
|
|
|
|
ctx.commit('namespaces/addListToNamespace', createdList, {root: true})
|
|
|
|
ctx.commit('setList', createdList)
|
|
|
|
return createdList
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
2021-05-26 14:46:16 +00:00
|
|
|
},
|
2021-10-11 17:37:20 +00:00
|
|
|
|
2022-07-21 16:45:58 +00:00
|
|
|
async updateList(ctx, list: IList) {
|
2021-06-24 13:38:25 +00:00
|
|
|
const cancel = setLoading(ctx, 'lists')
|
2020-09-06 14:20:48 +00:00
|
|
|
const listService = new ListService()
|
|
|
|
|
2021-10-11 17:37:20 +00:00
|
|
|
try {
|
|
|
|
await listService.update(list)
|
|
|
|
ctx.commit('setList', list)
|
|
|
|
ctx.commit('namespaces/setListInNamespaceById', list, {root: true})
|
2021-11-14 20:49:52 +00:00
|
|
|
|
2021-10-11 17:37:20 +00:00
|
|
|
// 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})
|
|
|
|
} else {
|
|
|
|
ctx.commit('namespaces/removeListFromNamespaceById', newList, {root: true})
|
|
|
|
}
|
|
|
|
ctx.dispatch('namespaces/loadNamespacesIfFavoritesDontExist', null, {root: true})
|
|
|
|
ctx.dispatch('namespaces/removeFavoritesNamespaceIfEmpty', null, {root: true})
|
|
|
|
return newList
|
2021-11-14 20:49:52 +00:00
|
|
|
} catch (e) {
|
2021-10-11 17:37:20 +00:00
|
|
|
// Reset the list state to the initial one to avoid confusion for the user
|
|
|
|
ctx.commit('setList', {
|
|
|
|
...list,
|
|
|
|
isFavorite: !list.isFavorite,
|
2020-09-06 14:20:48 +00:00
|
|
|
})
|
2021-10-11 17:37:20 +00:00
|
|
|
throw e
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
2021-07-06 20:22:57 +00:00
|
|
|
},
|
2021-10-11 17:37:20 +00:00
|
|
|
|
2022-07-21 16:45:58 +00:00
|
|
|
async deleteList(ctx, list: IList) {
|
2021-07-20 16:03:38 +00:00
|
|
|
const cancel = setLoading(ctx, 'lists')
|
|
|
|
const listService = new ListService()
|
|
|
|
|
2021-11-14 20:49:52 +00:00
|
|
|
try {
|
2021-10-11 17:37:20 +00:00
|
|
|
const response = await listService.delete(list)
|
|
|
|
ctx.commit('removeListById', list)
|
|
|
|
ctx.commit('namespaces/removeListFromNamespaceById', list, {root: true})
|
|
|
|
removeListFromHistory({id: list.id})
|
|
|
|
return response
|
2021-11-14 20:49:52 +00:00
|
|
|
} finally {
|
2021-10-11 17:37:20 +00:00
|
|
|
cancel()
|
|
|
|
}
|
2021-07-20 16:03:38 +00:00
|
|
|
},
|
2020-09-06 14:20:48 +00:00
|
|
|
},
|
2022-07-21 16:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default listsStore
|