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

<script lang="ts">
	import '../app.css';
	import { enhance } from '$app/forms';

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

	let listItems = $derived(data?.items?.filter((i) => !i.checked) ?? []);
	let catalogItems = $derived(data?.items?.filter((i) => i.checked) ?? []);
</script>

<h1>Groceries</h1>

<form class="add-form" method="POST" action="?/add" use:enhance>
	<input
		type="text"
		name="name"
		bind:value={name}
		placeholder="Add an item..."
		autocomplete="off"
	/>
	<button type="submit">Add</button>
</form>

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

<h2>List</h2>

<form method="POST" action="?/toggle" use:enhance>
	<ul class="item-list">
		{#each listItems as item (item.id)}
			<li>
				<button name="id" value={item.id} type="submit">
					{item.name}
				</button>
			</li>
		{/each}
	</ul>
</form>

{#if listItems.length === 0}
	<p class="empty">Nothing to buy</p>
{/if}

<hr />

<h2>Catalog</h2>

<form method="POST" action="?/toggle" use:enhance>
	<ul class="item-list">
		{#each catalogItems as item (item.id)}
			<li class="catalog-item">
				<button name="id" value={item.id} type="submit">
					{item.name}
				</button>
				<button
					class="delete-btn"
					name="id"
					value={item.id}
					type="submit"
					formaction="?/delete"
				>
					&times;
				</button>
			</li>
		{/each}
	</ul>
</form>

{#if catalogItems.length === 0}
	<p class="empty">No items yet</p>
{/if}

<style>
	.add-form {
		display: flex;
		gap: 0.5rem;
		margin-bottom: 1rem;
	}

	input {
		flex: 1;
		padding: 0.75rem;
		border: 1px solid #ccc;
		border-radius: 0.5rem;
		font-size: 1rem;
	}

	.add-form button[type='submit'] {
		padding: 0.75rem 1.25rem;
		border: none;
		border-radius: 0.5rem;
		background: #333;
		color: #fff;
		font-size: 1rem;
	}

	.error {
		color: #d00;
		margin-top: -0.5rem;
		margin-bottom: 1rem;
	}

	hr {
		border: none;
		border-top: 1px solid #ddd;
		margin: 1.5rem 0;
	}

	.item-list {
		list-style: none;
		display: flex;
		flex-direction: column;
		gap: 0.25rem;
	}

	.item-list li button {
		width: 100%;
		text-align: left;
		height: 2rem;
		padding: 0 0.75rem;
		border: 1px solid #e0e0e0;
		border-radius: 0.5rem;
		background: #fff;
		font-size: 1rem;
		display: flex;
		align-items: center;
		transition: background 0.15s;
	}

	.item-list li button:hover {
		background: #f5f5f5;
	}

	.catalog-item {
		display: flex;
		gap: 0.25rem;
		height: 2rem;
	}

	.catalog-item button:first-child {
		flex-grow: 1;
		width: auto;
		display: flex;
		align-items: center;
	}

	.catalog-item .delete-btn {
		border: 1px solid #e0e0e0;
		border-radius: 0.5rem;
		background: #fff;
		color: #d00;
		transition: background 0.15s;
		width: 2rem;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.catalog-item .delete-btn:hover {
		background: #fee;
	}

	.empty {
		color: #999;
		font-size: 0.875rem;
		margin-top: 0.5rem;
	}
</style>