diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-07-27 21:00:10 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2026-07-27 21:00:10 +0200 |
| commit | bc3678461c3000c2394e355b7aaa73b774a33fff (patch) | |
| tree | a08dd1ff29e16b07326a8cdb5d253a210033a61d /src/routes/+page.server.ts | |
| parent | 5f423e365ada8aae63ad07099031a23252f47514 (diff) | |
feat: compact sticky add form with Add + Edit buttons
- Sticky navbar + sticky add bar on list page
- Add button: quick add to list
- Edit button: add + redirect to detail page for references
- Removed layout top padding for seamless sticky behavior
Diffstat (limited to 'src/routes/+page.server.ts')
| -rw-r--r-- | src/routes/+page.server.ts | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts index d548d09..55ac82d 100644 --- a/src/routes/+page.server.ts +++ b/src/routes/+page.server.ts @@ -1,4 +1,4 @@ -import { fail } from '@sveltejs/kit'; +import { fail, redirect } from '@sveltejs/kit'; import { db } from '$lib/server/db'; import { groceries, categories, references } from '$lib/server/db/schema'; import { eq, desc } from 'drizzle-orm'; @@ -87,4 +87,41 @@ export const actions: Actions = { return { success: true }; }, + + addAndEdit: async ({ request }) => { + const data = await request.formData(); + const name = data.get('name')?.toString().trim(); + const categoryId = data.get('category_id')?.toString() || null; + + if (!name) { + return fail(400, { error: 'Name is required' }); + } + + const [existing] = await db + .select() + .from(groceries) + .where(eq(groceries.name, name)) + .limit(1); + + if (existing) { + await db + .update(groceries) + .set({ + checked: false, + categoryId: categoryId ? Number(categoryId) : existing.categoryId, + }) + .where(eq(groceries.id, existing.id)); + throw redirect(303, `/grocery/${existing.id}`); + } else { + const [inserted] = await db + .insert(groceries) + .values({ + name, + categoryId: categoryId ? Number(categoryId) : null, + createdAt: new Date().toISOString(), + }) + .returning(); + throw redirect(303, `/grocery/${inserted.id}`); + } + }, }; |
