Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ROMM-1019] Clickable filter buttons on details view #1040

Merged
merged 4 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions frontend/src/components/Details/Info/GameInfo.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
<script setup lang="ts">
import storeGalleryFilter from "@/stores/galleryFilter";
import storeGalleryFilter, { type FilterType } from "@/stores/galleryFilter";
import type { DetailedRom } from "@/stores/roms";
import { ref } from "vue";
import { useDisplay } from "vuetify";
import { useRouter } from "vue-router";

defineProps<{ rom: DetailedRom }>();
const props = defineProps<{ rom: DetailedRom }>();
const { xs } = useDisplay();
const galleryFilter = storeGalleryFilter();
const galleryFilterStore = storeGalleryFilter();
const show = ref(false);
const router = useRouter();

function onFilterClick(filter: FilterType, value: string) {
router.push({
name: "platform",
params: { platform: props.rom.platform_id },
query: { filter, value },
});
}
</script>
<template>
<v-row no-gutters>
<v-col>
<v-divider class="mx-2 my-4" />
<template v-for="filter in galleryFilter.filters" :key="filter">
<template v-for="filter in galleryFilterStore.filters" :key="filter">
<v-row
v-if="rom[filter].length > 0"
class="align-center my-3"
Expand All @@ -26,6 +36,7 @@ const show = ref(false);
<v-chip
v-for="value in rom[filter]"
:key="value"
@click="onFilterClick(filter, value)"
size="small"
variant="outlined"
class="my-1 mr-2"
Expand Down
16 changes: 10 additions & 6 deletions frontend/src/stores/galleryFilter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { normalizeString } from "@/utils";
import { defineStore } from "pinia";

const filters = ["genres", "franchises", "collections", "companies"] as const;

export type FilterType = (typeof filters)[number];

export default defineStore("galleryFilter", {
state: () => ({
activeFilterDrawer: false,
filterSearch: "",
filters: ["genres", "franchises", "collections", "companies"] as const,
filters: filters,
filterGenres: [] as string[],
filterFranchises: [] as string[],
filterCollections: [] as string[],
Expand All @@ -25,19 +29,19 @@ export default defineStore("galleryFilter", {
setFilterSearch(filterSearch: string) {
this.filterSearch = normalizeString(filterSearch);
},
setFilterGenre(genres: string[]) {
setFilterGenres(genres: string[]) {
this.filterGenres = genres;
},
setFilterFranchise(franchises: string[]) {
setFilterFranchises(franchises: string[]) {
this.filterFranchises = franchises;
},
setFilterCollection(collections: string[]) {
setFilterCollections(collections: string[]) {
this.filterCollections = collections;
},
setFilterCompany(companies: string[]) {
setFilterCompanies(companies: string[]) {
this.filterCompanies = companies;
},
setSelectedGenre(genre: string) {
setSelectedFilterGenre(genre: string) {
this.selectedGenre = genre;
},
setSelectedFilterFranchise(franchise: string) {
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/views/Gallery/Collection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,28 @@ async function fetchRoms() {
}

function setFilters() {
galleryFilterStore.setFilterGenre([
galleryFilterStore.setFilterGenres([
...new Set(
romsStore.filteredRoms
.flatMap((rom) => rom.genres.map((genre) => genre))
.sort()
),
]);
galleryFilterStore.setFilterFranchise([
galleryFilterStore.setFilterFranchises([
...new Set(
romsStore.filteredRoms
.flatMap((rom) => rom.franchises.map((franchise) => franchise))
.sort()
),
]);
galleryFilterStore.setFilterCompany([
galleryFilterStore.setFilterCompanies([
...new Set(
romsStore.filteredRoms
.flatMap((rom) => rom.companies.map((company) => company))
.sort()
),
]);
galleryFilterStore.setFilterCollection([
galleryFilterStore.setFilterCollections([
...new Set(
romsStore.filteredRoms
.flatMap((rom) => rom.collections.map((collection) => collection))
Expand Down
27 changes: 22 additions & 5 deletions frontend/src/views/Gallery/Platform.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import GameCard from "@/components/common/Game/Card/Base.vue";
import GameDataTable from "@/components/common/Game/Table.vue";
import platformApi from "@/services/api/platform";
import romApi from "@/services/api/rom";
import storeGalleryFilter from "@/stores/galleryFilter";
import storeGalleryFilter, { type FilterType } from "@/stores/galleryFilter";
import storeGalleryView from "@/stores/galleryView";
import storePlatforms from "@/stores/platforms";
import storeRoms, { type SimpleRom } from "@/stores/roms";
Expand Down Expand Up @@ -83,28 +83,28 @@ async function fetchRoms() {
}

function setFilters() {
galleryFilterStore.setFilterGenre([
galleryFilterStore.setFilterGenres([
...new Set(
romsStore.filteredRoms
.flatMap((rom) => rom.genres.map((genre) => genre))
.sort()
),
]);
galleryFilterStore.setFilterFranchise([
galleryFilterStore.setFilterFranchises([
...new Set(
romsStore.filteredRoms
.flatMap((rom) => rom.franchises.map((franchise) => franchise))
.sort()
),
]);
galleryFilterStore.setFilterCompany([
galleryFilterStore.setFilterCompanies([
...new Set(
romsStore.filteredRoms
.flatMap((rom) => rom.companies.map((company) => company))
.sort()
),
]);
galleryFilterStore.setFilterCollection([
galleryFilterStore.setFilterCollections([
...new Set(
romsStore.filteredRoms
.flatMap((rom) => rom.collections.map((collection) => collection))
Expand Down Expand Up @@ -196,6 +196,13 @@ function resetGallery() {
itemsShown.value = itemsPerBatch.value;
}

const filterToSetFilter: Record<FilterType, Function> = {
genres: galleryFilterStore.setSelectedFilterGenre,
franchises: galleryFilterStore.setSelectedFilterFranchise,
collections: galleryFilterStore.setSelectedFilterCollection,
companies: galleryFilterStore.setSelectedFilterCompany,
};

onMounted(async () => {
const routePlatformId = Number(route.params.platform);
const routePlatform = platforms.get(routePlatformId);
Expand All @@ -219,6 +226,16 @@ onMounted(async () => {
resetGallery();
await fetchRoms();
setFilters();

// Check if there are 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;
filterToSetFilter[filter](value);
onFilterChange(); // Update the UI
router.replace({ query: {} }); // Clear query params
Copy link
Member

@zurdi15 zurdi15 Aug 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we want to clear query parameters once filters are loaded?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc not clearing them out causes some weird behavior, and this was the fix

}

window.addEventListener("wheel", onScroll);
window.addEventListener("scroll", onScroll);
}
Expand Down
Loading