From 656097d2cb8a0fc527a027b622da2dac996d46f4 Mon Sep 17 00:00:00 2001 From: zurdi Date: Tue, 3 Dec 2024 15:11:30 +0000 Subject: [PATCH] added last visited collection on memory for faster loading --- frontend/src/views/Gallery/Collection.vue | 89 ++++++++++++++--------- frontend/src/views/Gallery/Platform.vue | 2 + 2 files changed, 56 insertions(+), 35 deletions(-) diff --git a/frontend/src/views/Gallery/Collection.vue b/frontend/src/views/Gallery/Collection.vue index 1615093e2..0029411e8 100644 --- a/frontend/src/views/Gallery/Collection.vue +++ b/frontend/src/views/Gallery/Collection.vue @@ -5,7 +5,6 @@ import EmptyCollection from "@/components/common/EmptyCollection.vue"; import EmptyGame from "@/components/common/EmptyGame.vue"; import GameCard from "@/components/common/Game/Card/Base.vue"; import GameDataTable from "@/components/common/Game/Table.vue"; -import collectionApi from "@/services/api/collection"; import romApi from "@/services/api/rom"; import storeCollections from "@/stores/collections"; import storeGalleryFilter from "@/stores/galleryFilter"; @@ -15,7 +14,7 @@ import type { Events } from "@/types/emitter"; import { normalizeString, views } from "@/utils"; import type { Emitter } from "mitt"; import { storeToRefs } from "pinia"; -import { inject, onBeforeUnmount, onMounted, ref } from "vue"; +import { inject, onBeforeUnmount, onMounted, ref, watch } from "vue"; import { onBeforeRouteUpdate, useRoute, useRouter } from "vue-router"; import { useDisplay } from "vuetify"; @@ -26,11 +25,13 @@ const galleryViewStore = storeGalleryView(); const galleryFilterStore = storeGalleryFilter(); const { scrolledToTop, currentView } = storeToRefs(galleryViewStore); const collectionsStore = storeCollections(); +const { allCollections } = storeToRefs(collectionsStore); const romsStore = storeRoms(); const { allRoms, filteredRoms, selectedRoms, + currentPlatform, currentCollection, itemsPerBatch, gettingRoms, @@ -210,29 +211,37 @@ function resetGallery() { onMounted(async () => { const routeCollectionId = Number(route.params.collection); - const routeCollection = collectionsStore.get(routeCollectionId); + currentPlatform.value = null; - if (!routeCollection) { - await collectionApi - .getCollection(routeCollectionId) - .then((data) => { - collectionsStore.add(data.data); - romsStore.setCurrentCollection(data.data); - }) - .catch((error) => { - console.log(error); - noCollectionError.value = true; - }); - } else { - romsStore.setCurrentCollection(routeCollection); - } - if (!noCollectionError.value) { - resetGallery(); - await fetchRoms(); - setFilters(); - window.addEventListener("wheel", onScroll); - window.addEventListener("scroll", onScroll); - } + watch( + () => allCollections.value, + (collections) => { + if ( + collections.length > 0 && + collections.some((collection) => collection.id === routeCollectionId) + ) { + const collection = collections.find( + (collection) => collection.id === routeCollectionId, + ); + + // Check if the current platform is different or no ROMs have been loaded + if ( + (currentCollection.value?.id !== routeCollectionId || + allRoms.value.length === 0) && + collection + ) { + romsStore.setCurrentCollection(collection); + resetGallery(); + fetchRoms(); + setFilters(); + } + + window.addEventListener("wheel", onScroll); + window.addEventListener("scroll", onScroll); + } + }, + { immediate: true }, // Ensure watcher is triggered immediately + ); }); onBeforeRouteUpdate(async (to, from) => { @@ -243,22 +252,32 @@ onBeforeRouteUpdate(async (to, from) => { resetGallery(); const routeCollectionId = Number(to.params.collection); - const routeCollection = collectionsStore.get(routeCollectionId); - if (!routeCollection) { - const { data } = await collectionApi.getCollection(routeCollectionId); - collectionsStore.add(data); - } else { - romsStore.setCurrentCollection(routeCollection); - } - await fetchRoms(); - setFilters(); + watch( + () => allCollections.value, + (collections) => { + if (collections.length > 0) { + const collection = collections.find( + (collection) => collection.id === routeCollectionId, + ); - return true; + // Only trigger fetchRoms if switching platforms or ROMs are not loaded + if ( + (currentCollection.value?.id !== routeCollectionId || + allRoms.value.length === 0) && + collection + ) { + romsStore.setCurrentCollection(collection); + fetchRoms(); + setFilters(); + } + } + }, + { immediate: true }, // Ensure watcher is triggered immediately + ); }); onBeforeUnmount(() => { - romsStore.setCurrentCollection(null); window.removeEventListener("wheel", onScroll); window.removeEventListener("scroll", onScroll); }); diff --git a/frontend/src/views/Gallery/Platform.vue b/frontend/src/views/Gallery/Platform.vue index 4b815a444..40a8be6bd 100644 --- a/frontend/src/views/Gallery/Platform.vue +++ b/frontend/src/views/Gallery/Platform.vue @@ -32,6 +32,7 @@ const { filteredRoms, selectedRoms, currentPlatform, + currentCollection, itemsPerBatch, gettingRoms, } = storeToRefs(romsStore); @@ -218,6 +219,7 @@ const filterToSetFilter: Record = { onMounted(async () => { const routePlatformId = Number(route.params.platform); + currentCollection.value = null; watch( () => allPlatforms.value,