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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
<script lang="ts">
import { deserialize } from '$app/forms';
import { goto } from '$app/navigation';
import { enhance } from '$app/forms';
let { data, form } = $props();
let name = $state('');
let inputRef: HTMLInputElement;
// eslint-disable-next-line svelte/prefer-writable-derived
let localItems = $state(data?.items ?? []);
let addError = $state('');
// Sync local list with server data when it changes externally (toggle, navigation)
$effect(() => {
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';
return;
}
addError = '';
const formData = new FormData();
formData.append('name', name);
const action = redirectToEdit ? '?/addAndEdit' : '?/add';
name = '';
inputRef.focus();
try {
const response = await fetch(action, {
method: 'POST',
headers: {
accept: 'application/json',
'x-sveltekit-action': 'true',
},
body: formData,
});
const result = deserialize(await response.text());
if (result.type === 'error') {
addError =
typeof result.error === 'string'
? result.error
: result.error?.message || 'Erreur';
return;
}
if (result.type === 'redirect') {
goto(result.location);
return;
}
if (result.type === 'success' && result.data?.items) {
localItems = result.data.items;
}
} catch (err) {
addError = 'Erreur réseau';
console.error(err);
}
}
</script>
<div class="add-bar">
<input
bind:this={inputRef}
class="form-input"
type="text"
bind:value={name}
placeholder="Ajouter un article..."
autocomplete="off"
list="item-suggestions"
onkeydown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
handleAdd(false);
}
}}
oninput={(e) => {
const val = e.currentTarget.value.trim().toLowerCase();
const match = data.catalog.find(
(item) => item.name.toLowerCase() === val,
);
if (match) {
name = match.name;
handleAdd(false);
}
}}
/>
<datalist id="item-suggestions">
{#each data.catalog as item (item.id)}
<option value={item.name}></option>
{/each}
</datalist>
<button type="button" aria-label="Ajouter" onclick={() => handleAdd(false)}
>+</button
>
<button type="button" aria-label="Modifier" onclick={() => handleAdd(true)}
>→</button
>
</div>
<main>
{#if addError || form?.error}
<p class="error">{addError || form?.error}</p>
{/if}
{#if localItems.length === 0}
<p class="empty">Rien à acheter</p>
{:else}
<form method="POST" action="?/toggle" use:enhance>
{#each groupByCategory(localItems) 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">
{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>
{/if}
</span>
</button>
<a class="ref-link" href="/grocery/{item.id}?from=/">→</a>
</li>
{/each}
</ul>
{/each}
</form>
{/if}
</main>
<style>
.add-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;
}
.add-bar input {
flex: 1;
background: var(--surface);
}
.add-bar button {
width: 3rem;
height: 3rem;
flex-shrink: 0;
padding: 0;
border: none;
border-radius: 0.75rem;
font-size: 1.25rem;
font-weight: 500;
font-family: inherit;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s ease;
}
.add-bar > button:nth-of-type(1) {
background: var(--accent);
color: var(--white);
box-shadow: 0 1px 2px var(--shadow);
}
.add-bar > button:nth-of-type(1):hover {
background: var(--accent-hover);
transform: translateY(-1px);
box-shadow: 0 4px 8px var(--shadow-accent-lg);
}
.add-bar > button:nth-of-type(2) {
background: var(--surface);
color: var(--text-secondary);
border: 1.5px solid var(--border);
}
.add-bar > button:nth-of-type(2):hover {
border-color: var(--accent);
color: var(--text);
}
.error {
margin-bottom: 1rem;
}
.empty {
margin-top: 1rem;
padding: 2rem;
}
</style>
|