Ansprechpartner: komfortable, wiederverwendbare Auswahl

- Auswahlfeld mit Suche, Foto und Funktion; echte Checkboxen (Tastatur,
  Screenreader) und Ansage beim Hinzufügen/Entfernen
- Reihenfolge der gewählten Personen änderbar
- Neue Person direkt aus dem Feld anlegen und hinzufügen
- Personenseite zeigt „Verwendet in“ (Seiten und Meldungen)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-30 16:01:20 +00:00
parent 7da1a10af2
commit ded828ddef
7 changed files with 224 additions and 46 deletions

View file

@ -7,13 +7,22 @@ import { fields, parseId } from '$lib/server/forms';
import { nextSortOrder } from '$lib/server/sorting';
import type { MediaItem } from '$lib/types';
/** Wo wird diese Person angezeigt? */
function usage(id: number) {
const hit = (ids: number[]) => ids.includes(id);
return [
...db.select({ id: pages.id, title: pages.title, contactIds: pages.contactIds }).from(pages).all().filter((p) => hit(p.contactIds)).map((p) => ({ href: `/admin/seiten/${p.id}`, title: p.title, kind: 'Seite' })),
...db.select({ id: posts.id, title: posts.title, contactIds: posts.contactIds }).from(posts).all().filter((p) => hit(p.contactIds)).map((p) => ({ href: `/admin/meldungen/${p.id}`, title: p.title, kind: 'Meldung' }))
];
}
export const load = ({ params }) => {
const id = parseId(params.id);
if (id === 'new')
return { isNew: true, contact: { id: 0, name: '', role: '', location: '', email: '', phone: '', listed: true, photo: null as MediaItem | null } };
return { isNew: true, usage: [] as ReturnType<typeof usage>, contact: { id: 0, name: '', role: '', location: '', email: '', phone: '', listed: true, photo: null as MediaItem | null } };
const c = db.select().from(contacts).where(eq(contacts.id, id)).get();
if (!c) error(404);
return { isNew: false, contact: { ...c, photo: getMedia(c.photoId) } };
return { isNew: false, usage: usage(c.id), contact: { ...c, photo: getMedia(c.photoId) } };
};
export const actions = {

View file

@ -42,6 +42,24 @@
<ImageField name="photoId" value={c.photo} label="Foto" aspect="aspect-square" />
<Switch name="listed" checked={c.listed} label="Im Verzeichnis zeigen" hint="Unter „Ansprechpartner“ in der App" />
</Panel>
{#if !data.isNew}
<Panel title="Verwendet in" description="Hier erscheint die Person. Änderungen gelten überall.">
{#if data.usage.length}
<ul class="space-y-1.5 -mt-2">
{#each data.usage as u (u.href)}
<li>
<a href={u.href} class="flex items-baseline gap-2 text-sm hover:text-brand-ink">
<span class="text-xs font-bold uppercase tracking-wider text-ink-3 w-16 shrink-0">{u.kind}</span>
<span class="font-semibold">{u.title}</span>
</a>
</li>
{/each}
</ul>
{:else}
<p class="text-sm text-ink-3 -mt-2">Noch bei keiner Seite oder Meldung eingetragen.</p>
{/if}
</Panel>
{/if}
{/snippet}
</EditForm>
{/key}

View file

@ -1,8 +1,8 @@
import { error, fail, redirect } from '@sveltejs/kit';
import { asc, eq } from 'drizzle-orm';
import { eq } from 'drizzle-orm';
import { db } from '$lib/server/db';
import { contacts, posts } from '$lib/server/db/schema';
import { attachmentsOf, getMedia, mediaByIds } from '$lib/server/content';
import { posts } from '$lib/server/db/schema';
import { attachmentsOf, contactOptions as listContactOptions, getMedia, mediaByIds } from '$lib/server/content';
import { fields, parseId, uniqueSlug } from '$lib/server/forms';
import { cleanHtml } from '$lib/server/sanitize';
import { fromLocalInput, toLocalInput } from '$lib/dates';
@ -10,11 +10,7 @@ import type { MediaItem } from '$lib/types';
export const load = ({ params }) => {
const id = parseId(params.id);
const contactOptions = db
.select({ id: contacts.id, name: contacts.name, role: contacts.role })
.from(contacts)
.orderBy(asc(contacts.name))
.all();
const contactOptions = listContactOptions();
if (id === 'new') {
return {

View file

@ -1,8 +1,8 @@
import { error, fail, redirect } from '@sveltejs/kit';
import { asc, eq, isNull } from 'drizzle-orm';
import { eq, isNull } from 'drizzle-orm';
import { db } from '$lib/server/db';
import { contacts, pages } from '$lib/server/db/schema';
import { attachmentsOf, getMedia } from '$lib/server/content';
import { pages } from '$lib/server/db/schema';
import { attachmentsOf, contactOptions as listContactOptions, getMedia } from '$lib/server/content';
import { fields, parseId, uniqueSlug } from '$lib/server/forms';
import { cleanHtml } from '$lib/server/sanitize';
import { descendantsOf, pageTree } from '$lib/server/pageTree';
@ -13,7 +13,7 @@ import type { MediaItem } from '$lib/types';
export const load = ({ params, url }) => {
const id = parseId(params.id);
const contactOptions = db.select({ id: contacts.id, name: contacts.name, role: contacts.role }).from(contacts).orderBy(asc(contacts.name)).all();
const contactOptions = listContactOptions();
const excluded = id === 'new' ? new Set<number>() : descendantsOf(id);
const parentOptions = pageTree()
.filter((r) => !excluded.has(r.id))

View file

@ -0,0 +1,21 @@
import { error, json } from '@sveltejs/kit';
import { db } from '$lib/server/db';
import { contacts } from '$lib/server/db/schema';
import { contactsByIds } from '$lib/server/content';
import { nextSortOrder } from '$lib/server/sorting';
/** Ansprechpartner direkt aus einem Auswahlfeld heraus anlegen */
export const POST = async ({ request }) => {
const body = await request.json().catch(() => null);
const str = (v: unknown, max = 200) => (typeof v === 'string' ? v.trim().slice(0, max) : '');
const name = str(body?.name);
const email = str(body?.email);
if (!name) error(400, 'Bitte gib einen Namen ein.');
if (email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) error(400, 'Die E-Mail-Adresse sieht nicht richtig aus.');
const created = db
.insert(contacts)
.values({ name, role: str(body?.role), location: str(body?.location), email, phone: str(body?.phone, 50), sortOrder: nextSortOrder(contacts) })
.returning({ id: contacts.id })
.get();
return json(contactsByIds([created.id])[0]);
};