2022-09-21 18:21:25 +02:00
|
|
|
import {watch, reactive, shallowReactive, unref, toRefs, readonly} from 'vue'
|
2022-09-21 22:45:11 +02:00
|
|
|
import {acceptHMRUpdate, defineStore} from 'pinia'
|
2022-09-21 18:21:25 +02:00
|
|
|
import {useI18n} from 'vue-i18n'
|
|
|
|
|
|
|
|
import ListService from '@/services/list'
|
2022-09-30 12:52:21 +02:00
|
|
|
import {setModuleLoading} from '@/stores/helper'
|
2022-09-21 18:21:25 +02:00
|
|
|
import {removeListFromHistory} from '@/modules/listHistory'
|
|
|
|
import {createNewIndexer} from '@/indexes'
|
2022-09-02 11:15:29 +02:00
|
|
|
import {useNamespaceStore} from './namespaces'
|
2022-09-21 18:21:25 +02:00
|
|
|
|
|
|
|
import type {IList} from '@/modelTypes/IList'
|
|
|
|
|
|
|
|
import type {MaybeRef} from '@vueuse/core'
|
|
|
|
|
|
|
|
import ListModel from '@/models/list'
|
|
|
|
import {success} from '@/message'
|
|
|
|
|
|
|
|
const {add, remove, search, update} = createNewIndexer('lists', ['title', 'description'])
|
|
|
|
|
|
|
|
const FavoriteListsNamespace = -2
|
|
|
|
|
2022-09-24 15:20:40 +02:00
|
|
|
export interface ListState {
|
|
|
|
lists: { [id: IList['id']]: IList },
|
|
|
|
isLoading: boolean,
|
|
|
|
}
|
|
|
|
|
2022-09-21 18:21:25 +02:00
|
|
|
export const useListStore = defineStore('list', {
|
|
|
|
state: () : ListState => ({
|
|
|
|
isLoading: false,
|
|
|
|
// The lists are stored as an object which has the list ids as keys.
|
|
|
|
lists: {},
|
|
|
|
}),
|
|
|
|
|
|
|
|
getters: {
|
|
|
|
getListById(state) {
|
|
|
|
return (id: IList['id']) => typeof state.lists[id] !== 'undefined' ? state.lists[id] : null
|
|
|
|
},
|
|
|
|
|
|
|
|
findListByExactname(state) {
|
|
|
|
return (name: string) => {
|
|
|
|
const list = Object.values(state.lists).find(l => {
|
|
|
|
return l.title.toLowerCase() === name.toLowerCase()
|
|
|
|
})
|
|
|
|
return typeof list === 'undefined' ? null : list
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
searchList(state) {
|
|
|
|
return (query: string, includeArchived = false) => {
|
|
|
|
return search(query)
|
|
|
|
?.filter(value => value > 0)
|
|
|
|
.map(id => state.lists[id])
|
|
|
|
.filter(list => list.isArchived === includeArchived)
|
|
|
|
|| []
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
setIsLoading(isLoading: boolean) {
|
|
|
|
this.isLoading = isLoading
|
|
|
|
},
|
|
|
|
|
|
|
|
setList(list: IList) {
|
|
|
|
this.lists[list.id] = list
|
|
|
|
update(list)
|
|
|
|
},
|
|
|
|
|
|
|
|
setLists(lists: IList[]) {
|
|
|
|
lists.forEach(l => {
|
|
|
|
this.lists[l.id] = l
|
|
|
|
add(l)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
removeListById(list: IList) {
|
|
|
|
remove(list)
|
|
|
|
delete this.lists[list.id]
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleListFavorite(list: IList) {
|
|
|
|
// The favorites pseudo list is always favorite
|
|
|
|
// Archived lists cannot be marked favorite
|
|
|
|
if (list.id === -1 || list.isArchived) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return this.updateList({
|
|
|
|
...list,
|
|
|
|
isFavorite: !list.isFavorite,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
async createList(list: IList) {
|
2022-09-30 12:52:21 +02:00
|
|
|
const cancel = setModuleLoading(this)
|
2022-09-21 18:21:25 +02:00
|
|
|
const listService = new ListService()
|
|
|
|
|
|
|
|
try {
|
|
|
|
const createdList = await listService.create(list)
|
|
|
|
createdList.namespaceId = list.namespaceId
|
2022-09-02 11:15:29 +02:00
|
|
|
const namespaceStore = useNamespaceStore()
|
|
|
|
namespaceStore.addListToNamespace(createdList)
|
2022-09-21 18:21:25 +02:00
|
|
|
this.setList(createdList)
|
|
|
|
return createdList
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async updateList(list: IList) {
|
2022-09-30 12:52:21 +02:00
|
|
|
const cancel = setModuleLoading(this)
|
2022-09-21 18:21:25 +02:00
|
|
|
const listService = new ListService()
|
|
|
|
|
|
|
|
try {
|
|
|
|
await listService.update(list)
|
|
|
|
this.setList(list)
|
2022-09-02 11:15:29 +02:00
|
|
|
const namespaceStore = useNamespaceStore()
|
|
|
|
namespaceStore.setListInNamespaceById(list)
|
2022-09-21 18:21:25 +02:00
|
|
|
|
|
|
|
// the returned list from listService.update is the same!
|
2022-09-24 15:20:40 +02:00
|
|
|
// in order to not create a manipulation in pinia store we have to create a new copy
|
2022-09-21 18:21:25 +02:00
|
|
|
const newList = {
|
|
|
|
...list,
|
|
|
|
namespaceId: FavoriteListsNamespace,
|
|
|
|
}
|
|
|
|
if (list.isFavorite) {
|
2022-09-02 11:15:29 +02:00
|
|
|
namespaceStore.addListToNamespace(newList)
|
2022-09-21 18:21:25 +02:00
|
|
|
} else {
|
2022-09-02 11:15:29 +02:00
|
|
|
namespaceStore.removeListFromNamespaceById(newList)
|
2022-09-21 18:21:25 +02:00
|
|
|
}
|
2022-09-02 11:15:29 +02:00
|
|
|
namespaceStore.loadNamespacesIfFavoritesDontExist(null)
|
|
|
|
namespaceStore.removeFavoritesNamespaceIfEmpty(null)
|
2022-09-21 18:21:25 +02:00
|
|
|
return newList
|
|
|
|
} catch (e) {
|
|
|
|
// Reset the list state to the initial one to avoid confusion for the user
|
|
|
|
this.setList({
|
|
|
|
...list,
|
|
|
|
isFavorite: !list.isFavorite,
|
|
|
|
})
|
|
|
|
throw e
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async deleteList(list: IList) {
|
2022-09-30 12:52:21 +02:00
|
|
|
const cancel = setModuleLoading(this)
|
2022-09-21 18:21:25 +02:00
|
|
|
const listService = new ListService()
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await listService.delete(list)
|
|
|
|
this.removeListById(list)
|
2022-09-02 11:15:29 +02:00
|
|
|
const namespaceStore = useNamespaceStore()
|
|
|
|
namespaceStore.removeListFromNamespaceById(list)
|
2022-09-21 18:21:25 +02:00
|
|
|
removeListFromHistory({id: list.id})
|
|
|
|
return response
|
|
|
|
} finally {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-09-15 12:52:38 +02:00
|
|
|
export function useList(listId: MaybeRef<IList['id']>) {
|
2022-09-21 18:21:25 +02:00
|
|
|
const listService = shallowReactive(new ListService())
|
|
|
|
const {loading: isLoading} = toRefs(listService)
|
2022-09-15 12:52:38 +02:00
|
|
|
const list: ListModel = reactive(new ListModel())
|
2022-09-21 18:21:25 +02:00
|
|
|
const {t} = useI18n({useScope: 'global'})
|
|
|
|
|
|
|
|
watch(
|
|
|
|
() => unref(listId),
|
|
|
|
async (listId) => {
|
|
|
|
const loadedList = await listService.get(new ListModel({id: listId}))
|
|
|
|
Object.assign(list, loadedList)
|
|
|
|
},
|
|
|
|
{immediate: true},
|
|
|
|
)
|
|
|
|
|
|
|
|
const listStore = useListStore()
|
|
|
|
async function save() {
|
|
|
|
await listStore.updateList(list)
|
|
|
|
success({message: t('list.edit.success')})
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
isLoading: readonly(isLoading),
|
|
|
|
list,
|
|
|
|
save,
|
|
|
|
}
|
2022-09-21 22:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// support hot reloading
|
|
|
|
if (import.meta.hot) {
|
|
|
|
import.meta.hot.accept(acceptHMRUpdate(useListStore, import.meta.hot))
|
2022-09-21 18:21:25 +02:00
|
|
|
}
|