blob: 7a367c11113b57702e3b97532107d6a0592c066d (
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
|
<script lang="ts">
import { enhance } from '$app/forms';
let { data, form } = $props();
let name = $state('');
let listItems = $derived(data?.items ?? []);
function groupByCategory(items: typeof listItems) {
const groups: Record<string, typeof listItems> = {};
for (const item of items) {
const key = item.categoryName ?? 'Non catégorisé';
(groups[key] ??= []).push(item);
}
return Object.entries(groups);
}
</script>
<form class="add-bar" method="POST" action="?/add" use:enhance>
<input
class="form-input"
type="text"
name="name"
bind:value={name}
placeholder="Ajouter un article..."
autocomplete="off"
/>
<button type="submit" aria-label="Ajouter">+</button>
<button type="submit" formaction="?/addAndEdit" aria-label="Modifier"
>→</button
>
</form>
{#if form?.error}
<p class="error">{form.error}</p>
{/if}
{#if listItems.length === 0}
<p class="empty">Rien à acheter</p>
{:else}
<form method="POST" action="?/toggle" use:enhance>
{#each groupByCategory(listItems) 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}">→</a>
</li>
{/each}
</ul>
{/each}
</form>
{/if}
<style>
.add-bar {
display: flex;
gap: 0.5rem;
padding: 0.75rem 0;
position: sticky;
top: 3.1rem;
background: var(--bg);
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[type='submit']:not([formaction]) {
background: var(--accent);
color: var(--white);
box-shadow: 0 1px 2px var(--shadow);
}
.add-bar button[type='submit']:not([formaction]):hover {
background: var(--accent-hover);
transform: translateY(-1px);
box-shadow: 0 4px 8px var(--shadow-accent-lg);
}
.add-bar button[formaction] {
background: var(--surface);
color: var(--text-secondary);
border: 1.5px solid var(--border);
}
.add-bar button[formaction]:hover {
border-color: var(--accent);
color: var(--text);
}
.error {
margin-bottom: 1rem;
}
.empty {
margin-top: 1rem;
padding: 2rem;
}
</style>
|