fix: setFavorite
continuous-integration/drone/pr Build is failing Details

This commit is contained in:
Dominik Pschenitschni 2022-11-20 23:09:52 +01:00
parent 78661dd57f
commit 5fbd4c80cb
Signed by: dpschen
GPG Key ID: B257AC0149F43A77
2 changed files with 12 additions and 15 deletions

View File

@ -29,6 +29,7 @@ import {success} from '@/message'
import {i18n} from '@/i18n'
import BackgroundUploadService from '@/services/backgroundUpload'
import {getBlurHash} from '@/helpers/blurhash'
import cloneDeep from 'lodash.clonedeep'
const {add, remove, search, update} = createNewIndexer('lists', ['title', 'description'])
@ -136,6 +137,8 @@ export const useListStore = defineStore('list', () => {
const cancel = setModuleLoading(setIsLoading)
const listService = new ListService()
const oldList = cloneDeep(getListById.value(list.id) as IList)
try {
await listService.update(list)
setList(list)
@ -143,24 +146,18 @@ export const useListStore = defineStore('list', () => {
// the returned list from listService.update is the same!
// in order to not create a manipulation in pinia store we have to create a new copy
const newList = {
...list,
namespaceId: NAMESPACE_ID.FAVORITES,
}
namespaceStore.removeListFromNamespaceById(newList)
const newList = {...list}
if (list.isFavorite) {
namespaceStore.addListToNamespace(newList)
namespaceStore.addListToNamespace(newList, NAMESPACE_ID.FAVORITES)
} else {
namespaceStore.removeListFromNamespaceById(newList, NAMESPACE_ID.FAVORITES)
}
namespaceStore.loadNamespacesIfFavoritesDontExist()
namespaceStore.removeFavoritesNamespaceIfEmpty()
return newList
} catch (e) {
// Reset the list state to the initial one to avoid confusion for the user
setList({
...list,
isFavorite: !list.isFavorite,
})
setList(oldList)
throw e
} finally {
cancel()

View File

@ -127,20 +127,20 @@ export const useNamespaceStore = defineStore('namespace', () => {
}
}
function addListToNamespace(list: IList) {
function addListToNamespace(list: IList, namespaceId?: INamespace['id']) {
for (const n in namespaces.value) {
if (namespaces.value[n].id === list.namespaceId) {
if (namespaces.value[n].id === (namespaceId ?? list.namespaceId)) {
namespaces.value[n].lists.push(list)
return
}
}
}
function removeListFromNamespaceById(list: IList) {
function removeListFromNamespaceById(list: IList, namespaceId?: INamespace['id']) {
for (const n in namespaces.value) {
// We don't have the namespace id on the list which means we need to loop over all lists until we find it.
// FIXME: Not ideal at all - we should fix that at the api level.
if (namespaces.value[n].id === list.namespaceId) {
if (namespaces.value[n].id === (namespaceId ?? list.namespaceId)) {
for (const l in namespaces.value[n].lists) {
if (namespaces.value[n].lists[l].id === list.id) {
namespaces.value[n].lists.splice(l, 1)