diff --git a/src/stores/lists.ts b/src/stores/lists.ts index 7d0f9832f..e4521ff29 100644 --- a/src/stores/lists.ts +++ b/src/stores/lists.ts @@ -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() diff --git a/src/stores/namespaces.ts b/src/stores/namespaces.ts index bf44f645c..34b297a39 100644 --- a/src/stores/namespaces.ts +++ b/src/stores/namespaces.ts @@ -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)