Compare commits
32 Commits
cleanup-ba
...
v1.2.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
633e497bdc | ||
|
|
b5c01b950e | ||
|
|
6bde3909d8 | ||
|
|
78c4f56992 | ||
|
|
c07d2f99ad | ||
|
|
8069c5c246 | ||
|
|
7d6ee04fec | ||
|
|
b646720a6e | ||
|
|
526c7433f4 | ||
|
|
96cb55495e | ||
|
|
a1baf7f30a | ||
|
|
b0d5f921e2 | ||
|
|
72816d6b35 | ||
|
|
ad5a6afcd9 | ||
|
|
30a409fd16 | ||
|
|
504fbb6cc6 | ||
|
|
d50841c5a6 | ||
|
|
defbb5e24d | ||
|
|
c43b1dca87 | ||
|
|
015cb432fb | ||
|
|
f273942286 | ||
|
|
c45ef2a613 | ||
|
|
e7067971a5 | ||
|
|
0ca42f3329 | ||
|
|
4b17f19038 | ||
|
|
4edddc38e3 | ||
|
|
fc47c78397 | ||
|
|
58ce19c160 | ||
|
|
7fd90643c5 | ||
|
|
3021ccb6a9 | ||
|
|
a7ad159c69 | ||
|
|
7da37d0a3d |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,4 +7,5 @@ data/
|
|||||||
*.log
|
*.log
|
||||||
test-results/
|
test-results/
|
||||||
playwright-report/
|
playwright-report/
|
||||||
|
playwright-report-remote/
|
||||||
.playwright-mcp/
|
.playwright-mcp/
|
||||||
|
|||||||
@@ -26,12 +26,14 @@ Selbstgehostete Rezept-PWA für die Familie Siegeln. Erreichbar unter `https://k
|
|||||||
- `src/routes/+layout.svelte` — Header, mobile expand, Dropdown-Search auf Rezeptseiten
|
- `src/routes/+layout.svelte` — Header, mobile expand, Dropdown-Search auf Rezeptseiten
|
||||||
- `src/routes/recipes/[id]/+page.svelte` — Rezept-Detail mit allen Actions (Rating, Favorit, Cooked, Wunschliste, Kommentar, Umbenennen, Löschen)
|
- `src/routes/recipes/[id]/+page.svelte` — Rezept-Detail mit allen Actions (Rating, Favorit, Cooked, Wunschliste, Kommentar, Umbenennen, Löschen)
|
||||||
- `src/routes/preview/+page.svelte` — importierte Vorschau vor dem Speichern
|
- `src/routes/preview/+page.svelte` — importierte Vorschau vor dem Speichern
|
||||||
|
- `src/lib/components/RecipeView.svelte` / `RecipeEditor.svelte` — Lesen/Edit-Mode des Rezepts. Editor ist in Sub-Components aufgeteilt: `IngredientRow`, `StepList`, `ImageUploadBox`, `TimeDisplay` (+ shared types `recipe-editor-types.ts`)
|
||||||
- `src/lib/server/search/searxng.ts` — Web-Suche + Thumbnail-Enrichment + SQLite-Cache
|
- `src/lib/server/search/searxng.ts` — Web-Suche + Thumbnail-Enrichment + SQLite-Cache
|
||||||
- `src/lib/server/recipes/importer.ts` — JSON-LD → Recipe, orchestriert Bild-Download
|
- `src/lib/server/recipes/importer.ts` — JSON-LD → Recipe, orchestriert Bild-Download
|
||||||
- `src/lib/server/db/migrations/*.sql` — Schema; bei Änderung immer **neue** Migration statt bestehende bearbeiten
|
- `src/lib/server/db/migrations/*.sql` — Schema; bei Änderung immer **neue** Migration statt bestehende bearbeiten
|
||||||
- `src/service-worker.ts` — Service-Worker-Orchestrator (Shell-Cache + Pre-Cache + SWR)
|
- `src/service-worker.ts` — Service-Worker-Orchestrator (Shell-Cache + Pre-Cache + SWR)
|
||||||
- `src/lib/sw/` — reine Logik (Cache-Strategy-Entscheider, Diff-Manifest) für Unit-Tests
|
- `src/lib/sw/` — reine Logik (Cache-Strategy-Entscheider, Diff-Manifest) für Unit-Tests
|
||||||
- `src/lib/client/*.svelte.ts` — Frontend-Stores (Network, Sync-Status, Toast, Install-Prompt)
|
- `src/lib/client/*.svelte.ts` — Frontend-Stores (Search, Network, Sync-Status, Toast, Install-Prompt, Wishlist, PWA, Profile, Confirm, Search-Filter)
|
||||||
|
- `tests/e2e/remote/` — Playwright gegen `kochwas-dev.siegeln.net` (CRUD erlaubt; workers:1, serviceWorkers:block)
|
||||||
|
|
||||||
## Arbeitsweise (wie wir es machen)
|
## Arbeitsweise (wie wir es machen)
|
||||||
|
|
||||||
@@ -67,7 +69,7 @@ docker compose -f docker-compose.prod.yml up --build
|
|||||||
|
|
||||||
## Offene Themen / Stand
|
## Offene Themen / Stand
|
||||||
|
|
||||||
Siehe Session-Handoff-Dokumente unter `docs/superpowers/` und dort besonders `session-handoff-2026-04-17.md`. Die Roadmap-Phasen liegen als `docs/superpowers/plans/*.md`. Was als „Later" markiert ist, ist nicht beauftragt.
|
Siehe die Plan-Dateien unter `docs/superpowers/plans/*.md` für abgeschlossene Implementierungs-Phasen (v1.0 Foundations → v1.1 Offline-PWA → Post-Review-Roadmap → Search-State-Store → Editor-Split → Ingredient-Sections = v1.2). Was als „Later" markiert ist, ist nicht beauftragt.
|
||||||
|
|
||||||
## Auto-Memory (lokal, nicht im Repo)
|
## Auto-Memory (lokal, nicht im Repo)
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,12 @@ src/
|
|||||||
├── app.html, app.d.ts # Shell + Env-Types
|
├── app.html, app.d.ts # Shell + Env-Types
|
||||||
├── service-worker.ts # PWA-Shell
|
├── service-worker.ts # PWA-Shell
|
||||||
├── lib/
|
├── lib/
|
||||||
│ ├── client/ # clientseitig: Profil-Store, Confirm-Dialog
|
│ ├── client/ # reaktive Stores (Profile, Search, Wishlist, PWA, Network, Sync, Toast, Install, Confirm, API-Fetch-Wrapper)
|
||||||
│ ├── components/ # Svelte-Komponenten (RecipeView, StarRating, ConfirmDialog, ProfileSwitcher)
|
│ ├── components/ # Svelte-Komponenten:
|
||||||
|
│ │ # - Recipe: RecipeView, RecipeEditor + Editor-Sub-Components
|
||||||
|
│ │ # (IngredientRow, StepList, ImageUploadBox, TimeDisplay, recipe-editor-types)
|
||||||
|
│ │ # - UI-Shell: ConfirmDialog, ProfileSwitcher, SyncIndicator, Toast, UpdateToast
|
||||||
|
│ │ # - Search: SearchFilter, SearchLoader, StarRating
|
||||||
│ ├── recipes/ # shared: Portionen-Scaler (Client UND Server)
|
│ ├── recipes/ # shared: Portionen-Scaler (Client UND Server)
|
||||||
│ ├── server/ # nur Server-Code (nie in Client-Bundle!)
|
│ ├── server/ # nur Server-Code (nie in Client-Bundle!)
|
||||||
│ │ ├── db/ # openDb, Migrations, DB-Singleton
|
│ │ ├── db/ # openDb, Migrations, DB-Singleton
|
||||||
@@ -56,6 +60,8 @@ src/
|
|||||||
|
|
||||||
### Web-Suche
|
### Web-Suche
|
||||||
|
|
||||||
|
Die gesamte Live-Search-Logik ist im `SearchStore` (`src/lib/client/search.svelte.ts`) gekapselt: Debounce, Race-Guard, Pagination, Web-Fallback, Snapshot/Restore für Back-Nav. Sowohl Header-Dropdown (`+layout.svelte`) als auch Startseite (`+page.svelte`) teilen sich die Klasse mit unterschiedlicher `filterParam`-Quelle.
|
||||||
|
|
||||||
1. User tippt → 300 ms Debounce → `/api/recipes/search?q=...` (lokal FTS5)
|
1. User tippt → 300 ms Debounce → `/api/recipes/search?q=...` (lokal FTS5)
|
||||||
2. Wenn 0 Treffer: automatisch `/api/recipes/search/web?q=...`
|
2. Wenn 0 Treffer: automatisch `/api/recipes/search/web?q=...`
|
||||||
3. `searxng.ts` → SearXNG-API mit `site:domain OR site:domain2 ...`-Filter aus Whitelist
|
3. `searxng.ts` → SearXNG-API mit `site:domain OR site:domain2 ...`-Filter aus Whitelist
|
||||||
@@ -86,7 +92,8 @@ Gemeinsame Komponente `ConfirmDialog.svelte` wird im Root-Layout einmal gemounte
|
|||||||
- **JSON-LD first**: Alle drei Ziel-Domains (Chefkoch, Emmi, Experimente) liefern `schema.org/Recipe` im JSON-LD. LLM-Fallback war geplant, aktuell nicht nötig.
|
- **JSON-LD first**: Alle drei Ziel-Domains (Chefkoch, Emmi, Experimente) liefern `schema.org/Recipe` im JSON-LD. LLM-Fallback war geplant, aktuell nicht nötig.
|
||||||
- **SearXNG als Such-Engine**: Self-hosted, daher keine API-Keys. Das Bot-Detection-Theater wird mit gesetzten `X-Forwarded-For`-Headern aus Docker-IPs umgangen.
|
- **SearXNG als Such-Engine**: Self-hosted, daher keine API-Keys. Das Bot-Detection-Theater wird mit gesetzten `X-Forwarded-For`-Headern aus Docker-IPs umgangen.
|
||||||
- **Thumbnail-Cache in SQLite**: 30 Tage TTL (per `KOCHWAS_THUMB_TTL_DAYS`). Negative Einträge (Seite ohne Bild) werden auch gecacht.
|
- **Thumbnail-Cache in SQLite**: 30 Tage TTL (per `KOCHWAS_THUMB_TTL_DAYS`). Negative Einträge (Seite ohne Bild) werden auch gecacht.
|
||||||
- **Svelte 5 Runes** — kein `$:` mehr, keine alten Stores außer `$app/stores`. Neue Stores via Klasse mit `$state`-Feldern.
|
- **Svelte 5 Runes** — kein `$:` mehr, keine alten Stores außer `$app/stores`. Neue Stores via Klasse mit `$state`-Feldern. Form-lokale Snapshots in Edit-Komponenten mit `untrack()` aus `svelte`, damit Prop-Updates nicht laufende Edits überschreiben.
|
||||||
|
- **Zutaten-Sektionen** (ab Migration 012, v1.2): `ingredient.section_heading TEXT NULL`. Ist das Feld gesetzt, startet an dieser Zeile eine neue Sektion — folgende Zutaten gehören dazu, bis die nächste Zeile wieder ein Heading hat. Kein zweites Tabellen-Modell, Ordnung bleibt `position`. Importer setzt immer `null` (schema.org/Recipe hat das Konzept nicht). Editor erlaubt Inline-Insert via `Abschnitt hinzufügen`-Button vor jeder Zeile; leeres Heading wird beim Save zu `null` normalisiert.
|
||||||
- **Service Worker** rein zum Shell-Cachen für Offline-First-PWA, kein intelligentes Cache-Matching (keine externe Rezept-Seiten).
|
- **Service Worker** rein zum Shell-Cachen für Offline-First-PWA, kein intelligentes Cache-Matching (keine externe Rezept-Seiten).
|
||||||
|
|
||||||
## Migrations-Workflow
|
## Migrations-Workflow
|
||||||
@@ -100,10 +107,12 @@ Bei Schema-Änderung:
|
|||||||
|
|
||||||
## Test-Strategie
|
## Test-Strategie
|
||||||
|
|
||||||
- **Unit**: `tests/unit/` — pure Funktionen (json-ld-recipe, iso8601-duration, quotes-random, smoke)
|
- **Unit**: `tests/unit/` — pure Funktionen + Client-Stores via jsdom (json-ld-recipe, iso8601-duration, quotes-random, scaler, ingredient-parser, SearchStore, PWA/Toast/Sync-Stores, SW-Logik).
|
||||||
- **Integration**: `tests/integration/` — mit `openInMemoryForTest()` fresh SQLite pro Test. Externe HTTP via `node:http`-TestServer auf Port 0 gemockt.
|
- **Integration**: `tests/integration/` — mit `openInMemoryForTest()` fresh SQLite pro Test. Externe HTTP via `node:http`-TestServer auf Port 0 gemockt.
|
||||||
- **Keine Svelte-Component-Tests** (bewusst, Aufwand/Nutzen stimmt nicht; UI wird manuell getestet)
|
- **E2E local**: `tests/e2e/` — Playwright gegen `npm run preview`, deckt PWA-Offline-Lifecycle ab (`offline.spec.ts`).
|
||||||
- **Vor Commit**: `npm test && npm run check` muss grün sein.
|
- **E2E remote**: `tests/e2e/remote/` — Playwright gegen `kochwas-dev.siegeln.net` via `playwright.remote.config.ts` (`workers:1`, `serviceWorkers:block`). Testet Live-API-Verhalten, inkl. destruktiver CRUD-Flows (Recipes, Kommentare, Favoriten). Run: `npm run test:e2e:remote`. Siehe `tests/e2e/remote/fixtures/` für Profile-Setup + idempotente API-Cleanup-Helper.
|
||||||
|
- **Keine Svelte-Component-Unit-Tests** (bewusst, Aufwand/Nutzen stimmt nicht; UI wird per E2E und manuell getestet).
|
||||||
|
- **Vor Commit**: `npm test && npm run check` muss grün sein. Vor Merge zu main: zusätzlich `npm run test:e2e:remote`.
|
||||||
|
|
||||||
### Service Worker (PWA)
|
### Service Worker (PWA)
|
||||||
|
|
||||||
@@ -111,11 +120,11 @@ Bei Schema-Änderung:
|
|||||||
|
|
||||||
- **Pre-Cache** (alle Rezepte + Bilder beim Initial-Sync), über paginierten Fetch von `/api/recipes/all`.
|
- **Pre-Cache** (alle Rezepte + Bilder beim Initial-Sync), über paginierten Fetch von `/api/recipes/all`.
|
||||||
- **Delta-Sync** beim App-Start (diff vs. Cache-Manifest, nur Delta laden).
|
- **Delta-Sync** beim App-Start (diff vs. Cache-Manifest, nur Delta laden).
|
||||||
- **Drei Cache-Strategien** (dispatcht per `resolveStrategy`): Shell = cache-first, Daten = SWR, Bilder = cache-first.
|
- **Drei Cache-Strategien** (dispatcht per `resolveStrategy`): Shell = cache-first, Daten = network-first mit 3 s-Timeout-Fallback auf Cache, Bilder = cache-first.
|
||||||
- **Message-Protokoll** (`sync-start`, `sync-progress`, `sync-done`, `sync-error`) zwischen SW und Client.
|
- **Message-Protokoll** (`sync-start`, `sync-progress`, `sync-done`, `sync-error`) zwischen SW und Client.
|
||||||
|
|
||||||
Reine Logik-Einheiten (testbar, Unit-Tests in `tests/unit/`):
|
Reine Logik-Einheiten (testbar, Unit-Tests in `tests/unit/`):
|
||||||
- `src/lib/sw/cache-strategy.ts` — `resolveStrategy({url, method})` → `'shell' | 'swr' | 'images' | 'network-only'`
|
- `src/lib/sw/cache-strategy.ts` — `resolveStrategy({url, method})` → `'shell' | 'network-first' | 'images' | 'network-only'`
|
||||||
- `src/lib/sw/diff-manifest.ts` — `diffManifest(current, cached)` → `{toAdd, toRemove}`
|
- `src/lib/sw/diff-manifest.ts` — `diffManifest(current, cached)` → `{toAdd, toRemove}`
|
||||||
|
|
||||||
Client-Stores (SSR-safe via typeof-Guards):
|
Client-Stores (SSR-safe via typeof-Guards):
|
||||||
|
|||||||
@@ -155,7 +155,7 @@ Kochwas ist eine installierbare PWA. Erkennbar an:
|
|||||||
|
|
||||||
Caches im Browser (siehe DevTools → Application → Cache Storage):
|
Caches im Browser (siehe DevTools → Application → Cache Storage):
|
||||||
- `kochwas-shell-<version>` — App-Shell (JS/CSS/Static-Icons), cache-first
|
- `kochwas-shell-<version>` — App-Shell (JS/CSS/Static-Icons), cache-first
|
||||||
- `kochwas-data-v1` — Rezept-HTMLs + API-JSON (SWR)
|
- `kochwas-data-v1` — Rezept-HTMLs + API-JSON (network-first, 3 s Timeout → Cache-Fallback)
|
||||||
- `kochwas-images-v1` — Bilder (cache-first)
|
- `kochwas-images-v1` — Bilder (cache-first)
|
||||||
- `kochwas-meta` — Cache-Manifest (Liste der gecachten Rezept-IDs unter `/__cache-manifest__`)
|
- `kochwas-meta` — Cache-Manifest (Liste der gecachten Rezept-IDs unter `/__cache-manifest__`)
|
||||||
|
|
||||||
@@ -171,3 +171,19 @@ Bei SW-Problemen Debug-Pfad:
|
|||||||
E2E-Tests (Playwright): `npm run test:e2e`. Setzt `npm run build` voraus (Playwright startet automatisch `npm run preview`).
|
E2E-Tests (Playwright): `npm run test:e2e`. Setzt `npm run build` voraus (Playwright startet automatisch `npm run preview`).
|
||||||
|
|
||||||
Icons einmalig rendern: `npm run render:icons` (schreibt nach `static/icon-*.png`, committen).
|
Icons einmalig rendern: `npm run render:icons` (schreibt nach `static/icon-*.png`, committen).
|
||||||
|
|
||||||
|
## Dev-System / Remote-E2E
|
||||||
|
|
||||||
|
`https://kochwas-dev.siegeln.net/` ist ein separates Deployment (eigener Container, eigene DB unter `/opt/docker/kochwas-dev/data/`). Zweck: E2E-Tests gegen eine prod-nahe Umgebung ohne Angst vor DB-Schäden. Die Remote-Suite (`tests/e2e/remote/`, Config `playwright.remote.config.ts`) darf dort frei CRUDen — User stellt die DB bei Bedarf per Backup wieder her.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run test:e2e:remote # gegen kochwas-dev
|
||||||
|
E2E_REMOTE_URL=https://... npm run test:e2e:remote # andere URL
|
||||||
|
```
|
||||||
|
|
||||||
|
Wichtige Config-Eigenschaften:
|
||||||
|
- `workers: 1` — DB-Race-Sicherheit bei CRUD-Tests.
|
||||||
|
- `serviceWorkers: 'block'` — verhindert Chromium-Crashes durch akkumulierten SW-State über 40+ Contexts.
|
||||||
|
- Fixtures unter `tests/e2e/remote/fixtures/`: `profile.ts` (Profile-Auswahl via localStorage vor Seitenladen), `api-cleanup.ts` (idempotente DELETE-Helfer für afterEach).
|
||||||
|
|
||||||
|
**Niemals gegen `kochwas.siegeln.net` (ohne `-dev`)** die destruktiven Tests laufen lassen — das ist Prod.
|
||||||
|
|||||||
897
docs/superpowers/plans/2026-04-19-editor-split.md
Normal file
897
docs/superpowers/plans/2026-04-19-editor-split.md
Normal file
@@ -0,0 +1,897 @@
|
|||||||
|
# Editor-Split Implementation Plan
|
||||||
|
|
||||||
|
> **For agentic workers:** REQUIRED SUB-SKILL: Use `superpowers:subagent-driven-development` (recommended) or `superpowers:executing-plans` to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||||
|
|
||||||
|
**Goal:** Split the monolithic `RecipeEditor.svelte` (628 L) and pull one readability-oriented block out of `RecipeView.svelte` (398 L) by extracting 4 focused Svelte components: `ImageUploadBox`, `IngredientRow`, `StepList`, `TimeDisplay`. No behavior changes, just structure.
|
||||||
|
|
||||||
|
**Architecture:** Parent-owned state stays in the parent (`RecipeEditor` still owns `ingredients: DraftIng[]`, `steps: DraftStep[]`). Sub-components receive props + callbacks and render their own template + scoped CSS. Shared draft types land in `src/lib/components/recipe-editor-types.ts` so sub-components and parent agree on the shape. `RecipeView.TimeDisplay` is pure presentational with no state.
|
||||||
|
|
||||||
|
**Tech Stack:** Svelte 5 runes (`$props`, `$state`, `$derived`), TypeScript-strict, no new runtime deps.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Why this is worth doing
|
||||||
|
|
||||||
|
- `RecipeEditor.svelte:42-89` (Bild-Upload) and `RecipeEditor.svelte:313-334` (Zubereitung) are each self-contained logic-islands with their own state and handlers. Extracting them caps the file a Claude can reason about in one shot.
|
||||||
|
- `IngredientRow` renders 10 lines of template with 5 ARIA labels and 6 grid-columns — a natural single-responsibility unit.
|
||||||
|
- `TimeDisplay` is pure formatting; owning it as a component lets future phases (preview, card hover) reuse it.
|
||||||
|
|
||||||
|
## What we are NOT doing
|
||||||
|
|
||||||
|
- No refactor of `RecipeView`'s tabs / servings-stepper / ingredient-display. Those work fine as-is; roadmap only names the 4 above.
|
||||||
|
- No component unit tests (kochwas has none for components; the e2e `recipe-detail.spec.ts` still covers View behavior, and edit-flow is manually smoked).
|
||||||
|
- No `<style global>` extraction. Small CSS duplication (`.add`, `.del` buttons) is accepted.
|
||||||
|
- No prop-type sharing via `<script module>` blocks. A `.ts` sibling file is simpler.
|
||||||
|
|
||||||
|
## Design Snapshot
|
||||||
|
|
||||||
|
**Shared types** — `src/lib/components/recipe-editor-types.ts`:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
export type DraftIng = {
|
||||||
|
qty: string;
|
||||||
|
unit: string;
|
||||||
|
name: string;
|
||||||
|
note: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type DraftStep = { text: string };
|
||||||
|
```
|
||||||
|
|
||||||
|
**Component APIs (locked before implementation):**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// ImageUploadBox.svelte
|
||||||
|
type Props = {
|
||||||
|
recipeId: number;
|
||||||
|
imagePath: string | null; // initial value; component owns its own state after
|
||||||
|
onchange: (path: string | null) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
// IngredientRow.svelte
|
||||||
|
type Props = {
|
||||||
|
ing: DraftIng; // passed by reference — bind:value=ing.* works transparently
|
||||||
|
idx: number;
|
||||||
|
total: number; // for "last row? disable move-down"
|
||||||
|
onmove: (dir: -1 | 1) => void;
|
||||||
|
onremove: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
// StepList.svelte
|
||||||
|
type Props = {
|
||||||
|
steps: DraftStep[]; // passed by reference
|
||||||
|
onadd: () => void;
|
||||||
|
onremove: (idx: number) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
// TimeDisplay.svelte
|
||||||
|
type Props = {
|
||||||
|
prepTimeMin: number | null;
|
||||||
|
cookTimeMin: number | null;
|
||||||
|
totalTimeMin: number | null;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
**Render-wrapping pattern:** The parent keeps the `<section class="block"><h2>…</h2> … </section>` wrappers. Sub-components render bare content (no outer utility-class wrapper), so the parent's scoped `.block` / `h2` styling continues to apply.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 1: Extract `ImageUploadBox`
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/lib/components/ImageUploadBox.svelte`
|
||||||
|
- Modify: `src/lib/components/RecipeEditor.svelte`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Create the new component**
|
||||||
|
|
||||||
|
```svelte
|
||||||
|
<!-- src/lib/components/ImageUploadBox.svelte -->
|
||||||
|
<script lang="ts">
|
||||||
|
import { ImagePlus, ImageOff } from 'lucide-svelte';
|
||||||
|
import { confirmAction } from '$lib/client/confirm.svelte';
|
||||||
|
import { asyncFetch } from '$lib/client/api-fetch-wrapper';
|
||||||
|
import { requireOnline } from '$lib/client/require-online';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
recipeId: number;
|
||||||
|
imagePath: string | null;
|
||||||
|
onchange: (path: string | null) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { recipeId, imagePath: initial, onchange }: Props = $props();
|
||||||
|
|
||||||
|
let imagePath = $state<string | null>(initial);
|
||||||
|
let uploading = $state(false);
|
||||||
|
let fileInput: HTMLInputElement | null = $state(null);
|
||||||
|
|
||||||
|
const imageSrc = $derived(
|
||||||
|
imagePath === null
|
||||||
|
? null
|
||||||
|
: /^https?:\/\//i.test(imagePath)
|
||||||
|
? imagePath
|
||||||
|
: `/images/${imagePath}`
|
||||||
|
);
|
||||||
|
|
||||||
|
async function onFileChosen(event: Event) {
|
||||||
|
const input = event.target as HTMLInputElement;
|
||||||
|
const file = input.files?.[0];
|
||||||
|
input.value = '';
|
||||||
|
if (!file) return;
|
||||||
|
if (!requireOnline('Der Bild-Upload')) return;
|
||||||
|
uploading = true;
|
||||||
|
try {
|
||||||
|
const fd = new FormData();
|
||||||
|
fd.append('file', file);
|
||||||
|
const res = await asyncFetch(
|
||||||
|
`/api/recipes/${recipeId}/image`,
|
||||||
|
{ method: 'POST', body: fd },
|
||||||
|
'Upload fehlgeschlagen'
|
||||||
|
);
|
||||||
|
if (!res) return;
|
||||||
|
const body = await res.json();
|
||||||
|
imagePath = body.image_path;
|
||||||
|
onchange(imagePath);
|
||||||
|
} finally {
|
||||||
|
uploading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function removeImage() {
|
||||||
|
if (imagePath === null) return;
|
||||||
|
const ok = await confirmAction({
|
||||||
|
title: 'Bild entfernen?',
|
||||||
|
message: 'Das Rezept wird danach ohne Titelbild angezeigt.',
|
||||||
|
confirmLabel: 'Entfernen',
|
||||||
|
destructive: true
|
||||||
|
});
|
||||||
|
if (!ok) return;
|
||||||
|
if (!requireOnline('Das Entfernen')) return;
|
||||||
|
uploading = true;
|
||||||
|
try {
|
||||||
|
const res = await asyncFetch(
|
||||||
|
`/api/recipes/${recipeId}/image`,
|
||||||
|
{ method: 'DELETE' },
|
||||||
|
'Entfernen fehlgeschlagen'
|
||||||
|
);
|
||||||
|
if (!res) return;
|
||||||
|
imagePath = null;
|
||||||
|
onchange(null);
|
||||||
|
} finally {
|
||||||
|
uploading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="image-row">
|
||||||
|
<div class="image-preview" class:empty={!imageSrc}>
|
||||||
|
{#if imageSrc}
|
||||||
|
<img src={imageSrc} alt="" />
|
||||||
|
{:else}
|
||||||
|
<span class="placeholder">Kein Bild</span>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
<div class="image-actions">
|
||||||
|
<button
|
||||||
|
class="btn"
|
||||||
|
type="button"
|
||||||
|
onclick={() => fileInput?.click()}
|
||||||
|
disabled={uploading}
|
||||||
|
>
|
||||||
|
<ImagePlus size={16} strokeWidth={2} />
|
||||||
|
<span>{imagePath ? 'Bild ersetzen' : 'Bild hochladen'}</span>
|
||||||
|
</button>
|
||||||
|
{#if imagePath}
|
||||||
|
<button class="btn ghost" type="button" onclick={removeImage} disabled={uploading}>
|
||||||
|
<ImageOff size={16} strokeWidth={2} />
|
||||||
|
<span>Entfernen</span>
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
{#if uploading}
|
||||||
|
<span class="upload-status">Lade …</span>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
<input
|
||||||
|
bind:this={fileInput}
|
||||||
|
type="file"
|
||||||
|
accept="image/jpeg,image/png,image/webp,image/gif,image/avif"
|
||||||
|
class="file-input"
|
||||||
|
onchange={onFileChosen}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<p class="image-hint">Max. 10 MB. JPG, PNG, WebP, GIF oder AVIF.</p>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.image-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 1rem;
|
||||||
|
align-items: flex-start;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
.image-preview {
|
||||||
|
width: 160px;
|
||||||
|
aspect-ratio: 16 / 10;
|
||||||
|
border-radius: 10px;
|
||||||
|
overflow: hidden;
|
||||||
|
background: #eef3ef;
|
||||||
|
border: 1px solid #e4eae7;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
.image-preview img {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
object-fit: cover;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
.image-preview.empty {
|
||||||
|
display: grid;
|
||||||
|
place-items: center;
|
||||||
|
color: #999;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
}
|
||||||
|
.image-preview .placeholder {
|
||||||
|
padding: 0 0.5rem;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.image-actions {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.5rem;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.upload-status {
|
||||||
|
color: #666;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
}
|
||||||
|
.file-input {
|
||||||
|
position: absolute;
|
||||||
|
width: 1px;
|
||||||
|
height: 1px;
|
||||||
|
opacity: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
.image-hint {
|
||||||
|
margin: 0.6rem 0 0;
|
||||||
|
color: #888;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
}
|
||||||
|
.btn {
|
||||||
|
padding: 0.55rem 0.85rem;
|
||||||
|
border-radius: 10px;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
background: white;
|
||||||
|
cursor: pointer;
|
||||||
|
font-family: inherit;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
min-height: 40px;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.4rem;
|
||||||
|
}
|
||||||
|
.btn.ghost {
|
||||||
|
color: #666;
|
||||||
|
}
|
||||||
|
.btn:disabled {
|
||||||
|
opacity: 0.6;
|
||||||
|
cursor: progress;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Wire up `RecipeEditor.svelte`**
|
||||||
|
|
||||||
|
Remove lines 30–89 (imagePath/uploading/fileInput state, imageSrc derived, onFileChosen, removeImage).
|
||||||
|
|
||||||
|
Remove these imports at the top:
|
||||||
|
```ts
|
||||||
|
import { Plus, Trash2, ChevronUp, ChevronDown, ImagePlus, ImageOff } from 'lucide-svelte';
|
||||||
|
import { confirmAction } from '$lib/client/confirm.svelte';
|
||||||
|
import { asyncFetch } from '$lib/client/api-fetch-wrapper';
|
||||||
|
import { requireOnline } from '$lib/client/require-online';
|
||||||
|
```
|
||||||
|
Replace with (Task 1 needs only Plus + Trash2 + Chevrons — the image-specific imports move to the sub-component; `confirmAction`/`asyncFetch`/`requireOnline` stay for future tasks):
|
||||||
|
```ts
|
||||||
|
import { Plus, Trash2, ChevronUp, ChevronDown } from 'lucide-svelte';
|
||||||
|
import ImageUploadBox from '$lib/components/ImageUploadBox.svelte';
|
||||||
|
```
|
||||||
|
|
||||||
|
Remove the image-related CSS (`.image-row`, `.image-preview*`, `.image-actions`, `.image-actions .btn`, `.upload-status`, `.file-input`, `.image-hint`, `.image-block` — those live in the sub-component now).
|
||||||
|
|
||||||
|
Replace the Bild section in the template:
|
||||||
|
```svelte
|
||||||
|
<section class="block">
|
||||||
|
<h2>Bild</h2>
|
||||||
|
<ImageUploadBox
|
||||||
|
recipeId={recipe.id}
|
||||||
|
imagePath={recipe.image_path}
|
||||||
|
onchange={(p) => onimagechange?.(p)}
|
||||||
|
/>
|
||||||
|
</section>
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Run checks**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run check
|
||||||
|
npm test
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: 0 errors, 196/196 tests pass.
|
||||||
|
|
||||||
|
- [ ] **Step 4: Manual smoke**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
Open any saved recipe → edit → upload an image → verify it shows up and `onimagechange` fires (parent's state updates). Remove the image → confirms the confirm-dialog and removes. Bail out if either flow breaks.
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/components/ImageUploadBox.svelte src/lib/components/RecipeEditor.svelte
|
||||||
|
git commit -m "$(cat <<'EOF'
|
||||||
|
refactor(editor): ImageUploadBox als eigenstaendige Component
|
||||||
|
|
||||||
|
Isoliert den Bild-Upload-Flow (File-Input, Preview, Entfernen-Dialog)
|
||||||
|
aus dem RecipeEditor. Parent haelt nur noch den <section>-Wrapper und
|
||||||
|
reicht recipe.id + image_path rein, kriegt Aenderungen per onchange
|
||||||
|
callback zurueck.
|
||||||
|
|
||||||
|
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
||||||
|
EOF
|
||||||
|
)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 2: Extract types + `IngredientRow`
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/lib/components/recipe-editor-types.ts`
|
||||||
|
- Create: `src/lib/components/IngredientRow.svelte`
|
||||||
|
- Modify: `src/lib/components/RecipeEditor.svelte`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Types file**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// src/lib/components/recipe-editor-types.ts
|
||||||
|
export type DraftIng = {
|
||||||
|
qty: string;
|
||||||
|
unit: string;
|
||||||
|
name: string;
|
||||||
|
note: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type DraftStep = { text: string };
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: IngredientRow component**
|
||||||
|
|
||||||
|
```svelte
|
||||||
|
<!-- src/lib/components/IngredientRow.svelte -->
|
||||||
|
<script lang="ts">
|
||||||
|
import { Trash2, ChevronUp, ChevronDown } from 'lucide-svelte';
|
||||||
|
import type { DraftIng } from './recipe-editor-types';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
ing: DraftIng;
|
||||||
|
idx: number;
|
||||||
|
total: number;
|
||||||
|
onmove: (dir: -1 | 1) => void;
|
||||||
|
onremove: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { ing, idx, total, onmove, onremove }: Props = $props();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<li class="ing-row">
|
||||||
|
<div class="move">
|
||||||
|
<button
|
||||||
|
class="move-btn"
|
||||||
|
type="button"
|
||||||
|
aria-label="Zutat nach oben"
|
||||||
|
disabled={idx === 0}
|
||||||
|
onclick={() => onmove(-1)}
|
||||||
|
>
|
||||||
|
<ChevronUp size={14} strokeWidth={2.5} />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="move-btn"
|
||||||
|
type="button"
|
||||||
|
aria-label="Zutat nach unten"
|
||||||
|
disabled={idx === total - 1}
|
||||||
|
onclick={() => onmove(1)}
|
||||||
|
>
|
||||||
|
<ChevronDown size={14} strokeWidth={2.5} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<input class="qty" type="text" bind:value={ing.qty} placeholder="Menge" aria-label="Menge" />
|
||||||
|
<input class="unit" type="text" bind:value={ing.unit} placeholder="Einheit" aria-label="Einheit" />
|
||||||
|
<input class="name" type="text" bind:value={ing.name} placeholder="Zutat" aria-label="Zutat" />
|
||||||
|
<input class="note" type="text" bind:value={ing.note} placeholder="Notiz" aria-label="Notiz" />
|
||||||
|
<button class="del" type="button" aria-label="Zutat entfernen" onclick={onremove}>
|
||||||
|
<Trash2 size={16} strokeWidth={2} />
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.ing-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 28px 70px 70px 1fr 1fr 40px;
|
||||||
|
gap: 0.35rem;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.move {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 2px;
|
||||||
|
}
|
||||||
|
.move-btn {
|
||||||
|
width: 28px;
|
||||||
|
height: 20px;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
background: white;
|
||||||
|
border-radius: 6px;
|
||||||
|
cursor: pointer;
|
||||||
|
color: #555;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
.move-btn:hover:not(:disabled) {
|
||||||
|
background: #f4f8f5;
|
||||||
|
}
|
||||||
|
.move-btn:disabled {
|
||||||
|
opacity: 0.3;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
.ing-row input {
|
||||||
|
padding: 0.5rem 0.55rem;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
min-height: 38px;
|
||||||
|
font-family: inherit;
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
.del {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
border: 1px solid #f1b4b4;
|
||||||
|
background: white;
|
||||||
|
color: #c53030;
|
||||||
|
border-radius: 8px;
|
||||||
|
cursor: pointer;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
.del:hover {
|
||||||
|
background: #fdf3f3;
|
||||||
|
}
|
||||||
|
@media (max-width: 560px) {
|
||||||
|
.ing-row {
|
||||||
|
grid-template-columns: 28px 70px 1fr 40px;
|
||||||
|
grid-template-areas:
|
||||||
|
'move qty name del'
|
||||||
|
'move unit unit del'
|
||||||
|
'note note note note';
|
||||||
|
}
|
||||||
|
.ing-row .move {
|
||||||
|
grid-area: move;
|
||||||
|
}
|
||||||
|
.ing-row .qty {
|
||||||
|
grid-area: qty;
|
||||||
|
}
|
||||||
|
.ing-row .unit {
|
||||||
|
grid-area: unit;
|
||||||
|
}
|
||||||
|
.ing-row .name {
|
||||||
|
grid-area: name;
|
||||||
|
}
|
||||||
|
.ing-row .note {
|
||||||
|
grid-area: note;
|
||||||
|
}
|
||||||
|
.ing-row .del {
|
||||||
|
grid-area: del;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Wire up `RecipeEditor.svelte`**
|
||||||
|
|
||||||
|
Replace the local `DraftIng` / `DraftStep` type declarations (lines 100–106) with:
|
||||||
|
```ts
|
||||||
|
import type { DraftIng, DraftStep } from '$lib/components/recipe-editor-types';
|
||||||
|
import IngredientRow from '$lib/components/IngredientRow.svelte';
|
||||||
|
```
|
||||||
|
|
||||||
|
In the template, swap the `<li class="ing-row">` block for:
|
||||||
|
```svelte
|
||||||
|
{#each ingredients as ing, idx (idx)}
|
||||||
|
<IngredientRow
|
||||||
|
{ing}
|
||||||
|
{idx}
|
||||||
|
total={ingredients.length}
|
||||||
|
onmove={(dir) => moveIngredient(idx, dir)}
|
||||||
|
onremove={() => removeIngredient(idx)}
|
||||||
|
/>
|
||||||
|
{/each}
|
||||||
|
```
|
||||||
|
|
||||||
|
Remove the CSS for `.ing-row`, `.move`, `.move-btn`, `.ing-row input`, `.del`, and the `@media (max-width: 560px)` block — all now live in `IngredientRow.svelte`.
|
||||||
|
|
||||||
|
Remove the unused imports `ChevronUp`, `ChevronDown`, `Trash2` from RecipeEditor (they moved to the sub-component, but wait — `Trash2` is also used for step-remove. Keep `Trash2`, remove the two Chevrons).
|
||||||
|
|
||||||
|
- [ ] **Step 4: Run checks**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run check
|
||||||
|
npm test
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 5: Manual smoke**
|
||||||
|
|
||||||
|
Open any recipe in edit mode. Add an ingredient, type into all 4 fields, reorder up/down, remove one. Verify save persists the ordering.
|
||||||
|
|
||||||
|
- [ ] **Step 6: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/components/recipe-editor-types.ts src/lib/components/IngredientRow.svelte src/lib/components/RecipeEditor.svelte
|
||||||
|
git commit -m "$(cat <<'EOF'
|
||||||
|
refactor(editor): IngredientRow + shared types
|
||||||
|
|
||||||
|
IngredientRow rendert eine einzelne editierbare Zutat-Zeile. DraftIng
|
||||||
|
und DraftStep sind jetzt in recipe-editor-types.ts, damit Parent und
|
||||||
|
Sub-Components auf dieselbe Form referenzieren.
|
||||||
|
|
||||||
|
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
||||||
|
EOF
|
||||||
|
)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 3: Extract `StepList`
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/lib/components/StepList.svelte`
|
||||||
|
- Modify: `src/lib/components/RecipeEditor.svelte`
|
||||||
|
|
||||||
|
- [ ] **Step 1: StepList component**
|
||||||
|
|
||||||
|
```svelte
|
||||||
|
<!-- src/lib/components/StepList.svelte -->
|
||||||
|
<script lang="ts">
|
||||||
|
import { Plus, Trash2 } from 'lucide-svelte';
|
||||||
|
import type { DraftStep } from './recipe-editor-types';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
steps: DraftStep[];
|
||||||
|
onadd: () => void;
|
||||||
|
onremove: (idx: number) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { steps, onadd, onremove }: Props = $props();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<ol class="step-list">
|
||||||
|
{#each steps as step, idx (idx)}
|
||||||
|
<li class="step-row">
|
||||||
|
<span class="num">{idx + 1}</span>
|
||||||
|
<textarea
|
||||||
|
bind:value={step.text}
|
||||||
|
rows="3"
|
||||||
|
placeholder="Schritt beschreiben …"
|
||||||
|
></textarea>
|
||||||
|
<button class="del" type="button" aria-label="Schritt entfernen" onclick={() => onremove(idx)}>
|
||||||
|
<Trash2 size={16} strokeWidth={2} />
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ol>
|
||||||
|
<button class="add" type="button" onclick={onadd}>
|
||||||
|
<Plus size={16} strokeWidth={2} />
|
||||||
|
<span>Schritt hinzufügen</span>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.step-list {
|
||||||
|
list-style: none;
|
||||||
|
padding: 0;
|
||||||
|
margin: 0 0 0.6rem;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.4rem;
|
||||||
|
}
|
||||||
|
.step-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 32px 1fr 40px;
|
||||||
|
gap: 0.5rem;
|
||||||
|
align-items: start;
|
||||||
|
}
|
||||||
|
.num {
|
||||||
|
width: 32px;
|
||||||
|
height: 32px;
|
||||||
|
background: #2b6a3d;
|
||||||
|
color: white;
|
||||||
|
border-radius: 50%;
|
||||||
|
display: grid;
|
||||||
|
place-items: center;
|
||||||
|
font-weight: 600;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
margin-top: 0.25rem;
|
||||||
|
}
|
||||||
|
.step-row textarea {
|
||||||
|
padding: 0.55rem 0.7rem;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
font-family: inherit;
|
||||||
|
resize: vertical;
|
||||||
|
min-height: 70px;
|
||||||
|
}
|
||||||
|
.del {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
border: 1px solid #f1b4b4;
|
||||||
|
background: white;
|
||||||
|
color: #c53030;
|
||||||
|
border-radius: 8px;
|
||||||
|
cursor: pointer;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
.del:hover {
|
||||||
|
background: #fdf3f3;
|
||||||
|
}
|
||||||
|
.add {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.35rem;
|
||||||
|
padding: 0.55rem 0.9rem;
|
||||||
|
border: 1px dashed #cfd9d1;
|
||||||
|
background: white;
|
||||||
|
color: #2b6a3d;
|
||||||
|
border-radius: 8px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
font-family: inherit;
|
||||||
|
}
|
||||||
|
.add:hover {
|
||||||
|
background: #f4f8f5;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Wire up `RecipeEditor.svelte`**
|
||||||
|
|
||||||
|
Add import:
|
||||||
|
```ts
|
||||||
|
import StepList from '$lib/components/StepList.svelte';
|
||||||
|
```
|
||||||
|
|
||||||
|
Replace the entire Zubereitung `<section class="block">` template block (starting `<section class="block">` with `<h2>Zubereitung</h2>` through the add-step button):
|
||||||
|
|
||||||
|
```svelte
|
||||||
|
<section class="block">
|
||||||
|
<h2>Zubereitung</h2>
|
||||||
|
<StepList {steps} onadd={addStep} onremove={removeStep} />
|
||||||
|
</section>
|
||||||
|
```
|
||||||
|
|
||||||
|
**CSS audit — what stays and what goes in the parent:**
|
||||||
|
|
||||||
|
Parent's template after Tasks 1–3 still contains:
|
||||||
|
- `<section class="block"><h2>Bild</h2><ImageUploadBox .../></section>` — no `.block` inner styles needed beyond what's in parent.
|
||||||
|
- `<div class="meta">` — still here. Keep `.meta`, `.field`, `.row`, `.small`, `.lbl`.
|
||||||
|
- `<section class="block"><h2>Zutaten</h2><ul class="ing-list">{#each ..}<IngredientRow/>{/each}</ul><button class="add">...</button></section>` — still uses `.ing-list` and `.add`.
|
||||||
|
- `<section class="block"><h2>Zubereitung</h2><StepList/></section>` — no inner CSS.
|
||||||
|
- `<div class="foot"><button class="btn ghost">...</button><button class="btn primary">...</button></div>` — keeps `.foot`, `.btn`, `.btn.ghost`, `.btn.primary`, `.btn:disabled`.
|
||||||
|
|
||||||
|
So parent CSS after Task 3 keeps: `.editor`, `.meta`, `.field`, `.lbl`, `.row`, `.small`, `.block`, `.block h2`, `.ing-list` (the `<ul>` wrapper), `.add` (for "Zutat hinzufügen"), `.foot`, `.btn` and variants.
|
||||||
|
|
||||||
|
Drop from parent CSS in Task 3: `.step-list`, `.step-row`, `.num`, `.step-row textarea`, `.del`.
|
||||||
|
|
||||||
|
- [ ] **Step 3: Run checks**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run check
|
||||||
|
npm test
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Manual smoke**
|
||||||
|
|
||||||
|
Open any recipe → edit → add a step, type, remove, save. Verify steps persist with correct ordering.
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/components/StepList.svelte src/lib/components/RecipeEditor.svelte
|
||||||
|
git commit -m "$(cat <<'EOF'
|
||||||
|
refactor(editor): StepList als eigenstaendige Component
|
||||||
|
|
||||||
|
Zubereitungs-Liste mit Add + Remove als Sub-Component. Parent steuert
|
||||||
|
nur noch den Wrapper und reicht steps + die zwei Callbacks rein.
|
||||||
|
|
||||||
|
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
||||||
|
EOF
|
||||||
|
)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 4: Extract `TimeDisplay` (RecipeView)
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/lib/components/TimeDisplay.svelte`
|
||||||
|
- Modify: `src/lib/components/RecipeView.svelte`
|
||||||
|
|
||||||
|
- [ ] **Step 1: TimeDisplay component**
|
||||||
|
|
||||||
|
```svelte
|
||||||
|
<!-- src/lib/components/TimeDisplay.svelte -->
|
||||||
|
<script lang="ts">
|
||||||
|
type Props = {
|
||||||
|
prepTimeMin: number | null;
|
||||||
|
cookTimeMin: number | null;
|
||||||
|
totalTimeMin: number | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { prepTimeMin, cookTimeMin, totalTimeMin }: Props = $props();
|
||||||
|
|
||||||
|
const summary = $derived.by(() => {
|
||||||
|
const parts: string[] = [];
|
||||||
|
if (prepTimeMin) parts.push(`Vorb. ${prepTimeMin} min`);
|
||||||
|
if (cookTimeMin) parts.push(`Kochen ${cookTimeMin} min`);
|
||||||
|
if (!prepTimeMin && !cookTimeMin && totalTimeMin)
|
||||||
|
parts.push(`Gesamt ${totalTimeMin} min`);
|
||||||
|
return parts.join(' · ');
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if summary}
|
||||||
|
<p class="times">{summary}</p>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.times {
|
||||||
|
margin: 0 0 0.25rem;
|
||||||
|
color: #666;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Wire up `RecipeView.svelte`**
|
||||||
|
|
||||||
|
Add import:
|
||||||
|
```ts
|
||||||
|
import TimeDisplay from '$lib/components/TimeDisplay.svelte';
|
||||||
|
```
|
||||||
|
|
||||||
|
Remove the local `timeSummary()` function (lines 45–52).
|
||||||
|
|
||||||
|
Replace the `{#if timeSummary()}<p class="times">...</p>{/if}` block in the template with:
|
||||||
|
```svelte
|
||||||
|
<TimeDisplay
|
||||||
|
prepTimeMin={recipe.prep_time_min}
|
||||||
|
cookTimeMin={recipe.cook_time_min}
|
||||||
|
totalTimeMin={recipe.total_time_min}
|
||||||
|
/>
|
||||||
|
```
|
||||||
|
|
||||||
|
Remove the `.times` CSS from RecipeView (it's in the sub-component now).
|
||||||
|
|
||||||
|
- [ ] **Step 3: Run checks**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run check
|
||||||
|
npm test
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Manual smoke**
|
||||||
|
|
||||||
|
Open any recipe → verify the time line still shows the same content (Vorb. / Kochen / Gesamt).
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/components/TimeDisplay.svelte src/lib/components/RecipeView.svelte
|
||||||
|
git commit -m "$(cat <<'EOF'
|
||||||
|
refactor(view): TimeDisplay als eigenstaendige Component
|
||||||
|
|
||||||
|
timeSummary-Formatierung in eine wiederverwendbare Component
|
||||||
|
gezogen. RecipeView liefert nur noch die drei Werte — zukuenftige
|
||||||
|
Call-Sites (Preview, Hover-Cards) koennen dieselbe Logik reusen.
|
||||||
|
|
||||||
|
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
||||||
|
EOF
|
||||||
|
)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 5: Self-review + push
|
||||||
|
|
||||||
|
- [ ] **Step 1: Line-count audit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
wc -l src/lib/components/RecipeEditor.svelte src/lib/components/RecipeView.svelte src/lib/components/ImageUploadBox.svelte src/lib/components/IngredientRow.svelte src/lib/components/StepList.svelte src/lib/components/TimeDisplay.svelte
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected shape (approximate, ±10%):
|
||||||
|
- `RecipeEditor.svelte`: 628 → ~330–370
|
||||||
|
- `RecipeView.svelte`: 398 → ~380
|
||||||
|
- `ImageUploadBox.svelte`: ~160
|
||||||
|
- `IngredientRow.svelte`: ~110
|
||||||
|
- `StepList.svelte`: ~100
|
||||||
|
- `TimeDisplay.svelte`: ~30
|
||||||
|
|
||||||
|
- [ ] **Step 2: Full test + typecheck**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm test
|
||||||
|
npm run check
|
||||||
|
```
|
||||||
|
|
||||||
|
Both green.
|
||||||
|
|
||||||
|
- [ ] **Step 3: Git log review**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git log --oneline main..HEAD
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected 4 commits:
|
||||||
|
1. `refactor(editor): ImageUploadBox als eigenstaendige Component`
|
||||||
|
2. `refactor(editor): IngredientRow + shared types`
|
||||||
|
3. `refactor(editor): StepList als eigenstaendige Component`
|
||||||
|
4. `refactor(view): TimeDisplay als eigenstaendige Component`
|
||||||
|
|
||||||
|
- [ ] **Step 4: Remote E2E after push**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git push -u origin editor-split
|
||||||
|
```
|
||||||
|
|
||||||
|
CI builds branch-tagged image. After deploy to `kochwas-dev.siegeln.net`:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run test:e2e:remote
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: 40/42 green (same as Search-State-Store baseline). `recipe-detail.spec.ts` (6 tests) specifically exercises the View side — must be clean.
|
||||||
|
|
||||||
|
Manual UAT pass on `https://kochwas-dev.siegeln.net/`:
|
||||||
|
- Edit a recipe → upload + remove image.
|
||||||
|
- Add / reorder / remove an ingredient → save → verify persistence on reload.
|
||||||
|
- Add / remove a step → save → verify.
|
||||||
|
- Check time-summary rendering on any recipe with prep/cook/total times set.
|
||||||
|
|
||||||
|
- [ ] **Step 5: Merge to main**
|
||||||
|
|
||||||
|
Once UAT is clean:
|
||||||
|
```bash
|
||||||
|
git checkout main
|
||||||
|
git merge --no-ff editor-split
|
||||||
|
git push origin main
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Risk Notes
|
||||||
|
|
||||||
|
- **Prop-reference mutability.** `IngredientRow` and `StepList` receive `ing` / `steps` by reference and use `bind:value` on their own `<input>` / `<textarea>` elements. Svelte 5 handles this correctly — writes propagate to the parent's `$state` array. Verified pattern with existing `searchFilterStore` usage and similar bind-through-prop in older Svelte 5 components in this codebase.
|
||||||
|
- **Confirm-dialog scope.** `ImageUploadBox` imports `confirmAction` directly rather than using a prop-callback. Consistent with the rest of the codebase (`confirmAction` is a global).
|
||||||
|
- **Scoped CSS duplication.** `.del` and `.add` button styles exist in multiple sub-components. Accepted — the alternative (global button classes) is out of scope for this phase.
|
||||||
|
- **No component unit tests.** Risk: a structural mistake (bad prop passing, missing callback wiring) wouldn't be caught by logic-layer tests. Mitigation: manual smoke test + `npm run check` type-safety + existing e2e coverage on RecipeView side.
|
||||||
|
|
||||||
|
## Deferred — NOT in this plan
|
||||||
|
|
||||||
|
- **Component unit tests with `@testing-library/svelte`:** Would add Vitest+browser setup. Worth doing in a separate phase once the project acquires a second component-refactor candidate.
|
||||||
|
- **Edit-flow E2E spec:** `tests/e2e/remote/recipe-edit.spec.ts` would cover the editor end-to-end. Valuable, but out of scope here — this phase is structural extraction, not test coverage expansion.
|
||||||
|
- **Extract `RecipeHero` / `ServingsStepper` / `TabSwitcher` from RecipeView:** Not on the roadmap. Add to a future phase if RecipeView grows further.
|
||||||
634
docs/superpowers/plans/2026-04-19-ingredient-sections.md
Normal file
634
docs/superpowers/plans/2026-04-19-ingredient-sections.md
Normal file
@@ -0,0 +1,634 @@
|
|||||||
|
# Ingredient Sections Implementation Plan
|
||||||
|
|
||||||
|
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||||
|
|
||||||
|
**Goal:** Zutaten können im Editor in benannte Sektionen (z. B. „Für den Teig", „Für die Füllung") gruppiert werden; in der View werden die Sektionen als Überschriften über den zugehörigen Zutatenblöcken gerendert.
|
||||||
|
|
||||||
|
**Architecture:** Eine neue nullable Spalte `section_heading` auf `ingredient`. Ist sie gesetzt, startet an dieser Zeile eine neue Sektion — alle folgenden Zutaten gehören dazu bis zur nächsten Zeile mit gesetzter `section_heading`. Ordnung bleibt `position`. Keine neue Tabelle, keine zweite Ordnungsachse, Scaler/FTS/Importer bleiben unverändert im Verhalten (nur Type-Passthrough). Inline-Button „Abschnitt hinzufügen" erscheint im Editor vor jeder Zutatenzeile und am Listenende.
|
||||||
|
|
||||||
|
**Tech Stack:** better-sqlite3 Migration, TypeScript-strict, Svelte 5 runes, vitest.
|
||||||
|
|
||||||
|
**Scope-Entscheidungen (vom User bestätigt):**
|
||||||
|
- Sektionen **nur für Zutaten**, nicht für Zubereitungsschritte.
|
||||||
|
- „Abschnitt hinzufügen"-Button inline vor jeder Zeile (plus einer am Listenende).
|
||||||
|
- Keine Import-Extraction — JSON-LD hat keine Sektionen, Emmikochteinfach rendert sie nur im HTML. Später via HTML-Parse möglich, aber out-of-scope.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 1: Migration + Type-Erweiterung + parseIngredient-Sites
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/lib/server/db/migrations/012_ingredient_section.sql`
|
||||||
|
- Modify: `src/lib/types.ts` (Ingredient type)
|
||||||
|
- Modify: `src/lib/server/parsers/ingredient.ts` (3 return sites)
|
||||||
|
- Test: `tests/unit/ingredient.test.ts` (bereits existierend, muss grün bleiben)
|
||||||
|
|
||||||
|
**Warum zusammen:** Nach der Type-Änderung schlägt `svelte-check` überall fehl, wo ein `Ingredient`-Literal gebaut wird. `parseIngredient` hat 3 solcher Stellen und ist vom selben Commit abhängig, sonst wird der Build rot.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Migration schreiben**
|
||||||
|
|
||||||
|
Create `src/lib/server/db/migrations/012_ingredient_section.sql`:
|
||||||
|
```sql
|
||||||
|
-- Nullable — alte Zeilen behalten NULL, neue dürfen eine Überschrift haben.
|
||||||
|
-- Rendering-Regel: Ist section_heading gesetzt (nicht NULL und nicht leer),
|
||||||
|
-- startet an dieser Zeile eine neue Sektion mit diesem Titel.
|
||||||
|
ALTER TABLE ingredient ADD COLUMN section_heading TEXT;
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Ingredient-Type erweitern**
|
||||||
|
|
||||||
|
Modify `src/lib/types.ts`:
|
||||||
|
```ts
|
||||||
|
export type Ingredient = {
|
||||||
|
position: number;
|
||||||
|
quantity: number | null;
|
||||||
|
unit: string | null;
|
||||||
|
name: string;
|
||||||
|
note: string | null;
|
||||||
|
raw_text: string;
|
||||||
|
section_heading: string | null;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: parseIngredient-Return-Sites aktualisieren**
|
||||||
|
|
||||||
|
Modify `src/lib/server/parsers/ingredient.ts`:
|
||||||
|
Alle drei `return { position, ... raw_text: rawText };`-Literale (Zeilen 108, 115, 119) bekommen `section_heading: null` am Ende. Beispiel für Zeile 108:
|
||||||
|
```ts
|
||||||
|
return { position, quantity, unit, name, note, raw_text: rawText, section_heading: null };
|
||||||
|
```
|
||||||
|
Analog für Zeilen 115 und 119.
|
||||||
|
|
||||||
|
- [ ] **Step 4: Bestehende Unit-Tests grün**
|
||||||
|
|
||||||
|
Run: `npm run test -- ingredient.test.ts`
|
||||||
|
Expected: PASS (Tests prüfen nur vorhandene Felder, neues Feld stört nicht).
|
||||||
|
|
||||||
|
- [ ] **Step 5: Svelte-Check muss noch rot sein**
|
||||||
|
|
||||||
|
Run: `npm run check`
|
||||||
|
Expected: FAIL mit Fehlern in `repository.ts` (Select-Statement ohne `section_heading`). Das ist erwartet — wird in Task 2 behoben. Nicht hier fixen.
|
||||||
|
|
||||||
|
- [ ] **Step 6: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/types.ts src/lib/server/db/migrations/012_ingredient_section.sql src/lib/server/parsers/ingredient.ts
|
||||||
|
git commit -m "feat(schema): ingredient.section_heading (Migration 012 + Type)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 2: Repository-Layer Persistenz
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/lib/server/recipes/repository.ts` (insertRecipe, replaceIngredients, getRecipeById)
|
||||||
|
- Test: `tests/integration/recipe-repository.test.ts`
|
||||||
|
|
||||||
|
**Warum jetzt:** Nach Task 1 ist der Type-Vertrag aufgemacht. Die DB muss das Feld lesen und schreiben, sonst gehen Sektionen beim Save/Load verloren.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Failing test für Roundtrip**
|
||||||
|
|
||||||
|
Add to `tests/integration/recipe-repository.test.ts` inside `describe('recipe repository', ...)`:
|
||||||
|
```ts
|
||||||
|
it('persistiert section_heading und gibt es beim Laden zurück', () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
const recipe = baseRecipe({
|
||||||
|
title: 'Torte',
|
||||||
|
ingredients: [
|
||||||
|
{ position: 1, quantity: 200, unit: 'g', name: 'Mehl', note: null, raw_text: '200 g Mehl', section_heading: 'Für den Teig' },
|
||||||
|
{ position: 2, quantity: 100, unit: 'g', name: 'Zucker', note: null, raw_text: '100 g Zucker', section_heading: null },
|
||||||
|
{ position: 3, quantity: 300, unit: 'g', name: 'Beeren', note: null, raw_text: '300 g Beeren', section_heading: 'Für die Füllung' }
|
||||||
|
]
|
||||||
|
});
|
||||||
|
const id = insertRecipe(db, recipe);
|
||||||
|
const loaded = getRecipeById(db, id);
|
||||||
|
expect(loaded!.ingredients[0].section_heading).toBe('Für den Teig');
|
||||||
|
expect(loaded!.ingredients[1].section_heading).toBeNull();
|
||||||
|
expect(loaded!.ingredients[2].section_heading).toBe('Für die Füllung');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('replaceIngredients persistiert section_heading', () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
const id = insertRecipe(db, baseRecipe({ title: 'X' }));
|
||||||
|
replaceIngredients(db, id, [
|
||||||
|
{ position: 1, quantity: null, unit: null, name: 'A', note: null, raw_text: 'A', section_heading: 'Kopf' }
|
||||||
|
]);
|
||||||
|
const loaded = getRecipeById(db, id);
|
||||||
|
expect(loaded!.ingredients[0].section_heading).toBe('Kopf');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Test laufen — muss fehlschlagen**
|
||||||
|
|
||||||
|
Run: `npm run test -- recipe-repository.test.ts`
|
||||||
|
Expected: FAIL — `section_heading` kommt als `undefined` zurück, weil SQL-SELECT es nicht holt.
|
||||||
|
|
||||||
|
- [ ] **Step 3: INSERT-Statements erweitern**
|
||||||
|
|
||||||
|
Modify `src/lib/server/recipes/repository.ts`:
|
||||||
|
|
||||||
|
In `insertRecipe` (line ~66): Spalte + Parameter anhängen.
|
||||||
|
```ts
|
||||||
|
const insIng = db.prepare(
|
||||||
|
`INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text, section_heading)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
|
||||||
|
);
|
||||||
|
for (const ing of recipe.ingredients) {
|
||||||
|
insIng.run(id, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text, ing.section_heading);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
In `replaceIngredients` (line ~217): gleiche Änderung.
|
||||||
|
```ts
|
||||||
|
const ins = db.prepare(
|
||||||
|
`INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text, section_heading)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
|
||||||
|
);
|
||||||
|
for (const ing of ingredients) {
|
||||||
|
ins.run(recipeId, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text, ing.section_heading);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: SELECT-Statement erweitern**
|
||||||
|
|
||||||
|
In `getRecipeById` (line ~105):
|
||||||
|
```ts
|
||||||
|
const ingredients = db
|
||||||
|
.prepare(
|
||||||
|
`SELECT position, quantity, unit, name, note, raw_text, section_heading
|
||||||
|
FROM ingredient WHERE recipe_id = ? ORDER BY position`
|
||||||
|
)
|
||||||
|
.all(id) as Ingredient[];
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 5: Tests grün**
|
||||||
|
|
||||||
|
Run: `npm run test -- recipe-repository.test.ts`
|
||||||
|
Expected: PASS.
|
||||||
|
|
||||||
|
- [ ] **Step 6: Volle Suite + svelte-check**
|
||||||
|
|
||||||
|
Run: `npm test && npm run check`
|
||||||
|
Expected: Beides PASS. `svelte-check` ist jetzt auf Repo-Ebene typ-clean; View/Editor noch nicht berührt, deren Nutzung von `Ingredient` bleibt (Feld darf fehlen, weil der Type optional wirkt? — Nein, es ist `string | null`, also **pflicht**. Falls `check` rot wird, liegt es an Importer/Scaler-Aufrufern, die `Ingredient`-Literale bauen. Das ist dann Task 3.)
|
||||||
|
|
||||||
|
- [ ] **Step 7: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/server/recipes/repository.ts tests/integration/recipe-repository.test.ts
|
||||||
|
git commit -m "feat(db): section_heading roundtrip in recipe-repository"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 3: Importer-Passthrough + Scaler-Test
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/lib/recipes/scaler.ts` (nur falls Test rot — siehe unten)
|
||||||
|
- Test: `tests/unit/scaler.test.ts`
|
||||||
|
- Test: evtl. `tests/integration/importer.test.ts`
|
||||||
|
|
||||||
|
**Warum:** parseIngredient setzt `section_heading: null` (Task 1). Das reicht für den Importer — keine JSON-LD-Extraction. Aber der Scaler ruft `.map((i) => ({ ...i, quantity: ... }))` auf; das Spread erhält `section_heading` automatisch. Wir fügen nur einen Regressions-Test hinzu, dass das stimmt.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Scaler-Regressions-Test**
|
||||||
|
|
||||||
|
Add to `tests/unit/scaler.test.ts`:
|
||||||
|
```ts
|
||||||
|
it('preserves section_heading through scaling', () => {
|
||||||
|
const input: Ingredient[] = [
|
||||||
|
{ position: 1, quantity: 200, unit: 'g', name: 'Mehl', note: null, raw_text: '200 g Mehl', section_heading: 'Teig' },
|
||||||
|
{ position: 2, quantity: null, unit: null, name: 'Ei', note: null, raw_text: 'Ei', section_heading: null }
|
||||||
|
];
|
||||||
|
const scaled = scaleIngredients(input, 2);
|
||||||
|
expect(scaled[0].section_heading).toBe('Teig');
|
||||||
|
expect(scaled[1].section_heading).toBeNull();
|
||||||
|
expect(scaled[0].quantity).toBe(400);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Test laufen**
|
||||||
|
|
||||||
|
Run: `npm run test -- scaler.test.ts`
|
||||||
|
Expected: PASS (weil `...i` das Feld durchreicht).
|
||||||
|
|
||||||
|
Falls FAIL: In `src/lib/recipes/scaler.ts` das `.map` prüfen — es sollte `...i` spreaden und nur `quantity` überschreiben. Bei Abweichung angleichen.
|
||||||
|
|
||||||
|
- [ ] **Step 3: Importer-Roundtrip-Test (Bolognese-Fixture)**
|
||||||
|
|
||||||
|
Prüfen, dass Importer für Emmi-Fixture `section_heading: null` auf allen Zutaten liefert. Der existierende `importer.test.ts` sollte automatisch grün bleiben (parseIngredient setzt das Feld auf null), aber wir schauen kurz nach:
|
||||||
|
|
||||||
|
Run: `npm run test -- importer.test.ts`
|
||||||
|
Expected: PASS.
|
||||||
|
|
||||||
|
- [ ] **Step 4: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add tests/unit/scaler.test.ts
|
||||||
|
git commit -m "test(scaler): section_heading ueberlebt Skalierung"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 4: IngredientRow — Heading-Anzeige + Inline Insert-Button
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/lib/components/recipe-editor-types.ts`
|
||||||
|
- Modify: `src/lib/components/IngredientRow.svelte`
|
||||||
|
- Test: neue Svelte-Component-Tests via vitest-browser — **ausgenommen**: wir haben keine Svelte-Component-Unit-Tests im Repo. Stattdessen decken E2E + manuelle Verifikation ab. Das ist konsistent mit der bestehenden Praxis.
|
||||||
|
|
||||||
|
**Verhalten:**
|
||||||
|
- `DraftIng` bekommt `section_heading: string | null` (immer gesetzt, aber nullable).
|
||||||
|
- Hat eine Zeile `section_heading` als String (auch leer), wird oberhalb der Row ein `<input>` für den Titel gerendert plus ein kleiner „Sektion entfernen"-Button.
|
||||||
|
- Hat eine Zeile `section_heading === null`, wird ein dezenter `<button class="add-section">Abschnitt hinzufügen</button>` **über** der Row gerendert.
|
||||||
|
- IngredientRow bekommt Callbacks `onaddSection`, `onremoveSection` — Parent verwaltet das Array.
|
||||||
|
|
||||||
|
- [ ] **Step 1: DraftIng-Typ erweitern**
|
||||||
|
|
||||||
|
Modify `src/lib/components/recipe-editor-types.ts`:
|
||||||
|
```ts
|
||||||
|
export type DraftIng = {
|
||||||
|
qty: string;
|
||||||
|
unit: string;
|
||||||
|
name: string;
|
||||||
|
note: string;
|
||||||
|
section_heading: string | null;
|
||||||
|
};
|
||||||
|
export type DraftStep = { text: string };
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: IngredientRow erweitern — Props**
|
||||||
|
|
||||||
|
Modify `src/lib/components/IngredientRow.svelte` Script-Block:
|
||||||
|
```svelte
|
||||||
|
<script lang="ts">
|
||||||
|
import { Trash2, ChevronUp, ChevronDown, Plus, X } from 'lucide-svelte';
|
||||||
|
import type { DraftIng } from './recipe-editor-types';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
ing: DraftIng;
|
||||||
|
idx: number;
|
||||||
|
total: number;
|
||||||
|
onmove: (dir: -1 | 1) => void;
|
||||||
|
onremove: () => void;
|
||||||
|
onaddSection: () => void;
|
||||||
|
onremoveSection: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { ing, idx, total, onmove, onremove, onaddSection, onremoveSection }: Props = $props();
|
||||||
|
</script>
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: IngredientRow-Template — Section-Block + Add-Button**
|
||||||
|
|
||||||
|
Replace the existing `<li class="ing-row">…</li>` with:
|
||||||
|
```svelte
|
||||||
|
{#if ing.section_heading === null}
|
||||||
|
<li class="section-insert">
|
||||||
|
<button type="button" class="add-section" onclick={onaddSection}>
|
||||||
|
<Plus size={12} strokeWidth={2.5} />
|
||||||
|
<span>Abschnitt hinzufügen</span>
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
{:else}
|
||||||
|
<li class="section-heading-row">
|
||||||
|
<input
|
||||||
|
class="section-heading"
|
||||||
|
type="text"
|
||||||
|
bind:value={ing.section_heading}
|
||||||
|
placeholder="Sektion, z. B. „Für den Teig""
|
||||||
|
aria-label="Sektionsüberschrift"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="section-remove"
|
||||||
|
aria-label="Sektion entfernen"
|
||||||
|
onclick={onremoveSection}
|
||||||
|
>
|
||||||
|
<X size={14} strokeWidth={2.5} />
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
{/if}
|
||||||
|
<li class="ing-row">
|
||||||
|
<div class="move">
|
||||||
|
<!-- unchanged -->
|
||||||
|
<button class="move-btn" type="button" aria-label="Zutat nach oben" disabled={idx === 0} onclick={() => onmove(-1)}>
|
||||||
|
<ChevronUp size={14} strokeWidth={2.5} />
|
||||||
|
</button>
|
||||||
|
<button class="move-btn" type="button" aria-label="Zutat nach unten" disabled={idx === total - 1} onclick={() => onmove(1)}>
|
||||||
|
<ChevronDown size={14} strokeWidth={2.5} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<input class="qty" type="text" bind:value={ing.qty} placeholder="Menge" aria-label="Menge" />
|
||||||
|
<input class="unit" type="text" bind:value={ing.unit} placeholder="Einheit" aria-label="Einheit" />
|
||||||
|
<input class="name" type="text" bind:value={ing.name} placeholder="Zutat" aria-label="Zutat" />
|
||||||
|
<input class="note" type="text" bind:value={ing.note} placeholder="Notiz" aria-label="Notiz" />
|
||||||
|
<button class="del" type="button" aria-label="Zutat entfernen" onclick={onremove}>
|
||||||
|
<Trash2 size={16} strokeWidth={2} />
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Hinweis:** Wir rendern pro Row zwei `<li>`: optional einen Sektions-Block (Insert-Button ODER Heading-Input), plus die bestehende Zutaten-Row. Das passt in die `<ul class="ing-list">` des Parents — semantisch unsauber (nicht-Zutat-`<li>` in Zutatenliste), aber praktikabel; alternativ könnte IngredientRow auf `<div>` umgestellt werden, das wäre aber ein Parent-Umbau. Wir bleiben bei `<li>` und geben dem Section-`<li>` `list-style: none` via CSS.
|
||||||
|
|
||||||
|
- [ ] **Step 4: Styles für Section-UI**
|
||||||
|
|
||||||
|
Add to `<style>`-Block in `IngredientRow.svelte`:
|
||||||
|
```css
|
||||||
|
.section-insert {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
list-style: none;
|
||||||
|
margin: -0.2rem 0 0.1rem;
|
||||||
|
opacity: 0;
|
||||||
|
transition: opacity 0.15s;
|
||||||
|
}
|
||||||
|
.ing-list:hover .section-insert,
|
||||||
|
.section-insert:focus-within {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
.add-section {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.25rem;
|
||||||
|
padding: 0.2rem 0.55rem;
|
||||||
|
border: 1px dashed #cfd9d1;
|
||||||
|
background: white;
|
||||||
|
color: #2b6a3d;
|
||||||
|
border-radius: 999px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 0.75rem;
|
||||||
|
font-family: inherit;
|
||||||
|
}
|
||||||
|
.add-section:hover {
|
||||||
|
background: #f4f8f5;
|
||||||
|
}
|
||||||
|
.section-heading-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 1fr 32px;
|
||||||
|
gap: 0.35rem;
|
||||||
|
list-style: none;
|
||||||
|
margin-top: 0.4rem;
|
||||||
|
}
|
||||||
|
.section-heading {
|
||||||
|
padding: 0.45rem 0.7rem;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
font-weight: 600;
|
||||||
|
color: #2b6a3d;
|
||||||
|
font-family: inherit;
|
||||||
|
background: #f4f8f5;
|
||||||
|
}
|
||||||
|
.section-remove {
|
||||||
|
width: 32px;
|
||||||
|
height: 38px;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
background: white;
|
||||||
|
border-radius: 8px;
|
||||||
|
color: #666;
|
||||||
|
cursor: pointer;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
.section-remove:hover {
|
||||||
|
background: #fdf3f3;
|
||||||
|
border-color: #f1b4b4;
|
||||||
|
color: #c53030;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Begründung `opacity: 0` + Hover:** Der Insert-Button erscheint vor **jeder** Zeile — das ist visuelles Rauschen auf statischem Zustand. Fade-in-on-hover hält die Zutatenliste lesbar und macht den Button auf Mouse-Interaktion trotzdem sichtbar. Auf Touch-Geräten ist `:hover` ggf. sticky — das ist OK, weil auf Mobile die Zutatenliste ohnehin explorativ bedient wird. `:focus-within` deckt Keyboard-Navigation ab.
|
||||||
|
|
||||||
|
- [ ] **Step 5: svelte-check**
|
||||||
|
|
||||||
|
Run: `npm run check`
|
||||||
|
Expected: FAIL — `RecipeEditor.svelte` gibt die neuen Callbacks `onaddSection` / `onremoveSection` noch nicht rein, und `DraftIng`-Literale im Editor haben noch kein `section_heading`. Wird in Task 5 behoben.
|
||||||
|
|
||||||
|
- [ ] **Step 6: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/components/IngredientRow.svelte src/lib/components/recipe-editor-types.ts
|
||||||
|
git commit -m "feat(editor): Sektionsueberschriften in IngredientRow + Insert-Button"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 5: RecipeEditor — State, Handler, Save-Patch
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/lib/components/RecipeEditor.svelte`
|
||||||
|
|
||||||
|
- [ ] **Step 1: DraftIng-Seeding erweitern**
|
||||||
|
|
||||||
|
In `RecipeEditor.svelte` Script-Block, `ingredients`-State (line ~40):
|
||||||
|
```ts
|
||||||
|
let ingredients = $state<DraftIng[]>(
|
||||||
|
untrack(() =>
|
||||||
|
recipe.ingredients.map((i) => ({
|
||||||
|
qty: i.quantity !== null ? String(i.quantity).replace('.', ',') : '',
|
||||||
|
unit: i.unit ?? '',
|
||||||
|
name: i.name,
|
||||||
|
note: i.note ?? '',
|
||||||
|
section_heading: i.section_heading
|
||||||
|
}))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: addIngredient aktualisieren**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
function addIngredient() {
|
||||||
|
ingredients = [...ingredients, { qty: '', unit: '', name: '', note: '', section_heading: null }];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Section-Handler einfügen**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
function addSection(idx: number) {
|
||||||
|
const next = [...ingredients];
|
||||||
|
next[idx] = { ...next[idx], section_heading: '' };
|
||||||
|
ingredients = next;
|
||||||
|
}
|
||||||
|
function removeSection(idx: number) {
|
||||||
|
const next = [...ingredients];
|
||||||
|
next[idx] = { ...next[idx], section_heading: null };
|
||||||
|
ingredients = next;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: save()-Patch erweitern**
|
||||||
|
|
||||||
|
In `save()` (line ~86), das `cleanedIngredients`-Mapping:
|
||||||
|
```ts
|
||||||
|
const cleanedIngredients: Ingredient[] = ingredients
|
||||||
|
.filter((i) => i.name.trim())
|
||||||
|
.map((i, idx) => {
|
||||||
|
const qty = parseQty(i.qty);
|
||||||
|
const unit = i.unit.trim() || null;
|
||||||
|
const name = i.name.trim();
|
||||||
|
const note = i.note.trim() || null;
|
||||||
|
const rawParts: string[] = [];
|
||||||
|
if (qty !== null) rawParts.push(String(qty).replace('.', ','));
|
||||||
|
if (unit) rawParts.push(unit);
|
||||||
|
rawParts.push(name);
|
||||||
|
const heading = i.section_heading === null ? null : (i.section_heading.trim() || null);
|
||||||
|
return {
|
||||||
|
position: idx + 1,
|
||||||
|
quantity: qty,
|
||||||
|
unit,
|
||||||
|
name,
|
||||||
|
note,
|
||||||
|
raw_text: rawParts.join(' '),
|
||||||
|
section_heading: heading
|
||||||
|
};
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
**Regel:** Eine leere Sektion (`section_heading === ''` nach Trim) wird beim Speichern zu `null`. Begründung: User tippt „Abschnitt hinzufügen" und lässt das Feld leer → keine unbenannte Sektion in der View. Nur Zeilen mit echtem Titel werden als Sektionsanker persistiert.
|
||||||
|
|
||||||
|
- [ ] **Step 5: IngredientRow-Callbacks verdrahten**
|
||||||
|
|
||||||
|
In `RecipeEditor.svelte` Template (line ~170):
|
||||||
|
```svelte
|
||||||
|
{#each ingredients as ing, idx (idx)}
|
||||||
|
<IngredientRow
|
||||||
|
{ing}
|
||||||
|
{idx}
|
||||||
|
total={ingredients.length}
|
||||||
|
onmove={(dir) => moveIngredient(idx, dir)}
|
||||||
|
onremove={() => removeIngredient(idx)}
|
||||||
|
onaddSection={() => addSection(idx)}
|
||||||
|
onremoveSection={() => removeSection(idx)}
|
||||||
|
/>
|
||||||
|
{/each}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 6: svelte-check + Tests**
|
||||||
|
|
||||||
|
Run: `npm run check && npm test`
|
||||||
|
Expected: Beides grün.
|
||||||
|
|
||||||
|
- [ ] **Step 7: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/components/RecipeEditor.svelte
|
||||||
|
git commit -m "feat(editor): Sektionen-Handler + save-Patch mit section_heading"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 6: RecipeView — Sektions-Überschriften rendern
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/lib/components/RecipeView.svelte`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Zutatenliste umbauen**
|
||||||
|
|
||||||
|
In `RecipeView.svelte` (line ~128), den `<ul class="ing-list">`-Block:
|
||||||
|
|
||||||
|
```svelte
|
||||||
|
<ul class="ing-list">
|
||||||
|
{#each scaled as ing, i (i)}
|
||||||
|
{#if ing.section_heading && ing.section_heading.trim()}
|
||||||
|
<li class="section-heading">{ing.section_heading}</li>
|
||||||
|
{/if}
|
||||||
|
<li>
|
||||||
|
{#if ing.quantity !== null || ing.unit}
|
||||||
|
<span class="qty">
|
||||||
|
{formatQty(ing.quantity)}
|
||||||
|
{#if ing.unit}
|
||||||
|
{' '}{ing.unit}
|
||||||
|
{/if}
|
||||||
|
</span>
|
||||||
|
{/if}
|
||||||
|
<span class="name">
|
||||||
|
{ing.name}
|
||||||
|
{#if ing.note}<span class="note"> ({ing.note})</span>{/if}
|
||||||
|
</span>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
**Hinweis:** `<li class="section-heading">` statt `<h3>` — wir sind in einer `<ul>` und dürfen dort nur `<li>` direkt verschachteln. Semantisch ist das OK, Screenreader lesen die Heading-Klasse nicht als Landmark, aber sie liest den Text als normales Listen-Item; für ein Rezept ist das akzeptabel. Alternativ: `<ul>` in mehrere `<section>`s aufsplitten — deutlich komplexer bei gleicher visueller Wirkung; verschoben, bis jemand klagt.
|
||||||
|
|
||||||
|
- [ ] **Step 2: Style für .section-heading**
|
||||||
|
|
||||||
|
Add to `<style>`-Block in `RecipeView.svelte`:
|
||||||
|
```css
|
||||||
|
.ing-list .section-heading {
|
||||||
|
list-style: none;
|
||||||
|
font-weight: 600;
|
||||||
|
color: #2b6a3d;
|
||||||
|
font-size: 1rem;
|
||||||
|
margin-top: 0.9rem;
|
||||||
|
margin-bottom: 0.2rem;
|
||||||
|
padding: 0.15rem 0;
|
||||||
|
border-bottom: 1px solid #e4eae7;
|
||||||
|
}
|
||||||
|
.ing-list .section-heading:first-child {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Tests + Check**
|
||||||
|
|
||||||
|
Run: `npm run check && npm test`
|
||||||
|
Expected: Beides grün.
|
||||||
|
|
||||||
|
- [ ] **Step 4: Dev-Build-Smoke-Test**
|
||||||
|
|
||||||
|
Run: `npm run build && npm run preview`
|
||||||
|
Manuell: Rezept öffnen, editieren, Sektion „Teig" auf Zeile 1 setzen und „Füllung" auf Zeile 3, speichern. Wechsel zu View → beide Überschriften sichtbar, Skalierung ändert nur Mengen. Screenshot ist nice-to-have, nicht Pflicht.
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/components/RecipeView.svelte
|
||||||
|
git commit -m "feat(view): Zutaten-Sektionen als Ueberschriften rendern"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 7: Ship
|
||||||
|
|
||||||
|
- [ ] **Step 1: Finale Testsuite**
|
||||||
|
|
||||||
|
Run: `npm run check && npm test`
|
||||||
|
Expected: Beides grün.
|
||||||
|
|
||||||
|
- [ ] **Step 2: Push**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git push -u origin feature/ingredient-sections
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Auf Deploy warten (CI-Image-Build, Pi-Pull)**
|
||||||
|
|
||||||
|
User wird manuell signalisieren, wenn deployed.
|
||||||
|
|
||||||
|
- [ ] **Step 4: Nach Deploy — Playwright Remote-Smoke**
|
||||||
|
|
||||||
|
Run: `npm run test:e2e:remote`
|
||||||
|
Expected: 42/42 green (unchanged suite, wir haben keine Recipe-Edit-E2E-Tests hinzugefügt).
|
||||||
|
|
||||||
|
- [ ] **Step 5: Merge zu main**
|
||||||
|
|
||||||
|
Falls E2E grün:
|
||||||
|
```bash
|
||||||
|
git checkout main
|
||||||
|
git merge --no-ff feature/ingredient-sections -m "Merge ingredient-sections — Zutaten-Gruppierung via section_heading"
|
||||||
|
git push
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Self-Review-Notiz
|
||||||
|
|
||||||
|
- Spec-Coverage: alle drei User-Anforderungen abgedeckt (Inline-Button vor jeder Zeile → Task 4, nur Zutaten → keine Step-Änderungen, Edit-Mode-only → Importer unverändert).
|
||||||
|
- Type-Konsistenz: `section_heading: string | null` überall einheitlich (Ingredient, DraftIng, Save-Patch).
|
||||||
|
- Keine Placeholder — alle SQL-/Code-Snippets ausgeschrieben.
|
||||||
|
- Migrations-Reihenfolge: `012_` nach `011_clear_favicon_for_rerun.sql`.
|
||||||
|
- FTS-Impact: `section_heading` taucht nicht im FTS-Trigger auf (`001_init.sql` nutzt `name`, `description`, `ingredients_concat`, `tags_concat`). Das ist bewusst so — Sektionstitel sind Organisationshilfen, kein Suchinhalt. User suchen nach „Mehl", nicht nach „Für den Teig".
|
||||||
971
docs/superpowers/plans/2026-04-19-search-state-store.md
Normal file
971
docs/superpowers/plans/2026-04-19-search-state-store.md
Normal file
@@ -0,0 +1,971 @@
|
|||||||
|
# Search-State-Store Implementation Plan
|
||||||
|
|
||||||
|
> **For agentic workers:** REQUIRED SUB-SKILL: Use `superpowers:executing-plans` to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||||
|
|
||||||
|
**Goal:** Extract the duplicated live-search state machine from `src/routes/+page.svelte` and `src/routes/+layout.svelte` into a single reusable `SearchStore` class in `src/lib/client/search.svelte.ts`, so both the home search and the header dropdown drive their UI from the same logic.
|
||||||
|
|
||||||
|
**Architecture:** Factory-class store (one instance per consumer, like `new SearchStore()` — not a shared singleton). Holds all `$state` fields currently inlined in the Svelte components (query, hits, webHits, searching flags, error, pagination state), plus imperative methods (`runDebounced`, `loadMore`, `reSearch`, `reset`, `captureSnapshot`, `restoreSnapshot`). Consumers keep UI-specific concerns (URL sync, dropdown open/close, snapshot hookup) in their component — the store owns only fetch/pagination/debounce.
|
||||||
|
|
||||||
|
**Tech Stack:** Svelte 5 runes (`$state` in class fields), TypeScript-strict, Vitest + jsdom, fetch injection for tests.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Design Snapshot
|
||||||
|
|
||||||
|
**API surface (locked before implementation):**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// src/lib/client/search.svelte.ts
|
||||||
|
import type { SearchHit } from '$lib/server/recipes/search-local';
|
||||||
|
import type { WebHit } from '$lib/server/search/searxng';
|
||||||
|
|
||||||
|
export type SearchSnapshot = {
|
||||||
|
query: string;
|
||||||
|
hits: SearchHit[];
|
||||||
|
webHits: WebHit[];
|
||||||
|
searchedFor: string | null;
|
||||||
|
webError: string | null;
|
||||||
|
localExhausted: boolean;
|
||||||
|
webPageno: number;
|
||||||
|
webExhausted: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SearchStoreOptions = {
|
||||||
|
pageSize?: number; // default 30
|
||||||
|
debounceMs?: number; // default 300
|
||||||
|
filterDebounceMs?: number; // default 150 (shorter for filter-change re-search)
|
||||||
|
minQueryLength?: number; // default 4 (query.trim().length > 3)
|
||||||
|
filterParam?: () => string; // e.g. () => searchFilterStore.queryParam → "foo,bar" or ""
|
||||||
|
fetchImpl?: typeof fetch; // injected for tests
|
||||||
|
};
|
||||||
|
|
||||||
|
export class SearchStore {
|
||||||
|
query = $state('');
|
||||||
|
hits = $state<SearchHit[]>([]);
|
||||||
|
webHits = $state<WebHit[]>([]);
|
||||||
|
searching = $state(false);
|
||||||
|
webSearching = $state(false);
|
||||||
|
webError = $state<string | null>(null);
|
||||||
|
searchedFor = $state<string | null>(null);
|
||||||
|
localExhausted = $state(false);
|
||||||
|
webPageno = $state(0);
|
||||||
|
webExhausted = $state(false);
|
||||||
|
loadingMore = $state(false);
|
||||||
|
|
||||||
|
constructor(opts?: SearchStoreOptions);
|
||||||
|
|
||||||
|
/** Call from `$effect(() => { store.query; store.runDebounced(); })`. Handles debounce + race-guard. */
|
||||||
|
runDebounced(): void;
|
||||||
|
/** Immediate (no debounce). Used by form `submit`. */
|
||||||
|
runSearch(q: string): Promise<void>;
|
||||||
|
/** Filter-change re-search — shorter debounce. */
|
||||||
|
reSearch(): void;
|
||||||
|
/** Paginate locally, then fall back to web. Idempotent while in-flight. */
|
||||||
|
loadMore(): Promise<void>;
|
||||||
|
/** Clear query + results + cancel any pending debounce (e.g. `afterNavigate`). */
|
||||||
|
reset(): void;
|
||||||
|
/** For SvelteKit `Snapshot<>` API. */
|
||||||
|
captureSnapshot(): SearchSnapshot;
|
||||||
|
restoreSnapshot(s: SearchSnapshot): void;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Behavior invariants (copied 1:1 from the current code — do NOT change):**
|
||||||
|
- Query threshold: `trim().length > 3` triggers search, `<= 3` clears results.
|
||||||
|
- Race-guard: after every `await fetch(...)`, bail if `this.query.trim() !== q`.
|
||||||
|
- When `hits.length === 0` after local search → auto-fire web search page 1.
|
||||||
|
- `loadMore`: first drains local (offset pagination), then switches to web (pageno pagination).
|
||||||
|
- Dedup: local by `id`, web by `url`.
|
||||||
|
- `webError`: keep the message text so UI can render it.
|
||||||
|
|
||||||
|
**What stays OUT of the store:**
|
||||||
|
- URL sync (`history.replaceState` with `?q=`) → stays in `+page.svelte`.
|
||||||
|
- Dropdown visibility (`navOpen`) → stays in `+layout.svelte`.
|
||||||
|
- `afterNavigate`-reset wiring → stays in `+layout.svelte`, just calls `store.reset()`.
|
||||||
|
- SvelteKit `Snapshot<>` wiring → stays in `+page.svelte`, delegates to store.
|
||||||
|
- Filter-change re-search `$effect` → stays in `+page.svelte`, just calls `store.reSearch()`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 1: Failing Unit Tests for SearchStore
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `tests/unit/search-store.test.ts`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write test file with full behavior coverage (runs red until Task 2)**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// @vitest-environment jsdom
|
||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||||
|
import { SearchStore } from '../../src/lib/client/search.svelte';
|
||||||
|
|
||||||
|
type FetchMock = ReturnType<typeof vi.fn>;
|
||||||
|
|
||||||
|
function mockFetch(responses: Array<{ ok?: boolean; status?: number; body: unknown }>): FetchMock {
|
||||||
|
const calls = [...responses];
|
||||||
|
return vi.fn(async () => {
|
||||||
|
const r = calls.shift();
|
||||||
|
if (!r) throw new Error('fetch called more times than expected');
|
||||||
|
return {
|
||||||
|
ok: r.ok ?? true,
|
||||||
|
status: r.status ?? 200,
|
||||||
|
json: async () => r.body
|
||||||
|
} as Response;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('SearchStore', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps results empty while query is <= 3 chars (debounced)', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 50 });
|
||||||
|
store.query = 'abc';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(100);
|
||||||
|
expect(store.searching).toBe(false);
|
||||||
|
expect(fetchImpl).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fires local search after debounce when query > 3 chars', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [{ id: 1, title: 'Pasta', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }] } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 50, pageSize: 30 });
|
||||||
|
store.query = 'pasta';
|
||||||
|
store.runDebounced();
|
||||||
|
expect(store.searching).toBe(true);
|
||||||
|
await vi.advanceTimersByTimeAsync(100);
|
||||||
|
await vi.waitFor(() => expect(fetchImpl).toHaveBeenCalled());
|
||||||
|
expect(fetchImpl.mock.calls[0][0]).toMatch(/\/api\/recipes\/search\?q=pasta&limit=30/);
|
||||||
|
expect(store.hits).toHaveLength(1);
|
||||||
|
expect(store.searchedFor).toBe('pasta');
|
||||||
|
expect(store.localExhausted).toBe(true); // 1 hit < pageSize → exhausted
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to web search when local returns zero hits', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [] } },
|
||||||
|
{ body: { hits: [{ url: 'https://chefkoch.de/x', title: 'Foo', domain: 'chefkoch.de', snippet: null, thumbnail: null }] } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 50 });
|
||||||
|
store.query = 'pizza';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(100);
|
||||||
|
await vi.waitFor(() => expect(store.webHits).toHaveLength(1));
|
||||||
|
expect(fetchImpl).toHaveBeenCalledTimes(2);
|
||||||
|
expect(fetchImpl.mock.calls[1][0]).toMatch(/\/api\/recipes\/search\/web\?q=pizza&pageno=1/);
|
||||||
|
expect(store.webPageno).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('races-guards: stale response discarded when query changed mid-flight', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [{ id: 99, title: 'Stale', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }] } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 10 });
|
||||||
|
store.query = 'stale-query';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
store.query = 'different'; // user kept typing
|
||||||
|
await vi.waitFor(() => expect(fetchImpl).toHaveBeenCalled());
|
||||||
|
expect(store.hits).toEqual([]); // stale discarded
|
||||||
|
});
|
||||||
|
|
||||||
|
it('loadMore: drains local first (offset pagination)', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const page1 = Array.from({ length: 30 }, (_, i) => ({ id: i, title: `r${i}`, description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }));
|
||||||
|
const page2 = Array.from({ length: 5 }, (_, i) => ({ id: i + 30, title: `r${i + 30}`, description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }));
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: page1 } },
|
||||||
|
{ body: { hits: page2 } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 10, pageSize: 30 });
|
||||||
|
store.query = 'meal';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
await vi.waitFor(() => expect(store.hits).toHaveLength(30));
|
||||||
|
expect(store.localExhausted).toBe(false);
|
||||||
|
await store.loadMore();
|
||||||
|
expect(store.hits).toHaveLength(35);
|
||||||
|
expect(fetchImpl.mock.calls[1][0]).toMatch(/offset=30/);
|
||||||
|
expect(store.localExhausted).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('loadMore: switches to web pagination after local exhausted', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const local = [{ id: 1, title: 'local', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }];
|
||||||
|
const webP1 = [{ url: 'https://a.com', title: 'A', domain: 'a.com', snippet: null, thumbnail: null }];
|
||||||
|
const webP2 = [{ url: 'https://b.com', title: 'B', domain: 'b.com', snippet: null, thumbnail: null }];
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: local } },
|
||||||
|
{ body: { hits: webP1 } }, // auto-fallback? No — local has 1 hit, so no fallback.
|
||||||
|
{ body: { hits: webP2 } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 10, pageSize: 30 });
|
||||||
|
store.query = 'soup';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
await vi.waitFor(() => expect(store.hits).toHaveLength(1));
|
||||||
|
expect(store.localExhausted).toBe(true);
|
||||||
|
await store.loadMore(); // web pageno=1
|
||||||
|
expect(store.webHits).toHaveLength(1);
|
||||||
|
await store.loadMore(); // web pageno=2
|
||||||
|
expect(store.webHits).toHaveLength(2);
|
||||||
|
expect(store.webPageno).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('web search error sets webError and marks webExhausted', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [] } },
|
||||||
|
{ ok: false, status: 502, body: { message: 'SearXNG unreachable' } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 10 });
|
||||||
|
store.query = 'anything';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
await vi.waitFor(() => expect(store.webError).toBe('SearXNG unreachable'));
|
||||||
|
expect(store.webExhausted).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reset(): clears query, results, and pending debounce', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 100 });
|
||||||
|
store.query = 'foobar';
|
||||||
|
store.runDebounced();
|
||||||
|
store.reset();
|
||||||
|
await vi.advanceTimersByTimeAsync(200);
|
||||||
|
expect(store.query).toBe('');
|
||||||
|
expect(store.hits).toEqual([]);
|
||||||
|
expect(fetchImpl).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('captureSnapshot / restoreSnapshot: round-trips without re-fetching', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 50 });
|
||||||
|
const snap: SearchSnapshot = {
|
||||||
|
query: 'lasagne',
|
||||||
|
hits: [{ id: 7, title: 'Lasagne', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }],
|
||||||
|
webHits: [],
|
||||||
|
searchedFor: 'lasagne',
|
||||||
|
webError: null,
|
||||||
|
localExhausted: true,
|
||||||
|
webPageno: 0,
|
||||||
|
webExhausted: false
|
||||||
|
};
|
||||||
|
store.restoreSnapshot(snap);
|
||||||
|
expect(store.query).toBe('lasagne');
|
||||||
|
expect(store.hits).toHaveLength(1);
|
||||||
|
store.runDebounced(); // should NOT re-fetch after restore
|
||||||
|
await vi.advanceTimersByTimeAsync(100);
|
||||||
|
expect(fetchImpl).not.toHaveBeenCalled();
|
||||||
|
const round = store.captureSnapshot();
|
||||||
|
expect(round).toEqual(snap);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('filterParam option: gets appended to both local and web requests', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [] } },
|
||||||
|
{ body: { hits: [] } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({
|
||||||
|
fetchImpl,
|
||||||
|
debounceMs: 10,
|
||||||
|
filterParam: () => '&domains=chefkoch.de'
|
||||||
|
});
|
||||||
|
store.query = 'curry';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
await vi.waitFor(() => expect(fetchImpl).toHaveBeenCalledTimes(2));
|
||||||
|
expect(fetchImpl.mock.calls[0][0]).toMatch(/&domains=chefkoch\.de/);
|
||||||
|
expect(fetchImpl.mock.calls[1][0]).toMatch(/&domains=chefkoch\.de/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reSearch: immediate re-run with current query on filter change', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
let filter = '';
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [] } },
|
||||||
|
{ body: { hits: [] } },
|
||||||
|
{ body: { hits: [{ id: 1, title: 'filtered', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }] } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({
|
||||||
|
fetchImpl,
|
||||||
|
debounceMs: 10,
|
||||||
|
filterDebounceMs: 5,
|
||||||
|
filterParam: () => filter
|
||||||
|
});
|
||||||
|
store.query = 'broth';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
// Simulate filter change
|
||||||
|
filter = '&domains=chefkoch.de';
|
||||||
|
store.reSearch();
|
||||||
|
await vi.advanceTimersByTimeAsync(10);
|
||||||
|
await vi.waitFor(() => expect(store.hits).toHaveLength(1));
|
||||||
|
// Last call should have filter param
|
||||||
|
const last = fetchImpl.mock.calls.at(-1)?.[0] as string;
|
||||||
|
expect(last).toMatch(/&domains=chefkoch\.de/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run tests to verify all fail with "SearchStore is not a constructor" or "Cannot find module"**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm test -- search-store.test
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: 12 tests, all failing because `src/lib/client/search.svelte.ts` doesn't exist yet.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 2: Implement SearchStore to pass tests
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `src/lib/client/search.svelte.ts`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Scaffold the class + types**
|
||||||
|
|
||||||
|
Create `src/lib/client/search.svelte.ts` with this content:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import type { SearchHit } from '$lib/server/recipes/search-local';
|
||||||
|
import type { WebHit } from '$lib/server/search/searxng';
|
||||||
|
|
||||||
|
export type SearchSnapshot = {
|
||||||
|
query: string;
|
||||||
|
hits: SearchHit[];
|
||||||
|
webHits: WebHit[];
|
||||||
|
searchedFor: string | null;
|
||||||
|
webError: string | null;
|
||||||
|
localExhausted: boolean;
|
||||||
|
webPageno: number;
|
||||||
|
webExhausted: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SearchStoreOptions = {
|
||||||
|
pageSize?: number;
|
||||||
|
debounceMs?: number;
|
||||||
|
filterDebounceMs?: number;
|
||||||
|
minQueryLength?: number;
|
||||||
|
filterParam?: () => string;
|
||||||
|
fetchImpl?: typeof fetch;
|
||||||
|
};
|
||||||
|
|
||||||
|
export class SearchStore {
|
||||||
|
query = $state('');
|
||||||
|
hits = $state<SearchHit[]>([]);
|
||||||
|
webHits = $state<WebHit[]>([]);
|
||||||
|
searching = $state(false);
|
||||||
|
webSearching = $state(false);
|
||||||
|
webError = $state<string | null>(null);
|
||||||
|
searchedFor = $state<string | null>(null);
|
||||||
|
localExhausted = $state(false);
|
||||||
|
webPageno = $state(0);
|
||||||
|
webExhausted = $state(false);
|
||||||
|
loadingMore = $state(false);
|
||||||
|
|
||||||
|
private readonly pageSize: number;
|
||||||
|
private readonly debounceMs: number;
|
||||||
|
private readonly filterDebounceMs: number;
|
||||||
|
private readonly minQueryLength: number;
|
||||||
|
private readonly filterParam: () => string;
|
||||||
|
private readonly fetchImpl: typeof fetch;
|
||||||
|
private debounceTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
private skipNextDebounce = false;
|
||||||
|
|
||||||
|
constructor(opts: SearchStoreOptions = {}) {
|
||||||
|
this.pageSize = opts.pageSize ?? 30;
|
||||||
|
this.debounceMs = opts.debounceMs ?? 300;
|
||||||
|
this.filterDebounceMs = opts.filterDebounceMs ?? 150;
|
||||||
|
this.minQueryLength = opts.minQueryLength ?? 4;
|
||||||
|
this.filterParam = opts.filterParam ?? (() => '');
|
||||||
|
this.fetchImpl = opts.fetchImpl ?? ((...a) => fetch(...a));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Implement `runDebounced`, `runSearch`, private `runWebSearch`**
|
||||||
|
|
||||||
|
Add to the class:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
runDebounced(): void {
|
||||||
|
// Consumer pattern:
|
||||||
|
// $effect(() => { store.query; store.runDebounced(); });
|
||||||
|
// The bare `store.query` read registers the reactive dep; this method
|
||||||
|
// then reads `this.query` live to kick off / debounce the search.
|
||||||
|
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
||||||
|
if (this.skipNextDebounce) {
|
||||||
|
this.skipNextDebounce = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const q = this.query.trim();
|
||||||
|
if (q.length < this.minQueryLength) {
|
||||||
|
this.resetResults();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.searching = true;
|
||||||
|
this.webHits = [];
|
||||||
|
this.webSearching = false;
|
||||||
|
this.webError = null;
|
||||||
|
this.debounceTimer = setTimeout(() => {
|
||||||
|
void this.runSearch(q);
|
||||||
|
}, this.debounceMs);
|
||||||
|
}
|
||||||
|
|
||||||
|
async runSearch(q: string): Promise<void> {
|
||||||
|
this.localExhausted = false;
|
||||||
|
this.webPageno = 0;
|
||||||
|
this.webExhausted = false;
|
||||||
|
try {
|
||||||
|
const res = await this.fetchImpl(
|
||||||
|
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${this.pageSize}${this.filterParam()}`
|
||||||
|
);
|
||||||
|
const body = (await res.json()) as { hits: SearchHit[] };
|
||||||
|
if (this.query.trim() !== q) return;
|
||||||
|
this.hits = body.hits;
|
||||||
|
this.searchedFor = q;
|
||||||
|
if (this.hits.length < this.pageSize) this.localExhausted = true;
|
||||||
|
if (this.hits.length === 0) {
|
||||||
|
await this.runWebSearch(q, 1);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (this.query.trim() === q) this.searching = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async runWebSearch(q: string, pageno: number): Promise<void> {
|
||||||
|
this.webSearching = true;
|
||||||
|
try {
|
||||||
|
const res = await this.fetchImpl(
|
||||||
|
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${pageno}${this.filterParam()}`
|
||||||
|
);
|
||||||
|
if (this.query.trim() !== q) return;
|
||||||
|
if (!res.ok) {
|
||||||
|
const err = (await res.json().catch(() => ({}))) as { message?: string };
|
||||||
|
this.webError = err.message ?? `HTTP ${res.status}`;
|
||||||
|
this.webExhausted = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const body = (await res.json()) as { hits: WebHit[] };
|
||||||
|
this.webHits = pageno === 1 ? body.hits : [...this.webHits, ...body.hits];
|
||||||
|
this.webPageno = pageno;
|
||||||
|
if (body.hits.length === 0) this.webExhausted = true;
|
||||||
|
} finally {
|
||||||
|
if (this.query.trim() === q) this.webSearching = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Implement `loadMore`**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
async loadMore(): Promise<void> {
|
||||||
|
if (this.loadingMore) return;
|
||||||
|
const q = this.query.trim();
|
||||||
|
if (!q) return;
|
||||||
|
this.loadingMore = true;
|
||||||
|
try {
|
||||||
|
if (!this.localExhausted) {
|
||||||
|
const res = await this.fetchImpl(
|
||||||
|
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${this.pageSize}&offset=${this.hits.length}${this.filterParam()}`
|
||||||
|
);
|
||||||
|
const body = (await res.json()) as { hits: SearchHit[] };
|
||||||
|
if (this.query.trim() !== q) return;
|
||||||
|
const more = body.hits;
|
||||||
|
const seen = new Set(this.hits.map((h) => h.id));
|
||||||
|
const deduped = more.filter((h) => !seen.has(h.id));
|
||||||
|
this.hits = [...this.hits, ...deduped];
|
||||||
|
if (more.length < this.pageSize) this.localExhausted = true;
|
||||||
|
} else if (!this.webExhausted) {
|
||||||
|
const nextPage = this.webPageno + 1;
|
||||||
|
const wasEmpty = this.webHits.length === 0;
|
||||||
|
if (wasEmpty) this.webSearching = true;
|
||||||
|
try {
|
||||||
|
const res = await this.fetchImpl(
|
||||||
|
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${nextPage}${this.filterParam()}`
|
||||||
|
);
|
||||||
|
if (this.query.trim() !== q) return;
|
||||||
|
if (!res.ok) {
|
||||||
|
const err = (await res.json().catch(() => ({}))) as { message?: string };
|
||||||
|
this.webError = err.message ?? `HTTP ${res.status}`;
|
||||||
|
this.webExhausted = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const body = (await res.json()) as { hits: WebHit[] };
|
||||||
|
const more = body.hits;
|
||||||
|
const seen = new Set(this.webHits.map((h) => h.url));
|
||||||
|
const deduped = more.filter((h) => !seen.has(h.url));
|
||||||
|
if (deduped.length === 0) {
|
||||||
|
this.webExhausted = true;
|
||||||
|
} else {
|
||||||
|
this.webHits = [...this.webHits, ...deduped];
|
||||||
|
this.webPageno = nextPage;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (this.query.trim() === q) this.webSearching = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
this.loadingMore = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Implement `reSearch`, `reset`, `resetResults`, snapshot methods**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
reSearch(): void {
|
||||||
|
const q = this.query.trim();
|
||||||
|
if (q.length < this.minQueryLength) return;
|
||||||
|
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
||||||
|
this.searching = true;
|
||||||
|
this.webHits = [];
|
||||||
|
this.webSearching = false;
|
||||||
|
this.webError = null;
|
||||||
|
this.debounceTimer = setTimeout(() => void this.runSearch(q), this.filterDebounceMs);
|
||||||
|
}
|
||||||
|
|
||||||
|
reset(): void {
|
||||||
|
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
||||||
|
this.debounceTimer = null;
|
||||||
|
this.query = '';
|
||||||
|
this.resetResults();
|
||||||
|
}
|
||||||
|
|
||||||
|
private resetResults(): void {
|
||||||
|
this.hits = [];
|
||||||
|
this.webHits = [];
|
||||||
|
this.searchedFor = null;
|
||||||
|
this.searching = false;
|
||||||
|
this.webSearching = false;
|
||||||
|
this.webError = null;
|
||||||
|
this.localExhausted = false;
|
||||||
|
this.webPageno = 0;
|
||||||
|
this.webExhausted = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
captureSnapshot(): SearchSnapshot {
|
||||||
|
return {
|
||||||
|
query: this.query,
|
||||||
|
hits: this.hits,
|
||||||
|
webHits: this.webHits,
|
||||||
|
searchedFor: this.searchedFor,
|
||||||
|
webError: this.webError,
|
||||||
|
localExhausted: this.localExhausted,
|
||||||
|
webPageno: this.webPageno,
|
||||||
|
webExhausted: this.webExhausted
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
restoreSnapshot(s: SearchSnapshot): void {
|
||||||
|
this.skipNextDebounce = true;
|
||||||
|
this.query = s.query;
|
||||||
|
this.hits = s.hits;
|
||||||
|
this.webHits = s.webHits;
|
||||||
|
this.searchedFor = s.searchedFor;
|
||||||
|
this.webError = s.webError;
|
||||||
|
this.localExhausted = s.localExhausted;
|
||||||
|
this.webPageno = s.webPageno;
|
||||||
|
this.webExhausted = s.webExhausted;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 5: Run tests, iterate until all green**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm test -- search-store.test
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: all 12 tests pass.
|
||||||
|
|
||||||
|
- [ ] **Step 6: `npm run check`**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run check
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: 0 errors, 0 warnings in `search.svelte.ts`.
|
||||||
|
|
||||||
|
- [ ] **Step 7: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/lib/client/search.svelte.ts tests/unit/search-store.test.ts
|
||||||
|
git commit -m "feat(search): SearchStore fuer Live-Search mit Web-Fallback
|
||||||
|
|
||||||
|
Extrahiert die duplizierte Such-Logik aus +page.svelte und
|
||||||
|
+layout.svelte in eine gemeinsame Klasse. Pure Datenschicht
|
||||||
|
mit injizierbarem fetch — UI-Concerns (URL-Sync, Dropdown,
|
||||||
|
Snapshot) bleiben in den Komponenten."
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 3: Migrate `+layout.svelte` header dropdown
|
||||||
|
|
||||||
|
**Why first:** Smaller surface than `+page.svelte`, no snapshot API, no URL sync. If the store is wrong, here we find out with less code at risk.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/routes/+layout.svelte:20-200`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Add import**
|
||||||
|
|
||||||
|
At the top of `<script>`:
|
||||||
|
```ts
|
||||||
|
import { SearchStore } from '$lib/client/search.svelte';
|
||||||
|
import { searchFilterStore } from '$lib/client/search-filter.svelte';
|
||||||
|
```
|
||||||
|
(Latter is already imported — just confirm.)
|
||||||
|
|
||||||
|
- [ ] **Step 2: Replace the 11 `$state` declarations (navQuery, navHits, navWebHits, navSearching, navWebSearching, navWebError, navLocalExhausted, navWebPageno, navWebExhausted, navLoadingMore, debounceTimer) with one store instance.**
|
||||||
|
|
||||||
|
Keep these (UI-only): `navOpen`, `navContainer`, `menuOpen`, `menuContainer`.
|
||||||
|
|
||||||
|
New:
|
||||||
|
```ts
|
||||||
|
const navStore = new SearchStore({
|
||||||
|
pageSize: 30,
|
||||||
|
filterParam: () => {
|
||||||
|
const p = searchFilterStore.queryParam;
|
||||||
|
return p ? `&domains=${encodeURIComponent(p)}` : '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Remove the local `filterParam()` helper — the store owns it now.
|
||||||
|
|
||||||
|
- [ ] **Step 3: Replace the big `$effect` (lines 52–109) with a 3-line `$effect`**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
$effect(() => {
|
||||||
|
// Bare reads register the reactive deps; then kick the store.
|
||||||
|
const q = navStore.query;
|
||||||
|
navStore.runDebounced();
|
||||||
|
// navOpen follows query length: open while typing, close when cleared.
|
||||||
|
navOpen = q.trim().length > 3;
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Replace `loadMoreNav` function (lines 111–159) with a pass-through**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
function loadMoreNav() {
|
||||||
|
return navStore.loadMore();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Or inline `onclick={() => navStore.loadMore()}` at the call-site — pick the less disruptive option when looking at the template.
|
||||||
|
|
||||||
|
- [ ] **Step 5: Replace `submitNav` (lines 161–167)**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
function submitNav(e: SubmitEvent) {
|
||||||
|
e.preventDefault();
|
||||||
|
const q = navStore.query.trim();
|
||||||
|
if (!q) return;
|
||||||
|
navOpen = false;
|
||||||
|
void goto(`/?q=${encodeURIComponent(q)}`);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 6: Replace `pickHit` (lines 185–190)**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
function pickHit() {
|
||||||
|
navOpen = false;
|
||||||
|
navStore.reset();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 7: Update `afterNavigate` (lines 192+)**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
afterNavigate(() => {
|
||||||
|
navStore.reset();
|
||||||
|
navOpen = false;
|
||||||
|
menuOpen = false;
|
||||||
|
// ... rest of existing body (wishlist refresh etc.)
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 8: Update the template**
|
||||||
|
|
||||||
|
Every `navQuery` → `navStore.query`, every `navHits` → `navStore.hits`, etc. This is a mechanical rename — use find+replace scoped to `src/routes/+layout.svelte` only.
|
||||||
|
|
||||||
|
Mapping:
|
||||||
|
- `navQuery` → `navStore.query`
|
||||||
|
- `navHits` → `navStore.hits`
|
||||||
|
- `navWebHits` → `navStore.webHits`
|
||||||
|
- `navSearching` → `navStore.searching`
|
||||||
|
- `navWebSearching` → `navStore.webSearching`
|
||||||
|
- `navWebError` → `navStore.webError`
|
||||||
|
- `navLocalExhausted` → `navStore.localExhausted`
|
||||||
|
- `navWebPageno` → `navStore.webPageno` (if referenced in template)
|
||||||
|
- `navWebExhausted` → `navStore.webExhausted`
|
||||||
|
- `navLoadingMore` → `navStore.loadingMore`
|
||||||
|
|
||||||
|
`bind:value={navQuery}` on the `<input>` → `bind:value={navStore.query}`.
|
||||||
|
|
||||||
|
- [ ] **Step 9: Run checks**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run check
|
||||||
|
npm test
|
||||||
|
```
|
||||||
|
|
||||||
|
Both must be clean.
|
||||||
|
|
||||||
|
- [ ] **Step 10: Smoke-test dev server manually**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
Open a recipe page → type in header dropdown → verify: dropdown opens, shows local hits, falls back to web for unknown query, "+ weitere Ergebnisse" paginates, clicking a hit closes the dropdown, navigating back/forward clears the dropdown.
|
||||||
|
|
||||||
|
- [ ] **Step 11: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/routes/+layout.svelte
|
||||||
|
git commit -m "refactor(layout): Header-Dropdown nutzt SearchStore
|
||||||
|
|
||||||
|
Ersetzt die 11 lokalen \$state und den Debounce-Effect durch
|
||||||
|
eine SearchStore-Instanz. Nav-Open-Toggle bleibt lokal, weil
|
||||||
|
UI-Concern."
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 4: Migrate `+page.svelte` home
|
||||||
|
|
||||||
|
**Why after Task 3:** The store is now field-tested. Home adds snapshot + URL sync + filter-change re-search on top.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `src/routes/+page.svelte:1-371`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Add imports**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { SearchStore, type SearchSnapshot } from '$lib/client/search.svelte';
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Remove the duplicated `$state` block (lines 17–32)**
|
||||||
|
|
||||||
|
Delete: `query`, `hits`, `webHits`, `searching`, `webSearching`, `webError`, `searchedFor`, `localExhausted`, `webPageno`, `webExhausted`, `loadingMore`, `skipNextSearch`, `debounceTimer`.
|
||||||
|
|
||||||
|
Keep: `quote`, `recent`, `favorites` (not search-related), and all `all*` state (All-Recipes listing — unrelated to search).
|
||||||
|
|
||||||
|
Add:
|
||||||
|
```ts
|
||||||
|
const store = new SearchStore({
|
||||||
|
pageSize: LOCAL_PAGE,
|
||||||
|
filterParam: () => {
|
||||||
|
const p = searchFilterStore.queryParam;
|
||||||
|
return p ? `&domains=${encodeURIComponent(p)}` : '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Remove the local `filterParam()` helper (lines 224–227).
|
||||||
|
|
||||||
|
- [ ] **Step 3: Rewire the `Snapshot<>` API (lines 50–83)**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
export const snapshot: Snapshot<SearchSnapshot> = {
|
||||||
|
capture: () => store.captureSnapshot(),
|
||||||
|
restore: (s) => store.restoreSnapshot(s)
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Delete the old `SearchSnapshot` local type alias (it's now imported).
|
||||||
|
|
||||||
|
- [ ] **Step 4: Replace the two search `$effect`s (filter-change + query-change) with two one-liners**
|
||||||
|
|
||||||
|
Remove lines 188–199 (filter-change effect) and lines 322–347 (query-change effect).
|
||||||
|
|
||||||
|
Add:
|
||||||
|
```ts
|
||||||
|
$effect(() => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||||
|
store.query; // register reactive dep
|
||||||
|
store.runDebounced();
|
||||||
|
});
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||||
|
searchFilterStore.active;
|
||||||
|
store.reSearch();
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 5: Keep the URL-sync `$effect` as-is, but read from `store.query`**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
$effect(() => {
|
||||||
|
if (typeof window === 'undefined') return;
|
||||||
|
const q = store.query.trim();
|
||||||
|
const url = new URL(window.location.href);
|
||||||
|
const current = url.searchParams.get('q') ?? '';
|
||||||
|
if (q === current) return;
|
||||||
|
if (q) url.searchParams.set('q', q);
|
||||||
|
else url.searchParams.delete('q');
|
||||||
|
history.replaceState(history.state, '', url.toString());
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 6: Update `onMount` URL-restore**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
const urlQ = ($page.url.searchParams.get('q') ?? '').trim();
|
||||||
|
if (urlQ) store.query = urlQ;
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 7: Delete `runSearch` and `loadMore` local functions (lines 229–320)**
|
||||||
|
|
||||||
|
The store provides both. Template references `loadMore` → change to `store.loadMore()`.
|
||||||
|
|
||||||
|
- [ ] **Step 8: Update `submit`**
|
||||||
|
|
||||||
|
```ts
|
||||||
|
function submit(e: SubmitEvent) {
|
||||||
|
e.preventDefault();
|
||||||
|
const q = store.query.trim();
|
||||||
|
if (q.length <= 3) return;
|
||||||
|
void store.runSearch(q);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 9: Update the template (same mechanical rename as Task 3)**
|
||||||
|
|
||||||
|
`query` → `store.query`, `hits` → `store.hits`, etc. for all 11 fields.
|
||||||
|
|
||||||
|
`bind:value={query}` → `bind:value={store.query}`.
|
||||||
|
|
||||||
|
`activeSearch` derived stays: `const activeSearch = $derived(store.query.trim().length > 3);`
|
||||||
|
|
||||||
|
- [ ] **Step 10: Run checks**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run check
|
||||||
|
npm test
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 11: Verify file is shorter than before**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
wc -l src/routes/+page.svelte
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: under 700 lines (was 808). Target from roadmap: under 700 L.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
wc -l src/routes/+layout.svelte
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: under 600 lines (was 681). Target from roadmap: under 600 L.
|
||||||
|
|
||||||
|
- [ ] **Step 12: Smoke-test dev manually**
|
||||||
|
|
||||||
|
- Type "lasagne" in home → local hits appear.
|
||||||
|
- Type "pizza margherita" → web fallback.
|
||||||
|
- Deep-link `/?q=lasagne` → query restored, results visible.
|
||||||
|
- Navigate to recipe → back → home query + results preserved (snapshot).
|
||||||
|
- Change domain filter while query is active → results re-fetch with new filter.
|
||||||
|
|
||||||
|
- [ ] **Step 13: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add src/routes/+page.svelte
|
||||||
|
git commit -m "refactor(home): Live-Search auf SearchStore migriert
|
||||||
|
|
||||||
|
Entfernt 11 duplizierte \$state, runSearch, loadMore und beide
|
||||||
|
Debounce-Effekte. URL-Sync, Snapshot und Filter-Re-Search bleiben
|
||||||
|
hier — aber alle delegieren an den Store."
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 5: Remote E2E smoke (optional — only if CI deploy happens)
|
||||||
|
|
||||||
|
**Trigger:** Only run this task if CI builds the `search-state-store` branch and deploys to `kochwas-dev.siegeln.net`. Otherwise skip to Task 6.
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Run: existing `tests/e2e/remote/search.spec.ts`
|
||||||
|
|
||||||
|
- [ ] **Step 1: Run remote suite**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run test:e2e:remote -- search.spec.ts
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: 4/4 pass (existing coverage is sufficient — no new specs needed for a pure refactor).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Task 6: Self-review + merge prep
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Review: all changed files
|
||||||
|
|
||||||
|
- [ ] **Step 1: `npm test` full suite**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm test
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: all pass (previous count + 12 new SearchStore tests).
|
||||||
|
|
||||||
|
- [ ] **Step 2: `npm run check` full repo**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run check
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected: 0 errors, 0 warnings.
|
||||||
|
|
||||||
|
- [ ] **Step 3: `git diff main...HEAD` review**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git diff main...HEAD --stat
|
||||||
|
git log main..HEAD --oneline
|
||||||
|
```
|
||||||
|
|
||||||
|
Expected commits:
|
||||||
|
1. `feat(search): SearchStore fuer Live-Search mit Web-Fallback`
|
||||||
|
2. `refactor(layout): Header-Dropdown nutzt SearchStore`
|
||||||
|
3. `refactor(home): Live-Search auf SearchStore migriert`
|
||||||
|
|
||||||
|
- [ ] **Step 4: Push branch**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git push -u origin search-state-store
|
||||||
|
```
|
||||||
|
|
||||||
|
CI builds branch-tagged image → user tests on `kochwas-dev.siegeln.net` → merges to main when clean.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Risk Notes
|
||||||
|
|
||||||
|
- **Svelte 5 `$state` in classes:** Standard pattern in this repo (`SearchFilterStore`, `PWAStore`). Works.
|
||||||
|
- **Two instances of `SearchStore` simultaneously:** Each has its own timer + state. No shared mutable state between them — verified because the store has no static fields.
|
||||||
|
- **Snapshot restore racing with `runDebounced`:** Handled via `skipNextDebounce` flag. Same mechanism as the current `skipNextSearch` in `+page.svelte`.
|
||||||
|
- **Filter change on home while query is empty:** `reSearch()` early-exits when `q.length < minQueryLength`. Safe.
|
||||||
|
- **`afterNavigate` firing during an in-flight search:** `reset()` clears timer and mutates `query`. Any in-flight fetch will race-guard-fail on the next `if (this.query.trim() !== q) return;`. Results get dropped, which is the desired behavior.
|
||||||
|
|
||||||
|
## Deferred — NOT in this plan
|
||||||
|
|
||||||
|
- **Search-Store-Tests mit echtem Browser-`$effect`:** Would need `@sveltejs/vite-plugin-svelte` test setup with component mount. Current Vitest setup is Node-only. Skip — the injected-fetch unit tests cover the state machine.
|
||||||
|
- **Shared store instance (singleton) instead of per-consumer:** Rejected during design — would couple home and header search semantically.
|
||||||
|
- **Web-Hit-Cache im Store:** Out of scope. The roadmap explicitly scopes this phase to state extraction, not perf work.
|
||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "kochwas",
|
"name": "kochwas",
|
||||||
"version": "0.1.0",
|
"version": "1.2.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "kochwas",
|
"name": "kochwas",
|
||||||
"version": "0.1.0",
|
"version": "1.2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/archiver": "^7.0.0",
|
"@types/archiver": "^7.0.0",
|
||||||
"@types/yauzl": "^2.10.3",
|
"@types/yauzl": "^2.10.3",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "kochwas",
|
"name": "kochwas",
|
||||||
"version": "0.1.0",
|
"version": "1.2.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -14,7 +14,8 @@
|
|||||||
"format": "prettier --write .",
|
"format": "prettier --write .",
|
||||||
"render:icons": "node scripts/render-icons.mjs",
|
"render:icons": "node scripts/render-icons.mjs",
|
||||||
"test:e2e": "playwright test",
|
"test:e2e": "playwright test",
|
||||||
"test:e2e:ui": "playwright test --ui"
|
"test:e2e:ui": "playwright test --ui",
|
||||||
|
"test:e2e:remote": "playwright test --config=playwright.remote.config.ts"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@playwright/test": "^1.59.1",
|
"@playwright/test": "^1.59.1",
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { defineConfig } from '@playwright/test';
|
|||||||
// Preview-Server (kein Dev-Server, damit der SW registrierbar ist).
|
// Preview-Server (kein Dev-Server, damit der SW registrierbar ist).
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
testDir: 'tests/e2e',
|
testDir: 'tests/e2e',
|
||||||
|
testIgnore: ['tests/e2e/remote/**'],
|
||||||
fullyParallel: false,
|
fullyParallel: false,
|
||||||
reporter: 'list',
|
reporter: 'list',
|
||||||
use: {
|
use: {
|
||||||
|
|||||||
32
playwright.remote.config.ts
Normal file
32
playwright.remote.config.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import { defineConfig } from '@playwright/test';
|
||||||
|
|
||||||
|
// Zweite Playwright-Config fuer E2E-Smoketests gegen ein deployed
|
||||||
|
// Environment (standardmaessig kochwas-dev.siegeln.net).
|
||||||
|
//
|
||||||
|
// Getrennt von playwright.config.ts, weil diese Tests:
|
||||||
|
// - keinen lokalen Preview-Server starten
|
||||||
|
// - gegen eine echte Datenbank laufen (daher workers: 1, afterEach-Cleanup)
|
||||||
|
// - Service-Worker-Lifecycle nicht manipulieren (das macht offline.spec.ts lokal)
|
||||||
|
//
|
||||||
|
// Ausfuehrung: npm run test:e2e:remote
|
||||||
|
// Ziel-URL ueberschreiben: E2E_REMOTE_URL=https://... npm run test:e2e:remote
|
||||||
|
const BASE_URL = process.env.E2E_REMOTE_URL ?? 'https://kochwas-dev.siegeln.net';
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
testDir: 'tests/e2e/remote',
|
||||||
|
fullyParallel: false,
|
||||||
|
workers: 1,
|
||||||
|
retries: 0,
|
||||||
|
reporter: [['list'], ['html', { open: 'never', outputFolder: 'playwright-report-remote' }]],
|
||||||
|
use: {
|
||||||
|
baseURL: BASE_URL,
|
||||||
|
headless: true,
|
||||||
|
trace: 'retain-on-failure',
|
||||||
|
screenshot: 'only-on-failure',
|
||||||
|
// Service-Worker blocken: Diese Suite testet Live-API-Verhalten gegen
|
||||||
|
// den Server, keine PWA-Features (dafuer offline.spec.ts lokal). Die
|
||||||
|
// frische SW-Registrierung pro Context akkumulierte im Single-Worker-
|
||||||
|
// Run Browser-State und crashte Chromium zufaellig nach 20-30 Specs.
|
||||||
|
serviceWorkers: 'block'
|
||||||
|
}
|
||||||
|
});
|
||||||
225
src/lib/client/search.svelte.ts
Normal file
225
src/lib/client/search.svelte.ts
Normal file
@@ -0,0 +1,225 @@
|
|||||||
|
import type { SearchHit } from '$lib/server/recipes/search-local';
|
||||||
|
import type { WebHit } from '$lib/server/search/searxng';
|
||||||
|
|
||||||
|
export type SearchSnapshot = {
|
||||||
|
query: string;
|
||||||
|
hits: SearchHit[];
|
||||||
|
webHits: WebHit[];
|
||||||
|
searchedFor: string | null;
|
||||||
|
webError: string | null;
|
||||||
|
localExhausted: boolean;
|
||||||
|
webPageno: number;
|
||||||
|
webExhausted: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SearchStoreOptions = {
|
||||||
|
pageSize?: number;
|
||||||
|
debounceMs?: number;
|
||||||
|
filterDebounceMs?: number;
|
||||||
|
minQueryLength?: number;
|
||||||
|
filterParam?: () => string;
|
||||||
|
fetchImpl?: typeof fetch;
|
||||||
|
};
|
||||||
|
|
||||||
|
export class SearchStore {
|
||||||
|
query = $state('');
|
||||||
|
hits = $state<SearchHit[]>([]);
|
||||||
|
webHits = $state<WebHit[]>([]);
|
||||||
|
searching = $state(false);
|
||||||
|
webSearching = $state(false);
|
||||||
|
webError = $state<string | null>(null);
|
||||||
|
searchedFor = $state<string | null>(null);
|
||||||
|
localExhausted = $state(false);
|
||||||
|
webPageno = $state(0);
|
||||||
|
webExhausted = $state(false);
|
||||||
|
loadingMore = $state(false);
|
||||||
|
|
||||||
|
private readonly pageSize: number;
|
||||||
|
private readonly debounceMs: number;
|
||||||
|
private readonly filterDebounceMs: number;
|
||||||
|
private readonly minQueryLength: number;
|
||||||
|
private readonly filterParam: () => string;
|
||||||
|
private readonly fetchImpl: typeof fetch;
|
||||||
|
private debounceTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
private skipNextDebounce = false;
|
||||||
|
|
||||||
|
constructor(opts: SearchStoreOptions = {}) {
|
||||||
|
this.pageSize = opts.pageSize ?? 30;
|
||||||
|
this.debounceMs = opts.debounceMs ?? 300;
|
||||||
|
this.filterDebounceMs = opts.filterDebounceMs ?? 150;
|
||||||
|
this.minQueryLength = opts.minQueryLength ?? 4;
|
||||||
|
this.filterParam = opts.filterParam ?? (() => '');
|
||||||
|
this.fetchImpl = opts.fetchImpl ?? ((...a) => fetch(...a));
|
||||||
|
}
|
||||||
|
|
||||||
|
runDebounced(): void {
|
||||||
|
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
||||||
|
if (this.skipNextDebounce) {
|
||||||
|
this.skipNextDebounce = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const q = this.query.trim();
|
||||||
|
if (q.length < this.minQueryLength) {
|
||||||
|
this.resetResults();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.searching = true;
|
||||||
|
this.webHits = [];
|
||||||
|
this.webSearching = false;
|
||||||
|
this.webError = null;
|
||||||
|
this.debounceTimer = setTimeout(() => {
|
||||||
|
void this.runSearch(q);
|
||||||
|
}, this.debounceMs);
|
||||||
|
}
|
||||||
|
|
||||||
|
async runSearch(q: string): Promise<void> {
|
||||||
|
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
||||||
|
this.debounceTimer = null;
|
||||||
|
this.localExhausted = false;
|
||||||
|
this.webPageno = 0;
|
||||||
|
this.webExhausted = false;
|
||||||
|
try {
|
||||||
|
const res = await this.fetchImpl(
|
||||||
|
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${this.pageSize}${this.filterParam()}`
|
||||||
|
);
|
||||||
|
const body = (await res.json()) as { hits: SearchHit[] };
|
||||||
|
if (this.query.trim() !== q) return;
|
||||||
|
this.hits = body.hits;
|
||||||
|
this.searchedFor = q;
|
||||||
|
if (this.hits.length < this.pageSize) this.localExhausted = true;
|
||||||
|
if (this.hits.length === 0) {
|
||||||
|
await this.runWebSearch(q, 1);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (this.query.trim() === q) this.searching = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async runWebSearch(q: string, pageno: number): Promise<void> {
|
||||||
|
this.webSearching = true;
|
||||||
|
try {
|
||||||
|
const res = await this.fetchImpl(
|
||||||
|
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${pageno}${this.filterParam()}`
|
||||||
|
);
|
||||||
|
if (this.query.trim() !== q) return;
|
||||||
|
if (!res.ok) {
|
||||||
|
const err = (await res.json().catch(() => ({}))) as { message?: string };
|
||||||
|
this.webError = err.message ?? `HTTP ${res.status}`;
|
||||||
|
this.webExhausted = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const body = (await res.json()) as { hits: WebHit[] };
|
||||||
|
this.webHits = pageno === 1 ? body.hits : [...this.webHits, ...body.hits];
|
||||||
|
this.webPageno = pageno;
|
||||||
|
if (body.hits.length === 0) this.webExhausted = true;
|
||||||
|
} finally {
|
||||||
|
if (this.query.trim() === q) this.webSearching = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadMore(): Promise<void> {
|
||||||
|
if (this.loadingMore) return;
|
||||||
|
const q = this.query.trim();
|
||||||
|
if (!q) return;
|
||||||
|
this.loadingMore = true;
|
||||||
|
try {
|
||||||
|
if (!this.localExhausted) {
|
||||||
|
const res = await this.fetchImpl(
|
||||||
|
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${this.pageSize}&offset=${this.hits.length}${this.filterParam()}`
|
||||||
|
);
|
||||||
|
const body = (await res.json()) as { hits: SearchHit[] };
|
||||||
|
if (this.query.trim() !== q) return;
|
||||||
|
const more = body.hits;
|
||||||
|
const seen = new Set(this.hits.map((h) => h.id));
|
||||||
|
const deduped = more.filter((h) => !seen.has(h.id));
|
||||||
|
this.hits = [...this.hits, ...deduped];
|
||||||
|
if (more.length < this.pageSize) this.localExhausted = true;
|
||||||
|
} else if (!this.webExhausted) {
|
||||||
|
const nextPage = this.webPageno + 1;
|
||||||
|
const wasEmpty = this.webHits.length === 0;
|
||||||
|
if (wasEmpty) this.webSearching = true;
|
||||||
|
try {
|
||||||
|
const res = await this.fetchImpl(
|
||||||
|
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${nextPage}${this.filterParam()}`
|
||||||
|
);
|
||||||
|
if (this.query.trim() !== q) return;
|
||||||
|
if (!res.ok) {
|
||||||
|
const err = (await res.json().catch(() => ({}))) as { message?: string };
|
||||||
|
this.webError = err.message ?? `HTTP ${res.status}`;
|
||||||
|
this.webExhausted = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const body = (await res.json()) as { hits: WebHit[] };
|
||||||
|
const more = body.hits;
|
||||||
|
const seen = new Set(this.webHits.map((h) => h.url));
|
||||||
|
const deduped = more.filter((h) => !seen.has(h.url));
|
||||||
|
if (deduped.length === 0) {
|
||||||
|
this.webExhausted = true;
|
||||||
|
} else {
|
||||||
|
this.webHits = [...this.webHits, ...deduped];
|
||||||
|
this.webPageno = nextPage;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (this.query.trim() === q) this.webSearching = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
this.loadingMore = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reSearch(): void {
|
||||||
|
const q = this.query.trim();
|
||||||
|
if (q.length < this.minQueryLength) return;
|
||||||
|
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
||||||
|
this.searching = true;
|
||||||
|
this.webHits = [];
|
||||||
|
this.webSearching = false;
|
||||||
|
this.webError = null;
|
||||||
|
this.debounceTimer = setTimeout(() => void this.runSearch(q), this.filterDebounceMs);
|
||||||
|
}
|
||||||
|
|
||||||
|
reset(): void {
|
||||||
|
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
||||||
|
this.debounceTimer = null;
|
||||||
|
this.query = '';
|
||||||
|
this.resetResults();
|
||||||
|
}
|
||||||
|
|
||||||
|
private resetResults(): void {
|
||||||
|
this.hits = [];
|
||||||
|
this.webHits = [];
|
||||||
|
this.searchedFor = null;
|
||||||
|
this.searching = false;
|
||||||
|
this.webSearching = false;
|
||||||
|
this.webError = null;
|
||||||
|
this.localExhausted = false;
|
||||||
|
this.webPageno = 0;
|
||||||
|
this.webExhausted = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
captureSnapshot(): SearchSnapshot {
|
||||||
|
return {
|
||||||
|
query: this.query,
|
||||||
|
hits: this.hits,
|
||||||
|
webHits: this.webHits,
|
||||||
|
searchedFor: this.searchedFor,
|
||||||
|
webError: this.webError,
|
||||||
|
localExhausted: this.localExhausted,
|
||||||
|
webPageno: this.webPageno,
|
||||||
|
webExhausted: this.webExhausted
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
restoreSnapshot(s: SearchSnapshot): void {
|
||||||
|
this.skipNextDebounce = true;
|
||||||
|
this.query = s.query;
|
||||||
|
this.hits = s.hits;
|
||||||
|
this.webHits = s.webHits;
|
||||||
|
this.searchedFor = s.searchedFor;
|
||||||
|
this.webError = s.webError;
|
||||||
|
this.localExhausted = s.localExhausted;
|
||||||
|
this.webPageno = s.webPageno;
|
||||||
|
this.webExhausted = s.webExhausted;
|
||||||
|
}
|
||||||
|
}
|
||||||
190
src/lib/components/ImageUploadBox.svelte
Normal file
190
src/lib/components/ImageUploadBox.svelte
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { untrack } from 'svelte';
|
||||||
|
import { ImagePlus, ImageOff } from 'lucide-svelte';
|
||||||
|
import { confirmAction } from '$lib/client/confirm.svelte';
|
||||||
|
import { asyncFetch } from '$lib/client/api-fetch-wrapper';
|
||||||
|
import { requireOnline } from '$lib/client/require-online';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
recipeId: number;
|
||||||
|
imagePath: string | null;
|
||||||
|
onchange: (path: string | null) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { recipeId, imagePath: initial, onchange }: Props = $props();
|
||||||
|
|
||||||
|
let imagePath = $state<string | null>(untrack(() => initial));
|
||||||
|
let uploading = $state(false);
|
||||||
|
let fileInput: HTMLInputElement | null = $state(null);
|
||||||
|
|
||||||
|
const imageSrc = $derived(
|
||||||
|
imagePath === null
|
||||||
|
? null
|
||||||
|
: /^https?:\/\//i.test(imagePath)
|
||||||
|
? imagePath
|
||||||
|
: `/images/${imagePath}`
|
||||||
|
);
|
||||||
|
|
||||||
|
async function onFileChosen(event: Event) {
|
||||||
|
const input = event.target as HTMLInputElement;
|
||||||
|
const file = input.files?.[0];
|
||||||
|
input.value = '';
|
||||||
|
if (!file) return;
|
||||||
|
if (!requireOnline('Der Bild-Upload')) return;
|
||||||
|
uploading = true;
|
||||||
|
try {
|
||||||
|
const fd = new FormData();
|
||||||
|
fd.append('file', file);
|
||||||
|
const res = await asyncFetch(
|
||||||
|
`/api/recipes/${recipeId}/image`,
|
||||||
|
{ method: 'POST', body: fd },
|
||||||
|
'Upload fehlgeschlagen'
|
||||||
|
);
|
||||||
|
if (!res) return;
|
||||||
|
const body = await res.json();
|
||||||
|
imagePath = body.image_path;
|
||||||
|
onchange(imagePath);
|
||||||
|
} finally {
|
||||||
|
uploading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function removeImage() {
|
||||||
|
if (imagePath === null) return;
|
||||||
|
const ok = await confirmAction({
|
||||||
|
title: 'Bild entfernen?',
|
||||||
|
message: 'Das Rezept wird danach ohne Titelbild angezeigt.',
|
||||||
|
confirmLabel: 'Entfernen',
|
||||||
|
destructive: true
|
||||||
|
});
|
||||||
|
if (!ok) return;
|
||||||
|
if (!requireOnline('Das Entfernen')) return;
|
||||||
|
uploading = true;
|
||||||
|
try {
|
||||||
|
const res = await asyncFetch(
|
||||||
|
`/api/recipes/${recipeId}/image`,
|
||||||
|
{ method: 'DELETE' },
|
||||||
|
'Entfernen fehlgeschlagen'
|
||||||
|
);
|
||||||
|
if (!res) return;
|
||||||
|
imagePath = null;
|
||||||
|
onchange(null);
|
||||||
|
} finally {
|
||||||
|
uploading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="image-row">
|
||||||
|
<div class="image-preview" class:empty={!imageSrc}>
|
||||||
|
{#if imageSrc}
|
||||||
|
<img src={imageSrc} alt="" />
|
||||||
|
{:else}
|
||||||
|
<span class="placeholder">Kein Bild</span>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
<div class="image-actions">
|
||||||
|
<button
|
||||||
|
class="btn"
|
||||||
|
type="button"
|
||||||
|
onclick={() => fileInput?.click()}
|
||||||
|
disabled={uploading}
|
||||||
|
>
|
||||||
|
<ImagePlus size={16} strokeWidth={2} />
|
||||||
|
<span>{imagePath ? 'Bild ersetzen' : 'Bild hochladen'}</span>
|
||||||
|
</button>
|
||||||
|
{#if imagePath}
|
||||||
|
<button class="btn ghost" type="button" onclick={removeImage} disabled={uploading}>
|
||||||
|
<ImageOff size={16} strokeWidth={2} />
|
||||||
|
<span>Entfernen</span>
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
{#if uploading}
|
||||||
|
<span class="upload-status">Lade …</span>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
<input
|
||||||
|
bind:this={fileInput}
|
||||||
|
type="file"
|
||||||
|
accept="image/jpeg,image/png,image/webp,image/gif,image/avif"
|
||||||
|
class="file-input"
|
||||||
|
onchange={onFileChosen}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<p class="image-hint">Max. 10 MB. JPG, PNG, WebP, GIF oder AVIF.</p>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.image-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 1rem;
|
||||||
|
align-items: flex-start;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
.image-preview {
|
||||||
|
width: 160px;
|
||||||
|
aspect-ratio: 16 / 10;
|
||||||
|
border-radius: 10px;
|
||||||
|
overflow: hidden;
|
||||||
|
background: #eef3ef;
|
||||||
|
border: 1px solid #e4eae7;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
.image-preview img {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
object-fit: cover;
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
.image-preview.empty {
|
||||||
|
display: grid;
|
||||||
|
place-items: center;
|
||||||
|
color: #999;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
}
|
||||||
|
.image-preview .placeholder {
|
||||||
|
padding: 0 0.5rem;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.image-actions {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.5rem;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.upload-status {
|
||||||
|
color: #666;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
}
|
||||||
|
.file-input {
|
||||||
|
position: absolute;
|
||||||
|
width: 1px;
|
||||||
|
height: 1px;
|
||||||
|
opacity: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
.image-hint {
|
||||||
|
margin: 0.6rem 0 0;
|
||||||
|
color: #888;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
}
|
||||||
|
.btn {
|
||||||
|
padding: 0.55rem 0.85rem;
|
||||||
|
border-radius: 10px;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
background: white;
|
||||||
|
cursor: pointer;
|
||||||
|
font-family: inherit;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
min-height: 40px;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.4rem;
|
||||||
|
}
|
||||||
|
.btn.ghost {
|
||||||
|
color: #666;
|
||||||
|
}
|
||||||
|
.btn:disabled {
|
||||||
|
opacity: 0.6;
|
||||||
|
cursor: progress;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
221
src/lib/components/IngredientRow.svelte
Normal file
221
src/lib/components/IngredientRow.svelte
Normal file
@@ -0,0 +1,221 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { Trash2, ChevronUp, ChevronDown, Plus } from 'lucide-svelte';
|
||||||
|
import type { DraftIng } from './recipe-editor-types';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
ing: DraftIng;
|
||||||
|
idx: number;
|
||||||
|
total: number;
|
||||||
|
onmove: (dir: -1 | 1) => void;
|
||||||
|
onremove: () => void;
|
||||||
|
onaddSection: () => void;
|
||||||
|
onremoveSection: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { ing, idx, total, onmove, onremove, onaddSection, onremoveSection }: Props = $props();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if ing.section_heading === null}
|
||||||
|
<li class="section-insert">
|
||||||
|
<button type="button" class="add-section" onclick={onaddSection}>
|
||||||
|
<Plus size={12} strokeWidth={2.5} />
|
||||||
|
<span>Abschnitt hinzufügen</span>
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
{:else}
|
||||||
|
<li class="section-heading-row">
|
||||||
|
<input
|
||||||
|
class="section-heading"
|
||||||
|
type="text"
|
||||||
|
bind:value={ing.section_heading}
|
||||||
|
placeholder='Sektion, z. B. „Für den Teig"'
|
||||||
|
aria-label="Sektionsüberschrift"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class="section-remove"
|
||||||
|
aria-label="Sektion entfernen"
|
||||||
|
onclick={onremoveSection}
|
||||||
|
>
|
||||||
|
<Trash2 size={14} strokeWidth={2} />
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
{/if}
|
||||||
|
<li class="ing-row">
|
||||||
|
<div class="move">
|
||||||
|
<button
|
||||||
|
class="move-btn"
|
||||||
|
type="button"
|
||||||
|
aria-label="Zutat nach oben"
|
||||||
|
disabled={idx === 0}
|
||||||
|
onclick={() => onmove(-1)}
|
||||||
|
>
|
||||||
|
<ChevronUp size={14} strokeWidth={2.5} />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
class="move-btn"
|
||||||
|
type="button"
|
||||||
|
aria-label="Zutat nach unten"
|
||||||
|
disabled={idx === total - 1}
|
||||||
|
onclick={() => onmove(1)}
|
||||||
|
>
|
||||||
|
<ChevronDown size={14} strokeWidth={2.5} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<input class="qty" type="text" bind:value={ing.qty} placeholder="Menge" aria-label="Menge" />
|
||||||
|
<input class="unit" type="text" bind:value={ing.unit} placeholder="Einheit" aria-label="Einheit" />
|
||||||
|
<input class="name" type="text" bind:value={ing.name} placeholder="Zutat" aria-label="Zutat" />
|
||||||
|
<input class="note" type="text" bind:value={ing.note} placeholder="Notiz" aria-label="Notiz" />
|
||||||
|
<button class="del" type="button" aria-label="Zutat entfernen" onclick={onremove}>
|
||||||
|
<Trash2 size={16} strokeWidth={2} />
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.ing-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 28px 70px 70px 1fr 1fr 40px;
|
||||||
|
gap: 0.35rem;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.move {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 2px;
|
||||||
|
}
|
||||||
|
.move-btn {
|
||||||
|
width: 28px;
|
||||||
|
height: 20px;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
background: white;
|
||||||
|
border-radius: 6px;
|
||||||
|
cursor: pointer;
|
||||||
|
color: #555;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
.move-btn:hover:not(:disabled) {
|
||||||
|
background: #f4f8f5;
|
||||||
|
}
|
||||||
|
.move-btn:disabled {
|
||||||
|
opacity: 0.3;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
.ing-row input {
|
||||||
|
padding: 0.5rem 0.55rem;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
min-height: 38px;
|
||||||
|
font-family: inherit;
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
.del {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
border: 1px solid #f1b4b4;
|
||||||
|
background: white;
|
||||||
|
color: #c53030;
|
||||||
|
border-radius: 8px;
|
||||||
|
cursor: pointer;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
.del:hover {
|
||||||
|
background: #fdf3f3;
|
||||||
|
}
|
||||||
|
@media (max-width: 560px) {
|
||||||
|
.ing-row {
|
||||||
|
grid-template-columns: 28px 70px 1fr 40px;
|
||||||
|
grid-template-areas:
|
||||||
|
'move qty name del'
|
||||||
|
'move unit unit del'
|
||||||
|
'note note note note';
|
||||||
|
}
|
||||||
|
.ing-row .move {
|
||||||
|
grid-area: move;
|
||||||
|
}
|
||||||
|
.ing-row .qty {
|
||||||
|
grid-area: qty;
|
||||||
|
}
|
||||||
|
.ing-row .unit {
|
||||||
|
grid-area: unit;
|
||||||
|
}
|
||||||
|
.ing-row .name {
|
||||||
|
grid-area: name;
|
||||||
|
}
|
||||||
|
.ing-row .note {
|
||||||
|
grid-area: note;
|
||||||
|
}
|
||||||
|
.ing-row .del {
|
||||||
|
grid-area: del;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.section-insert {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
list-style: none;
|
||||||
|
margin: -0.2rem 0 0.1rem;
|
||||||
|
opacity: 0;
|
||||||
|
transition: opacity 0.15s;
|
||||||
|
}
|
||||||
|
/* Parent-UL liegt im RecipeEditor, daher :global(.ing-list). Ohne das
|
||||||
|
scopt Svelte die Klasse und der Selector matcht zur Laufzeit nicht. */
|
||||||
|
:global(.ing-list):hover .section-insert,
|
||||||
|
.section-insert:focus-within {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
.add-section {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.25rem;
|
||||||
|
padding: 0.2rem 0.55rem;
|
||||||
|
border: 1px dashed #cfd9d1;
|
||||||
|
background: white;
|
||||||
|
color: #2b6a3d;
|
||||||
|
border-radius: 999px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 0.75rem;
|
||||||
|
font-family: inherit;
|
||||||
|
}
|
||||||
|
.add-section:hover {
|
||||||
|
background: #f4f8f5;
|
||||||
|
}
|
||||||
|
.section-heading-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 1fr 32px;
|
||||||
|
gap: 0.35rem;
|
||||||
|
list-style: none;
|
||||||
|
margin-top: 0.4rem;
|
||||||
|
}
|
||||||
|
.section-heading {
|
||||||
|
padding: 0.45rem 0.7rem;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
font-weight: 600;
|
||||||
|
color: #2b6a3d;
|
||||||
|
font-family: inherit;
|
||||||
|
background: #f4f8f5;
|
||||||
|
}
|
||||||
|
.section-remove {
|
||||||
|
width: 32px;
|
||||||
|
height: 38px;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
background: white;
|
||||||
|
border-radius: 8px;
|
||||||
|
color: #666;
|
||||||
|
cursor: pointer;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
.section-remove:hover {
|
||||||
|
background: #fdf3f3;
|
||||||
|
border-color: #f1b4b4;
|
||||||
|
color: #c53030;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { untrack } from 'svelte';
|
import { untrack } from 'svelte';
|
||||||
import { Plus, Trash2, ChevronUp, ChevronDown, ImagePlus, ImageOff } from 'lucide-svelte';
|
import { Plus } from 'lucide-svelte';
|
||||||
import type { Recipe, Ingredient, Step } from '$lib/types';
|
import type { Recipe, Ingredient, Step } from '$lib/types';
|
||||||
import { confirmAction } from '$lib/client/confirm.svelte';
|
import ImageUploadBox from '$lib/components/ImageUploadBox.svelte';
|
||||||
import { asyncFetch } from '$lib/client/api-fetch-wrapper';
|
import IngredientRow from '$lib/components/IngredientRow.svelte';
|
||||||
import { requireOnline } from '$lib/client/require-online';
|
import StepList from '$lib/components/StepList.svelte';
|
||||||
|
import type { DraftIng, DraftStep } from '$lib/components/recipe-editor-types';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
recipe: Recipe;
|
recipe: Recipe;
|
||||||
@@ -27,67 +28,6 @@
|
|||||||
|
|
||||||
let { recipe, saving = false, onsave, oncancel, onimagechange }: Props = $props();
|
let { recipe, saving = false, onsave, oncancel, onimagechange }: Props = $props();
|
||||||
|
|
||||||
let imagePath = $state<string | null>(untrack(() => recipe.image_path));
|
|
||||||
let uploading = $state(false);
|
|
||||||
let fileInput: HTMLInputElement | null = $state(null);
|
|
||||||
|
|
||||||
const imageSrc = $derived(
|
|
||||||
imagePath === null
|
|
||||||
? null
|
|
||||||
: /^https?:\/\//i.test(imagePath)
|
|
||||||
? imagePath
|
|
||||||
: `/images/${imagePath}`
|
|
||||||
);
|
|
||||||
|
|
||||||
async function onFileChosen(event: Event) {
|
|
||||||
const input = event.target as HTMLInputElement;
|
|
||||||
const file = input.files?.[0];
|
|
||||||
input.value = '';
|
|
||||||
if (!file) return;
|
|
||||||
if (!requireOnline('Der Bild-Upload')) return;
|
|
||||||
uploading = true;
|
|
||||||
try {
|
|
||||||
const fd = new FormData();
|
|
||||||
fd.append('file', file);
|
|
||||||
const res = await asyncFetch(
|
|
||||||
`/api/recipes/${recipe.id}/image`,
|
|
||||||
{ method: 'POST', body: fd },
|
|
||||||
'Upload fehlgeschlagen'
|
|
||||||
);
|
|
||||||
if (!res) return;
|
|
||||||
const body = await res.json();
|
|
||||||
imagePath = body.image_path;
|
|
||||||
onimagechange?.(imagePath);
|
|
||||||
} finally {
|
|
||||||
uploading = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function removeImage() {
|
|
||||||
if (imagePath === null) return;
|
|
||||||
const ok = await confirmAction({
|
|
||||||
title: 'Bild entfernen?',
|
|
||||||
message: 'Das Rezept wird danach ohne Titelbild angezeigt.',
|
|
||||||
confirmLabel: 'Entfernen',
|
|
||||||
destructive: true
|
|
||||||
});
|
|
||||||
if (!ok) return;
|
|
||||||
if (!requireOnline('Das Entfernen')) return;
|
|
||||||
uploading = true;
|
|
||||||
try {
|
|
||||||
const res = await asyncFetch(
|
|
||||||
`/api/recipes/${recipe.id}/image`,
|
|
||||||
{ method: 'DELETE' },
|
|
||||||
'Entfernen fehlgeschlagen'
|
|
||||||
);
|
|
||||||
if (!res) return;
|
|
||||||
imagePath = null;
|
|
||||||
onimagechange?.(null);
|
|
||||||
} finally {
|
|
||||||
uploading = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Form-lokaler Zustand: Initialwerte aus dem Prop snapshotten (untrack),
|
// Form-lokaler Zustand: Initialwerte aus dem Prop snapshotten (untrack),
|
||||||
// damit User-Edits nicht von prop-Updates ueberschrieben werden.
|
// damit User-Edits nicht von prop-Updates ueberschrieben werden.
|
||||||
let title = $state(untrack(() => recipe.title));
|
let title = $state(untrack(() => recipe.title));
|
||||||
@@ -97,21 +37,14 @@
|
|||||||
let cookMin = $state<number | ''>(untrack(() => recipe.cook_time_min ?? ''));
|
let cookMin = $state<number | ''>(untrack(() => recipe.cook_time_min ?? ''));
|
||||||
let totalMin = $state<number | ''>(untrack(() => recipe.total_time_min ?? ''));
|
let totalMin = $state<number | ''>(untrack(() => recipe.total_time_min ?? ''));
|
||||||
|
|
||||||
type DraftIng = {
|
|
||||||
qty: string;
|
|
||||||
unit: string;
|
|
||||||
name: string;
|
|
||||||
note: string;
|
|
||||||
};
|
|
||||||
type DraftStep = { text: string };
|
|
||||||
|
|
||||||
let ingredients = $state<DraftIng[]>(
|
let ingredients = $state<DraftIng[]>(
|
||||||
untrack(() =>
|
untrack(() =>
|
||||||
recipe.ingredients.map((i) => ({
|
recipe.ingredients.map((i) => ({
|
||||||
qty: i.quantity !== null ? String(i.quantity).replace('.', ',') : '',
|
qty: i.quantity !== null ? String(i.quantity).replace('.', ',') : '',
|
||||||
unit: i.unit ?? '',
|
unit: i.unit ?? '',
|
||||||
name: i.name,
|
name: i.name,
|
||||||
note: i.note ?? ''
|
note: i.note ?? '',
|
||||||
|
section_heading: i.section_heading
|
||||||
}))
|
}))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@@ -120,7 +53,7 @@
|
|||||||
);
|
);
|
||||||
|
|
||||||
function addIngredient() {
|
function addIngredient() {
|
||||||
ingredients = [...ingredients, { qty: '', unit: '', name: '', note: '' }];
|
ingredients = [...ingredients, { qty: '', unit: '', name: '', note: '', section_heading: null }];
|
||||||
}
|
}
|
||||||
function removeIngredient(idx: number) {
|
function removeIngredient(idx: number) {
|
||||||
ingredients = ingredients.filter((_, i) => i !== idx);
|
ingredients = ingredients.filter((_, i) => i !== idx);
|
||||||
@@ -132,6 +65,16 @@
|
|||||||
[next[idx], next[target]] = [next[target], next[idx]];
|
[next[idx], next[target]] = [next[target], next[idx]];
|
||||||
ingredients = next;
|
ingredients = next;
|
||||||
}
|
}
|
||||||
|
function addSection(idx: number) {
|
||||||
|
const next = [...ingredients];
|
||||||
|
next[idx] = { ...next[idx], section_heading: '' };
|
||||||
|
ingredients = next;
|
||||||
|
}
|
||||||
|
function removeSection(idx: number) {
|
||||||
|
const next = [...ingredients];
|
||||||
|
next[idx] = { ...next[idx], section_heading: null };
|
||||||
|
ingredients = next;
|
||||||
|
}
|
||||||
function addStep() {
|
function addStep() {
|
||||||
steps = [...steps, { text: '' }];
|
steps = [...steps, { text: '' }];
|
||||||
}
|
}
|
||||||
@@ -162,13 +105,15 @@
|
|||||||
if (qty !== null) rawParts.push(String(qty).replace('.', ','));
|
if (qty !== null) rawParts.push(String(qty).replace('.', ','));
|
||||||
if (unit) rawParts.push(unit);
|
if (unit) rawParts.push(unit);
|
||||||
rawParts.push(name);
|
rawParts.push(name);
|
||||||
|
const heading = i.section_heading === null ? null : (i.section_heading.trim() || null);
|
||||||
return {
|
return {
|
||||||
position: idx + 1,
|
position: idx + 1,
|
||||||
quantity: qty,
|
quantity: qty,
|
||||||
unit,
|
unit,
|
||||||
name,
|
name,
|
||||||
note,
|
note,
|
||||||
raw_text: rawParts.join(' ')
|
raw_text: rawParts.join(' '),
|
||||||
|
section_heading: heading
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
const cleanedSteps: Step[] = steps
|
const cleanedSteps: Step[] = steps
|
||||||
@@ -189,50 +134,13 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="editor">
|
<div class="editor">
|
||||||
<section class="block image-block">
|
<section class="block">
|
||||||
<h2>Bild</h2>
|
<h2>Bild</h2>
|
||||||
<div class="image-row">
|
<ImageUploadBox
|
||||||
<div class="image-preview" class:empty={!imageSrc}>
|
recipeId={recipe.id!}
|
||||||
{#if imageSrc}
|
imagePath={recipe.image_path}
|
||||||
<img src={imageSrc} alt="" />
|
onchange={(p) => onimagechange?.(p)}
|
||||||
{:else}
|
|
||||||
<span class="placeholder">Kein Bild</span>
|
|
||||||
{/if}
|
|
||||||
</div>
|
|
||||||
<div class="image-actions">
|
|
||||||
<button
|
|
||||||
class="btn"
|
|
||||||
type="button"
|
|
||||||
onclick={() => fileInput?.click()}
|
|
||||||
disabled={uploading}
|
|
||||||
>
|
|
||||||
<ImagePlus size={16} strokeWidth={2} />
|
|
||||||
<span>{imagePath ? 'Bild ersetzen' : 'Bild hochladen'}</span>
|
|
||||||
</button>
|
|
||||||
{#if imagePath}
|
|
||||||
<button
|
|
||||||
class="btn ghost"
|
|
||||||
type="button"
|
|
||||||
onclick={removeImage}
|
|
||||||
disabled={uploading}
|
|
||||||
>
|
|
||||||
<ImageOff size={16} strokeWidth={2} />
|
|
||||||
<span>Entfernen</span>
|
|
||||||
</button>
|
|
||||||
{/if}
|
|
||||||
{#if uploading}
|
|
||||||
<span class="upload-status">Lade …</span>
|
|
||||||
{/if}
|
|
||||||
</div>
|
|
||||||
<input
|
|
||||||
bind:this={fileInput}
|
|
||||||
type="file"
|
|
||||||
accept="image/jpeg,image/png,image/webp,image/gif,image/avif"
|
|
||||||
class="file-input"
|
|
||||||
onchange={onFileChosen}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
|
||||||
<p class="image-hint">Max. 10 MB. JPG, PNG, WebP, GIF oder AVIF.</p>
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<div class="meta">
|
<div class="meta">
|
||||||
@@ -273,35 +181,15 @@
|
|||||||
<h2>Zutaten</h2>
|
<h2>Zutaten</h2>
|
||||||
<ul class="ing-list">
|
<ul class="ing-list">
|
||||||
{#each ingredients as ing, idx (idx)}
|
{#each ingredients as ing, idx (idx)}
|
||||||
<li class="ing-row">
|
<IngredientRow
|
||||||
<div class="move">
|
{ing}
|
||||||
<button
|
{idx}
|
||||||
class="move-btn"
|
total={ingredients.length}
|
||||||
type="button"
|
onmove={(dir) => moveIngredient(idx, dir)}
|
||||||
aria-label="Zutat nach oben"
|
onremove={() => removeIngredient(idx)}
|
||||||
disabled={idx === 0}
|
onaddSection={() => addSection(idx)}
|
||||||
onclick={() => moveIngredient(idx, -1)}
|
onremoveSection={() => removeSection(idx)}
|
||||||
>
|
/>
|
||||||
<ChevronUp size={14} strokeWidth={2.5} />
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
class="move-btn"
|
|
||||||
type="button"
|
|
||||||
aria-label="Zutat nach unten"
|
|
||||||
disabled={idx === ingredients.length - 1}
|
|
||||||
onclick={() => moveIngredient(idx, 1)}
|
|
||||||
>
|
|
||||||
<ChevronDown size={14} strokeWidth={2.5} />
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<input class="qty" type="text" bind:value={ing.qty} placeholder="Menge" aria-label="Menge" />
|
|
||||||
<input class="unit" type="text" bind:value={ing.unit} placeholder="Einheit" aria-label="Einheit" />
|
|
||||||
<input class="name" type="text" bind:value={ing.name} placeholder="Zutat" aria-label="Zutat" />
|
|
||||||
<input class="note" type="text" bind:value={ing.note} placeholder="Notiz" aria-label="Notiz" />
|
|
||||||
<button class="del" type="button" aria-label="Zutat entfernen" onclick={() => removeIngredient(idx)}>
|
|
||||||
<Trash2 size={16} strokeWidth={2} />
|
|
||||||
</button>
|
|
||||||
</li>
|
|
||||||
{/each}
|
{/each}
|
||||||
</ul>
|
</ul>
|
||||||
<button class="add" type="button" onclick={addIngredient}>
|
<button class="add" type="button" onclick={addIngredient}>
|
||||||
@@ -312,25 +200,7 @@
|
|||||||
|
|
||||||
<section class="block">
|
<section class="block">
|
||||||
<h2>Zubereitung</h2>
|
<h2>Zubereitung</h2>
|
||||||
<ol class="step-list">
|
<StepList {steps} onadd={addStep} onremove={removeStep} />
|
||||||
{#each steps as step, idx (idx)}
|
|
||||||
<li class="step-row">
|
|
||||||
<span class="num">{idx + 1}</span>
|
|
||||||
<textarea
|
|
||||||
bind:value={step.text}
|
|
||||||
rows="3"
|
|
||||||
placeholder="Schritt beschreiben …"
|
|
||||||
></textarea>
|
|
||||||
<button class="del" type="button" aria-label="Schritt entfernen" onclick={() => removeStep(idx)}>
|
|
||||||
<Trash2 size={16} strokeWidth={2} />
|
|
||||||
</button>
|
|
||||||
</li>
|
|
||||||
{/each}
|
|
||||||
</ol>
|
|
||||||
<button class="add" type="button" onclick={addStep}>
|
|
||||||
<Plus size={16} strokeWidth={2} />
|
|
||||||
<span>Schritt hinzufügen</span>
|
|
||||||
</button>
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<div class="foot">
|
<div class="foot">
|
||||||
@@ -396,74 +266,12 @@
|
|||||||
border-radius: 12px;
|
border-radius: 12px;
|
||||||
padding: 1rem;
|
padding: 1rem;
|
||||||
}
|
}
|
||||||
.image-row {
|
|
||||||
display: flex;
|
|
||||||
gap: 1rem;
|
|
||||||
align-items: flex-start;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
}
|
|
||||||
.image-preview {
|
|
||||||
width: 160px;
|
|
||||||
aspect-ratio: 16 / 10;
|
|
||||||
border-radius: 10px;
|
|
||||||
overflow: hidden;
|
|
||||||
background: #eef3ef;
|
|
||||||
border: 1px solid #e4eae7;
|
|
||||||
flex-shrink: 0;
|
|
||||||
}
|
|
||||||
.image-preview img {
|
|
||||||
width: 100%;
|
|
||||||
height: 100%;
|
|
||||||
object-fit: cover;
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
.image-preview.empty {
|
|
||||||
display: grid;
|
|
||||||
place-items: center;
|
|
||||||
color: #999;
|
|
||||||
font-size: 0.85rem;
|
|
||||||
}
|
|
||||||
.image-preview .placeholder {
|
|
||||||
padding: 0 0.5rem;
|
|
||||||
text-align: center;
|
|
||||||
}
|
|
||||||
.image-actions {
|
|
||||||
display: flex;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
gap: 0.5rem;
|
|
||||||
align-items: center;
|
|
||||||
}
|
|
||||||
.image-actions .btn {
|
|
||||||
display: inline-flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 0.4rem;
|
|
||||||
padding: 0.55rem 0.85rem;
|
|
||||||
min-height: 40px;
|
|
||||||
font-size: 0.9rem;
|
|
||||||
}
|
|
||||||
.upload-status {
|
|
||||||
color: #666;
|
|
||||||
font-size: 0.9rem;
|
|
||||||
}
|
|
||||||
.file-input {
|
|
||||||
position: absolute;
|
|
||||||
width: 1px;
|
|
||||||
height: 1px;
|
|
||||||
opacity: 0;
|
|
||||||
pointer-events: none;
|
|
||||||
}
|
|
||||||
.image-hint {
|
|
||||||
margin: 0.6rem 0 0;
|
|
||||||
color: #888;
|
|
||||||
font-size: 0.8rem;
|
|
||||||
}
|
|
||||||
.block h2 {
|
.block h2 {
|
||||||
font-size: 1.05rem;
|
font-size: 1.05rem;
|
||||||
margin: 0 0 0.75rem;
|
margin: 0 0 0.75rem;
|
||||||
color: #2b6a3d;
|
color: #2b6a3d;
|
||||||
}
|
}
|
||||||
.ing-list,
|
.ing-list {
|
||||||
.step-list {
|
|
||||||
list-style: none;
|
list-style: none;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
margin: 0 0 0.6rem;
|
margin: 0 0 0.6rem;
|
||||||
@@ -471,88 +279,6 @@
|
|||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 0.4rem;
|
gap: 0.4rem;
|
||||||
}
|
}
|
||||||
.ing-row {
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: 28px 70px 70px 1fr 1fr 40px;
|
|
||||||
gap: 0.35rem;
|
|
||||||
align-items: center;
|
|
||||||
}
|
|
||||||
.move {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: 2px;
|
|
||||||
}
|
|
||||||
.move-btn {
|
|
||||||
width: 28px;
|
|
||||||
height: 20px;
|
|
||||||
border: 1px solid #cfd9d1;
|
|
||||||
background: white;
|
|
||||||
border-radius: 6px;
|
|
||||||
cursor: pointer;
|
|
||||||
color: #555;
|
|
||||||
display: inline-flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
.move-btn:hover:not(:disabled) {
|
|
||||||
background: #f4f8f5;
|
|
||||||
}
|
|
||||||
.move-btn:disabled {
|
|
||||||
opacity: 0.3;
|
|
||||||
cursor: not-allowed;
|
|
||||||
}
|
|
||||||
.ing-row input {
|
|
||||||
padding: 0.5rem 0.55rem;
|
|
||||||
border: 1px solid #cfd9d1;
|
|
||||||
border-radius: 8px;
|
|
||||||
font-size: 0.9rem;
|
|
||||||
min-height: 38px;
|
|
||||||
font-family: inherit;
|
|
||||||
min-width: 0;
|
|
||||||
}
|
|
||||||
.step-row {
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: 32px 1fr 40px;
|
|
||||||
gap: 0.5rem;
|
|
||||||
align-items: start;
|
|
||||||
}
|
|
||||||
.num {
|
|
||||||
width: 32px;
|
|
||||||
height: 32px;
|
|
||||||
background: #2b6a3d;
|
|
||||||
color: white;
|
|
||||||
border-radius: 50%;
|
|
||||||
display: grid;
|
|
||||||
place-items: center;
|
|
||||||
font-weight: 600;
|
|
||||||
font-size: 0.9rem;
|
|
||||||
margin-top: 0.25rem;
|
|
||||||
}
|
|
||||||
.step-row textarea {
|
|
||||||
padding: 0.55rem 0.7rem;
|
|
||||||
border: 1px solid #cfd9d1;
|
|
||||||
border-radius: 8px;
|
|
||||||
font-size: 0.95rem;
|
|
||||||
font-family: inherit;
|
|
||||||
resize: vertical;
|
|
||||||
min-height: 70px;
|
|
||||||
}
|
|
||||||
.del {
|
|
||||||
width: 40px;
|
|
||||||
height: 40px;
|
|
||||||
border: 1px solid #f1b4b4;
|
|
||||||
background: white;
|
|
||||||
color: #c53030;
|
|
||||||
border-radius: 8px;
|
|
||||||
cursor: pointer;
|
|
||||||
display: inline-flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
}
|
|
||||||
.del:hover {
|
|
||||||
background: #fdf3f3;
|
|
||||||
}
|
|
||||||
.add {
|
.add {
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
@@ -598,31 +324,4 @@
|
|||||||
opacity: 0.6;
|
opacity: 0.6;
|
||||||
cursor: progress;
|
cursor: progress;
|
||||||
}
|
}
|
||||||
@media (max-width: 560px) {
|
|
||||||
.ing-row {
|
|
||||||
grid-template-columns: 28px 70px 1fr 40px;
|
|
||||||
grid-template-areas:
|
|
||||||
'move qty name del'
|
|
||||||
'move unit unit del'
|
|
||||||
'note note note note';
|
|
||||||
}
|
|
||||||
.ing-row .move {
|
|
||||||
grid-area: move;
|
|
||||||
}
|
|
||||||
.ing-row .qty {
|
|
||||||
grid-area: qty;
|
|
||||||
}
|
|
||||||
.ing-row .unit {
|
|
||||||
grid-area: unit;
|
|
||||||
}
|
|
||||||
.ing-row .name {
|
|
||||||
grid-area: name;
|
|
||||||
}
|
|
||||||
.ing-row .note {
|
|
||||||
grid-area: note;
|
|
||||||
}
|
|
||||||
.ing-row .del {
|
|
||||||
grid-area: del;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { scaleIngredients } from '$lib/recipes/scaler';
|
import { scaleIngredients } from '$lib/recipes/scaler';
|
||||||
import type { Recipe } from '$lib/types';
|
import type { Recipe } from '$lib/types';
|
||||||
|
import TimeDisplay from '$lib/components/TimeDisplay.svelte';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
recipe: Recipe;
|
recipe: Recipe;
|
||||||
@@ -41,15 +42,6 @@
|
|||||||
if (Number.isInteger(q)) return String(q);
|
if (Number.isInteger(q)) return String(q);
|
||||||
return q.toLocaleString('de-DE', { maximumFractionDigits: 2 });
|
return q.toLocaleString('de-DE', { maximumFractionDigits: 2 });
|
||||||
}
|
}
|
||||||
|
|
||||||
function timeSummary(): string {
|
|
||||||
const parts: string[] = [];
|
|
||||||
if (recipe.prep_time_min) parts.push(`Vorb. ${recipe.prep_time_min} min`);
|
|
||||||
if (recipe.cook_time_min) parts.push(`Kochen ${recipe.cook_time_min} min`);
|
|
||||||
if (!recipe.prep_time_min && !recipe.cook_time_min && recipe.total_time_min)
|
|
||||||
parts.push(`Gesamt ${recipe.total_time_min} min`);
|
|
||||||
return parts.join(' · ');
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if banner}
|
{#if banner}
|
||||||
@@ -79,9 +71,11 @@
|
|||||||
{/each}
|
{/each}
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{#if timeSummary()}
|
<TimeDisplay
|
||||||
<p class="times">{timeSummary()}</p>
|
prepTimeMin={recipe.prep_time_min}
|
||||||
{/if}
|
cookTimeMin={recipe.cook_time_min}
|
||||||
|
totalTimeMin={recipe.total_time_min}
|
||||||
|
/>
|
||||||
{#if recipe.source_url}
|
{#if recipe.source_url}
|
||||||
<p class="src">
|
<p class="src">
|
||||||
Quelle: <a href={recipe.source_url} target="_blank" rel="noopener">{recipe.source_domain}</a>
|
Quelle: <a href={recipe.source_url} target="_blank" rel="noopener">{recipe.source_domain}</a>
|
||||||
@@ -133,6 +127,9 @@
|
|||||||
</div>
|
</div>
|
||||||
<ul class="ing-list">
|
<ul class="ing-list">
|
||||||
{#each scaled as ing, i (i)}
|
{#each scaled as ing, i (i)}
|
||||||
|
{#if ing.section_heading && ing.section_heading.trim()}
|
||||||
|
<li class="section-heading">{ing.section_heading}</li>
|
||||||
|
{/if}
|
||||||
<li>
|
<li>
|
||||||
{#if ing.quantity !== null || ing.unit}
|
{#if ing.quantity !== null || ing.unit}
|
||||||
<span class="qty">
|
<span class="qty">
|
||||||
@@ -212,11 +209,6 @@
|
|||||||
font-size: 0.8rem;
|
font-size: 0.8rem;
|
||||||
color: #888;
|
color: #888;
|
||||||
}
|
}
|
||||||
.times {
|
|
||||||
margin: 0 0 0.25rem;
|
|
||||||
color: #666;
|
|
||||||
font-size: 0.9rem;
|
|
||||||
}
|
|
||||||
.src {
|
.src {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
font-size: 0.85rem;
|
font-size: 0.85rem;
|
||||||
@@ -292,6 +284,19 @@
|
|||||||
padding: 0;
|
padding: 0;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
.ing-list .section-heading {
|
||||||
|
list-style: none;
|
||||||
|
font-weight: 700;
|
||||||
|
color: #2b6a3d;
|
||||||
|
font-size: 1.2rem;
|
||||||
|
margin-top: 1.1rem;
|
||||||
|
margin-bottom: 0.3rem;
|
||||||
|
padding: 0.2rem 0;
|
||||||
|
border-bottom: 1px solid #e4eae7;
|
||||||
|
}
|
||||||
|
.ing-list .section-heading:first-child {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
.ing-list li {
|
.ing-list li {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 0.75rem;
|
gap: 0.75rem;
|
||||||
|
|||||||
101
src/lib/components/StepList.svelte
Normal file
101
src/lib/components/StepList.svelte
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { Plus, Trash2 } from 'lucide-svelte';
|
||||||
|
import type { DraftStep } from './recipe-editor-types';
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
steps: DraftStep[];
|
||||||
|
onadd: () => void;
|
||||||
|
onremove: (idx: number) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { steps, onadd, onremove }: Props = $props();
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<ol class="step-list">
|
||||||
|
{#each steps as step, idx (idx)}
|
||||||
|
<li class="step-row">
|
||||||
|
<span class="num">{idx + 1}</span>
|
||||||
|
<textarea
|
||||||
|
bind:value={step.text}
|
||||||
|
rows="3"
|
||||||
|
placeholder="Schritt beschreiben …"
|
||||||
|
></textarea>
|
||||||
|
<button class="del" type="button" aria-label="Schritt entfernen" onclick={() => onremove(idx)}>
|
||||||
|
<Trash2 size={16} strokeWidth={2} />
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ol>
|
||||||
|
<button class="add" type="button" onclick={onadd}>
|
||||||
|
<Plus size={16} strokeWidth={2} />
|
||||||
|
<span>Schritt hinzufügen</span>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.step-list {
|
||||||
|
list-style: none;
|
||||||
|
padding: 0;
|
||||||
|
margin: 0 0 0.6rem;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.4rem;
|
||||||
|
}
|
||||||
|
.step-row {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 32px 1fr 40px;
|
||||||
|
gap: 0.5rem;
|
||||||
|
align-items: start;
|
||||||
|
}
|
||||||
|
.num {
|
||||||
|
width: 32px;
|
||||||
|
height: 32px;
|
||||||
|
background: #2b6a3d;
|
||||||
|
color: white;
|
||||||
|
border-radius: 50%;
|
||||||
|
display: grid;
|
||||||
|
place-items: center;
|
||||||
|
font-weight: 600;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
margin-top: 0.25rem;
|
||||||
|
}
|
||||||
|
.step-row textarea {
|
||||||
|
padding: 0.55rem 0.7rem;
|
||||||
|
border: 1px solid #cfd9d1;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
font-family: inherit;
|
||||||
|
resize: vertical;
|
||||||
|
min-height: 70px;
|
||||||
|
}
|
||||||
|
.del {
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
border: 1px solid #f1b4b4;
|
||||||
|
background: white;
|
||||||
|
color: #c53030;
|
||||||
|
border-radius: 8px;
|
||||||
|
cursor: pointer;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
.del:hover {
|
||||||
|
background: #fdf3f3;
|
||||||
|
}
|
||||||
|
.add {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.35rem;
|
||||||
|
padding: 0.55rem 0.9rem;
|
||||||
|
border: 1px dashed #cfd9d1;
|
||||||
|
background: white;
|
||||||
|
color: #2b6a3d;
|
||||||
|
border-radius: 8px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
font-family: inherit;
|
||||||
|
}
|
||||||
|
.add:hover {
|
||||||
|
background: #f4f8f5;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
30
src/lib/components/TimeDisplay.svelte
Normal file
30
src/lib/components/TimeDisplay.svelte
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
type Props = {
|
||||||
|
prepTimeMin: number | null;
|
||||||
|
cookTimeMin: number | null;
|
||||||
|
totalTimeMin: number | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
let { prepTimeMin, cookTimeMin, totalTimeMin }: Props = $props();
|
||||||
|
|
||||||
|
const summary = $derived.by(() => {
|
||||||
|
const parts: string[] = [];
|
||||||
|
if (prepTimeMin) parts.push(`Vorb. ${prepTimeMin} min`);
|
||||||
|
if (cookTimeMin) parts.push(`Kochen ${cookTimeMin} min`);
|
||||||
|
if (!prepTimeMin && !cookTimeMin && totalTimeMin)
|
||||||
|
parts.push(`Gesamt ${totalTimeMin} min`);
|
||||||
|
return parts.join(' · ');
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if summary}
|
||||||
|
<p class="times">{summary}</p>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.times {
|
||||||
|
margin: 0 0 0.25rem;
|
||||||
|
color: #666;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
9
src/lib/components/recipe-editor-types.ts
Normal file
9
src/lib/components/recipe-editor-types.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
export type DraftIng = {
|
||||||
|
qty: string;
|
||||||
|
unit: string;
|
||||||
|
name: string;
|
||||||
|
note: string;
|
||||||
|
section_heading: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type DraftStep = { text: string };
|
||||||
7
src/lib/server/db/migrations/012_ingredient_section.sql
Normal file
7
src/lib/server/db/migrations/012_ingredient_section.sql
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
-- Nullable-Spalte fuer optionale Sektionsueberschriften bei Zutaten. User
|
||||||
|
-- soll im Editor gruppieren koennen ("Fuer den Teig", "Fuer die Fuellung").
|
||||||
|
-- Rendering-Regel: Ist section_heading gesetzt (nicht NULL, nicht leer),
|
||||||
|
-- startet an dieser Zeile eine neue Sektion mit diesem Titel; alle folgenden
|
||||||
|
-- Zutaten gehoeren dazu, bis die naechste Zeile wieder eine Ueberschrift hat.
|
||||||
|
-- Ordnung bleibt die bestehende position-Spalte.
|
||||||
|
ALTER TABLE ingredient ADD COLUMN section_heading TEXT;
|
||||||
@@ -105,16 +105,16 @@ export function parseIngredient(raw: string, position = 0): Ingredient {
|
|||||||
if (tail.length > 0) {
|
if (tail.length > 0) {
|
||||||
const quantity = clampQuantity(UNICODE_FRACTION_MAP[firstChar]);
|
const quantity = clampQuantity(UNICODE_FRACTION_MAP[firstChar]);
|
||||||
const { unit, name } = splitUnitAndName(tail);
|
const { unit, name } = splitUnitAndName(tail);
|
||||||
return { position, quantity, unit, name, note, raw_text: rawText };
|
return { position, quantity, unit, name, note, raw_text: rawText, section_heading: null };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const qtyPattern = /^((?:\d+[.,]?\d*(?:\s*[-–]\s*\d+[.,]?\d*)?)|(?:\d+\/\d+))\s+(.+)$/;
|
const qtyPattern = /^((?:\d+[.,]?\d*(?:\s*[-–]\s*\d+[.,]?\d*)?)|(?:\d+\/\d+))\s+(.+)$/;
|
||||||
const qtyMatch = qtyPattern.exec(working);
|
const qtyMatch = qtyPattern.exec(working);
|
||||||
if (!qtyMatch) {
|
if (!qtyMatch) {
|
||||||
return { position, quantity: null, unit: null, name: working, note, raw_text: rawText };
|
return { position, quantity: null, unit: null, name: working, note, raw_text: rawText, section_heading: null };
|
||||||
}
|
}
|
||||||
const quantity = clampQuantity(parseQuantity(qtyMatch[1]));
|
const quantity = clampQuantity(parseQuantity(qtyMatch[1]));
|
||||||
const { unit, name } = splitUnitAndName(qtyMatch[2]);
|
const { unit, name } = splitUnitAndName(qtyMatch[2]);
|
||||||
return { position, quantity, unit, name, note, raw_text: rawText };
|
return { position, quantity, unit, name, note, raw_text: rawText, section_heading: null };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,11 +64,11 @@ export function insertRecipe(db: Database.Database, recipe: Recipe): number {
|
|||||||
const id = Number(info.lastInsertRowid);
|
const id = Number(info.lastInsertRowid);
|
||||||
|
|
||||||
const insIng = db.prepare(
|
const insIng = db.prepare(
|
||||||
`INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text)
|
`INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text, section_heading)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
|
||||||
);
|
);
|
||||||
for (const ing of recipe.ingredients) {
|
for (const ing of recipe.ingredients) {
|
||||||
insIng.run(id, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text);
|
insIng.run(id, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text, ing.section_heading);
|
||||||
}
|
}
|
||||||
|
|
||||||
const insStep = db.prepare(
|
const insStep = db.prepare(
|
||||||
@@ -104,7 +104,7 @@ export function getRecipeById(db: Database.Database, id: number): Recipe | null
|
|||||||
|
|
||||||
const ingredients = db
|
const ingredients = db
|
||||||
.prepare(
|
.prepare(
|
||||||
`SELECT position, quantity, unit, name, note, raw_text
|
`SELECT position, quantity, unit, name, note, raw_text, section_heading
|
||||||
FROM ingredient WHERE recipe_id = ? ORDER BY position`
|
FROM ingredient WHERE recipe_id = ? ORDER BY position`
|
||||||
)
|
)
|
||||||
.all(id) as Ingredient[];
|
.all(id) as Ingredient[];
|
||||||
@@ -215,11 +215,11 @@ export function replaceIngredients(
|
|||||||
const tx = db.transaction(() => {
|
const tx = db.transaction(() => {
|
||||||
db.prepare('DELETE FROM ingredient WHERE recipe_id = ?').run(recipeId);
|
db.prepare('DELETE FROM ingredient WHERE recipe_id = ?').run(recipeId);
|
||||||
const ins = db.prepare(
|
const ins = db.prepare(
|
||||||
`INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text)
|
`INSERT INTO ingredient(recipe_id, position, quantity, unit, name, note, raw_text, section_heading)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
|
||||||
);
|
);
|
||||||
for (const ing of ingredients) {
|
for (const ing of ingredients) {
|
||||||
ins.run(recipeId, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text);
|
ins.run(recipeId, ing.position, ing.quantity, ing.unit, ing.name, ing.note, ing.raw_text, ing.section_heading);
|
||||||
}
|
}
|
||||||
refreshFts(db, recipeId);
|
refreshFts(db, recipeId);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export type CacheStrategy = 'shell' | 'swr' | 'images' | 'network-only';
|
export type CacheStrategy = 'shell' | 'network-first' | 'images' | 'network-only';
|
||||||
|
|
||||||
type RequestShape = { url: string; method: string };
|
type RequestShape = { url: string; method: string };
|
||||||
|
|
||||||
@@ -37,6 +37,7 @@ export function resolveStrategy(req: RequestShape): CacheStrategy {
|
|||||||
return 'shell';
|
return 'shell';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Everything else: recipe pages, API reads, lists — all SWR.
|
// Everything else: recipe pages, API reads, lists — network-first with
|
||||||
return 'swr';
|
// timeout fallback to cache (handled in service-worker.ts).
|
||||||
|
return 'network-first';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export type Ingredient = {
|
|||||||
name: string;
|
name: string;
|
||||||
note: string | null;
|
note: string | null;
|
||||||
raw_text: string;
|
raw_text: string;
|
||||||
|
section_heading: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Step = {
|
export type Step = {
|
||||||
|
|||||||
@@ -17,26 +17,20 @@
|
|||||||
import { network } from '$lib/client/network.svelte';
|
import { network } from '$lib/client/network.svelte';
|
||||||
import { installPrompt } from '$lib/client/install-prompt.svelte';
|
import { installPrompt } from '$lib/client/install-prompt.svelte';
|
||||||
import { registerServiceWorker } from '$lib/client/sw-register';
|
import { registerServiceWorker } from '$lib/client/sw-register';
|
||||||
import type { SearchHit } from '$lib/server/recipes/search-local';
|
import { SearchStore } from '$lib/client/search.svelte';
|
||||||
import type { WebHit } from '$lib/server/search/searxng';
|
|
||||||
|
|
||||||
let { children } = $props();
|
let { children } = $props();
|
||||||
|
|
||||||
const NAV_PAGE_SIZE = 30;
|
const navStore = new SearchStore({
|
||||||
|
pageSize: 30,
|
||||||
|
filterParam: () => {
|
||||||
|
const p = searchFilterStore.queryParam;
|
||||||
|
return p ? `&domains=${encodeURIComponent(p)}` : '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let navQuery = $state('');
|
|
||||||
let navHits = $state<SearchHit[]>([]);
|
|
||||||
let navWebHits = $state<WebHit[]>([]);
|
|
||||||
let navSearching = $state(false);
|
|
||||||
let navWebSearching = $state(false);
|
|
||||||
let navWebError = $state<string | null>(null);
|
|
||||||
let navOpen = $state(false);
|
let navOpen = $state(false);
|
||||||
let navLocalExhausted = $state(false);
|
|
||||||
let navWebPageno = $state(0);
|
|
||||||
let navWebExhausted = $state(false);
|
|
||||||
let navLoadingMore = $state(false);
|
|
||||||
let navContainer: HTMLElement | undefined = $state();
|
let navContainer: HTMLElement | undefined = $state();
|
||||||
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
|
|
||||||
let menuOpen = $state(false);
|
let menuOpen = $state(false);
|
||||||
let menuContainer: HTMLElement | undefined = $state();
|
let menuContainer: HTMLElement | undefined = $state();
|
||||||
|
|
||||||
@@ -44,123 +38,21 @@
|
|||||||
$page.url.pathname.startsWith('/recipes/') || $page.url.pathname === '/preview'
|
$page.url.pathname.startsWith('/recipes/') || $page.url.pathname === '/preview'
|
||||||
);
|
);
|
||||||
|
|
||||||
function filterParam(): string {
|
|
||||||
const p = searchFilterStore.queryParam;
|
|
||||||
return p ? `&domains=${encodeURIComponent(p)}` : '';
|
|
||||||
}
|
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
const q = navQuery.trim();
|
// Bare reads register the reactive deps; then kick the store.
|
||||||
if (debounceTimer) clearTimeout(debounceTimer);
|
const q = navStore.query;
|
||||||
if (q.length <= 3) {
|
navStore.runDebounced();
|
||||||
navHits = [];
|
// navOpen follows query length: open while typing, close when cleared.
|
||||||
navWebHits = [];
|
navOpen = q.trim().length > 3;
|
||||||
navSearching = false;
|
|
||||||
navWebSearching = false;
|
|
||||||
navWebError = null;
|
|
||||||
navOpen = false;
|
|
||||||
navLocalExhausted = false;
|
|
||||||
navWebPageno = 0;
|
|
||||||
navWebExhausted = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
navSearching = true;
|
|
||||||
navWebHits = [];
|
|
||||||
navWebSearching = false;
|
|
||||||
navWebError = null;
|
|
||||||
navOpen = true;
|
|
||||||
navLocalExhausted = false;
|
|
||||||
navWebPageno = 0;
|
|
||||||
navWebExhausted = false;
|
|
||||||
debounceTimer = setTimeout(async () => {
|
|
||||||
try {
|
|
||||||
const res = await fetch(
|
|
||||||
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${NAV_PAGE_SIZE}${filterParam()}`
|
|
||||||
);
|
|
||||||
const body = await res.json();
|
|
||||||
if (navQuery.trim() !== q) return;
|
|
||||||
navHits = body.hits;
|
|
||||||
if (navHits.length < NAV_PAGE_SIZE) navLocalExhausted = true;
|
|
||||||
if (navHits.length === 0) {
|
|
||||||
navWebSearching = true;
|
|
||||||
try {
|
|
||||||
const wres = await fetch(
|
|
||||||
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=1${filterParam()}`
|
|
||||||
);
|
|
||||||
if (navQuery.trim() !== q) return;
|
|
||||||
if (!wres.ok) {
|
|
||||||
const err = await wres.json().catch(() => ({}));
|
|
||||||
navWebError = err.message ?? `HTTP ${wres.status}`;
|
|
||||||
navWebExhausted = true;
|
|
||||||
} else {
|
|
||||||
const wbody = await wres.json();
|
|
||||||
navWebHits = wbody.hits;
|
|
||||||
navWebPageno = 1;
|
|
||||||
if (navWebHits.length === 0) navWebExhausted = true;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (navQuery.trim() === q) navWebSearching = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (navQuery.trim() === q) navSearching = false;
|
|
||||||
}
|
|
||||||
}, 300);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
async function loadMoreNav() {
|
function loadMoreNav() {
|
||||||
if (navLoadingMore) return;
|
return navStore.loadMore();
|
||||||
const q = navQuery.trim();
|
|
||||||
if (!q) return;
|
|
||||||
navLoadingMore = true;
|
|
||||||
try {
|
|
||||||
if (!navLocalExhausted) {
|
|
||||||
const res = await fetch(
|
|
||||||
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${NAV_PAGE_SIZE}&offset=${navHits.length}${filterParam()}`
|
|
||||||
);
|
|
||||||
const body = await res.json();
|
|
||||||
if (navQuery.trim() !== q) return;
|
|
||||||
const more = body.hits as SearchHit[];
|
|
||||||
const seen = new Set(navHits.map((h) => h.id));
|
|
||||||
const deduped = more.filter((h) => !seen.has(h.id));
|
|
||||||
navHits = [...navHits, ...deduped];
|
|
||||||
if (more.length < NAV_PAGE_SIZE) navLocalExhausted = true;
|
|
||||||
} else if (!navWebExhausted) {
|
|
||||||
const nextPage = navWebPageno + 1;
|
|
||||||
navWebSearching = navWebHits.length === 0;
|
|
||||||
try {
|
|
||||||
const wres = await fetch(
|
|
||||||
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${nextPage}${filterParam()}`
|
|
||||||
);
|
|
||||||
if (navQuery.trim() !== q) return;
|
|
||||||
if (!wres.ok) {
|
|
||||||
const err = await wres.json().catch(() => ({}));
|
|
||||||
navWebError = err.message ?? `HTTP ${wres.status}`;
|
|
||||||
navWebExhausted = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const wbody = await wres.json();
|
|
||||||
const more = wbody.hits as WebHit[];
|
|
||||||
const seen = new Set(navWebHits.map((h) => h.url));
|
|
||||||
const deduped = more.filter((h) => !seen.has(h.url));
|
|
||||||
if (deduped.length === 0) {
|
|
||||||
navWebExhausted = true;
|
|
||||||
} else {
|
|
||||||
navWebHits = [...navWebHits, ...deduped];
|
|
||||||
navWebPageno = nextPage;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (navQuery.trim() === q) navWebSearching = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
navLoadingMore = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function submitNav(e: SubmitEvent) {
|
function submitNav(e: SubmitEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const q = navQuery.trim();
|
const q = navStore.query.trim();
|
||||||
if (!q) return;
|
if (!q) return;
|
||||||
navOpen = false;
|
navOpen = false;
|
||||||
void goto(`/?q=${encodeURIComponent(q)}`);
|
void goto(`/?q=${encodeURIComponent(q)}`);
|
||||||
@@ -184,15 +76,11 @@
|
|||||||
|
|
||||||
function pickHit() {
|
function pickHit() {
|
||||||
navOpen = false;
|
navOpen = false;
|
||||||
navQuery = '';
|
navStore.reset();
|
||||||
navHits = [];
|
|
||||||
navWebHits = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
afterNavigate(() => {
|
afterNavigate(() => {
|
||||||
navQuery = '';
|
navStore.reset();
|
||||||
navHits = [];
|
|
||||||
navWebHits = [];
|
|
||||||
navOpen = false;
|
navOpen = false;
|
||||||
menuOpen = false;
|
menuOpen = false;
|
||||||
// Badge nach jeder Client-Navigation frisch halten — sonst kann er
|
// Badge nach jeder Client-Navigation frisch halten — sonst kann er
|
||||||
@@ -239,9 +127,9 @@
|
|||||||
<SearchFilter inline />
|
<SearchFilter inline />
|
||||||
<input
|
<input
|
||||||
type="search"
|
type="search"
|
||||||
bind:value={navQuery}
|
bind:value={navStore.query}
|
||||||
onfocus={() => {
|
onfocus={() => {
|
||||||
if (navHits.length > 0 || navQuery.trim().length > 3) navOpen = true;
|
if (navStore.hits.length > 0 || navStore.query.trim().length > 3) navOpen = true;
|
||||||
}}
|
}}
|
||||||
placeholder="Rezept suchen…"
|
placeholder="Rezept suchen…"
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
@@ -251,12 +139,12 @@
|
|||||||
</form>
|
</form>
|
||||||
{#if navOpen}
|
{#if navOpen}
|
||||||
<div class="dropdown" role="listbox">
|
<div class="dropdown" role="listbox">
|
||||||
{#if navSearching && navHits.length === 0 && navWebHits.length === 0}
|
{#if navStore.searching && navStore.hits.length === 0 && navStore.webHits.length === 0}
|
||||||
<SearchLoader scope="local" size="sm" />
|
<SearchLoader scope="local" size="sm" />
|
||||||
{:else}
|
{:else}
|
||||||
{#if navHits.length > 0}
|
{#if navStore.hits.length > 0}
|
||||||
<ul class="dd-list">
|
<ul class="dd-list">
|
||||||
{#each navHits as r (r.id)}
|
{#each navStore.hits as r (r.id)}
|
||||||
<li>
|
<li>
|
||||||
<a
|
<a
|
||||||
href={`/recipes/${r.id}`}
|
href={`/recipes/${r.id}`}
|
||||||
@@ -282,14 +170,14 @@
|
|||||||
</ul>
|
</ul>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if navWebHits.length > 0}
|
{#if navStore.webHits.length > 0}
|
||||||
{#if navHits.length > 0}
|
{#if navStore.hits.length > 0}
|
||||||
<p class="dd-section">Aus dem Internet</p>
|
<p class="dd-section">Aus dem Internet</p>
|
||||||
{:else}
|
{:else}
|
||||||
<p class="dd-section">Keine lokalen Rezepte – aus dem Internet:</p>
|
<p class="dd-section">Keine lokalen Rezepte – aus dem Internet:</p>
|
||||||
{/if}
|
{/if}
|
||||||
<ul class="dd-list">
|
<ul class="dd-list">
|
||||||
{#each navWebHits as w (w.url)}
|
{#each navStore.webHits as w (w.url)}
|
||||||
<li>
|
<li>
|
||||||
<a
|
<a
|
||||||
href={`/preview?url=${encodeURIComponent(w.url)}`}
|
href={`/preview?url=${encodeURIComponent(w.url)}`}
|
||||||
@@ -313,23 +201,23 @@
|
|||||||
</ul>
|
</ul>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if navWebSearching}
|
{#if navStore.webSearching}
|
||||||
<SearchLoader scope="web" size="sm" />
|
<SearchLoader scope="web" size="sm" />
|
||||||
{:else if navWebError && navWebHits.length === 0}
|
{:else if navStore.webError && navStore.webHits.length === 0}
|
||||||
<p class="dd-status dd-error">Internet-Suche zurzeit nicht möglich.</p>
|
<p class="dd-status dd-error">Internet-Suche zurzeit nicht möglich.</p>
|
||||||
{:else if navHits.length === 0 && navWebHits.length === 0 && !navSearching}
|
{:else if navStore.hits.length === 0 && navStore.webHits.length === 0 && !navStore.searching}
|
||||||
<p class="dd-status">Auch im Internet nichts gefunden.</p>
|
<p class="dd-status">Auch im Internet nichts gefunden.</p>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if !(navLocalExhausted && navWebExhausted) && (navHits.length > 0 || navWebHits.length > 0)}
|
{#if !(navStore.localExhausted && navStore.webExhausted) && (navStore.hits.length > 0 || navStore.webHits.length > 0)}
|
||||||
<button
|
<button
|
||||||
class="dd-web"
|
class="dd-web"
|
||||||
type="button"
|
type="button"
|
||||||
onclick={loadMoreNav}
|
onclick={loadMoreNav}
|
||||||
disabled={navLoadingMore || navWebSearching}
|
disabled={navStore.loadingMore || navStore.webSearching}
|
||||||
>
|
>
|
||||||
<span
|
<span
|
||||||
>{navLoadingMore || navWebSearching
|
>{navStore.loadingMore || navStore.webSearching
|
||||||
? 'Lade …'
|
? 'Lade …'
|
||||||
: '+ weitere Ergebnisse'}</span
|
: '+ weitere Ergebnisse'}</span
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -4,32 +4,27 @@
|
|||||||
import { CookingPot, X } from 'lucide-svelte';
|
import { CookingPot, X } from 'lucide-svelte';
|
||||||
import type { Snapshot } from './$types';
|
import type { Snapshot } from './$types';
|
||||||
import type { SearchHit } from '$lib/server/recipes/search-local';
|
import type { SearchHit } from '$lib/server/recipes/search-local';
|
||||||
import type { WebHit } from '$lib/server/search/searxng';
|
|
||||||
import { randomQuote } from '$lib/quotes';
|
import { randomQuote } from '$lib/quotes';
|
||||||
import SearchLoader from '$lib/components/SearchLoader.svelte';
|
import SearchLoader from '$lib/components/SearchLoader.svelte';
|
||||||
import SearchFilter from '$lib/components/SearchFilter.svelte';
|
import SearchFilter from '$lib/components/SearchFilter.svelte';
|
||||||
import { profileStore } from '$lib/client/profile.svelte';
|
import { profileStore } from '$lib/client/profile.svelte';
|
||||||
import { searchFilterStore } from '$lib/client/search-filter.svelte';
|
import { searchFilterStore } from '$lib/client/search-filter.svelte';
|
||||||
import { requireOnline } from '$lib/client/require-online';
|
import { requireOnline } from '$lib/client/require-online';
|
||||||
|
import { SearchStore, type SearchSnapshot } from '$lib/client/search.svelte';
|
||||||
|
|
||||||
const LOCAL_PAGE = 30;
|
const LOCAL_PAGE = 30;
|
||||||
|
|
||||||
let query = $state('');
|
const store = new SearchStore({
|
||||||
|
pageSize: LOCAL_PAGE,
|
||||||
|
filterParam: () => {
|
||||||
|
const p = searchFilterStore.queryParam;
|
||||||
|
return p ? `&domains=${encodeURIComponent(p)}` : '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let quote = $state('');
|
let quote = $state('');
|
||||||
let recent = $state<SearchHit[]>([]);
|
let recent = $state<SearchHit[]>([]);
|
||||||
let favorites = $state<SearchHit[]>([]);
|
let favorites = $state<SearchHit[]>([]);
|
||||||
let hits = $state<SearchHit[]>([]);
|
|
||||||
let webHits = $state<WebHit[]>([]);
|
|
||||||
let searching = $state(false);
|
|
||||||
let webSearching = $state(false);
|
|
||||||
let webError = $state<string | null>(null);
|
|
||||||
let searchedFor = $state<string | null>(null);
|
|
||||||
let localExhausted = $state(false);
|
|
||||||
let webPageno = $state(0);
|
|
||||||
let webExhausted = $state(false);
|
|
||||||
let loadingMore = $state(false);
|
|
||||||
let skipNextSearch = false;
|
|
||||||
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
|
|
||||||
|
|
||||||
const ALL_PAGE = 10;
|
const ALL_PAGE = 10;
|
||||||
type AllSort = 'name' | 'rating' | 'cooked' | 'created';
|
type AllSort = 'name' | 'rating' | 'cooked' | 'created';
|
||||||
@@ -47,39 +42,9 @@
|
|||||||
let allChips: HTMLElement | undefined = $state();
|
let allChips: HTMLElement | undefined = $state();
|
||||||
let allObserver: IntersectionObserver | null = null;
|
let allObserver: IntersectionObserver | null = null;
|
||||||
|
|
||||||
type SearchSnapshot = {
|
|
||||||
query: string;
|
|
||||||
hits: SearchHit[];
|
|
||||||
webHits: WebHit[];
|
|
||||||
searchedFor: string | null;
|
|
||||||
webError: string | null;
|
|
||||||
localExhausted: boolean;
|
|
||||||
webPageno: number;
|
|
||||||
webExhausted: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const snapshot: Snapshot<SearchSnapshot> = {
|
export const snapshot: Snapshot<SearchSnapshot> = {
|
||||||
capture: () => ({
|
capture: () => store.captureSnapshot(),
|
||||||
query,
|
restore: (s) => store.restoreSnapshot(s)
|
||||||
hits,
|
|
||||||
webHits,
|
|
||||||
searchedFor,
|
|
||||||
webError,
|
|
||||||
localExhausted,
|
|
||||||
webPageno,
|
|
||||||
webExhausted
|
|
||||||
}),
|
|
||||||
restore: (v) => {
|
|
||||||
query = v.query;
|
|
||||||
hits = v.hits;
|
|
||||||
webHits = v.webHits;
|
|
||||||
searchedFor = v.searchedFor;
|
|
||||||
webError = v.webError;
|
|
||||||
localExhausted = v.localExhausted;
|
|
||||||
webPageno = v.webPageno;
|
|
||||||
webExhausted = v.webExhausted;
|
|
||||||
skipNextSearch = true;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
async function loadRecent() {
|
async function loadRecent() {
|
||||||
@@ -152,7 +117,7 @@
|
|||||||
// Restore query from URL so history.back() from preview/recipe
|
// Restore query from URL so history.back() from preview/recipe
|
||||||
// brings the user back to the same search results.
|
// brings the user back to the same search results.
|
||||||
const urlQ = ($page.url.searchParams.get('q') ?? '').trim();
|
const urlQ = ($page.url.searchParams.get('q') ?? '').trim();
|
||||||
if (urlQ) query = urlQ;
|
if (urlQ) store.query = urlQ;
|
||||||
void loadRecent();
|
void loadRecent();
|
||||||
void searchFilterStore.load();
|
void searchFilterStore.load();
|
||||||
const saved = localStorage.getItem('kochwas.allSort');
|
const saved = localStorage.getItem('kochwas.allSort');
|
||||||
@@ -188,14 +153,7 @@
|
|||||||
$effect(() => {
|
$effect(() => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||||
searchFilterStore.active;
|
searchFilterStore.active;
|
||||||
const q = query.trim();
|
store.reSearch();
|
||||||
if (!q || q.length <= 3) return;
|
|
||||||
if (debounceTimer) clearTimeout(debounceTimer);
|
|
||||||
searching = true;
|
|
||||||
webHits = [];
|
|
||||||
webSearching = false;
|
|
||||||
webError = null;
|
|
||||||
debounceTimer = setTimeout(() => void runSearch(q), 150);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Sync current query back into the URL as ?q=... via replaceState,
|
// Sync current query back into the URL as ?q=... via replaceState,
|
||||||
@@ -203,7 +161,7 @@
|
|||||||
// when the user clicks a result or otherwise navigates away.
|
// when the user clicks a result or otherwise navigates away.
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (typeof window === 'undefined') return;
|
if (typeof window === 'undefined') return;
|
||||||
const q = query.trim();
|
const q = store.query.trim();
|
||||||
const url = new URL(window.location.href);
|
const url = new URL(window.location.href);
|
||||||
const current = url.searchParams.get('q') ?? '';
|
const current = url.searchParams.get('q') ?? '';
|
||||||
if (q === current) return;
|
if (q === current) return;
|
||||||
@@ -221,138 +179,17 @@
|
|||||||
void loadFavorites(active.id);
|
void loadFavorites(active.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
function filterParam(): string {
|
|
||||||
const p = searchFilterStore.queryParam;
|
|
||||||
return p ? `&domains=${encodeURIComponent(p)}` : '';
|
|
||||||
}
|
|
||||||
|
|
||||||
async function runSearch(q: string) {
|
|
||||||
localExhausted = false;
|
|
||||||
webPageno = 0;
|
|
||||||
webExhausted = false;
|
|
||||||
try {
|
|
||||||
const res = await fetch(
|
|
||||||
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${LOCAL_PAGE}${filterParam()}`
|
|
||||||
);
|
|
||||||
const body = await res.json();
|
|
||||||
if (query.trim() !== q) return;
|
|
||||||
hits = body.hits;
|
|
||||||
searchedFor = q;
|
|
||||||
if (hits.length < LOCAL_PAGE) localExhausted = true;
|
|
||||||
if (hits.length === 0) {
|
|
||||||
// Gar keine lokalen Treffer → erste Web-Seite gleich laden,
|
|
||||||
// damit der User nicht extra auf „+ weitere" klicken muss.
|
|
||||||
webSearching = true;
|
|
||||||
try {
|
|
||||||
const wres = await fetch(
|
|
||||||
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=1${filterParam()}`
|
|
||||||
);
|
|
||||||
if (query.trim() !== q) return;
|
|
||||||
if (!wres.ok) {
|
|
||||||
const err = await wres.json().catch(() => ({}));
|
|
||||||
webError = err.message ?? `HTTP ${wres.status}`;
|
|
||||||
webExhausted = true;
|
|
||||||
} else {
|
|
||||||
const wbody = await wres.json();
|
|
||||||
webHits = wbody.hits;
|
|
||||||
webPageno = 1;
|
|
||||||
if (wbody.hits.length === 0) webExhausted = true;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (query.trim() === q) webSearching = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (query.trim() === q) searching = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function loadMore() {
|
|
||||||
if (loadingMore) return;
|
|
||||||
const q = query.trim();
|
|
||||||
if (!q) return;
|
|
||||||
loadingMore = true;
|
|
||||||
try {
|
|
||||||
if (!localExhausted) {
|
|
||||||
// Noch mehr lokale Treffer holen.
|
|
||||||
const res = await fetch(
|
|
||||||
`/api/recipes/search?q=${encodeURIComponent(q)}&limit=${LOCAL_PAGE}&offset=${hits.length}${filterParam()}`
|
|
||||||
);
|
|
||||||
const body = await res.json();
|
|
||||||
if (query.trim() !== q) return;
|
|
||||||
const more = body.hits as SearchHit[];
|
|
||||||
const seen = new Set(hits.map((h) => h.id));
|
|
||||||
const deduped = more.filter((h) => !seen.has(h.id));
|
|
||||||
hits = [...hits, ...deduped];
|
|
||||||
if (more.length < LOCAL_PAGE) localExhausted = true;
|
|
||||||
} else if (!webExhausted) {
|
|
||||||
// Lokale erschöpft → auf Web umschalten / weiterblättern.
|
|
||||||
const nextPage = webPageno + 1;
|
|
||||||
webSearching = webHits.length === 0;
|
|
||||||
try {
|
|
||||||
const wres = await fetch(
|
|
||||||
`/api/recipes/search/web?q=${encodeURIComponent(q)}&pageno=${nextPage}${filterParam()}`
|
|
||||||
);
|
|
||||||
if (query.trim() !== q) return;
|
|
||||||
if (!wres.ok) {
|
|
||||||
const err = await wres.json().catch(() => ({}));
|
|
||||||
webError = err.message ?? `HTTP ${wres.status}`;
|
|
||||||
webExhausted = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const wbody = await wres.json();
|
|
||||||
const more = wbody.hits as WebHit[];
|
|
||||||
const seen = new Set(webHits.map((h) => h.url));
|
|
||||||
const deduped = more.filter((h) => !seen.has(h.url));
|
|
||||||
if (deduped.length === 0) {
|
|
||||||
webExhausted = true;
|
|
||||||
} else {
|
|
||||||
webHits = [...webHits, ...deduped];
|
|
||||||
webPageno = nextPage;
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (query.trim() === q) webSearching = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
loadingMore = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
const q = query.trim();
|
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
||||||
if (debounceTimer) clearTimeout(debounceTimer);
|
store.query; // register reactive dep
|
||||||
if (skipNextSearch) {
|
store.runDebounced();
|
||||||
// Snapshot-Restore hat hits/webHits/searchedFor wiederhergestellt —
|
|
||||||
// nicht erneut fetchen.
|
|
||||||
skipNextSearch = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (q.length <= 3) {
|
|
||||||
hits = [];
|
|
||||||
webHits = [];
|
|
||||||
searchedFor = null;
|
|
||||||
searching = false;
|
|
||||||
webSearching = false;
|
|
||||||
webError = null;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
searching = true;
|
|
||||||
webHits = [];
|
|
||||||
webSearching = false;
|
|
||||||
webError = null;
|
|
||||||
debounceTimer = setTimeout(() => {
|
|
||||||
void runSearch(q);
|
|
||||||
}, 300);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function submit(e: SubmitEvent) {
|
function submit(e: SubmitEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const q = query.trim();
|
const q = store.query.trim();
|
||||||
if (q.length <= 3) return;
|
if (q.length <= 3) return;
|
||||||
if (debounceTimer) clearTimeout(debounceTimer);
|
void store.runSearch(q);
|
||||||
searching = true;
|
|
||||||
void runSearch(q);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function dismissFromRecent(recipeId: number, e: MouseEvent) {
|
async function dismissFromRecent(recipeId: number, e: MouseEvent) {
|
||||||
@@ -367,7 +204,7 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const activeSearch = $derived(query.trim().length > 3);
|
const activeSearch = $derived(store.query.trim().length > 3);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<section class="hero">
|
<section class="hero">
|
||||||
@@ -378,7 +215,7 @@
|
|||||||
<SearchFilter inline />
|
<SearchFilter inline />
|
||||||
<input
|
<input
|
||||||
type="search"
|
type="search"
|
||||||
bind:value={query}
|
bind:value={store.query}
|
||||||
placeholder="Rezept suchen…"
|
placeholder="Rezept suchen…"
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
inputmode="search"
|
inputmode="search"
|
||||||
@@ -390,12 +227,12 @@
|
|||||||
|
|
||||||
{#if activeSearch}
|
{#if activeSearch}
|
||||||
<section class="results">
|
<section class="results">
|
||||||
{#if searching && hits.length === 0 && webHits.length === 0}
|
{#if store.searching && store.hits.length === 0 && store.webHits.length === 0}
|
||||||
<SearchLoader scope="local" />
|
<SearchLoader scope="local" />
|
||||||
{:else}
|
{:else}
|
||||||
{#if hits.length > 0}
|
{#if store.hits.length > 0}
|
||||||
<ul class="cards">
|
<ul class="cards">
|
||||||
{#each hits as r (r.id)}
|
{#each store.hits as r (r.id)}
|
||||||
<li>
|
<li>
|
||||||
<a href={`/recipes/${r.id}`} class="card">
|
<a href={`/recipes/${r.id}`} class="card">
|
||||||
{#if r.image_path}
|
{#if r.image_path}
|
||||||
@@ -413,20 +250,20 @@
|
|||||||
</li>
|
</li>
|
||||||
{/each}
|
{/each}
|
||||||
</ul>
|
</ul>
|
||||||
{:else if searchedFor === query.trim() && !webSearching && webHits.length === 0 && !webError}
|
{:else if store.searchedFor === store.query.trim() && !store.webSearching && store.webHits.length === 0 && !store.webError}
|
||||||
<p class="muted no-local-msg">Keine lokalen Rezepte für „{searchedFor}".</p>
|
<p class="muted no-local-msg">Keine lokalen Rezepte für „{store.searchedFor}".</p>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if webHits.length > 0}
|
{#if store.webHits.length > 0}
|
||||||
{#if hits.length > 0}
|
{#if store.hits.length > 0}
|
||||||
<h3 class="sep">Aus dem Internet</h3>
|
<h3 class="sep">Aus dem Internet</h3>
|
||||||
{:else if searchedFor === query.trim()}
|
{:else if store.searchedFor === store.query.trim()}
|
||||||
<p class="muted no-local-msg">
|
<p class="muted no-local-msg">
|
||||||
Keine lokalen Rezepte für „{searchedFor}" — Ergebnisse aus dem Internet:
|
Keine lokalen Rezepte für „{store.searchedFor}" — Ergebnisse aus dem Internet:
|
||||||
</p>
|
</p>
|
||||||
{/if}
|
{/if}
|
||||||
<ul class="cards">
|
<ul class="cards">
|
||||||
{#each webHits as w (w.url)}
|
{#each store.webHits as w (w.url)}
|
||||||
<li>
|
<li>
|
||||||
<a class="card" href={`/preview?url=${encodeURIComponent(w.url)}`}>
|
<a class="card" href={`/preview?url=${encodeURIComponent(w.url)}`}>
|
||||||
{#if w.thumbnail}
|
{#if w.thumbnail}
|
||||||
@@ -444,16 +281,16 @@
|
|||||||
</ul>
|
</ul>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if webSearching}
|
{#if store.webSearching}
|
||||||
<SearchLoader scope="web" />
|
<SearchLoader scope="web" />
|
||||||
{:else if webError && webHits.length === 0}
|
{:else if store.webError && store.webHits.length === 0}
|
||||||
<p class="error">Internet-Suche zurzeit nicht möglich: {webError}</p>
|
<p class="error">Internet-Suche zurzeit nicht möglich: {store.webError}</p>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if searchedFor === query.trim() && !(localExhausted && webExhausted) && !(searching && hits.length === 0)}
|
{#if store.searchedFor === store.query.trim() && !(store.localExhausted && store.webExhausted) && !(store.searching && store.hits.length === 0)}
|
||||||
<div class="more-cta">
|
<div class="more-cta">
|
||||||
<button class="more-btn" onclick={loadMore} disabled={loadingMore || webSearching}>
|
<button class="more-btn" onclick={() => store.loadMore()} disabled={store.loadingMore || store.webSearching}>
|
||||||
{loadingMore || webSearching ? 'Lade …' : '+ weitere Ergebnisse'}
|
{store.loadingMore || store.webSearching ? 'Lade …' : '+ weitere Ergebnisse'}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ const IngredientSchema = z.object({
|
|||||||
unit: z.string().max(30).nullable(),
|
unit: z.string().max(30).nullable(),
|
||||||
name: z.string().min(1).max(200),
|
name: z.string().min(1).max(200),
|
||||||
note: z.string().max(300).nullable(),
|
note: z.string().max(300).nullable(),
|
||||||
raw_text: z.string().max(500)
|
raw_text: z.string().max(500),
|
||||||
|
section_heading: z.string().max(200).nullable()
|
||||||
});
|
});
|
||||||
|
|
||||||
const StepSchema = z.object({
|
const StepSchema = z.object({
|
||||||
|
|||||||
@@ -56,11 +56,13 @@ self.addEventListener('fetch', (event) => {
|
|||||||
event.respondWith(cacheFirst(req, SHELL_CACHE));
|
event.respondWith(cacheFirst(req, SHELL_CACHE));
|
||||||
} else if (strategy === 'images') {
|
} else if (strategy === 'images') {
|
||||||
event.respondWith(cacheFirst(req, IMAGES_CACHE));
|
event.respondWith(cacheFirst(req, IMAGES_CACHE));
|
||||||
} else if (strategy === 'swr') {
|
} else if (strategy === 'network-first') {
|
||||||
event.respondWith(staleWhileRevalidate(req, DATA_CACHE));
|
event.respondWith(networkFirstWithTimeout(req, DATA_CACHE, NETWORK_TIMEOUT_MS));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const NETWORK_TIMEOUT_MS = 3000;
|
||||||
|
|
||||||
async function cacheFirst(req: Request, cacheName: string): Promise<Response> {
|
async function cacheFirst(req: Request, cacheName: string): Promise<Response> {
|
||||||
const cache = await caches.open(cacheName);
|
const cache = await caches.open(cacheName);
|
||||||
const hit = await cache.match(req);
|
const hit = await cache.match(req);
|
||||||
@@ -70,16 +72,36 @@ async function cacheFirst(req: Request, cacheName: string): Promise<Response> {
|
|||||||
return fresh;
|
return fresh;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function staleWhileRevalidate(req: Request, cacheName: string): Promise<Response> {
|
// Network-first mit Timeout-Fallback: frische Daten gewinnen, wenn das Netz
|
||||||
|
// innerhalb von NETWORK_TIMEOUT_MS antwortet. Sonst wird der Cache geliefert
|
||||||
|
// (falls vorhanden), während der Netz-Fetch noch im Hintergrund weiterläuft
|
||||||
|
// und den Cache für den nächsten Request aktualisiert. Ohne Cache wartet der
|
||||||
|
// Client trotzdem aufs Netz, weil ein Error-Response hier nichts nützt.
|
||||||
|
async function networkFirstWithTimeout(
|
||||||
|
req: Request,
|
||||||
|
cacheName: string,
|
||||||
|
timeoutMs: number
|
||||||
|
): Promise<Response> {
|
||||||
const cache = await caches.open(cacheName);
|
const cache = await caches.open(cacheName);
|
||||||
const hit = await cache.match(req);
|
const networkPromise: Promise<Response | null> = fetch(req)
|
||||||
const fetchPromise = fetch(req)
|
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res.ok) cache.put(req, res.clone()).catch(() => {});
|
if (res.ok) cache.put(req, res.clone()).catch(() => {});
|
||||||
return res;
|
return res;
|
||||||
})
|
})
|
||||||
.catch(() => hit ?? Response.error());
|
.catch(() => null);
|
||||||
return hit ?? fetchPromise;
|
|
||||||
|
const timeoutPromise = new Promise<'timeout'>((resolve) =>
|
||||||
|
setTimeout(() => resolve('timeout'), timeoutMs)
|
||||||
|
);
|
||||||
|
|
||||||
|
const winner = await Promise.race([networkPromise, timeoutPromise]);
|
||||||
|
if (winner instanceof Response) return winner;
|
||||||
|
|
||||||
|
// Timeout oder Netzwerk-Fehler: Cache bevorzugen, sonst auf Netz warten.
|
||||||
|
const hit = await cache.match(req);
|
||||||
|
if (hit) return hit;
|
||||||
|
const late = await networkPromise;
|
||||||
|
return late ?? Response.error();
|
||||||
}
|
}
|
||||||
|
|
||||||
const META_CACHE = 'kochwas-meta';
|
const META_CACHE = 'kochwas-meta';
|
||||||
|
|||||||
68
tests/e2e/remote/README.md
Normal file
68
tests/e2e/remote/README.md
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
# E2E-Tests gegen kochwas-dev
|
||||||
|
|
||||||
|
Playwright-Smoketests gegen ein deployed Environment — standardmaessig
|
||||||
|
`https://kochwas-dev.siegeln.net`. Loest die bisherigen manuellen
|
||||||
|
MCP-Runs ab.
|
||||||
|
|
||||||
|
## Setup (einmalig)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install
|
||||||
|
npx playwright install chromium
|
||||||
|
```
|
||||||
|
|
||||||
|
## Ausfuehren
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run test:e2e:remote # Headless, alle Tests
|
||||||
|
npm run test:e2e:remote -- --ui # Mit Playwright-UI (Trace-Viewer)
|
||||||
|
npm run test:e2e:remote -- --debug # Step-by-Step
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternative URL:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
E2E_REMOTE_URL=https://kochwas.siegeln.net npm run test:e2e:remote
|
||||||
|
```
|
||||||
|
|
||||||
|
## Was abgedeckt ist
|
||||||
|
|
||||||
|
### Happy Paths (UI)
|
||||||
|
|
||||||
|
| Spec | Was |
|
||||||
|
|---|---|
|
||||||
|
| `homepage.spec.ts` | H1, Recents/Alle-Rezepte-Sektionen, Sort-Tabs rendern unterschiedlich, keine Console-Errors |
|
||||||
|
| `search.spec.ts` | Lokaler Treffer, Web-Fallback, Empty-State, Deep-Link `?q=` |
|
||||||
|
| `profile.spec.ts` | Switcher-Dialog, Auswahl persistiert, "Deine Favoriten" erscheint nach Login |
|
||||||
|
| `recipe-detail.spec.ts` | Header, Portionen-Skalierung (4->6, Mengen proportional), Favorit-Toggle, Rating persistiert ueber Reload, Gekocht-Counter, Wunschliste-Toggle |
|
||||||
|
| `comments.spec.ts` | Eigenen Kommentar erstellen + via UI-Button loeschen; fremder Kommentar hat keinen Delete-Button |
|
||||||
|
| `wishlist.spec.ts` | Seite laedt, Sort-Tabs, Header-Badge spiegelt API-Zaehler |
|
||||||
|
| `preview.spec.ts` | Guard ohne `?url=`, echte URL laedt JSON-LD-Parsing, unparsbare URL zeigt error-box |
|
||||||
|
| `admin.spec.ts` | Alle 4 Admin-Subrouten laden mit Tab-Nav, `/admin` redirected |
|
||||||
|
|
||||||
|
### Negative Paths (API)
|
||||||
|
|
||||||
|
| Spec | Was |
|
||||||
|
|---|---|
|
||||||
|
| `api-errors.spec.ts` | `parsePositiveIntParam` → 400 `Invalid id` (4 Call-Sites), `validateBody` → 400 `{message, issues}` (4 Call-Sites), 404 auf missing Ressource, Positiv-Sanity fuer /health, /profiles, /domains |
|
||||||
|
|
||||||
|
## Design-Entscheidungen
|
||||||
|
|
||||||
|
**`workers: 1`.** Tests mutieren echte Daten auf `kochwas-dev` (Rating,
|
||||||
|
Favorit, Wunschliste, Kommentare). Parallelitaet wuerde Race-Conditions
|
||||||
|
geben. `afterEach` raeumt per API auf — idempotent.
|
||||||
|
|
||||||
|
**Hardcoded Test-Fixtures.** Rezept-ID 66 (Chicken Teriyaki) und
|
||||||
|
Profile 1/2/3 (Hendrik/Verena/Leana) sind stabil auf dev. Bei
|
||||||
|
DB-Reset muessen ggf. die Konstanten angepasst werden.
|
||||||
|
|
||||||
|
**Kein Build/Server-Start.** Im Gegensatz zur lokalen `playwright.config.ts`
|
||||||
|
startet diese Config keinen Preview-Server — die Tests laufen gegen das
|
||||||
|
CI-Build auf dev.
|
||||||
|
|
||||||
|
## Was NICHT hier ist
|
||||||
|
|
||||||
|
- **Service-Worker-Lifecycle / Offline** → `tests/e2e/offline.spec.ts` (lokal).
|
||||||
|
- **Bild-Upload** — File-Dialog + echte Dateien; nur manuell sinnvoll.
|
||||||
|
- **Drucken** — oeffnet `window.print()`, headless unzuverlaessig.
|
||||||
|
- **Sync unter Last** — braucht dediziertes Harness, nicht Smoke-Scope.
|
||||||
20
tests/e2e/remote/admin.spec.ts
Normal file
20
tests/e2e/remote/admin.spec.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
|
||||||
|
test.describe('Admin-Routen', () => {
|
||||||
|
const SUBROUTES = ['domains', 'profiles', 'backup', 'app'] as const;
|
||||||
|
|
||||||
|
for (const sub of SUBROUTES) {
|
||||||
|
test(`/admin/${sub} laedt mit Nav-Tabs`, async ({ page }) => {
|
||||||
|
await page.goto(`/admin/${sub}`);
|
||||||
|
// Alle Admin-Subseiten haben dieselbe Tab-Leiste.
|
||||||
|
for (const label of ['Domains', 'Profile', 'Backup', 'App']) {
|
||||||
|
await expect(page.getByRole('link', { name: label })).toBeVisible();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test('/admin redirected auf /admin/domains', async ({ page }) => {
|
||||||
|
await page.goto('/admin');
|
||||||
|
await expect(page).toHaveURL(/\/admin\/domains$/);
|
||||||
|
});
|
||||||
|
});
|
||||||
101
tests/e2e/remote/api-errors.spec.ts
Normal file
101
tests/e2e/remote/api-errors.spec.ts
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
|
||||||
|
// Negative-Path Tests fuer die api-helpers: parsePositiveIntParam und
|
||||||
|
// validateBody. Jeder neue API-Handler sollte dieselben Error-Shapes
|
||||||
|
// liefern — wenn dieser Suite-Block kippt, ist der Helper-Contract kaputt.
|
||||||
|
|
||||||
|
test.describe('API Error-Shapes', () => {
|
||||||
|
test.describe('parsePositiveIntParam', () => {
|
||||||
|
test('GET /api/recipes/abc -> 400 Invalid id', async ({ request }) => {
|
||||||
|
const r = await request.get('/api/recipes/abc');
|
||||||
|
expect(r.status()).toBe(400);
|
||||||
|
expect(await r.json()).toEqual({ message: 'Invalid id' });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET /api/recipes/-1 -> 400 Invalid id', async ({ request }) => {
|
||||||
|
const r = await request.get('/api/recipes/-1');
|
||||||
|
expect(r.status()).toBe(400);
|
||||||
|
expect(await r.json()).toEqual({ message: 'Invalid id' });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET /api/recipes/0 -> 400 Invalid id', async ({ request }) => {
|
||||||
|
const r = await request.get('/api/recipes/0');
|
||||||
|
expect(r.status()).toBe(400);
|
||||||
|
expect(await r.json()).toEqual({ message: 'Invalid id' });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('POST /api/recipes/abc/comments -> 400 Invalid id', async ({ request }) => {
|
||||||
|
const r = await request.post('/api/recipes/abc/comments', { data: {} });
|
||||||
|
expect(r.status()).toBe(400);
|
||||||
|
expect(await r.json()).toEqual({ message: 'Invalid id' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test.describe('validateBody', () => {
|
||||||
|
test('POST /api/wishlist leer -> 400 {message, issues}', async ({ request }) => {
|
||||||
|
const r = await request.post('/api/wishlist', { data: {} });
|
||||||
|
expect(r.status()).toBe(400);
|
||||||
|
const body = (await r.json()) as { message: string; issues?: unknown[] };
|
||||||
|
expect(body.message).toBe('Invalid body');
|
||||||
|
expect(Array.isArray(body.issues)).toBe(true);
|
||||||
|
expect((body.issues ?? []).length).toBeGreaterThanOrEqual(2); // recipe_id + profile_id
|
||||||
|
});
|
||||||
|
|
||||||
|
test('POST /api/recipes/66/comments leer -> 400 {message, issues}', async ({ request }) => {
|
||||||
|
const r = await request.post('/api/recipes/66/comments', { data: {} });
|
||||||
|
expect(r.status()).toBe(400);
|
||||||
|
const body = (await r.json()) as { message: string; issues?: unknown[] };
|
||||||
|
expect(body.message).toBe('Invalid body');
|
||||||
|
expect((body.issues ?? []).length).toBeGreaterThanOrEqual(1); // profile_id oder text
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PUT /api/recipes/66/favorite leer -> 400 {message, issues}', async ({ request }) => {
|
||||||
|
const r = await request.put('/api/recipes/66/favorite', { data: {} });
|
||||||
|
expect(r.status()).toBe(400);
|
||||||
|
const body = (await r.json()) as { message: string; issues?: unknown[] };
|
||||||
|
expect(body.message).toBe('Invalid body');
|
||||||
|
expect((body.issues ?? []).length).toBeGreaterThanOrEqual(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('POST /api/domains leer -> 400 {message, issues}', async ({ request }) => {
|
||||||
|
const r = await request.post('/api/domains', { data: {} });
|
||||||
|
expect(r.status()).toBe(400);
|
||||||
|
const body = (await r.json()) as { message: string; issues?: unknown[] };
|
||||||
|
expect(body.message).toBe('Invalid body');
|
||||||
|
expect((body.issues ?? []).length).toBeGreaterThanOrEqual(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test.describe('404 auf missing Ressourcen', () => {
|
||||||
|
test('GET /api/recipes/99999 -> 404 Recipe not found', async ({ request }) => {
|
||||||
|
const r = await request.get('/api/recipes/99999');
|
||||||
|
expect(r.status()).toBe(404);
|
||||||
|
expect(await r.json()).toEqual({ message: 'Recipe not found' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test.describe('Positive Sanity-Checks', () => {
|
||||||
|
test('GET /api/health -> 200 mit db:"ok"', async ({ request }) => {
|
||||||
|
const r = await request.get('/api/health');
|
||||||
|
expect(r.status()).toBe(200);
|
||||||
|
const body = (await r.json()) as { db: string };
|
||||||
|
expect(body.db).toBe('ok');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET /api/profiles -> drei Profile', async ({ request }) => {
|
||||||
|
const r = await request.get('/api/profiles');
|
||||||
|
expect(r.status()).toBe(200);
|
||||||
|
const body = (await r.json()) as { id: number; name: string }[];
|
||||||
|
expect(body.length).toBeGreaterThanOrEqual(3);
|
||||||
|
const names = body.map((p) => p.name).sort();
|
||||||
|
expect(names).toEqual(expect.arrayContaining(['Hendrik', 'Leana', 'Verena']));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('GET /api/domains -> liefert Array', async ({ request }) => {
|
||||||
|
const r = await request.get('/api/domains');
|
||||||
|
expect(r.status()).toBe(200);
|
||||||
|
const body = await r.json();
|
||||||
|
expect(Array.isArray(body)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
71
tests/e2e/remote/comments.spec.ts
Normal file
71
tests/e2e/remote/comments.spec.ts
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
import { setActiveProfile, HENDRIK_ID } from './fixtures/profile';
|
||||||
|
import { cleanupE2EComments, deleteComment } from './fixtures/api-cleanup';
|
||||||
|
|
||||||
|
const RECIPE_ID = 66;
|
||||||
|
|
||||||
|
test.describe('Kommentare', () => {
|
||||||
|
test.beforeEach(async ({ page, request }) => {
|
||||||
|
await setActiveProfile(page, HENDRIK_ID);
|
||||||
|
// Stray E2E-Kommentare aus abgebrochenen Runs wegraeumen.
|
||||||
|
await cleanupE2EComments(request, RECIPE_ID, HENDRIK_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
test.afterEach(async ({ request }) => {
|
||||||
|
await cleanupE2EComments(request, RECIPE_ID, HENDRIK_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Kommentar erstellen, Delete-Button erscheint, Loeschen via UI', async ({
|
||||||
|
page
|
||||||
|
}) => {
|
||||||
|
const unique = `E2E ${Date.now()}`;
|
||||||
|
await page.goto(`/recipes/${RECIPE_ID}`);
|
||||||
|
|
||||||
|
await page.getByRole('textbox').filter({ hasText: '' }).last().fill(unique);
|
||||||
|
await page.getByRole('button', { name: 'Kommentar speichern' }).click();
|
||||||
|
|
||||||
|
// Neuer Kommentar sichtbar
|
||||||
|
await expect(page.getByText(unique)).toBeVisible({ timeout: 5000 });
|
||||||
|
|
||||||
|
// Delete-Button NUR beim eigenen Kommentar
|
||||||
|
const delBtn = page.getByRole('button', { name: 'Kommentar löschen' });
|
||||||
|
await expect(delBtn).toBeVisible();
|
||||||
|
|
||||||
|
await delBtn.click();
|
||||||
|
// ConfirmDialog "Kommentar loeschen?" mit Loeschen-Button.
|
||||||
|
// Es gibt mehrere "Löschen"-Buttons auf der Seite (Rezept-Delete,
|
||||||
|
// Kommentar-Trash, Dialog-Bestaetigung) — deshalb Locator auf den
|
||||||
|
// Dialog einschraenken.
|
||||||
|
const dialog = page.getByRole('dialog', { name: /Kommentar löschen/i });
|
||||||
|
await expect(dialog).toBeVisible();
|
||||||
|
await dialog.getByRole('button', { name: 'Löschen' }).click();
|
||||||
|
|
||||||
|
await expect(page.getByText(unique)).not.toBeVisible({ timeout: 5000 });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Fremder Kommentar zeigt KEINEN Delete-Button fuers aktuelle Profil', async ({
|
||||||
|
page,
|
||||||
|
request
|
||||||
|
}) => {
|
||||||
|
// Wir legen den Kommentar fuer ein anderes Profil (Leana, id=3) per API an.
|
||||||
|
const text = `E2E fremd ${Date.now()}`;
|
||||||
|
const res = await request.post(`/api/recipes/${RECIPE_ID}/comments`, {
|
||||||
|
data: { profile_id: 3, text }
|
||||||
|
});
|
||||||
|
expect(res.status()).toBe(201);
|
||||||
|
const { id } = (await res.json()) as { id: number };
|
||||||
|
|
||||||
|
try {
|
||||||
|
await page.goto(`/recipes/${RECIPE_ID}`);
|
||||||
|
const item = page
|
||||||
|
.locator('.comments li')
|
||||||
|
.filter({ hasText: text });
|
||||||
|
await expect(item).toBeVisible();
|
||||||
|
await expect(
|
||||||
|
item.getByRole('button', { name: 'Kommentar löschen' })
|
||||||
|
).toHaveCount(0);
|
||||||
|
} finally {
|
||||||
|
await deleteComment(request, RECIPE_ID, id);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
67
tests/e2e/remote/fixtures/api-cleanup.ts
Normal file
67
tests/e2e/remote/fixtures/api-cleanup.ts
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
import type { APIRequestContext } from '@playwright/test';
|
||||||
|
|
||||||
|
// Cleanup-Helfer fuer afterEach-Hooks. Alle sind idempotent — wenn der
|
||||||
|
// Zustand schon weg ist (z. B. der Test ist zwischen Action und Check
|
||||||
|
// abgebrochen), fliegt nichts.
|
||||||
|
|
||||||
|
export async function clearRating(
|
||||||
|
api: APIRequestContext,
|
||||||
|
recipeId: number,
|
||||||
|
profileId: number
|
||||||
|
): Promise<void> {
|
||||||
|
await api.delete(`/api/recipes/${recipeId}/rating`, {
|
||||||
|
data: { profile_id: profileId }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function clearFavorite(
|
||||||
|
api: APIRequestContext,
|
||||||
|
recipeId: number,
|
||||||
|
profileId: number
|
||||||
|
): Promise<void> {
|
||||||
|
await api.delete(`/api/recipes/${recipeId}/favorite`, {
|
||||||
|
data: { profile_id: profileId }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function removeFromWishlist(
|
||||||
|
api: APIRequestContext,
|
||||||
|
recipeId: number,
|
||||||
|
profileId: number
|
||||||
|
): Promise<void> {
|
||||||
|
await api.delete(`/api/wishlist/${recipeId}?profile_id=${profileId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteComment(
|
||||||
|
api: APIRequestContext,
|
||||||
|
recipeId: number,
|
||||||
|
commentId: number
|
||||||
|
): Promise<void> {
|
||||||
|
await api.delete(`/api/recipes/${recipeId}/comments`, {
|
||||||
|
data: { comment_id: commentId }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safety-Net: loescht alle E2E-Kommentare eines Profils. Gedacht fuer
|
||||||
|
* afterEach/afterAll, falls ein Test abbricht bevor der eigene Cleanup
|
||||||
|
* greift. Markiert E2E-Kommentare am Prefix "E2E ".
|
||||||
|
*/
|
||||||
|
export async function cleanupE2EComments(
|
||||||
|
api: APIRequestContext,
|
||||||
|
recipeId: number,
|
||||||
|
profileId: number
|
||||||
|
): Promise<void> {
|
||||||
|
const res = await api.get(`/api/recipes/${recipeId}/comments`);
|
||||||
|
if (!res.ok()) return;
|
||||||
|
const list = (await res.json()) as {
|
||||||
|
id: number;
|
||||||
|
profile_id: number;
|
||||||
|
text: string;
|
||||||
|
}[];
|
||||||
|
for (const c of list) {
|
||||||
|
if (c.profile_id === profileId && c.text.startsWith('E2E ')) {
|
||||||
|
await deleteComment(api, recipeId, c.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
tests/e2e/remote/fixtures/profile.ts
Normal file
26
tests/e2e/remote/fixtures/profile.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import type { Page } from '@playwright/test';
|
||||||
|
|
||||||
|
// Profil-IDs auf kochwas-dev: 1 = Hendrik, 2 = Verena, 3 = Leana.
|
||||||
|
// Die Tests hardcoden Hendrik als Standard, weil die Dev-DB diese
|
||||||
|
// Profile stabil enthaelt.
|
||||||
|
export const HENDRIK_ID = 1;
|
||||||
|
export const VERENA_ID = 2;
|
||||||
|
export const LEANA_ID = 3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setzt das aktive Profil in localStorage, BEVOR die Seite geladen wird.
|
||||||
|
* addInitScript laeuft vor jedem Skript der Seite — damit ist das Profil
|
||||||
|
* schon da, wenn profileStore.load() das erste Mal liest.
|
||||||
|
*/
|
||||||
|
export async function setActiveProfile(page: Page, id: number): Promise<void> {
|
||||||
|
await page.addInitScript(
|
||||||
|
(pid) => window.localStorage.setItem('kochwas.activeProfileId', String(pid)),
|
||||||
|
id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function clearActiveProfile(page: Page): Promise<void> {
|
||||||
|
await page.addInitScript(() =>
|
||||||
|
window.localStorage.removeItem('kochwas.activeProfileId')
|
||||||
|
);
|
||||||
|
}
|
||||||
43
tests/e2e/remote/homepage.spec.ts
Normal file
43
tests/e2e/remote/homepage.spec.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
|
||||||
|
test.describe('Startseite', () => {
|
||||||
|
test('laedt mit H1, Zuletzt-hinzugefuegt und Alle-Rezepte', async ({ page }) => {
|
||||||
|
await page.goto('/');
|
||||||
|
await expect(page).toHaveTitle(/Kochwas/);
|
||||||
|
await expect(page.getByRole('heading', { level: 1, name: 'Kochwas' })).toBeVisible();
|
||||||
|
await expect(
|
||||||
|
page.getByRole('heading', { level: 2, name: 'Zuletzt hinzugefügt' })
|
||||||
|
).toBeVisible();
|
||||||
|
await expect(page.getByRole('heading', { level: 2, name: 'Alle Rezepte' })).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Sort-Tabs rendern unterschiedliche Top-Eintraege', async ({ page }) => {
|
||||||
|
await page.goto('/');
|
||||||
|
// Liste unter "Alle Rezepte"
|
||||||
|
const allSection = page.locator('section', { has: page.getByRole('heading', { name: 'Alle Rezepte' }) });
|
||||||
|
const firstItem = () => allSection.locator('li a').first().innerText();
|
||||||
|
|
||||||
|
await page.getByRole('tab', { name: 'Name' }).click();
|
||||||
|
await page.waitForTimeout(400);
|
||||||
|
const nameTop = await firstItem();
|
||||||
|
|
||||||
|
await page.getByRole('tab', { name: 'Hinzugefügt' }).click();
|
||||||
|
await page.waitForTimeout(400);
|
||||||
|
const addedTop = await firstItem();
|
||||||
|
|
||||||
|
expect(nameTop).not.toEqual(addedTop);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('hat keine Console-Errors', async ({ page }) => {
|
||||||
|
const errors: string[] = [];
|
||||||
|
page.on('console', (msg) => {
|
||||||
|
if (msg.type() === 'error') errors.push(msg.text());
|
||||||
|
});
|
||||||
|
await page.goto('/');
|
||||||
|
await page.waitForLoadState('networkidle');
|
||||||
|
// 404s auf externen Bildern (chefkoch-cdn, cloudfront) ignorieren —
|
||||||
|
// das ist kein App-Fehler, sondern externe Thumbnails.
|
||||||
|
const appErrors = errors.filter((e) => !/Failed to load resource/i.test(e));
|
||||||
|
expect(appErrors).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
216
tests/e2e/remote/ingredient-sections.spec.ts
Normal file
216
tests/e2e/remote/ingredient-sections.spec.ts
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
import { test, expect, type APIRequestContext } from '@playwright/test';
|
||||||
|
import { setActiveProfile, HENDRIK_ID } from './fixtures/profile';
|
||||||
|
|
||||||
|
// Helper: idempotent recipe delete.
|
||||||
|
async function deleteRecipe(request: APIRequestContext, id: number): Promise<void> {
|
||||||
|
await request.delete(`/api/recipes/${id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shared ingredient payload builder — fills all required Zod fields.
|
||||||
|
function makeIngredient(
|
||||||
|
position: number,
|
||||||
|
name: string,
|
||||||
|
section_heading: string | null,
|
||||||
|
overrides: Partial<{
|
||||||
|
quantity: number | null;
|
||||||
|
unit: string | null;
|
||||||
|
note: string | null;
|
||||||
|
raw_text: string;
|
||||||
|
}> = {}
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
position,
|
||||||
|
quantity: overrides.quantity ?? null,
|
||||||
|
unit: overrides.unit ?? null,
|
||||||
|
name,
|
||||||
|
note: overrides.note ?? null,
|
||||||
|
raw_text: overrides.raw_text ?? name,
|
||||||
|
section_heading
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
// Per-test cleanup scaffolding — single variable, reset in beforeEach.
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
let createdId: number | null = null;
|
||||||
|
|
||||||
|
test.beforeEach(() => {
|
||||||
|
createdId = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
test.afterEach(async ({ request }) => {
|
||||||
|
if (createdId !== null) {
|
||||||
|
await deleteRecipe(request, createdId);
|
||||||
|
createdId = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
// Test 1 — pure API roundtrip (no browser needed)
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
test('API: section_heading persistiert ueber PATCH + GET', async ({ request }) => {
|
||||||
|
// 1. Create blank recipe.
|
||||||
|
const createRes = await request.post('/api/recipes/blank');
|
||||||
|
expect(createRes.status()).toBe(200);
|
||||||
|
const { id } = (await createRes.json()) as { id: number };
|
||||||
|
createdId = id;
|
||||||
|
|
||||||
|
// 2. PATCH with 3 ingredients carrying section_heading values.
|
||||||
|
const patchRes = await request.patch(`/api/recipes/${id}`, {
|
||||||
|
data: {
|
||||||
|
ingredients: [
|
||||||
|
makeIngredient(1, 'Mehl', 'Fuer den Teig', { quantity: 200, unit: 'g', raw_text: '200 g Mehl' }),
|
||||||
|
makeIngredient(2, 'Zucker', null, { quantity: 100, unit: 'g', raw_text: '100 g Zucker' }),
|
||||||
|
makeIngredient(3, 'Beeren', 'Fuer die Fuellung', { quantity: 150, unit: 'g', raw_text: '150 g Beeren' })
|
||||||
|
]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
expect(patchRes.status()).toBe(200);
|
||||||
|
|
||||||
|
// 3. GET and assert persisted values.
|
||||||
|
const getRes = await request.get(`/api/recipes/${id}`);
|
||||||
|
expect(getRes.status()).toBe(200);
|
||||||
|
const body = (await getRes.json()) as {
|
||||||
|
recipe: { ingredients: Array<{ name: string; section_heading: string | null }> };
|
||||||
|
};
|
||||||
|
const ings = body.recipe.ingredients;
|
||||||
|
|
||||||
|
const mehl = ings.find((i) => i.name === 'Mehl');
|
||||||
|
const zucker = ings.find((i) => i.name === 'Zucker');
|
||||||
|
const beeren = ings.find((i) => i.name === 'Beeren');
|
||||||
|
|
||||||
|
expect(mehl?.section_heading).toBe('Fuer den Teig');
|
||||||
|
expect(zucker?.section_heading).toBeNull();
|
||||||
|
expect(beeren?.section_heading).toBe('Fuer die Fuellung');
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
// Test 2 — UI edit flow: add section, save, assert view renders heading
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
test('Editor: Abschnitt via Inline-Button anlegen, View rendert Ueberschrift', async ({
|
||||||
|
page,
|
||||||
|
request
|
||||||
|
}) => {
|
||||||
|
// 1. Create blank recipe via API.
|
||||||
|
const createRes = await request.post('/api/recipes/blank');
|
||||||
|
expect(createRes.status()).toBe(200);
|
||||||
|
const { id } = (await createRes.json()) as { id: number };
|
||||||
|
createdId = id;
|
||||||
|
|
||||||
|
// 2. Open recipe in edit mode.
|
||||||
|
await setActiveProfile(page, HENDRIK_ID);
|
||||||
|
await page.goto(`/recipes/${id}?edit=1`);
|
||||||
|
|
||||||
|
// 3. Add two ingredient rows.
|
||||||
|
const addIngBtn = page.getByRole('button', { name: /Zutat hinzufügen/i });
|
||||||
|
await addIngBtn.click();
|
||||||
|
await addIngBtn.click();
|
||||||
|
|
||||||
|
// Fill the two ingredient rows by aria-label "Zutat" inputs.
|
||||||
|
const nameInputs = page.locator('.ing-list .ing-row input[aria-label="Zutat"]');
|
||||||
|
await nameInputs.nth(0).fill('Mehl');
|
||||||
|
await nameInputs.nth(1).fill('Zucker');
|
||||||
|
|
||||||
|
// 4. Click "Abschnitt hinzufügen" above the first row.
|
||||||
|
// The button is inside .section-insert which is opacity:0 until hover/focus.
|
||||||
|
// Hover the ing-list to trigger visibility, then click.
|
||||||
|
await page.hover('.ing-list');
|
||||||
|
await page.locator('.ing-list .add-section').first().click();
|
||||||
|
|
||||||
|
// 5. Type heading text into the section-heading input that appeared.
|
||||||
|
const headingInput = page.locator('.ing-list input[aria-label="Sektionsüberschrift"]').first();
|
||||||
|
await headingInput.fill('Fuer den Teig');
|
||||||
|
|
||||||
|
// 6. Save — exact match to avoid colliding with "Kommentar speichern".
|
||||||
|
await page.getByRole('button', { name: 'Speichern', exact: true }).click();
|
||||||
|
|
||||||
|
// After save, editMode becomes false — page switches to view mode.
|
||||||
|
// Wait for the section-heading element to confirm view mode is active.
|
||||||
|
await expect(page.locator('.ing-list .section-heading').first()).toBeVisible({ timeout: 8000 });
|
||||||
|
|
||||||
|
// 7. Assert heading text is rendered.
|
||||||
|
await expect(page.locator('.ing-list .section-heading').first()).toHaveText('Fuer den Teig');
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
// Test 3 — UI: remove an existing section heading, save, confirm it's gone
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
test('Editor: Sektion entfernen speichert ohne Ueberschrift', async ({ page, request }) => {
|
||||||
|
// 1. Create blank recipe and pre-populate via API.
|
||||||
|
const createRes = await request.post('/api/recipes/blank');
|
||||||
|
expect(createRes.status()).toBe(200);
|
||||||
|
const { id } = (await createRes.json()) as { id: number };
|
||||||
|
createdId = id;
|
||||||
|
|
||||||
|
await request.patch(`/api/recipes/${id}`, {
|
||||||
|
data: {
|
||||||
|
ingredients: [makeIngredient(1, 'Butter', 'Teig', { raw_text: 'Butter' })]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 2. Open editor.
|
||||||
|
await setActiveProfile(page, HENDRIK_ID);
|
||||||
|
await page.goto(`/recipes/${id}?edit=1`);
|
||||||
|
|
||||||
|
// The section-heading-row should be visible since heading = 'Teig'.
|
||||||
|
const removeBtn = page
|
||||||
|
.locator('.ing-list')
|
||||||
|
.getByRole('button', { name: 'Sektion entfernen' });
|
||||||
|
await expect(removeBtn).toBeVisible({ timeout: 6000 });
|
||||||
|
|
||||||
|
// 3. Click the section-remove X button.
|
||||||
|
await removeBtn.click();
|
||||||
|
|
||||||
|
// 4. Save — exact match to avoid colliding with "Kommentar speichern".
|
||||||
|
await page.getByRole('button', { name: 'Speichern', exact: true }).click();
|
||||||
|
|
||||||
|
// Wait for view mode (editMode = false makes RecipeEditor unmount).
|
||||||
|
// The .section-heading-row is part of the editor; in view mode we check
|
||||||
|
// the view's .ing-list for absence of .section-heading items.
|
||||||
|
await expect(page.locator('.ing-list .section-heading')).toHaveCount(0, { timeout: 8000 });
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
// Test 4 — empty heading trims to null on save
|
||||||
|
// ─────────────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
test('Editor: leeres Heading wird beim Speichern zu null', async ({ page, request }) => {
|
||||||
|
// 1. Create blank recipe.
|
||||||
|
const createRes = await request.post('/api/recipes/blank');
|
||||||
|
expect(createRes.status()).toBe(200);
|
||||||
|
const { id } = (await createRes.json()) as { id: number };
|
||||||
|
createdId = id;
|
||||||
|
|
||||||
|
// 2. Open editor, add one ingredient, open section input and leave it empty.
|
||||||
|
await setActiveProfile(page, HENDRIK_ID);
|
||||||
|
await page.goto(`/recipes/${id}?edit=1`);
|
||||||
|
|
||||||
|
await page.getByRole('button', { name: /Zutat hinzufügen/i }).click();
|
||||||
|
await page.locator('.ing-list .ing-row input[aria-label="Zutat"]').first().fill('Eier');
|
||||||
|
|
||||||
|
// Trigger add-section visibility and click.
|
||||||
|
await page.hover('.ing-list');
|
||||||
|
await page.locator('.ing-list .add-section').first().click();
|
||||||
|
|
||||||
|
// Leave the heading input empty (do not type anything).
|
||||||
|
// The save() function trims '' → null.
|
||||||
|
|
||||||
|
// 3. Save — exact match to avoid colliding with "Kommentar speichern".
|
||||||
|
await page.getByRole('button', { name: 'Speichern', exact: true }).click();
|
||||||
|
|
||||||
|
// Wait until view mode is active (editor gone).
|
||||||
|
await expect(page.locator('.ing-list .section-heading')).toHaveCount(0, { timeout: 8000 });
|
||||||
|
|
||||||
|
// 4. Confirm via API that section_heading is null.
|
||||||
|
const getRes = await request.get(`/api/recipes/${id}`);
|
||||||
|
expect(getRes.status()).toBe(200);
|
||||||
|
const body = (await getRes.json()) as {
|
||||||
|
recipe: { ingredients: Array<{ name: string; section_heading: string | null }> };
|
||||||
|
};
|
||||||
|
const eier = body.recipe.ingredients.find((i) => i.name === 'Eier');
|
||||||
|
expect(eier?.section_heading).toBeNull();
|
||||||
|
});
|
||||||
29
tests/e2e/remote/preview.spec.ts
Normal file
29
tests/e2e/remote/preview.spec.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
|
||||||
|
test.describe('Preview-Route', () => {
|
||||||
|
test('ohne ?url= zeigt Guard-Fehlermeldung', async ({ page }) => {
|
||||||
|
await page.goto('/preview');
|
||||||
|
await expect(page.getByText(/Kein \?url=-Parameter/)).toBeVisible();
|
||||||
|
await expect(page.getByRole('heading', { name: /kein Rezept/i })).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('mit echter URL laedt Vorschau + Speichern-Button', async ({ page }) => {
|
||||||
|
const u = encodeURIComponent('https://emmikochteinfach.de/chicken-teriyaki/');
|
||||||
|
await page.goto(`/preview?url=${u}`);
|
||||||
|
await expect(page.getByText('Vorschau — noch nicht gespeichert')).toBeVisible({
|
||||||
|
timeout: 20000
|
||||||
|
});
|
||||||
|
await expect(page.getByRole('button', { name: /speichern/i })).toBeVisible();
|
||||||
|
// Zutaten aus dem JSON-LD sollten geparst sein.
|
||||||
|
await expect(page.getByText(/Hähnchenbrustfilet/i).first()).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('mit unparsbarer URL zeigt error-box', async ({ page }) => {
|
||||||
|
// google.com hat kein Recipe-JSON-LD -> Parser-Fehler.
|
||||||
|
const u = encodeURIComponent('https://www.google.com');
|
||||||
|
await page.goto(`/preview?url=${u}`);
|
||||||
|
await expect(page.getByRole('heading', { name: /kein Rezept/i })).toBeVisible({
|
||||||
|
timeout: 20000
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
40
tests/e2e/remote/profile.spec.ts
Normal file
40
tests/e2e/remote/profile.spec.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
import { clearActiveProfile, setActiveProfile, HENDRIK_ID } from './fixtures/profile';
|
||||||
|
|
||||||
|
test.describe('Profil', () => {
|
||||||
|
test('Switcher zeigt alle 3 Profile', async ({ page }) => {
|
||||||
|
await clearActiveProfile(page);
|
||||||
|
await page.goto('/');
|
||||||
|
await page.getByRole('button', { name: 'Profil wechseln' }).click();
|
||||||
|
await expect(page.getByText('Wer kocht heute?')).toBeVisible();
|
||||||
|
for (const name of ['Hendrik', 'Verena', 'Leana']) {
|
||||||
|
await expect(
|
||||||
|
page.locator('.profile-btn', { hasText: name })
|
||||||
|
).toBeVisible();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Profil-Auswahl persistiert im Header', async ({ page }) => {
|
||||||
|
await clearActiveProfile(page);
|
||||||
|
await page.goto('/');
|
||||||
|
await page.getByRole('button', { name: 'Profil wechseln' }).click();
|
||||||
|
await page.locator('.profile-btn', { hasText: 'Hendrik' }).click();
|
||||||
|
await expect(page.getByRole('button', { name: 'Profil wechseln' })).toContainText('Hendrik');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('mit aktivem Profil: "Deine Favoriten"-Sektion erscheint', async ({ page }) => {
|
||||||
|
await setActiveProfile(page, HENDRIK_ID);
|
||||||
|
await page.goto('/');
|
||||||
|
await expect(
|
||||||
|
page.getByRole('heading', { level: 2, name: 'Deine Favoriten' })
|
||||||
|
).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ohne Profil: Rating-Klick oeffnet Standard-Hinweis', async ({ page }) => {
|
||||||
|
await clearActiveProfile(page);
|
||||||
|
await page.goto('/recipes/66');
|
||||||
|
await page.getByRole('button', { name: '5 Sterne' }).click();
|
||||||
|
await expect(page.getByText('Kein Profil gewählt')).toBeVisible();
|
||||||
|
await expect(page.getByText(/klappt die Aktion/)).toBeVisible();
|
||||||
|
});
|
||||||
|
});
|
||||||
84
tests/e2e/remote/recipe-detail.spec.ts
Normal file
84
tests/e2e/remote/recipe-detail.spec.ts
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
import { setActiveProfile, HENDRIK_ID } from './fixtures/profile';
|
||||||
|
import {
|
||||||
|
clearFavorite,
|
||||||
|
clearRating,
|
||||||
|
removeFromWishlist
|
||||||
|
} from './fixtures/api-cleanup';
|
||||||
|
|
||||||
|
// Chicken Teriyaki auf kochwas-dev: 4 Portionen, 500 g Haehnchen, 100 ml Soja.
|
||||||
|
const RECIPE_ID = 66;
|
||||||
|
|
||||||
|
test.describe('Rezept-Detail', () => {
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
await setActiveProfile(page, HENDRIK_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
test.afterEach(async ({ request }) => {
|
||||||
|
await clearRating(request, RECIPE_ID, HENDRIK_ID);
|
||||||
|
await clearFavorite(request, RECIPE_ID, HENDRIK_ID);
|
||||||
|
await removeFromWishlist(request, RECIPE_ID, HENDRIK_ID);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Header + Zutaten sichtbar', async ({ page }) => {
|
||||||
|
await page.goto(`/recipes/${RECIPE_ID}`);
|
||||||
|
await expect(
|
||||||
|
page.getByRole('heading', { level: 1, name: /Chicken Teriyaki/i })
|
||||||
|
).toBeVisible();
|
||||||
|
await expect(page.getByText('Hähnchenbrustfilet').first()).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Portionen-Scaler: 4 -> 6 skaliert Mengen proportional', async ({ page }) => {
|
||||||
|
await page.goto(`/recipes/${RECIPE_ID}`);
|
||||||
|
// Start: 4 Portionen, 500 g Haehnchen, 100 ml Soja.
|
||||||
|
await expect(page.locator('.srv-value strong').first()).toHaveText('4');
|
||||||
|
await page.getByRole('button', { name: 'Mehr' }).first().click();
|
||||||
|
await page.getByRole('button', { name: 'Mehr' }).first().click();
|
||||||
|
await expect(page.locator('.srv-value strong').first()).toHaveText('6');
|
||||||
|
// Skalierte Mengen 1.5x — ueber das Item-Name-Filter, robuster
|
||||||
|
// gegenueber Whitespace-Quirks zwischen <span class="qty">-Teilen.
|
||||||
|
await expect(
|
||||||
|
page.locator('.ing-list li', { hasText: 'Hähnchenbrustfilet' })
|
||||||
|
).toContainText('750 g');
|
||||||
|
await expect(
|
||||||
|
page.locator('.ing-list li', { hasText: 'Sojasauce' })
|
||||||
|
).toContainText('150 ml');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Favorit toggelt heart-Klasse sauber', async ({ page }) => {
|
||||||
|
await page.goto(`/recipes/${RECIPE_ID}`);
|
||||||
|
const favBtn = page.getByRole('button', { name: 'Favorit' });
|
||||||
|
await expect(favBtn).not.toHaveClass(/heart/);
|
||||||
|
await favBtn.click();
|
||||||
|
await expect(favBtn).toHaveClass(/heart/);
|
||||||
|
await favBtn.click();
|
||||||
|
await expect(favBtn).not.toHaveClass(/heart/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Rating persistiert ueber Reload', async ({ page }) => {
|
||||||
|
await page.goto(`/recipes/${RECIPE_ID}`);
|
||||||
|
await page.getByRole('button', { name: '4 Sterne' }).click();
|
||||||
|
await expect(page.getByRole('button', { name: '4 Sterne' })).toHaveClass(/filled/);
|
||||||
|
await page.reload();
|
||||||
|
await expect(page.getByRole('button', { name: '4 Sterne' })).toHaveClass(/filled/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Heute gekocht inkrementiert Counter', async ({ page }) => {
|
||||||
|
await page.goto(`/recipes/${RECIPE_ID}`);
|
||||||
|
const cookedBtn = page.getByRole('button', { name: /Heute gekocht/i });
|
||||||
|
const before = (await cookedBtn.innerText()).trim();
|
||||||
|
await cookedBtn.click();
|
||||||
|
// Der Button bekommt einen "(N)"-Suffix bzw. der existierende zaehler
|
||||||
|
// steigt. Wir pruefen nur, dass sich der Text aendert.
|
||||||
|
await expect(cookedBtn).not.toHaveText(before);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Auf Wunschliste-Toggle funktioniert', async ({ page }) => {
|
||||||
|
await page.goto(`/recipes/${RECIPE_ID}`);
|
||||||
|
const wishBtn = page.getByRole('button', { name: /Auf Wunschliste/i });
|
||||||
|
const initialLabel = (await wishBtn.getAttribute('aria-label')) ?? '';
|
||||||
|
await wishBtn.click();
|
||||||
|
// aria-label wechselt zwischen "setzen" und "Von der Wunschliste entfernen"
|
||||||
|
await expect(wishBtn).not.toHaveAttribute('aria-label', initialLabel);
|
||||||
|
});
|
||||||
|
});
|
||||||
39
tests/e2e/remote/search.spec.ts
Normal file
39
tests/e2e/remote/search.spec.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
|
||||||
|
test.describe('Suche', () => {
|
||||||
|
test('lokaler Treffer erscheint live beim Tippen', async ({ page }) => {
|
||||||
|
await page.goto('/');
|
||||||
|
await page.getByRole('searchbox', { name: 'Suchbegriff' }).fill('lasagne');
|
||||||
|
await expect(page.getByRole('link', { name: /Pfannen Lasagne/i })).toBeVisible({
|
||||||
|
timeout: 5000
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Web-Fallback bei unbekanntem Begriff', async ({ page }) => {
|
||||||
|
// Direkt per URL — spart den Debounce-Timer.
|
||||||
|
await page.goto('/?q=pizza+margherita');
|
||||||
|
await expect(page.getByText(/Keine lokalen Rezepte/i)).toBeVisible({ timeout: 15000 });
|
||||||
|
// Mindestens ein Web-Treffer mit einer Domain-Labeling.
|
||||||
|
await expect(page.getByText(/chefkoch\.de|rezeptwelt\.de/i).first()).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Nonsense-Query rendert Fallback ohne Crash', async ({ page }) => {
|
||||||
|
// SearXNG matcht loose — selbst Nonsense gibt oft Fuzzy-Treffer.
|
||||||
|
// Wir pruefen deshalb nur, dass die Seite sinnvoll reagiert
|
||||||
|
// (entweder echter Empty-State ODER Web-Fallback) und kein JS-Fehler
|
||||||
|
// fliegt.
|
||||||
|
const errors: string[] = [];
|
||||||
|
page.on('pageerror', (err) => errors.push(err.message));
|
||||||
|
await page.goto('/?q=xxyyzznotarecipexxxxxxxx');
|
||||||
|
await expect(
|
||||||
|
page.getByText(/Schaue unter den Topfdeckeln|Keine lokalen Rezepte/i)
|
||||||
|
).toBeVisible({ timeout: 15000 });
|
||||||
|
expect(errors).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Deep-Link ?q=lasagne stellt Query im Input wieder her', async ({ page }) => {
|
||||||
|
await page.goto('/?q=lasagne');
|
||||||
|
const sb = page.getByRole('searchbox', { name: 'Suchbegriff' });
|
||||||
|
await expect(sb).toHaveValue('lasagne');
|
||||||
|
});
|
||||||
|
});
|
||||||
43
tests/e2e/remote/wishlist.spec.ts
Normal file
43
tests/e2e/remote/wishlist.spec.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import { test, expect } from '@playwright/test';
|
||||||
|
import { clearActiveProfile, setActiveProfile, HENDRIK_ID } from './fixtures/profile';
|
||||||
|
|
||||||
|
test.describe('Wunschliste-Seite', () => {
|
||||||
|
test('laedt Header + Sort-Tabs', async ({ page }) => {
|
||||||
|
await setActiveProfile(page, HENDRIK_ID);
|
||||||
|
await page.goto('/wishlist');
|
||||||
|
await expect(page.getByRole('heading', { level: 1, name: 'Wunschliste' })).toBeVisible();
|
||||||
|
for (const label of ['Meist gewünscht', 'Neueste', 'Älteste']) {
|
||||||
|
await expect(page.getByRole('tab', { name: label })).toBeVisible();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Badge im Header stimmt mit Anzahl Eintraegen ueberein', async ({ page, request }) => {
|
||||||
|
await setActiveProfile(page, HENDRIK_ID);
|
||||||
|
await page.goto('/wishlist');
|
||||||
|
// Die API zaehlt die Wunschlisten-Rezepte — der Header-Badge sollte
|
||||||
|
// die gleiche Zahl zeigen.
|
||||||
|
const res = await request.get('/api/wishlist?sort=popular');
|
||||||
|
const body = (await res.json()) as { entries: unknown[] };
|
||||||
|
const expected = body.entries.length;
|
||||||
|
if (expected === 0) {
|
||||||
|
// Kein Badge bei Null — der Link hat dann gar keine Zahl.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const badge = page.locator('a[href="/wishlist"]').first();
|
||||||
|
await expect(badge).toContainText(String(expected));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('requireProfile zeigt Custom-Message "um mitzuwuenschen"', async ({ page }) => {
|
||||||
|
await clearActiveProfile(page);
|
||||||
|
await page.goto('/wishlist');
|
||||||
|
// Erster "Ich will das auch"-Button eines beliebigen Eintrags.
|
||||||
|
// Falls Wunschliste leer ist, ueberspringen.
|
||||||
|
const btn = page.getByRole('button', { name: /Ich will das auch/i }).first();
|
||||||
|
const count = await btn.count();
|
||||||
|
test.skip(count === 0, 'Wunschliste leer — Custom-Message-Test uebersprungen');
|
||||||
|
|
||||||
|
await btn.click();
|
||||||
|
await expect(page.getByText('Kein Profil gewählt')).toBeVisible();
|
||||||
|
await expect(page.getByText('um mitzuwünschen')).toBeVisible();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -70,7 +70,8 @@ describe('recipe repository', () => {
|
|||||||
unit: 'g',
|
unit: 'g',
|
||||||
name: 'Pancetta',
|
name: 'Pancetta',
|
||||||
note: null,
|
note: null,
|
||||||
raw_text: '200 g Pancetta'
|
raw_text: '200 g Pancetta',
|
||||||
|
section_heading: null
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
tags: ['Italienisch']
|
tags: ['Italienisch']
|
||||||
@@ -118,13 +119,13 @@ describe('recipe repository', () => {
|
|||||||
baseRecipe({
|
baseRecipe({
|
||||||
title: 'Pasta',
|
title: 'Pasta',
|
||||||
ingredients: [
|
ingredients: [
|
||||||
{ position: 1, quantity: 200, unit: 'g', name: 'Pancetta', note: null, raw_text: '200 g Pancetta' }
|
{ position: 1, quantity: 200, unit: 'g', name: 'Pancetta', note: null, raw_text: '200 g Pancetta', section_heading: null }
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
replaceIngredients(db, id, [
|
replaceIngredients(db, id, [
|
||||||
{ position: 1, quantity: 500, unit: 'g', name: 'Nudeln', note: null, raw_text: '500 g Nudeln' },
|
{ position: 1, quantity: 500, unit: 'g', name: 'Nudeln', note: null, raw_text: '500 g Nudeln', section_heading: null },
|
||||||
{ position: 2, quantity: 2, unit: null, name: 'Eier', note: null, raw_text: '2 Eier' }
|
{ position: 2, quantity: 2, unit: null, name: 'Eier', note: null, raw_text: '2 Eier', section_heading: null }
|
||||||
]);
|
]);
|
||||||
const loaded = getRecipeById(db, id);
|
const loaded = getRecipeById(db, id);
|
||||||
expect(loaded?.ingredients.length).toBe(2);
|
expect(loaded?.ingredients.length).toBe(2);
|
||||||
@@ -154,4 +155,31 @@ describe('recipe repository', () => {
|
|||||||
const loaded = getRecipeById(db, id);
|
const loaded = getRecipeById(db, id);
|
||||||
expect(loaded?.steps.map((s) => s.text)).toEqual(['Erst', 'Dann']);
|
expect(loaded?.steps.map((s) => s.text)).toEqual(['Erst', 'Dann']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('persistiert section_heading und gibt es beim Laden zurueck', () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
const recipe = baseRecipe({
|
||||||
|
title: 'Torte',
|
||||||
|
ingredients: [
|
||||||
|
{ position: 1, quantity: 200, unit: 'g', name: 'Mehl', note: null, raw_text: '200 g Mehl', section_heading: 'Für den Teig' },
|
||||||
|
{ position: 2, quantity: 100, unit: 'g', name: 'Zucker', note: null, raw_text: '100 g Zucker', section_heading: null },
|
||||||
|
{ position: 3, quantity: 300, unit: 'g', name: 'Beeren', note: null, raw_text: '300 g Beeren', section_heading: 'Für die Füllung' }
|
||||||
|
]
|
||||||
|
});
|
||||||
|
const id = insertRecipe(db, recipe);
|
||||||
|
const loaded = getRecipeById(db, id);
|
||||||
|
expect(loaded!.ingredients[0].section_heading).toBe('Für den Teig');
|
||||||
|
expect(loaded!.ingredients[1].section_heading).toBeNull();
|
||||||
|
expect(loaded!.ingredients[2].section_heading).toBe('Für die Füllung');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('replaceIngredients persistiert section_heading', () => {
|
||||||
|
const db = openInMemoryForTest();
|
||||||
|
const id = insertRecipe(db, baseRecipe({ title: 'X' }));
|
||||||
|
replaceIngredients(db, id, [
|
||||||
|
{ position: 1, quantity: null, unit: null, name: 'A', note: null, raw_text: 'A', section_heading: 'Kopf' }
|
||||||
|
]);
|
||||||
|
const loaded = getRecipeById(db, id);
|
||||||
|
expect(loaded!.ingredients[0].section_heading).toBe('Kopf');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ describe('searchLocal', () => {
|
|||||||
recipe({
|
recipe({
|
||||||
title: 'Pasta',
|
title: 'Pasta',
|
||||||
ingredients: [
|
ingredients: [
|
||||||
{ position: 1, quantity: 200, unit: 'g', name: 'Pancetta', note: null, raw_text: '' }
|
{ position: 1, quantity: 200, unit: 'g', name: 'Pancetta', note: null, raw_text: '', section_heading: null }
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -6,14 +6,16 @@ describe('resolveStrategy', () => {
|
|||||||
expect(resolveStrategy({ url: '/images/favicon-abc.png', method: 'GET' })).toBe('images');
|
expect(resolveStrategy({ url: '/images/favicon-abc.png', method: 'GET' })).toBe('images');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('swr for recipe HTML pages', () => {
|
it('network-first for recipe HTML pages', () => {
|
||||||
expect(resolveStrategy({ url: '/recipes/42', method: 'GET' })).toBe('swr');
|
expect(resolveStrategy({ url: '/recipes/42', method: 'GET' })).toBe('network-first');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('swr for recipe API reads', () => {
|
it('network-first for recipe API reads', () => {
|
||||||
expect(resolveStrategy({ url: '/api/recipes/42', method: 'GET' })).toBe('swr');
|
expect(resolveStrategy({ url: '/api/recipes/42', method: 'GET' })).toBe('network-first');
|
||||||
expect(resolveStrategy({ url: '/api/recipes/all?sort=name', method: 'GET' })).toBe('swr');
|
expect(resolveStrategy({ url: '/api/recipes/all?sort=name', method: 'GET' })).toBe(
|
||||||
expect(resolveStrategy({ url: '/api/wishlist', method: 'GET' })).toBe('swr');
|
'network-first'
|
||||||
|
);
|
||||||
|
expect(resolveStrategy({ url: '/api/wishlist', method: 'GET' })).toBe('network-first');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('network-only for write methods', () => {
|
it('network-only for write methods', () => {
|
||||||
@@ -34,8 +36,8 @@ describe('resolveStrategy', () => {
|
|||||||
expect(resolveStrategy({ url: '/manifest.webmanifest', method: 'GET' })).toBe('shell');
|
expect(resolveStrategy({ url: '/manifest.webmanifest', method: 'GET' })).toBe('shell');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('falls through to swr for other same-origin GETs (e.g. root page)', () => {
|
it('falls through to network-first for other same-origin GETs (e.g. root page)', () => {
|
||||||
expect(resolveStrategy({ url: '/', method: 'GET' })).toBe('swr');
|
expect(resolveStrategy({ url: '/', method: 'GET' })).toBe('network-first');
|
||||||
expect(resolveStrategy({ url: '/wishlist', method: 'GET' })).toBe('swr');
|
expect(resolveStrategy({ url: '/wishlist', method: 'GET' })).toBe('network-first');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ const mk = (q: number | null, unit: string | null, name: string): Ingredient =>
|
|||||||
unit,
|
unit,
|
||||||
name,
|
name,
|
||||||
note: null,
|
note: null,
|
||||||
raw_text: ''
|
raw_text: '',
|
||||||
|
section_heading: null
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('roundQuantity', () => {
|
describe('roundQuantity', () => {
|
||||||
@@ -40,4 +41,15 @@ describe('scaleIngredients', () => {
|
|||||||
const scaled = scaleIngredients([mk(100, 'g', 'Butter')], 1 / 3);
|
const scaled = scaleIngredients([mk(100, 'g', 'Butter')], 1 / 3);
|
||||||
expect(scaled[0].quantity).toBe(33);
|
expect(scaled[0].quantity).toBe(33);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('preserves section_heading through scaling', () => {
|
||||||
|
const input: Ingredient[] = [
|
||||||
|
{ position: 1, quantity: 200, unit: 'g', name: 'Mehl', note: null, raw_text: '200 g Mehl', section_heading: 'Teig' },
|
||||||
|
{ position: 2, quantity: null, unit: null, name: 'Ei', note: null, raw_text: 'Ei', section_heading: null }
|
||||||
|
];
|
||||||
|
const scaled = scaleIngredients(input, 2);
|
||||||
|
expect(scaled[0].section_heading).toBe('Teig');
|
||||||
|
expect(scaled[1].section_heading).toBeNull();
|
||||||
|
expect(scaled[0].quantity).toBe(400);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
264
tests/unit/search-store.test.ts
Normal file
264
tests/unit/search-store.test.ts
Normal file
@@ -0,0 +1,264 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||||
|
import { SearchStore, type SearchSnapshot } from '../../src/lib/client/search.svelte';
|
||||||
|
|
||||||
|
type FetchMock = ReturnType<typeof vi.fn>;
|
||||||
|
|
||||||
|
function mockFetch(responses: Array<{ ok?: boolean; status?: number; body: unknown }>): FetchMock {
|
||||||
|
const calls = [...responses];
|
||||||
|
return vi.fn(async () => {
|
||||||
|
const r = calls.shift();
|
||||||
|
if (!r) throw new Error('fetch called more times than expected');
|
||||||
|
return {
|
||||||
|
ok: r.ok ?? true,
|
||||||
|
status: r.status ?? 200,
|
||||||
|
json: async () => r.body
|
||||||
|
} as Response;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('SearchStore', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps results empty while query is <= 3 chars (debounced)', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 50 });
|
||||||
|
store.query = 'abc';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(100);
|
||||||
|
expect(store.searching).toBe(false);
|
||||||
|
expect(fetchImpl).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fires local search after debounce when query > 3 chars', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [{ id: 1, title: 'Pasta', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }] } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 50, pageSize: 30 });
|
||||||
|
store.query = 'pasta';
|
||||||
|
store.runDebounced();
|
||||||
|
expect(store.searching).toBe(true);
|
||||||
|
await vi.advanceTimersByTimeAsync(100);
|
||||||
|
await vi.waitFor(() => expect(fetchImpl).toHaveBeenCalled());
|
||||||
|
expect(fetchImpl.mock.calls[0][0]).toMatch(/\/api\/recipes\/search\?q=pasta&limit=30/);
|
||||||
|
expect(store.hits).toHaveLength(1);
|
||||||
|
expect(store.searchedFor).toBe('pasta');
|
||||||
|
expect(store.localExhausted).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to web search when local returns zero hits', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [] } },
|
||||||
|
{ body: { hits: [{ url: 'https://chefkoch.de/x', title: 'Foo', domain: 'chefkoch.de', snippet: null, thumbnail: null }] } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 50 });
|
||||||
|
store.query = 'pizza';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(100);
|
||||||
|
await vi.waitFor(() => expect(store.webHits).toHaveLength(1));
|
||||||
|
expect(fetchImpl).toHaveBeenCalledTimes(2);
|
||||||
|
expect(fetchImpl.mock.calls[1][0]).toMatch(/\/api\/recipes\/search\/web\?q=pizza&pageno=1/);
|
||||||
|
expect(store.webPageno).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('race-guard: stale fetch response discarded when query was cleared/changed', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
let resolveFetch!: (v: Response) => void;
|
||||||
|
const fetchImpl = vi.fn(
|
||||||
|
() =>
|
||||||
|
new Promise<Response>((resolve) => {
|
||||||
|
resolveFetch = resolve;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 10 });
|
||||||
|
store.query = 'stale-query';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
expect(fetchImpl).toHaveBeenCalledTimes(1);
|
||||||
|
// User keeps typing BEFORE the response arrives — race-guard should kick in
|
||||||
|
// when the fetch finally resolves.
|
||||||
|
store.query = 'different';
|
||||||
|
resolveFetch({
|
||||||
|
ok: true,
|
||||||
|
status: 200,
|
||||||
|
json: async () => ({
|
||||||
|
hits: [
|
||||||
|
{
|
||||||
|
id: 99,
|
||||||
|
title: 'Stale',
|
||||||
|
description: null,
|
||||||
|
image_path: null,
|
||||||
|
source_domain: null,
|
||||||
|
avg_stars: null,
|
||||||
|
last_cooked_at: null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
} as Response);
|
||||||
|
// Flush microtasks so the awaited response + race-guard run.
|
||||||
|
await vi.runOnlyPendingTimersAsync();
|
||||||
|
await Promise.resolve();
|
||||||
|
await Promise.resolve();
|
||||||
|
expect(store.hits).toEqual([]);
|
||||||
|
expect(store.searchedFor).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('loadMore: drains local first (offset pagination)', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const page1 = Array.from({ length: 30 }, (_, i) => ({ id: i, title: `r${i}`, description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }));
|
||||||
|
const page2 = Array.from({ length: 5 }, (_, i) => ({ id: i + 30, title: `r${i + 30}`, description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }));
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: page1 } },
|
||||||
|
{ body: { hits: page2 } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 10, pageSize: 30 });
|
||||||
|
store.query = 'meal';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
await vi.waitFor(() => expect(store.hits).toHaveLength(30));
|
||||||
|
expect(store.localExhausted).toBe(false);
|
||||||
|
await store.loadMore();
|
||||||
|
expect(store.hits).toHaveLength(35);
|
||||||
|
expect(fetchImpl.mock.calls[1][0]).toMatch(/offset=30/);
|
||||||
|
expect(store.localExhausted).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('loadMore: switches to web pagination after local exhausted', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const local = [{ id: 1, title: 'local', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }];
|
||||||
|
const webP1 = [{ url: 'https://a.com', title: 'A', domain: 'a.com', snippet: null, thumbnail: null }];
|
||||||
|
const webP2 = [{ url: 'https://b.com', title: 'B', domain: 'b.com', snippet: null, thumbnail: null }];
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: local } },
|
||||||
|
{ body: { hits: webP1 } },
|
||||||
|
{ body: { hits: webP2 } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 10, pageSize: 30 });
|
||||||
|
store.query = 'soup';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
await vi.waitFor(() => expect(store.hits).toHaveLength(1));
|
||||||
|
expect(store.localExhausted).toBe(true);
|
||||||
|
await store.loadMore();
|
||||||
|
expect(store.webHits).toHaveLength(1);
|
||||||
|
await store.loadMore();
|
||||||
|
expect(store.webHits).toHaveLength(2);
|
||||||
|
expect(store.webPageno).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('web search error sets webError and marks webExhausted', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [] } },
|
||||||
|
{ ok: false, status: 502, body: { message: 'SearXNG unreachable' } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 10 });
|
||||||
|
store.query = 'anything';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
await vi.waitFor(() => expect(store.webError).toBe('SearXNG unreachable'));
|
||||||
|
expect(store.webExhausted).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reset(): clears query, results, and pending debounce', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 100 });
|
||||||
|
store.query = 'foobar';
|
||||||
|
store.runDebounced();
|
||||||
|
store.reset();
|
||||||
|
await vi.advanceTimersByTimeAsync(200);
|
||||||
|
expect(store.query).toBe('');
|
||||||
|
expect(store.hits).toEqual([]);
|
||||||
|
expect(fetchImpl).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('captureSnapshot / restoreSnapshot: round-trips without re-fetching', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 50 });
|
||||||
|
const snap: SearchSnapshot = {
|
||||||
|
query: 'lasagne',
|
||||||
|
hits: [{ id: 7, title: 'Lasagne', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }],
|
||||||
|
webHits: [],
|
||||||
|
searchedFor: 'lasagne',
|
||||||
|
webError: null,
|
||||||
|
localExhausted: true,
|
||||||
|
webPageno: 0,
|
||||||
|
webExhausted: false
|
||||||
|
};
|
||||||
|
store.restoreSnapshot(snap);
|
||||||
|
expect(store.query).toBe('lasagne');
|
||||||
|
expect(store.hits).toHaveLength(1);
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(100);
|
||||||
|
expect(fetchImpl).not.toHaveBeenCalled();
|
||||||
|
const round = store.captureSnapshot();
|
||||||
|
expect(round).toEqual(snap);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('filterParam option: gets appended to both local and web requests', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [] } },
|
||||||
|
{ body: { hits: [] } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({
|
||||||
|
fetchImpl,
|
||||||
|
debounceMs: 10,
|
||||||
|
filterParam: () => '&domains=chefkoch.de'
|
||||||
|
});
|
||||||
|
store.query = 'curry';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
await vi.waitFor(() => expect(fetchImpl).toHaveBeenCalledTimes(2));
|
||||||
|
expect(fetchImpl.mock.calls[0][0]).toMatch(/&domains=chefkoch\.de/);
|
||||||
|
expect(fetchImpl.mock.calls[1][0]).toMatch(/&domains=chefkoch\.de/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('runSearch(q) cancels pending debounce to avoid double-fetch', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [{ id: 1, title: 'immediate', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }] } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({ fetchImpl, debounceMs: 300 });
|
||||||
|
store.query = 'meal';
|
||||||
|
store.runDebounced(); // schedules the 300ms timer
|
||||||
|
// Before the timer fires, call runSearch immediately (e.g. form submit).
|
||||||
|
await store.runSearch('meal');
|
||||||
|
expect(fetchImpl).toHaveBeenCalledTimes(1);
|
||||||
|
// Now advance past the original debounce — timer must not still fire.
|
||||||
|
await vi.advanceTimersByTimeAsync(400);
|
||||||
|
expect(fetchImpl).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reSearch: immediate re-run with current query on filter change', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
let filter = '';
|
||||||
|
const fetchImpl = mockFetch([
|
||||||
|
{ body: { hits: [] } },
|
||||||
|
{ body: { hits: [] } },
|
||||||
|
{ body: { hits: [{ id: 1, title: 'filtered', description: null, image_path: null, source_domain: null, avg_stars: null, last_cooked_at: null }] } }
|
||||||
|
]);
|
||||||
|
const store = new SearchStore({
|
||||||
|
fetchImpl,
|
||||||
|
debounceMs: 10,
|
||||||
|
filterDebounceMs: 5,
|
||||||
|
filterParam: () => filter
|
||||||
|
});
|
||||||
|
store.query = 'broth';
|
||||||
|
store.runDebounced();
|
||||||
|
await vi.advanceTimersByTimeAsync(15);
|
||||||
|
filter = '&domains=chefkoch.de';
|
||||||
|
store.reSearch();
|
||||||
|
await vi.advanceTimersByTimeAsync(10);
|
||||||
|
await vi.waitFor(() => expect(store.hits).toHaveLength(1));
|
||||||
|
const last = fetchImpl.mock.calls.at(-1)?.[0] as string;
|
||||||
|
expect(last).toMatch(/&domains=chefkoch\.de/);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user