aboutsummaryrefslogtreecommitdiff
path: root/src/routes/grocery
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2026-10-06 00:25:15 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2026-10-06 00:25:15 +0200
commitf7366834ee41bbb4ac8a9a252224241a884af41b (patch)
treeae7585a191353a82f968aed90277c2f21d4864f0 /src/routes/grocery
parentf6563f22a9a0c9d876f80e8f758b902dcc3bec78 (diff)
fix: save grocery category immediately on select change
Picking a category only committed on blur, so the save fired after focus left the select. Save on change instead, flush any pick made while a previous submit is in flight, and only re-sync fields when navigating to a different item so rapid re-picks aren't dropped or reverted by the post-save reload.
Diffstat (limited to 'src/routes/grocery')
-rw-r--r--src/routes/grocery/[id]/+page.svelte14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/routes/grocery/[id]/+page.svelte b/src/routes/grocery/[id]/+page.svelte
index 56e57fd..027c079 100644
--- a/src/routes/grocery/[id]/+page.svelte
+++ b/src/routes/grocery/[id]/+page.svelte
@@ -10,7 +10,12 @@
let isSubmitting = $state(false);
let formRef: HTMLFormElement;
+ // Only re-sync fields when navigating to a different item, so an edit made
+ // while a save is in flight isn't clobbered by the post-save reload.
+ let syncedId = -1;
$effect(() => {
+ if (data.item.id === syncedId) return;
+ syncedId = data.item.id;
name = data.item.name;
categoryId = data.item.categoryId ?? -1;
});
@@ -49,6 +54,8 @@
return async ({ update }) => {
await update({ reset: false });
isSubmitting = false;
+ // flush a change made while the previous submit was in flight
+ submitIfChanged();
};
}}
>
@@ -73,8 +80,11 @@
<select
class="form-select"
name="category_id"
- bind:value={categoryId}
- onblur={submitIfChanged}
+ value={categoryId}
+ onchange={(e) => {
+ categoryId = Number(e.currentTarget.value);
+ submitIfChanged();
+ }}
>
<option value={-1}>—</option>
{#each data.categories as cat (cat.id)}