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:
35
frontend/src/routes/household/setup/+page.server.ts
Normal file
35
frontend/src/routes/household/setup/+page.server.ts
Normal 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;
|
||||
Reference in New Issue
Block a user