aboutsummaryrefslogtreecommitdiff
path: root/src/routes/catalog/+page.svelte
blob: 4b43c02f87a01e3ea56704450d0d4b49b6685a67 (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

<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>

{#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}">&rarr;</a>
					</li>
				{/each}
			</ul>
		{/each}
	</form>
{/if}

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

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