vikunja-frontend/src/store/modules/lists.ts

127 lines
3.7 KiB
TypeScript
Raw Normal View History

import ListService from '@/services/list'
import {setLoading} from '@/store/helper'
2022-02-15 13:07:59 +01:00
import {removeListFromHistory} from '@/modules/listHistory'
import {createNewIndexer} from '@/indexes'
2022-07-21 00:42:36 +02:00
import type {ListState, RootStoreState} from '@/store/types'
import type {ActionContext} from 'vuex'
import type {IList} from '@/models/list'
const {add, remove, search, update} = createNewIndexer('lists', ['title', 'description'])
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.
2022-07-21 00:42:36 +02:00
state: (): ListState => ({}),
2020-05-11 16:52:58 +02:00
mutations: {
2022-07-21 00:42:36 +02:00
setList(state: ListState, list: IList) {
state[list.id] = list
update(list)
2020-05-11 16:52:58 +02:00
},
2022-07-21 00:42:36 +02:00
setLists(state: ListState, lists: IList[]) {
2020-05-11 16:52:58 +02:00
lists.forEach(l => {
state[l.id] = l
add(l)
2020-05-11 16:52:58 +02:00
})
},
2022-07-21 00:42:36 +02:00
removeListById(state: ListState, list: IList) {
remove(list)
delete state[list.id]
},
2020-05-11 16:52:58 +02:00
},
getters: {
2022-07-21 00:42:36 +02:00
getListById: (state: ListState) => (id: IList['id']) => {
if (typeof state[id] !== 'undefined') {
2020-05-11 16:52:58 +02:00
return state[id]
}
return null
},
2022-07-21 00:42:36 +02:00
findListByExactname: (state: ListState) => (name: string) => {
const list = Object.values(state).find(l => {
return l.title.toLowerCase() === name.toLowerCase()
})
return typeof list === 'undefined' ? null : list
},
2022-07-21 00:42:36 +02:00
searchList: (state: ListState) => (query: string, includeArchived = false) => {
return search(query)
?.filter(value => value > 0)
.map(id => state[id])
.filter(list => list.isArchived === includeArchived)
|| []
},
2020-05-11 16:52:58 +02:00
},
actions: {
2022-07-21 00:42:36 +02:00
toggleListFavorite(ctx: ActionContext<ListState, RootStoreState>, list: IList) {
return ctx.dispatch('updateList', {
...list,
isFavorite: !list.isFavorite,
})
},
2022-07-21 00:42:36 +02:00
async createList(ctx: ActionContext<ListState, RootStoreState>, list: IList) {
const cancel = setLoading(ctx, 'lists')
const listService = new ListService()
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()
}
},
2022-07-21 00:42:36 +02:00
async updateList(ctx: ActionContext<ListState, RootStoreState>, list: IList) {
const cancel = setLoading(ctx, 'lists')
const listService = new ListService()
try {
await listService.update(list)
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})
} else {
ctx.commit('namespaces/removeListFromNamespaceById', newList, {root: true})
}
ctx.dispatch('namespaces/loadNamespacesIfFavoritesDontExist', null, {root: true})
ctx.dispatch('namespaces/removeFavoritesNamespaceIfEmpty', null, {root: true})
return newList
} catch (e) {
// Reset the list state to the initial one to avoid confusion for the user
ctx.commit('setList', {
...list,
isFavorite: !list.isFavorite,
})
throw e
} finally {
cancel()
}
2021-07-06 22:22:57 +02:00
},
2022-07-21 00:42:36 +02:00
async deleteList(ctx: ActionContext<ListState, RootStoreState>, list: IList) {
const cancel = setLoading(ctx, 'lists')
const listService = new ListService()
try {
const response = await listService.delete(list)
ctx.commit('removeListById', list)
ctx.commit('namespaces/removeListFromNamespaceById', list, {root: true})
removeListFromHistory({id: list.id})
return response
} finally {
cancel()
}
},
},
2020-05-11 16:52:58 +02:00
}