[ROMM-1495] Fix filters in filter bar

This commit is contained in:
Georges-Antoine Assi
2025-01-16 14:48:41 -05:00
parent 424266c65c
commit 5ee3f631eb
3 changed files with 92 additions and 92 deletions

View File

@@ -61,45 +61,44 @@ function setFilters() {
]);
}
function fetchRoms() {
async function fetchRoms() {
if (searchText.value) {
// Auto hide android keyboard
const inputElement = document.getElementById("search-text-field");
inputElement?.blur();
gettingRoms.value = true;
romApi
.getRoms({ searchTerm: searchText.value })
.then(({ data }) => {
data = data.sort((a, b) => {
return a.platform_name.localeCompare(b.platform_name);
});
romsStore.set(data);
romsStore.setFiltered(data, galleryFilterStore);
})
.catch((error) => {
emitter?.emit("snackbarShow", {
msg: `Couldn't fetch roms: ${error}`,
icon: "mdi-close-circle",
color: "red",
timeout: 4000,
});
console.error(`Couldn't fetch roms: ${error}`);
})
.finally(() => {
gettingRoms.value = false;
try {
const { data } = await romApi.getRoms({ searchTerm: searchText.value });
const sortedData = data.sort((a, b) => {
return a.platform_name.localeCompare(b.platform_name);
});
galleryFilterStore.setFilterPlatforms([
...new Map(
romsStore.filteredRoms.map((rom) => {
const platform = allPlatforms.value.find(
(p) => p.id === rom.platform_id,
);
return [rom.platform_name, platform];
}),
).values(),
] as Platform[]);
setFilters();
galleryFilterStore.activeFilterDrawer = false;
romsStore.set(sortedData);
romsStore.setFiltered(sortedData, galleryFilterStore);
} catch (error) {
emitter?.emit("snackbarShow", {
msg: `Couldn't fetch roms: ${error}`,
icon: "mdi-close-circle",
color: "red",
timeout: 4000,
});
console.error(`Couldn't fetch roms: ${error}`);
} finally {
gettingRoms.value = false;
galleryFilterStore.setFilterPlatforms([
...new Map(
romsStore.filteredRoms.map((rom) => {
const platform = allPlatforms.value.find(
(p) => p.id === rom.platform_id,
);
return [rom.platform_name, platform];
}),
).values(),
] as Platform[]);
setFilters();
galleryFilterStore.activeFilterDrawer = false;
}
}
}
</script>

View File

@@ -53,34 +53,37 @@ async function fetchRoms() {
scrim: false,
});
await romApi
.getRoms({
try {
const { data } = await romApi.getRoms({
collectionId: romsStore.currentCollection?.id,
searchTerm: normalizeString(galleryFilterStore.filterText),
})
.then(({ data }) => {
romsStore.set(data);
romsStore.setFiltered(data, galleryFilterStore);
})
.catch((error) => {
emitter?.emit("snackbarShow", {
msg: `Couldn't fetch roms for collection ID ${currentCollection.value?.id}: ${error}`,
icon: "mdi-close-circle",
color: "red",
timeout: 4000,
});
console.error(
`Couldn't fetch roms for collection ID ${currentCollection.value?.id}: ${error}`,
);
noCollectionError.value = true;
})
.finally(() => {
gettingRoms.value = false;
emitter?.emit("showLoadingDialog", {
loading: gettingRoms.value,
scrim: false,
});
});
romsStore.set(data);
romsStore.setFiltered(data, galleryFilterStore);
gettingRoms.value = false;
emitter?.emit("showLoadingDialog", {
loading: gettingRoms.value,
scrim: false,
});
} catch (error) {
emitter?.emit("snackbarShow", {
msg: `Couldn't fetch roms for collection ID ${currentCollection.value?.id}: ${error}`,
icon: "mdi-close-circle",
color: "red",
timeout: 4000,
});
console.error(
`Couldn't fetch roms for collection ID ${currentCollection.value?.id}: ${error}`,
);
noCollectionError.value = true;
} finally {
gettingRoms.value = false;
emitter?.emit("showLoadingDialog", {
loading: gettingRoms.value,
scrim: false,
});
}
}
function setFilters() {
@@ -216,7 +219,7 @@ onMounted(async () => {
watch(
() => allCollections.value,
(collections) => {
async (collections) => {
if (
collections.length > 0 &&
collections.some((collection) => collection.id === routeCollectionId)
@@ -233,7 +236,7 @@ onMounted(async () => {
) {
romsStore.setCurrentCollection(collection);
resetGallery();
fetchRoms();
await fetchRoms();
setFilters();
}
@@ -256,7 +259,7 @@ onBeforeRouteUpdate(async (to, from) => {
watch(
() => allCollections.value,
(collections) => {
async (collections) => {
if (collections.length > 0) {
const collection = collections.find(
(collection) => collection.id === routeCollectionId,
@@ -269,7 +272,7 @@ onBeforeRouteUpdate(async (to, from) => {
collection
) {
romsStore.setCurrentCollection(collection);
fetchRoms();
await fetchRoms();
setFilters();
}
}

View File

@@ -44,7 +44,7 @@ const emitter = inject<Emitter<Events>>("emitter");
emitter?.on("filter", onFilterChange);
// Functions
function fetchRoms() {
async function fetchRoms() {
if (gettingRoms.value) return;
gettingRoms.value = true;
@@ -53,34 +53,32 @@ function fetchRoms() {
scrim: false,
});
romApi
.getRoms({
try {
const { data } = await romApi.getRoms({
platformId: romsStore.currentPlatform?.id,
searchTerm: normalizeString(galleryFilterStore.filterText),
})
.then(({ data }) => {
romsStore.set(data);
romsStore.setFiltered(data, galleryFilterStore);
})
.catch((error) => {
emitter?.emit("snackbarShow", {
msg: `Couldn't fetch roms for platform ID ${currentPlatform.value?.id}: ${error}`,
icon: "mdi-close-circle",
color: "red",
timeout: 4000,
});
console.error(
`Couldn't fetch roms for platform ID ${currentPlatform.value?.id}: ${error}`,
);
noPlatformError.value = true;
})
.finally(() => {
gettingRoms.value = false;
emitter?.emit("showLoadingDialog", {
loading: gettingRoms.value,
scrim: false,
});
});
romsStore.set(data);
romsStore.setFiltered(data, galleryFilterStore);
} catch (error) {
emitter?.emit("snackbarShow", {
msg: `Couldn't fetch roms for platform ID ${currentPlatform.value?.id}: ${error}`,
icon: "mdi-close-circle",
color: "red",
timeout: 4000,
});
console.error(
`Couldn't fetch roms for platform ID ${currentPlatform.value?.id}: ${error}`,
);
noPlatformError.value = true;
} finally {
gettingRoms.value = false;
emitter?.emit("showLoadingDialog", {
loading: gettingRoms.value,
scrim: false,
});
}
}
function setFilters() {
@@ -225,7 +223,7 @@ onMounted(async () => {
watch(
() => allPlatforms.value,
(platforms) => {
async (platforms) => {
if (platforms.length > 0) {
if (platforms.some((platform) => platform.id === routePlatformId)) {
const platform = platforms.find(
@@ -240,7 +238,7 @@ onMounted(async () => {
) {
romsStore.setCurrentPlatform(platform);
resetGallery();
fetchRoms();
await fetchRoms();
setFilters();
}
@@ -274,7 +272,7 @@ onBeforeRouteUpdate(async (to, from) => {
watch(
() => allPlatforms.value,
(platforms) => {
async (platforms) => {
if (platforms.length > 0) {
const platform = platforms.find(
(platform) => platform.id === routePlatformId,
@@ -287,7 +285,7 @@ onBeforeRouteUpdate(async (to, from) => {
platform
) {
romsStore.setCurrentPlatform(platform);
fetchRoms();
await fetchRoms();
setFilters();
} else {
noPlatformError.value = true;