diff --git a/frontend/src/routes/persons/new/+page.server.ts b/frontend/src/routes/persons/new/+page.server.ts index eae1424c..367cc4fc 100644 --- a/frontend/src/routes/persons/new/+page.server.ts +++ b/frontend/src/routes/persons/new/+page.server.ts @@ -1,5 +1,6 @@ import { error, fail, redirect } from '@sveltejs/kit'; import { createApiClient } from '$lib/api.server'; +import { getErrorMessage } from '$lib/errors'; export async function load({ locals }: { locals: App.Locals }) { const canWrite = @@ -12,6 +13,12 @@ export async function load({ locals }: { locals: App.Locals }) { export const actions = { default: async ({ request, fetch }) => { const formData = await request.formData(); + const personType = (formData.get('personType')?.toString() ?? 'PERSON') as + | 'PERSON' + | 'INSTITUTION' + | 'GROUP' + | 'UNKNOWN'; + const title = formData.get('title')?.toString().trim() || undefined; const firstName = formData.get('firstName')?.toString().trim(); const lastName = formData.get('lastName')?.toString().trim(); const alias = formData.get('alias')?.toString().trim() || undefined; @@ -19,8 +26,25 @@ export const actions = { const deathYearStr = formData.get('deathYear')?.toString().trim(); const notes = formData.get('notes')?.toString().trim() || undefined; - if (!firstName || !lastName) { - return fail(400, { error: 'Vor- und Nachname sind Pflichtfelder.' }); + if (!lastName) { + return fail(400, { + error: 'Nachname ist Pflichtfeld.', + personType, + title, + firstName, + lastName: '', + alias + }); + } + if (personType === 'PERSON' && !firstName) { + return fail(400, { + error: 'Vorname ist Pflichtfeld.', + personType, + title, + firstName: '', + lastName, + alias + }); } const birthYear = birthYearStr ? parseInt(birthYearStr, 10) : undefined; @@ -29,8 +53,10 @@ export const actions = { const api = createApiClient(fetch); const result = await api.POST('/api/persons', { body: { - firstName, - lastName, + personType, + ...(title ? { title } : {}), + ...(firstName ? { firstName } : {}), + lastName: lastName!, ...(alias ? { alias } : {}), ...(birthYear ? { birthYear } : {}), ...(deathYear ? { deathYear } : {}), @@ -39,7 +65,15 @@ export const actions = { }); if (!result.response.ok) { - return fail(result.response.status, { error: 'Person konnte nicht gespeichert werden.' }); + const code = (result.error as unknown as { code?: string })?.code; + return fail(result.response.status, { + error: getErrorMessage(code), + personType, + title, + firstName, + lastName: lastName!, + alias + }); } throw redirect(303, `/persons/${result.data!.id}`); diff --git a/frontend/src/routes/persons/new/+page.svelte b/frontend/src/routes/persons/new/+page.svelte index a6387ac5..cda6e7f7 100644 --- a/frontend/src/routes/persons/new/+page.svelte +++ b/frontend/src/routes/persons/new/+page.svelte @@ -1,11 +1,35 @@
-

{m.persons_new_heading()}

@@ -22,79 +46,92 @@ let { form } = $props();
-
- - +

{m.form_label_person_type()}

+ (selectedType = type)} />
-
- + {#if isPerson} +
+ + +
+
+ + +
+ {/if} + +
+
+ {#if isPerson} +
+ + +
+
+ + +
+
+ + +
+ {/if} +
- - -
- -
- - -
- -
- - -
- -
- +