From ceb163570f56cdcf365ce42a4db01ba779570c11 Mon Sep 17 00:00:00 2001 From: advplyr Date: Fri, 29 Apr 2022 18:57:46 -0500 Subject: [PATCH] Fix:Set next accessible library when currently selected library is removed --- client/layouts/default.vue | 22 +++++++++++++++++++++- client/store/libraries.js | 13 +++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/client/layouts/default.vue b/client/layouts/default.vue index 2f14f426..c7c170e8 100644 --- a/client/layouts/default.vue +++ b/client/layouts/default.vue @@ -161,8 +161,28 @@ export default { libraryUpdated(library) { this.$store.commit('libraries/addUpdate', library) }, - libraryRemoved(library) { + async libraryRemoved(library) { this.$store.commit('libraries/remove', library) + + // When removed currently selected library then set next accessible library + const currLibraryId = this.$store.state.libraries.currentLibraryId + if (currLibraryId === library.id) { + var nextLibrary = this.$store.getters['libraries/getNextAccessibleLibrary'] + if (nextLibrary) { + await this.$store.dispatch('libraries/fetch', nextLibrary.id) + + if (this.$route.name.startsWith('config')) { + // No need to refresh + } else if (this.$route.name.startsWith('library')) { + var newRoute = this.$route.path.replace(currLibraryId, nextLibrary.id) + this.$router.push(newRoute) + } else { + this.$router.push(`/library/${nextLibrary.id}`) + } + } else { + console.error('User has no accessible libraries') + } + } }, libraryItemAdded(libraryItem) { // this.$store.commit('libraries/updateFilterDataWithAudiobook', libraryItem) diff --git a/client/store/libraries.js b/client/store/libraries.js index c3866ada..7890a4e7 100644 --- a/client/store/libraries.js +++ b/client/store/libraries.js @@ -29,6 +29,19 @@ export const getters = { var library = state.libraries.find(l => l.id === libraryId) if (!library) return null return library.provider + }, + getNextAccessibleLibrary: (state, getters, rootState, rootGetters) => { + var librariesSorted = getters['getSortedLibraries']() + if (!librariesSorted.length) return null + + var canAccessAllLibraries = rootGetters['user/getUserCanAccessAllLibraries'] + var userAccessibleLibraries = rootGetters['user/getLibrariesAccessible'] + if (canAccessAllLibraries) return librariesSorted[0] + librariesSorted = librariesSorted.filter((lib) => { + return userAccessibleLibraries.includes(lib.id) + }) + if (!librariesSorted.length) return null + return librariesSorted[0] } }