aboutsummaryrefslogtreecommitdiff
path: root/src/routes/catalog/+page.svelte
blob: 7d3ebbede461d4ea7a4293c0cc4a4134684d0c5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

<script lang="ts">
	import { enhance } from '$app/forms';

	let { data, form } = $props();

	let catalogItems = $derived(data?.items ?? []);

	function groupByCategory(items: typeof catalogItems) {
		const groups: Record<string, typeof catalogItems> = {};
		for (const item of items) {
			const key = item.categoryName ?? 'Non catégorisé';
			(groups[key] ??= []).push(item);
		}
		return Object.entries(groups);
	}
</script>

<main>
	{#if catalogItems.length === 0}
		<p class="empty">Aucun article</p>
	{:else}
		<form method="POST" action="?/toggle" use:enhance>
			{#each groupByCategory(catalogItems) as [cat, items] (cat)}
				<h3 class="category-name">{cat}</h3>
				<ul class="item-list">
					{#each items as item (item.id)}
						<li class="item-row item-row--fixed">
							<button
								class="item-btn item-btn--grow"
								name="id"
								value={item.id}
								type="submit"
								aria-pressed={item.checked}
							>
								<span class="item-text">
									<span class:checked={item.checked}>{item.name}</span>
									{#if item.bestRef}
										<span class="best-ref"
											>{item.bestRef.reference}
											{#if item.bestRef.price && item.bestRef.unit}
												&nbsp;{item.bestRef.price.toFixed(2)}
												{item.bestRef.unit}
											{/if}
											{#if item.bestRef.qualityScore != null}
												&nbsp;({item.bestRef.qualityScore}/100)
											{/if}
										</span>
									{/if}
								</span>
							</button>
							<a class="ref-link" href="/grocery/{item.id}?from=/catalog"
								>&rarr;</a
							>
						</li>
					{/each}
				</ul>
			{/each}
		</form>
	{/if}
</main>

<style>
	.checked {
		text-decoration: line-through;
		color: var(--text-secondary);
		font-weight: 400;
	}

	.empty {
		margin-top: 1rem;
		padding: 2rem;
	}
</style>