diff --git a/frontend/src/views/Gallery/Platform.vue b/frontend/src/views/Gallery/Platform.vue index 9ed783dc0..3fff5801a 100644 --- a/frontend/src/views/Gallery/Platform.vue +++ b/frontend/src/views/Gallery/Platform.vue @@ -219,7 +219,6 @@ const filterToSetFilter: Record = { onMounted(async () => { const routePlatformId = Number(route.params.platform); - const routePlatform = platformsStore.get(routePlatformId); watch( () => allPlatforms.value, @@ -228,14 +227,22 @@ onMounted(async () => { platforms.length > 0 && platforms.some((platform) => platform.id === routePlatformId) ) { - romsStore.setCurrentPlatform( - platforms.find((platform) => platform.id === routePlatformId), + const newPlatform = platforms.find( + (platform) => platform.id === routePlatformId, ); - resetGallery(); - fetchRoms(); - setFilters(); - // Check if there are query params to set filters + // Check if the current platform is different or no ROMs have been loaded + if ( + currentPlatform.value?.id !== routePlatformId || + allRoms.value.length === 0 + ) { + romsStore.setCurrentPlatform(newPlatform); + resetGallery(); + fetchRoms(); + setFilters(); + } + + // Check for query params to set filters if (route.query.filter && route.query.value) { const filter = route.query.filter as FilterType; const value = route.query.value as string; @@ -248,40 +255,42 @@ onMounted(async () => { window.addEventListener("scroll", onScroll); } }, - { immediate: true }, // This ensures the watcher is triggered immediately if allPlatforms is already populated + { immediate: true }, // Ensure watcher is triggered immediately ); }); onBeforeRouteUpdate(async (to, from) => { - // Triggers when change param of the same route - // Reset store if switching to another platform + // Avoid unnecessary actions if navigating within the same path if (to.path === from.path) return; resetGallery(); const routePlatformId = Number(to.params.platform); - const routePlatform = platformsStore.get(routePlatformId); watch( () => allPlatforms.value, (platforms) => { - if ( - platforms.length > 0 && - platforms.some((platform) => platform.id === routePlatformId) - ) { - romsStore.setCurrentPlatform( - platforms.find((platform) => platform.id === routePlatformId), + if (platforms.length > 0) { + const newPlatform = platforms.find( + (platform) => platform.id === routePlatformId, ); - fetchRoms(); - setFilters(); + + // Only trigger fetchRoms if switching platforms or ROMs are not loaded + if ( + currentPlatform.value?.id !== routePlatformId || + allRoms.value.length === 0 + ) { + romsStore.setCurrentPlatform(newPlatform); + fetchRoms(); + setFilters(); + } } }, - { immediate: true }, // This ensures the watcher is triggered immediately if allPlatforms is already populated + { immediate: true }, // Ensure watcher is triggered immediately ); }); onBeforeUnmount(() => { - romsStore.setCurrentPlatform(null); window.removeEventListener("wheel", onScroll); window.removeEventListener("scroll", onScroll); }); diff --git a/frontend/src/views/GameDetails.vue b/frontend/src/views/GameDetails.vue index 52e24a85f..a450ff2c3 100644 --- a/frontend/src/views/GameDetails.vue +++ b/frontend/src/views/GameDetails.vue @@ -26,7 +26,6 @@ import { useDisplay } from "vuetify"; // Props const route = useRoute(); -const platform = ref(); const tab = ref< | "details" | "saves" @@ -40,9 +39,10 @@ const { smAndDown, mdAndDown, mdAndUp, lgAndUp } = useDisplay(); const emitter = inject>("emitter"); const noRomError = ref(false); const romsStore = storeRoms(); -const { currentRom } = storeToRefs(romsStore); +const { currentRom, currentPlatform, gettingRoms } = storeToRefs(romsStore); async function fetchDetails() { + gettingRoms.value = true; await romApi .getRom({ romId: parseInt(route.params.rom as string) }) .then(({ data }) => { @@ -54,44 +54,49 @@ async function fetchDetails() { }) .finally(() => { emitter?.emit("showLoadingDialog", { loading: false, scrim: false }); + gettingRoms.value = false; }); if (!noRomError.value) { const platformsStore = storePlatforms(); const { allPlatforms } = storeToRefs(platformsStore); - platform.value = allPlatforms.value.find( + currentPlatform.value = allPlatforms.value.find( (platform) => platform.id === currentRom.value?.platform_id, ); } } onBeforeMount(async () => { - emitter?.emit("showLoadingDialog", { loading: true, scrim: false }); - if (currentRom.value?.id == parseInt(route.params.rom as string)) { - emitter?.emit("showLoadingDialog", { loading: false, scrim: false }); - } else { + const romId = parseInt(route.params.rom as string); + + // Only fetch details if the currentRom ID differs + if (currentRom.value?.id !== romId) { + emitter?.emit("showLoadingDialog", { loading: true, scrim: false }); await fetchDetails(); + } else { + emitter?.emit("showLoadingDialog", { loading: false, scrim: false }); } + const downloadStore = storeDownload(); downloadStore.clear(); }); -onBeforeRouteLeave(() => { - currentRom.value = null; - return true; -}); - watch( () => route.fullPath, async () => { - await fetchDetails(); + const romId = parseInt(route.params.rom as string); + + // Only fetch details if the currentRom ID differs + if (currentRom.value?.id !== romId) { + await fetchDetails(); + } }, );