blob: df1b97b0387b66293f01a9ce68041a8f74e5766e (
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
|
<script lang="ts">
import { deserialize } from '$app/forms';
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();
let name = $state('');
// eslint-disable-next-line svelte/prefer-writable-derived
let localItems = $state<EnrichedGrocery[]>([]);
let addError = $state('');
// Sync local list with server data when it changes externally (toggle, navigation)
$effect(() => {
localItems = data?.items ?? [];
});
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 = '';
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 as EnrichedGrocery[];
}
} catch (err) {
addError = 'Erreur réseau';
console.error(err);
}
}
</script>
<div class="grocery-add-bar">
<FuzzyAutocomplete
items={data.catalog}
bind:value={name}
placeholder="Ajouter un article..."
onselect={(item) => {
name = item.name;
handleAdd(false);
}}
/>
<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, data.categories) 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}
<BestRef ref={item.bestRef} />
{/if}
</span>
</button>
<a class="ref-link" href="/grocery/{item.id}?from=/groceries"
>→</a
>
</li>
{/each}
</ul>
{/each}
</form>
{/if}
</main>
<style>
.grocery-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;
}
.grocery-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;
}
.grocery-add-bar > button:nth-of-type(1) {
background: var(--accent);
color: var(--white);
box-shadow: 0 1px 2px var(--shadow);
}
.grocery-add-bar > button:nth-of-type(1):hover {
background: var(--accent-hover);
transform: translateY(-1px);
box-shadow: 0 4px 8px var(--shadow-accent-lg);
}
.grocery-add-bar > button:nth-of-type(2) {
background: var(--surface);
color: var(--text-secondary);
border: 1.5px solid var(--border);
}
.grocery-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>
|