diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-07 19:30:50 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-09-07 19:30:50 +0200 |
| commit | 20a7a08e97e75c3a45b3430a74c2931554498f1c (patch) | |
| tree | d74bdeef3d426e563b2127942ab71ae31aaf0707 /src/routes/groceries/+page.svelte | |
| parent | 37573109a05a07ac50827759ea63793209baa73a (diff) | |
Extract shared groceries helpers and BestRef component
Diffstat (limited to 'src/routes/groceries/+page.svelte')
| -rw-r--r-- | src/routes/groceries/+page.svelte | 30 |
1 files changed, 4 insertions, 26 deletions
diff --git a/src/routes/groceries/+page.svelte b/src/routes/groceries/+page.svelte index 4471738..a159209 100644 --- a/src/routes/groceries/+page.svelte +++ b/src/routes/groceries/+page.svelte @@ -3,6 +3,8 @@ import { goto } from '$app/navigation'; import { enhance } from '$app/forms'; import FuzzyAutocomplete from '$lib/components/FuzzyAutocomplete.svelte'; + import BestRef from '$lib/components/BestRef.svelte'; + import { groupByCategory } from '$lib/shared/group-by-category'; import type { EnrichedGrocery } from '$lib/server/services/groceries'; let { data, form } = $props(); @@ -15,21 +17,6 @@ localItems = data?.items ?? []; }); - function groupByCategory(items: typeof localItems) { - const groups: Record<string, typeof localItems> = {}; - for (const item of items) { - const key = item.categoryName ?? 'Non catégorisé'; - (groups[key] ??= []).push(item); - } - const result: [string, typeof localItems][] = data.categories - .filter((cat) => groups[cat.name]?.length > 0) - .map((cat) => [cat.name, groups[cat.name]]); - if (groups['Non catégorisé']?.length > 0) { - result.unshift(['Non catégorisé', groups['Non catégorisé']]); - } - return result; - } - async function handleAdd(redirectToEdit: boolean) { if (!name.trim()) { addError = 'Le nom est requis'; @@ -105,7 +92,7 @@ <p class="empty">Rien à acheter</p> {:else} <form method="POST" action="?/toggle" use:enhance> - {#each groupByCategory(localItems) as [cat, items] (cat)} + {#each groupByCategory(localItems, data.categories) as [cat, items] (cat)} <h3 class="category-name">{cat}</h3> <ul class="item-list"> {#each items as item (item.id)} @@ -120,16 +107,7 @@ <span class="item-text"> {item.name} {#if item.bestRef} - <span class="best-ref" - >{item.bestRef.reference} - {#if item.bestRef.price && item.bestRef.unit} - {item.bestRef.price.toFixed(2)} - {item.bestRef.unit} - {/if} - {#if item.bestRef.qualityScore != null} - ({item.bestRef.qualityScore}/100) - {/if} - </span> + <BestRef ref={item.bestRef} /> {/if} </span> </button> |
