fix(ui): switch alias operations from client fetch to form actions
Some checks failed
CI / Unit & Component Tests (push) Failing after 1s
CI / Backend Unit Tests (push) Failing after 2s
CI / Unit & Component Tests (pull_request) Failing after 1s
CI / Backend Unit Tests (pull_request) Failing after 1s

Replaces raw client-side fetch with SvelteKit form actions
(addAlias, removeAlias) using the server-side API client for
proper auth handling. 10 new component tests for NameHistoryEditCard.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Marcel
2026-04-07 16:05:56 +02:00
parent 036843bf8f
commit e204ed89b6
4 changed files with 206 additions and 123 deletions

View File

@@ -1,10 +1,8 @@
<script lang="ts">
import { untrack } from 'svelte';
import { enhance } from '$app/forms';
import { m } from '$lib/paraglide/messages.js';
interface Props {
personId: string;
personFirstName: string;
aliases: Array<{
id: string;
lastName: string;
@@ -13,21 +11,16 @@ interface Props {
sortOrder: number;
}>;
canWrite: boolean;
aliasError?: string | null;
}
let { personId, personFirstName, aliases: initialAliases, canWrite }: Props = $props();
let { aliases, canWrite, aliasError = null }: Props = $props();
let aliases = $state.raw(untrack(() => initialAliases.map((a) => ({ ...a }))));
let sorted = $derived([...aliases].sort((a, b) => a.sortOrder - b.sortOrder));
let showDeleteModal = $state(false);
let deleteTargetId: string | null = $state(null);
let newType = $state('BIRTH');
let newLastName = $state('');
let newFirstName = $state('');
let addError = $state('');
function typeLabel(type: string): string {
switch (type) {
case 'BIRTH':
@@ -45,62 +38,6 @@ function confirmDelete(id: string) {
deleteTargetId = id;
showDeleteModal = true;
}
async function removeAlias() {
if (!deleteTargetId) return;
const aliasId = deleteTargetId;
try {
const res = await fetch(`/api/persons/${personId}/aliases/${aliasId}`, {
method: 'DELETE'
});
if (!res.ok) {
const body = await res.json().catch(() => null);
addError = body?.message ?? res.statusText;
return;
}
aliases = aliases.filter((a) => a.id !== aliasId);
} catch (e) {
addError = String(e);
} finally {
showDeleteModal = false;
deleteTargetId = null;
}
}
async function addAlias() {
addError = '';
const lastName = newLastName.trim();
if (!lastName) return;
try {
const res = await fetch(`/api/persons/${personId}/aliases`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
type: newType,
lastName,
firstName: newFirstName.trim() || null
})
});
if (!res.ok) {
const body = await res.json().catch(() => null);
addError = body?.message ?? res.statusText;
return;
}
const created = await res.json();
aliases = [...aliases, created];
newType = 'BIRTH';
newLastName = '';
newFirstName = '';
} catch (e) {
addError = String(e);
}
}
</script>
<div class="mt-6 rounded-sm border border-line bg-surface p-6 shadow-sm">
@@ -108,6 +45,10 @@ async function addAlias() {
{m.person_alias_heading()}
</h2>
{#if aliasError}
<p class="mb-3 text-sm text-red-600">{aliasError}</p>
{/if}
{#if sorted.length === 0}
<p class="text-sm text-ink-2 italic">{m.person_alias_empty()}</p>
{:else}
@@ -117,7 +58,7 @@ async function addAlias() {
<span>
<span class="text-ink-2 italic">{typeLabel(alias.type)}</span>
<span class="font-serif text-ink">
{alias.firstName ?? personFirstName}
{#if alias.firstName}{alias.firstName}{/if}
{alias.lastName}
</span>
</span>
@@ -155,52 +96,50 @@ async function addAlias() {
{m.person_alias_add_heading()}
</h3>
{#if addError}
<p class="mb-3 text-sm text-red-600">{addError}</p>
{/if}
<form method="POST" action="?/addAlias" use:enhance>
<div class="grid gap-3 md:grid-cols-2">
<label class="block">
<span class="text-xs font-medium text-ink-2">{m.person_alias_label_type()}</span>
<select
name="type"
class="mt-1 block w-full rounded-sm border border-line bg-surface px-3 py-2 text-sm text-ink focus:border-primary focus:outline-none"
>
<option value="BIRTH">{m.person_alias_type_BIRTH()}</option>
<option value="WIDOWED">{m.person_alias_type_WIDOWED()}</option>
<option value="DIVORCED">{m.person_alias_type_DIVORCED()}</option>
<option value="OTHER">{m.person_alias_type_OTHER()}</option>
</select>
</label>
<div class="grid gap-3 md:grid-cols-2">
<label class="block">
<span class="text-xs font-medium text-ink-2">{m.person_alias_label_type()}</span>
<select
bind:value={newType}
class="mt-1 block w-full rounded-sm border border-line bg-surface px-3 py-2 text-sm text-ink focus:border-brand-navy focus:outline-none"
>
<option value="BIRTH">{m.person_alias_type_BIRTH()}</option>
<option value="WIDOWED">{m.person_alias_type_WIDOWED()}</option>
<option value="DIVORCED">{m.person_alias_type_DIVORCED()}</option>
<option value="OTHER">{m.person_alias_type_OTHER()}</option>
</select>
</label>
<label class="block">
<span class="text-xs font-medium text-ink-2">{m.person_alias_label_last_name()}</span>
<input
type="text"
name="lastName"
required
class="mt-1 block w-full rounded-sm border border-line bg-surface px-3 py-2 text-sm text-ink focus:border-primary focus:outline-none"
/>
</label>
<label class="block">
<span class="text-xs font-medium text-ink-2">{m.person_alias_label_last_name()}</span>
<input
type="text"
bind:value={newLastName}
class="mt-1 block w-full rounded-sm border border-line bg-surface px-3 py-2 text-sm text-ink focus:border-brand-navy focus:outline-none"
/>
</label>
<label class="block">
<span class="text-xs font-medium text-ink-2">{m.person_alias_label_first_name()}</span>
<input
type="text"
name="firstName"
class="mt-1 block w-full rounded-sm border border-line bg-surface px-3 py-2 text-sm text-ink focus:border-primary focus:outline-none"
/>
</label>
<label class="block">
<span class="text-xs font-medium text-ink-2">{m.person_alias_label_first_name()}</span>
<input
type="text"
bind:value={newFirstName}
class="mt-1 block w-full rounded-sm border border-line bg-surface px-3 py-2 text-sm text-ink focus:border-brand-navy focus:outline-none"
/>
</label>
<div class="flex items-end">
<button
type="button"
onclick={addAlias}
class="w-full rounded-sm bg-brand-navy px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-brand-navy/90"
>
{m.person_alias_btn_add()}
</button>
<div class="flex items-end">
<button
type="submit"
class="w-full rounded-sm bg-primary px-4 py-2 text-sm font-medium text-primary-fg transition-colors hover:bg-primary/80"
>
{m.person_alias_btn_add()}
</button>
</div>
</div>
</div>
</form>
</div>
{/if}
</div>
@@ -213,18 +152,33 @@ async function addAlias() {
<div class="flex items-center justify-end gap-3">
<button
type="button"
onclick={() => { showDeleteModal = false; deleteTargetId = null; }}
class="rounded-sm border border-line px-4 py-2 text-sm font-medium text-ink-2 transition-colors hover:bg-gray-50"
onclick={() => {
showDeleteModal = false;
deleteTargetId = null;
}}
class="rounded-sm border border-line px-4 py-2 text-sm font-medium text-ink-2 transition-colors hover:bg-muted"
>
{m.btn_cancel()}
</button>
<button
type="button"
onclick={removeAlias}
class="rounded-sm bg-red-600 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-red-700"
<form
method="POST"
action="?/removeAlias"
use:enhance={() => {
return async ({ update }) => {
showDeleteModal = false;
deleteTargetId = null;
await update();
};
}}
>
{m.person_alias_btn_delete()}
</button>
<input type="hidden" name="aliasId" value={deleteTargetId} />
<button
type="submit"
class="rounded-sm bg-red-600 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-red-700"
>
{m.person_alias_btn_delete()}
</button>
</form>
</div>
</div>
</div>