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: '' }); } if (name.length > 100) { return fail(400, { errors: { name: 'Haushaltsname darf maximal 100 Zeichen lang sein' }, 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;