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

This commit is contained in:
Dominik Pschenitschni 2022-11-20 23:09:52 +01:00
parent cba7692f16
commit fad87f12e0
Signed by: dpschen
GPG Key ID: B257AC0149F43A77
2 changed files with 11 additions and 14 deletions

View File

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