Fails fast before the API call with a clear German error message. Tests boundary: 100 chars accepted, 101 rejected. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
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;
|