recipes: fix filter panel on category, tag, and favorites pages
All checks were successful
CI / update (push) Successful in 1m55s

Search component needs access to all recipes to filter across
categories/tags/etc. Previously these pages only passed their
pre-filtered subset, so selecting additional filters yielded
no results. Now each page fetches allRecipes in parallel and
passes it to Search, falling back to the route-specific subset
when no filters are active.
This commit is contained in:
2026-03-10 10:36:21 +01:00
parent 03fb0dde95
commit e427dc2d25
6 changed files with 69 additions and 23 deletions

View File

@@ -4,18 +4,19 @@ import { getUserFavorites, addFavoriteStatusToRecipes } from "$lib/server/favori
export const load: PageServerLoad = async ({ fetch, locals, params }) => { export const load: PageServerLoad = async ({ fetch, locals, params }) => {
const apiBase = `/api/${params.recipeLang}`; const apiBase = `/api/${params.recipeLang}`;
const res = await fetch(`${apiBase}/items/category/${params.category}`); const [res, allRes, userFavorites, session] = await Promise.all([
const items = await res.json(); fetch(`${apiBase}/items/category/${params.category}`),
fetch(`${apiBase}/items/all_brief`),
// Get user favorites and session
const [userFavorites, session] = await Promise.all([
getUserFavorites(fetch, locals), getUserFavorites(fetch, locals),
locals.auth() locals.auth()
]); ]);
const [items, allRecipes] = await Promise.all([res.json(), allRes.json()]);
return { return {
category: params.category, category: params.category,
recipes: addFavoriteStatusToRecipes(items, userFavorites), recipes: addFavoriteStatusToRecipes(items, userFavorites),
allRecipes: addFavoriteStatusToRecipes(allRecipes, userFavorites),
session session
}; };
}; };

View File

