fix: add category and favorites filters to all recipe pages

- Move categories logic into Search component for centralization
- Add isLoggedIn prop to SeasonLayout and IconLayout components
- Fix FilterPanel CSS to properly handle hidden favorites filter
- Fix FavoritesFilter to trigger onToggle when checkbox changes
- Update Search effect to track all filter states (category, tags, icon, season, favorites)
- Hide favorites filter on favorites page while maintaining proper grid layout
- All filters now work consistently across entire site
This commit is contained in:
2026-01-10 17:19:55 +01:00
parent 7ab3482850
commit bc170abcdf
12 changed files with 44 additions and 28 deletions

View File

@@ -14,13 +14,13 @@
let checked = $state(enabled); let checked = $state(enabled);
// Watch for changes to checked and call onToggle
$effect(() => { $effect(() => {
checked = enabled; // Track checked as dependency
const currentChecked = checked;
// Call onToggle whenever checked changes
onToggle(currentChecked);
}); });
function handleChange() {
onToggle(checked);
}
</script> </script>
<style> <style>
@@ -69,6 +69,5 @@
<Toggle <Toggle
bind:checked={checked} bind:checked={checked}
label="" label=""
onchange={handleChange}
/> />
</div> </div>

View File

@@ -17,6 +17,7 @@
selectedFavoritesOnly = false, selectedFavoritesOnly = false,
lang = 'de', lang = 'de',
isLoggedIn = false, isLoggedIn = false,
hideFavoritesFilter = false,
onCategoryChange = () => {}, onCategoryChange = () => {},
onTagToggle = () => {}, onTagToggle = () => {},
onIconChange = () => {}, onIconChange = () => {},
@@ -133,7 +134,7 @@
<span class="arrow" class:open={filtersOpen}>▼</span> <span class="arrow" class:open={filtersOpen}>▼</span>
</button> </button>
<div class="filter-panel" class:open={filtersOpen} class:with-favorites={isLoggedIn} class:without-favorites={!isLoggedIn}> <div class="filter-panel" class:open={filtersOpen} class:with-favorites={isLoggedIn && !hideFavoritesFilter} class:without-favorites={!isLoggedIn || hideFavoritesFilter}>
<CategoryFilter <CategoryFilter
categories={availableCategories} categories={availableCategories}
selected={selectedCategory} selected={selectedCategory}
@@ -162,7 +163,7 @@
{months} {months}
/> />
{#if isLoggedIn} {#if isLoggedIn && !hideFavoritesFilter}
<FavoritesFilter <FavoritesFilter
enabled={selectedFavoritesOnly} enabled={selectedFavoritesOnly}
onToggle={onFavoritesToggle} onToggle={onFavoritesToggle}

View File

@@ -10,6 +10,7 @@
routePrefix = '/rezepte', routePrefix = '/rezepte',
lang = 'de', lang = 'de',
recipes = [], recipes = [],
isLoggedIn = false,
onSearchResults = (ids, categories) => {}, onSearchResults = (ids, categories) => {},
recipesSlot recipesSlot
}: { }: {
@@ -18,6 +19,7 @@
routePrefix?: string, routePrefix?: string,
lang?: string, lang?: string,
recipes?: any[], recipes?: any[],
isLoggedIn?: boolean,
onSearchResults?: (ids: any[], categories: any[]) => void, onSearchResults?: (ids: any[], categories: any[]) => void,
recipesSlot?: Snippet recipesSlot?: Snippet
} = $props(); } = $props();
@@ -89,7 +91,7 @@
{/each} {/each}
</div> </div>
<section> <section>
<Search icon={active_icon} {lang} {recipes} {onSearchResults}></Search> <Search icon={active_icon} {lang} {recipes} {isLoggedIn} {onSearchResults}></Search>
</section> </section>
<section> <section>
{@render recipesSlot?.()} {@render recipesSlot?.()}

View File

