aboutsummaryrefslogtreecommitdiff
path: root/src/routes/groceries/catalog/+page.svelte
blob: 4cbcc2771aa79313e919abab3cf577d89b68c078 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

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

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

	let catalogItems = $derived(data?.items ?? []);
	let selectedIds = new SvelteSet<number>();
	let searchQuery = $state('');

	let filteredItems = $derived(
		searchQuery.trim() === ''
			? catalogItems
			: catalogItems.filter((item) =>
					item.name.toLowerCase().includes(searchQuery.toLowerCase()),
				),
	);

	function toggleId(id: number) {
		if (selectedIds.has(id)) selectedIds.delete(id);
		else selectedIds.add(id);
	}

	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);
		}
		const result: [string, typeof catalogItems][] = [];
		for (const cat of data.categories) {
			if (groups[cat.name]?.length > 0) {
				result.push([cat.name, groups[cat.name]]);
			}
		}
		if (groups['Non catégorisé']?.length > 0) {
			result.unshift(['Non catégorisé', groups['Non catégorisé']]);
		}
		return result;
	}
</script>

{#if catalogItems.length > 0}
	<div class="bulk-bar">
		<form
			method="POST"
			action="?/bulkUpdateCategory"
			use:enhance={() => {
				return async ({ update }) => {
					await update({ reset: false });
					selectedIds.clear();
				};
			}}
		>
			{#each Array.from(selectedIds) as id (id)}
				<input type="hidden" name="ids" value={id} />
			{/each}
			<select name="category_id" class="form-select">
				<option value={-1}>Non catégorisé</option>
				{#each data.categories as cat (cat.id)}
					<option value={cat.id}>{cat.name}</option>
				{/each}
			</select>
			<button
				class="btn-primary"
				type="submit"
				disabled={selectedIds.size === 0}
			>
				Mettre à jour ({selectedIds.size})
			</button>
		</form>
	</div>
{/if}

<main>
	{#if form?.error}
		<p class="error">{form.error}</p>
	{/if}

	{#if catalogItems.length === 0}
		<p class="empty">Aucun article</p>
	{:else}
		<input
			type="search"
			bind:value={searchQuery}
			placeholder="Rechercher un article..."
			class="search-input"
		/>

		{#if filteredItems.length === 0}
			<p class="empty">Aucun résultat</p>
		{:else}
			<form method="POST" action="?/toggle" use:enhance>
				{#each groupByCategory(filteredItems) 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">
								<input
									type="checkbox"
									class="item-check"
									checked={selectedIds.has(item.id)}
									onchange={() => toggleId(item.id)}
								/>
								<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=/groceries/catalog">&rarr;</a
								>
							</li>
						{/each}
					</ul>
				{/each}
			</form>
		{/if}
	{/if}
</main>

<style>
	.bulk-bar {
		display: flex;
		gap: 0.5rem;
		padding: 0.5rem 1rem;
		position: sticky;
		top: var(--nav-height);
		background: var(--bg);
		border-bottom: 1px solid var(--border);
		z-index: 10;
	}

	.bulk-bar form {
		display: flex;
		gap: 0.5rem;
		align-items: center;
		width: 100%;
	}

	.bulk-bar .form-select {
		flex: 1;
		min-width: 0;
	}

	.bulk-bar .btn-primary {
		padding: 0.75rem 1rem;
		align-self: center;
		white-space: nowrap;
	}

	.bulk-bar .btn-primary:disabled {
		opacity: 0.5;
		cursor: not-allowed;
		transform: none;
		box-shadow: none;
	}

	.item-check {
		width: 1.25rem;
		height: 1.25rem;
		accent-color: var(--accent);
		cursor: pointer;
		flex-shrink: 0;
	}

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

	.error {
		margin-bottom: 1rem;
	}

	.empty {
		margin-top: 1rem;
		padding: 2rem;
	}

	.search-input {
		width: 100%;
		padding: 0.75rem 1rem;
		border: 1px solid var(--border);
		border-radius: 0.5rem;
		background: var(--surface);
		color: var(--text);
		font-size: 1rem;
		margin-top: 1rem;
	}
</style>