@@ -3,7 +3,6 @@
import Search from '$lib/components/recipes/Search.svelte'; import Search from '$lib/components/recipes/Search.svelte';
import CompactCard from '$lib/components/recipes/CompactCard.svelte'; import CompactCard from '$lib/components/recipes/CompactCard.svelte';
import { rand_array } from '$lib/js/randomize'; import { rand_array } from '$lib/js/randomize';
import { createSearchFilter } from '$lib/js/searchFilter.svelte';
let { data } = $props<{ data: PageData }>(); let { data } = $props<{ data: PageData }>();
let current_month = new Date().getMonth() + 1; let current_month = new Date().getMonth() + 1;
@@ -12,7 +11,18 @@
const label = $derived(isEnglish ? 'Recipes in Category' : 'Rezepte in Kategorie'); const label = $derived(isEnglish ? 'Recipes in Category' : 'Rezepte in Kategorie');
const siteTitle = $derived(isEnglish ? 'Bocken Recipes' : 'Bocken Rezepte'); const siteTitle = $derived(isEnglish ? 'Bocken Recipes' : 'Bocken Rezepte');
const { filtered: filteredRecipes, handleSearchResults } = createSearchFilter(() => data.recipes); let matchedRecipeIds = $state(new Set());
let hasActiveSearch = $state(false);
function handleSearchResults(ids: Set<string>, categories: Set<string>) {
matchedRecipeIds = ids;
hasActiveSearch = ids.size < data.allRecipes.length;
}
const displayRecipes = $derived.by(() => {
if (!hasActiveSearch) return data.recipes;
return data.allRecipes.filter((r: any) => matchedRecipeIds.has(r._id));
});
</script> </script>
<style> <style>
h1 { h1 {
@@ -26,9 +36,9 @@
</svelte:head> </svelte:head>
<h1>{label} <q>{data.category}</q>:</h1> <h1>{label} <q>{data.category}</q>:</h1>
<Search category={data.category} lang={data.lang} recipes={data.recipes} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search> <Search category={data.category} lang={data.lang} recipes={data.allRecipes} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
<div class="recipe-grid"> <div class="recipe-grid">
{#each rand_array(filteredRecipes) as recipe (recipe._id)} {#each rand_array(displayRecipes) as recipe (recipe._id)}
<CompactCard {recipe} {current_month} isFavorite={recipe.isFavorite} showFavoriteIndicator={!!data.session?.user} routePrefix="/{data.recipeLang}" /> <CompactCard {recipe} {current_month} isFavorite={recipe.isFavorite} showFavoriteIndicator={!!data.session?.user} routePrefix="/{data.recipeLang}" />
{/each} {/each}
</div> </div>

View File

@@ -11,15 +11,20 @@ export const load: PageServerLoad = async ({ fetch, locals, params }) => {
} }
try { try {
const res = await fetch(`${apiBase}/favorites/recipes`); const [res, allRes] = await Promise.all([
fetch(`${apiBase}/favorites/recipes`),
fetch(`${apiBase}/items/all_brief`)
]);
if (!res.ok) { if (!res.ok) {
return { return {
favorites: [], favorites: [],
allRecipes: [],
error: 'Failed to load favorites' error: 'Failed to load favorites'
}; };
} }
const favorites = await res.json(); const [favorites, allRecipes] = await Promise.all([res.json(), allRes.json()]);
// Mark all favorites with isFavorite flag for filter compatibility // Mark all favorites with isFavorite flag for filter compatibility
const favoritesWithFlag = favorites.map((recipe: any) => ({ const favoritesWithFlag = favorites.map((recipe: any) => ({
@@ -27,13 +32,22 @@ export const load: PageServerLoad = async ({ fetch, locals, params }) => {
isFavorite: true isFavorite: true
})); }));
// Get favorite IDs for marking in allRecipes
const favoriteIds = new Set(favoritesWithFlag.map((r: any) => r._id));
const allRecipesWithFavorites = allRecipes.map((recipe: any) => ({
...recipe,
isFavorite: favoriteIds.has(recipe._id)
}));
return { return {
favorites: favoritesWithFlag, favorites: favoritesWithFlag,
allRecipes: allRecipesWithFavorites,
session session
}; };
} catch (e) { } catch (e) {
return { return {
favorites: [], favorites: [],
allRecipes: [],
error: 'Failed to load favorites' error: 'Failed to load favorites'
}; };
} }

View File

@@ -2,7 +2,6 @@
import type { PageData } from './$types'; import type { PageData } from './$types';
import CompactCard from '$lib/components/recipes/CompactCard.svelte'; import CompactCard from '$lib/components/recipes/CompactCard.svelte';
import Search from '$lib/components/recipes/Search.svelte'; import Search from '$lib/components/recipes/Search.svelte';
import { createSearchFilter } from '$lib/js/searchFilter.svelte';
let { data } = $props<{ data: PageData }>(); let { data } = $props<{ data: PageData }>();
let current_month = new Date().getMonth() + 1; let current_month = new Date().getMonth() + 1;
@@ -29,7 +28,18 @@
toTry: isEnglish ? 'Recipes to try' : 'Zum Ausprobieren' toTry: isEnglish ? 'Recipes to try' : 'Zum Ausprobieren'
}); });
const { filtered: filteredFavorites, handleSearchResults } = createSearchFilter(() => data.favorites); let matchedRecipeIds = $state(new Set());
let hasActiveSearch = $state(false);
function handleSearchResults(ids: Set<string>, categories: Set<string>) {
matchedRecipeIds = ids;
hasActiveSearch = ids.size < (data.allRecipes?.length || data.favorites.length);
}
const filteredFavorites = $derived.by(() => {
if (!hasActiveSearch) return data.favorites;
return data.allRecipes.filter((r: any) => matchedRecipeIds.has(r._id));
});
</script> </script>
<style> <style>
@@ -88,7 +98,7 @@ h1{
<p class="to-try-link"><a href="/{data.recipeLang}/to-try">{labels.toTry} &rarr;</a></p> <p class="to-try-link"><a href="/{data.recipeLang}/to-try">{labels.toTry} &rarr;</a></p>
<Search favoritesOnly={true} lang={data.lang} recipes={data.favorites} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search> <Search favoritesOnly={true} lang={data.lang} recipes={data.allRecipes || data.favorites} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
{#if data.error} {#if data.error}
<p class="empty-state">{labels.errorLoading} {data.error}</p> <p class="empty-state">{labels.errorLoading} {data.error}</p>

View File

@@ -4,18 +4,19 @@ import { getUserFavorites, addFavoriteStatusToRecipes } from "$lib/server/favori
export const load: PageServerLoad = async ({ fetch, locals, params }) => { export const load: PageServerLoad = async ({ fetch, locals, params }) => {
const apiBase = `/api/${params.recipeLang}`; const apiBase = `/api/${params.recipeLang}`;
const res_tag = await fetch(`${apiBase}/items/tag/${params.tag}`); const [res_tag, allRes, userFavorites, session] = await Promise.all([
const items_tag = await res_tag.json(); fetch(`${apiBase}/items/tag/${params.tag}`),
fetch(`${apiBase}/items/all_brief`),
// Get user favorites and session
const [userFavorites, session] = await Promise.all([
getUserFavorites(fetch, locals), getUserFavorites(fetch, locals),
locals.auth() locals.auth()
]); ]);
const [items_tag, allRecipes] = await Promise.all([res_tag.json(), allRes.json()]);
return { return {
tag: params.tag, tag: params.tag,
recipes: addFavoriteStatusToRecipes(items_tag, userFavorites), recipes: addFavoriteStatusToRecipes(items_tag, userFavorites),
allRecipes: addFavoriteStatusToRecipes(allRecipes, userFavorites),
session session
}; };
}; };

View File

@@ -3,7 +3,6 @@
import CompactCard from '$lib/components/recipes/CompactCard.svelte'; import CompactCard from '$lib/components/recipes/CompactCard.svelte';
import Search from '$lib/components/recipes/Search.svelte'; import Search from '$lib/components/recipes/Search.svelte';
import { rand_array } from '$lib/js/randomize'; import { rand_array } from '$lib/js/randomize';
import { createSearchFilter } from '$lib/js/searchFilter.svelte';
let { data } = $props<{ data: PageData }>(); let { data } = $props<{ data: PageData }>();
let current_month = new Date().getMonth() + 1; let current_month = new Date().getMonth() + 1;
@@ -12,7 +11,18 @@
const label = $derived(isEnglish ? 'Recipes with Keyword' : 'Rezepte mit Stichwort'); const label = $derived(isEnglish ? 'Recipes with Keyword' : 'Rezepte mit Stichwort');
const siteTitle = $derived(isEnglish ? 'Bocken Recipes' : 'Bocken Rezepte'); const siteTitle = $derived(isEnglish ? 'Bocken Recipes' : 'Bocken Rezepte');
const { filtered: filteredRecipes, handleSearchResults } = createSearchFilter(() => data.recipes); let matchedRecipeIds = $state(new Set());
let hasActiveSearch = $state(false);
function handleSearchResults(ids: Set<string>, categories: Set<string>) {
matchedRecipeIds = ids;
hasActiveSearch = ids.size < data.allRecipes.length;
}
const displayRecipes = $derived.by(() => {
if (!hasActiveSearch) return data.recipes;
return data.allRecipes.filter((r: any) => matchedRecipeIds.has(r._id));
});
</script> </script>
<style> <style>
h1 { h1 {
@@ -26,9 +36,9 @@
</svelte:head> </svelte:head>
<h1>{label} <q>{data.tag}</q>:</h1> <h1>{label} <q>{data.tag}</q>:</h1>
<Search tag={data.tag} lang={data.lang} recipes={data.recipes} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search> <Search tag={data.tag} lang={data.lang} recipes={data.allRecipes} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
<div class="recipe-grid"> <div class="recipe-grid">
{#each rand_array(filteredRecipes) as recipe (recipe._id)} {#each rand_array(displayRecipes) as recipe (recipe._id)}
<CompactCard {recipe} {current_month} isFavorite={recipe.isFavorite} showFavoriteIndicator={!!data.session?.user} routePrefix="/{data.recipeLang}" /> <CompactCard {recipe} {current_month} isFavorite={recipe.isFavorite} showFavoriteIndicator={!!data.session?.user} routePrefix="/{data.recipeLang}" />
{/each} {/each}
</div> </div>