@@ -3,6 +3,7 @@
import { browser } from '$app/environment'; import { browser } from '$app/environment';
import "$lib/css/nordtheme.css"; import "$lib/css/nordtheme.css";
import FilterPanel from './FilterPanel.svelte'; import FilterPanel from './FilterPanel.svelte';
import { getCategories } from '$lib/js/categories';
// Filter props for different contexts // Filter props for different contexts
let { let {
@@ -13,11 +14,13 @@
favoritesOnly = false, favoritesOnly = false,
lang = 'de', lang = 'de',
recipes = [], recipes = [],
categories = [],
onSearchResults = (matchedIds, matchedCategories) => {}, onSearchResults = (matchedIds, matchedCategories) => {},
isLoggedIn = false isLoggedIn = false
} = $props(); } = $props();
// Generate categories internally based on language
const categories = $derived(getCategories(lang));
const isEnglish = $derived(lang === 'en'); const isEnglish = $derived(lang === 'en');
const searchResultsUrl = $derived(isEnglish ? '/recipes/search' : '/rezepte/search'); const searchResultsUrl = $derived(isEnglish ? '/recipes/search' : '/rezepte/search');
const labels = $derived({ const labels = $derived({
@@ -158,10 +161,9 @@
} }
} }
// Filter change handlers // Filter change handlers - the effect will automatically trigger search
function handleCategoryChange(newCategory) { function handleCategoryChange(newCategory) {
selectedCategory = newCategory; selectedCategory = newCategory;
performSearch(searchQuery);
} }
function handleTagToggle(tag) { function handleTagToggle(tag) {
@@ -170,22 +172,18 @@
} else { } else {
selectedTags = [...selectedTags, tag]; selectedTags = [...selectedTags, tag];
} }
performSearch(searchQuery);
} }
function handleIconChange(newIcon) { function handleIconChange(newIcon) {
selectedIcon = newIcon; selectedIcon = newIcon;
performSearch(searchQuery);
} }
function handleSeasonChange(newSeasons) { function handleSeasonChange(newSeasons) {
selectedSeasons = newSeasons; selectedSeasons = newSeasons;
performSearch(searchQuery);
} }
function handleFavoritesToggle(enabled) { function handleFavoritesToggle(enabled) {
selectedFavoritesOnly = enabled; selectedFavoritesOnly = enabled;
performSearch(searchQuery);
} }
function handleSubmit(event) { function handleSubmit(event) {
@@ -202,11 +200,16 @@
performSearch(''); performSearch('');
} }
// Debounced search effect - only triggers search 100ms after user stops typing // Debounced search effect - triggers search when query or filters change
$effect(() => { $effect(() => {
if (browser && recipes.length > 0) { if (browser && recipes.length > 0) {
// Read searchQuery to track it as a dependency // Read all dependencies to track them
const query = searchQuery; const query = searchQuery;
const cat = selectedCategory;
const tags = selectedTags;
const icn = selectedIcon;
const seasons = selectedSeasons;
const favsOnly = selectedFavoritesOnly;
// Set debounce timer // Set debounce timer
const timer = setTimeout(() => { const timer = setTimeout(() => {
@@ -357,6 +360,7 @@ scale: 0.8 0.8;
{selectedFavoritesOnly} {selectedFavoritesOnly}
{lang} {lang}
{isLoggedIn} {isLoggedIn}
hideFavoritesFilter={favoritesOnly}
onCategoryChange={handleCategoryChange} onCategoryChange={handleCategoryChange}
onTagToggle={handleTagToggle} onTagToggle={handleTagToggle}
onIconChange={handleIconChange} onIconChange={handleIconChange}

View File

@@ -10,6 +10,7 @@
routePrefix = '/rezepte', routePrefix = '/rezepte',
lang = 'de', lang = 'de',
recipes = [], recipes = [],
isLoggedIn = false,
onSearchResults = (ids, categories) => {}, onSearchResults = (ids, categories) => {},
recipesSlot recipesSlot
}: { }: {
@@ -18,6 +19,7 @@
routePrefix?: string, routePrefix?: string,
lang?: string, lang?: string,
recipes?: any[], recipes?: any[],
isLoggedIn?: boolean,
onSearchResults?: (ids: any[], categories: any[]) => void, onSearchResults?: (ids: any[], categories: any[]) => void,
recipesSlot?: Snippet recipesSlot?: Snippet
} = $props(); } = $props();
@@ -58,7 +60,7 @@ a.month:hover,
{/each} {/each}
</div> </div>
<section> <section>
<Search season={active_index + 1} {lang} {recipes} {onSearchResults}></Search> <Search season={active_index + 1} {lang} {recipes} {isLoggedIn} {onSearchResults}></Search>
</section> </section>
<section> <section>
{@render recipesSlot?.()} {@render recipesSlot?.()}

9
src/lib/js/categories.ts Normal file
View File

@@ -0,0 +1,9 @@
/**
* Get the list of recipe categories based on language
*/
export function getCategories(lang: string): string[] {
const isEnglish = lang === 'en';
return isEnglish
? ["Main course", "Noodle", "Bread", "Dessert", "Soup", "Side dish", "Salad", "Cake", "Breakfast", "Sauce", "Ingredient", "Drink", "Spread", "Biscuits", "Snack"]
: ["Hauptspeise", "Nudel", "Brot", "Dessert", "Suppe", "Beilage", "Salat", "Kuchen", "Frühstück", "Sauce", "Zutat", "Getränk", "Aufstrich", "Guetzli", "Snack"];
}

View File

@@ -5,6 +5,7 @@
import Card from '$lib/components/Card.svelte'; import Card from '$lib/components/Card.svelte';
import Search from '$lib/components/Search.svelte'; import Search from '$lib/components/Search.svelte';
import LazyCategory from '$lib/components/LazyCategory.svelte'; import LazyCategory from '$lib/components/LazyCategory.svelte';
import { getCategories } from '$lib/js/categories';
let { data } = $props<{ data: PageData }>(); let { data } = $props<{ data: PageData }>();
let current_month = new Date().getMonth() + 1; let current_month = new Date().getMonth() + 1;
@@ -21,9 +22,7 @@
} }
const isEnglish = $derived(data.lang === 'en'); const isEnglish = $derived(data.lang === 'en');
const categories = $derived(isEnglish const categories = $derived(getCategories(data.lang));
? ["Main course", "Noodle", "Bread", "Dessert", "Soup", "Side dish", "Salad", "Cake", "Breakfast", "Sauce", "Ingredient", "Drink", "Spread", "Biscuits", "Snack"]
: ["Hauptspeise", "Nudel", "Brot", "Dessert", "Suppe", "Beilage", "Salat", "Kuchen", "Frühstück", "Sauce", "Zutat", "Getränk", "Aufstrich", "Guetzli", "Snack"]);
// Pre-compute category-to-recipes Map for O(1) lookups // Pre-compute category-to-recipes Map for O(1) lookups
const recipesByCategory = $derived.by(() => { const recipesByCategory = $derived.by(() => {
@@ -101,7 +100,7 @@ h1{
<h1>{labels.title}</h1> <h1>{labels.title}</h1>
<p class=subheading>{labels.subheading}</p> <p class=subheading>{labels.subheading}</p>
<Search lang={data.lang} recipes={data.all_brief} categories={categories} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search> <Search lang={data.lang} recipes={data.all_brief} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
{#if seasonRecipes.length > 0} {#if seasonRecipes.length > 0}
<LazyCategory title={labels.inSeason} eager={true}> <LazyCategory title={labels.inSeason} eager={true}>

View File

@@ -35,7 +35,7 @@
} }
</style> </style>
<h1>{label} <q>{data.category}</q>:</h1> <h1>{label} <q>{data.category}</q>:</h1>
<Search category={data.category} lang={data.lang} recipes={data.recipes} onSearchResults={handleSearchResults}></Search> <Search category={data.category} lang={data.lang} recipes={data.recipes} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
<section> <section>
<Recipes> <Recipes>
{#each rand_array(filteredRecipes) as recipe} {#each rand_array(filteredRecipes) as recipe}

View File

@@ -79,7 +79,7 @@ h1{
{/if} {/if}
</p> </p>
<Search favoritesOnly={true} lang={data.lang} recipes={data.favorites} onSearchResults={handleSearchResults}></Search> <Search favoritesOnly={true} lang={data.lang} recipes={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

@@ -26,7 +26,7 @@
return data.season.filter(r => matchedRecipeIds.has(r._id)); return data.season.filter(r => matchedRecipeIds.has(r._id));
}); });
</script> </script>
<IconLayout icons={data.icons} active_icon={data.icon} routePrefix="/{data.recipeLang}" lang={data.lang} recipes={data.season} onSearchResults={handleSearchResults}> <IconLayout icons={data.icons} active_icon={data.icon} routePrefix="/{data.recipeLang}" lang={data.lang} recipes={data.season} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}>
{#snippet recipesSlot()} {#snippet recipesSlot()}
<Recipes> <Recipes>
{#each rand_array(filteredRecipes) as recipe} {#each rand_array(filteredRecipes) as recipe}

View File

@@ -34,7 +34,7 @@
}); });
</script> </script>
<SeasonLayout active_index={current_month-1} {months} routePrefix="/{data.recipeLang}" lang={data.lang} recipes={data.season} onSearchResults={handleSearchResults}> <SeasonLayout active_index={current_month-1} {months} routePrefix="/{data.recipeLang}" lang={data.lang} recipes={data.season} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}>
{#snippet recipesSlot()} {#snippet recipesSlot()}
<Recipes> <Recipes>
{#each rand_array(filteredRecipes) as recipe} {#each rand_array(filteredRecipes) as recipe}

View File

@@ -35,7 +35,7 @@
} }
</style> </style>
<h1>{label} <q>{data.tag}</q>:</h1> <h1>{label} <q>{data.tag}</q>:</h1>
<Search tag={data.tag} lang={data.lang} recipes={data.recipes} onSearchResults={handleSearchResults}></Search> <Search tag={data.tag} lang={data.lang} recipes={data.recipes} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
<section> <section>
<Recipes> <Recipes>
{#each rand_array(filteredRecipes) as recipe} {#each rand_array(filteredRecipes) as recipe}