improved api requests of gallery and details based on stores

This commit is contained in:
zurdi
2024-11-29 00:01:11 +00:00
parent a4867252d9
commit faea201628
2 changed files with 50 additions and 36 deletions

View File

@@ -219,7 +219,6 @@ const filterToSetFilter: Record<FilterType, Function> = {
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);
});

View File

@@ -26,7 +26,6 @@ import { useDisplay } from "vuetify";
// Props
const route = useRoute();
const platform = ref<Platform>();
const tab = ref<
| "details"
| "saves"
@@ -40,9 +39,10 @@ const { smAndDown, mdAndDown, mdAndUp, lgAndUp } = useDisplay();
const emitter = inject<Emitter<Events>>("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();
}
},
);
</script>
<template>
<!-- TODO: review layout on certain roms - ej: mortal kombat 2 for gb -->
<template v-if="currentRom && platform">
<template v-if="currentRom && currentPlatform && !gettingRoms">
<background-header />
<v-row
@@ -134,7 +139,7 @@ watch(
'justify-center': smAndDown,
}"
>
<title-info :rom="currentRom" :platform="platform" />
<title-info :rom="currentRom" :platform="currentPlatform" />
</div>
<v-row
:class="{