2026-04-21 23:40:52 +02:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { onMount } from 'svelte';
|
|
|
|
|
import { ShoppingCart } from 'lucide-svelte';
|
|
|
|
|
import type { ShoppingListSnapshot } from '$lib/server/shopping/repository';
|
2026-04-21 23:43:00 +02:00
|
|
|
import ShoppingListRow from '$lib/components/ShoppingListRow.svelte';
|
2026-04-21 23:45:32 +02:00
|
|
|
import ShoppingCartChip from '$lib/components/ShoppingCartChip.svelte';
|
2026-04-21 23:43:00 +02:00
|
|
|
import type { ShoppingListRow as Row } from '$lib/server/shopping/repository';
|
|
|
|
|
import { shoppingCartStore } from '$lib/client/shopping-cart.svelte';
|
2026-04-21 23:40:52 +02:00
|
|
|
|
|
|
|
|
let snapshot = $state<ShoppingListSnapshot>({ recipes: [], rows: [], uncheckedCount: 0 });
|
|
|
|
|
let loading = $state(true);
|
|
|
|
|
|
|
|
|
|
async function load() {
|
|
|
|
|
loading = true;
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch('/api/shopping-list');
|
|
|
|
|
snapshot = await res.json();
|
|
|
|
|
} finally {
|
|
|
|
|
loading = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 23:43:00 +02:00
|
|
|
async function onToggleRow(row: Row, next: boolean) {
|
|
|
|
|
const method = next ? 'POST' : 'DELETE';
|
|
|
|
|
await fetch('/api/shopping-list/check', {
|
|
|
|
|
method,
|
|
|
|
|
headers: { 'content-type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ name_key: row.name_key, unit_key: row.unit_key })
|
|
|
|
|
});
|
|
|
|
|
await load();
|
|
|
|
|
void shoppingCartStore.refresh();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 23:45:32 +02:00
|
|
|
async function onServingsChange(recipeId: number, servings: number) {
|
|
|
|
|
await fetch(`/api/shopping-list/recipe/${recipeId}`, {
|
|
|
|
|
method: 'PATCH',
|
|
|
|
|
headers: { 'content-type': 'application/json' },
|
|
|
|
|
body: JSON.stringify({ servings })
|
|
|
|
|
});
|
|
|
|
|
await load();
|
|
|
|
|
void shoppingCartStore.refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function onRemoveRecipe(recipeId: number) {
|
|
|
|
|
await fetch(`/api/shopping-list/recipe/${recipeId}`, { method: 'DELETE' });
|
|
|
|
|
await load();
|
|
|
|
|
void shoppingCartStore.refresh();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 23:40:52 +02:00
|
|
|
onMount(load);
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<header class="head">
|
|
|
|
|
<h1>Einkaufsliste</h1>
|
|
|
|
|
{#if snapshot.recipes.length > 0}
|
|
|
|
|
<p class="sub">
|
|
|
|
|
{snapshot.uncheckedCount} noch zu besorgen · {snapshot.recipes.length} Rezept{snapshot.recipes.length === 1 ? '' : 'e'} im Wagen
|
|
|
|
|
</p>
|
|
|
|
|
{/if}
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
{#if loading}
|
|
|
|
|
<p class="muted">Lädt …</p>
|
|
|
|
|
{:else if snapshot.recipes.length === 0}
|
|
|
|
|
<section class="empty">
|
|
|
|
|
<div class="big"><ShoppingCart size={48} strokeWidth={1.5} /></div>
|
|
|
|
|
<p>Einkaufswagen ist leer.</p>
|
|
|
|
|
<p class="hint">Lege Rezepte auf der Wunschliste in den Wagen, um sie hier zu sehen.</p>
|
|
|
|
|
</section>
|
2026-04-21 23:43:00 +02:00
|
|
|
{:else}
|
2026-04-21 23:45:32 +02:00
|
|
|
<div class="chips">
|
|
|
|
|
{#each snapshot.recipes as r (r.recipe_id)}
|
|
|
|
|
<ShoppingCartChip recipe={r} {onServingsChange} onRemove={onRemoveRecipe} />
|
|
|
|
|
{/each}
|
|
|
|
|
</div>
|
2026-04-21 23:43:00 +02:00
|
|
|
<ul class="list">
|
|
|
|
|
{#each snapshot.rows as row (row.name_key + '|' + row.unit_key)}
|
|
|
|
|
<li>
|
|
|
|
|
<ShoppingListRow {row} onToggle={onToggleRow} />
|
|
|
|
|
</li>
|
|
|
|
|
{/each}
|
|
|
|
|
</ul>
|
2026-04-21 23:40:52 +02:00
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
.head { padding: 1.25rem 0 0.5rem; }
|
|
|
|
|
.head h1 { margin: 0; font-size: 1.6rem; color: #2b6a3d; }
|
|
|
|
|
.sub { margin: 0.2rem 0 0; color: #666; }
|
|
|
|
|
.muted { color: #888; text-align: center; padding: 2rem 0; }
|
|
|
|
|
.empty { text-align: center; padding: 3rem 1rem; }
|
|
|
|
|
.big { color: #8fb097; display: inline-flex; margin: 0 0 0.5rem; }
|
|
|
|
|
.hint { color: #888; font-size: 0.9rem; }
|
2026-04-21 23:43:00 +02:00
|
|
|
.list {
|
|
|
|
|
list-style: none;
|
|
|
|
|
padding: 0;
|
|
|
|
|
margin: 0.75rem 0;
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
gap: 0.5rem;
|
|
|
|
|
}
|
2026-04-21 23:45:32 +02:00
|
|
|
.chips {
|
|
|
|
|
display: flex;
|
|
|
|
|
gap: 0.5rem;
|
|
|
|
|
overflow-x: auto;
|
|
|
|
|
padding: 0.5rem 0;
|
|
|
|
|
margin: 0.5rem 0;
|
|
|
|
|
-webkit-overflow-scrolling: touch;
|
|
|
|
|
}
|
2026-04-21 23:40:52 +02:00
|
|
|
</style>
|