added last visited collection on memory for faster loading

This commit is contained in:
zurdi
2024-12-03 15:11:30 +00:00
parent 2f80b3cbac
commit 656097d2cb
2 changed files with 56 additions and 35 deletions

View File

@@ -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);
});

View File

@@ -32,6 +32,7 @@ const {
filteredRoms,
selectedRoms,
currentPlatform,
currentCollection,
itemsPerBatch,
gettingRoms,
} = storeToRefs(romsStore);
@@ -218,6 +219,7 @@ const filterToSetFilter: Record<FilterType, Function> = {
onMounted(async () => {
const routePlatformId = Number(route.params.platform);
currentCollection.value = null;
watch(
() => allPlatforms.value,