feat(onboarding): add household setup page server action and load guard

Creates household via POST /v1/households, redirects to /household/staples.
Load guard redirects users who already have a household to /planner.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-02 19:14:39 +02:00
parent 175bfbe7dd
commit e85a7ca313
2 changed files with 172 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
import { redirect, fail } from '@sveltejs/kit';
import { apiClient } from '$lib/server/api';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ locals }) => {
if (locals.haushalt?.id) {
throw redirect(303, '/planner');
}
return {};
};
export const actions = {
default: async ({ request, fetch }) => {
const formData = await request.formData();
const name = (formData.get('name') ?? '').toString().trim();
if (!name) {
return fail(400, { errors: { name: 'Haushaltsname ist erforderlich' }, name: '' });
}
const api = apiClient(fetch);
const { data, error } = await api.POST('/v1/households', {
body: { name }
});
if (error || !data?.data) {
return fail(500, {
errors: { form: 'Haushalt konnte nicht erstellt werden. Bitte versuche es erneut.' },
name
});
}
throw redirect(303, '/household/staples');
}
} satisfies Actions;