diff --git a/src/lib/admin/ContactsField.svelte b/src/lib/admin/ContactsField.svelte index f655522..e15a027 100644 --- a/src/lib/admin/ContactsField.svelte +++ b/src/lib/admin/ContactsField.svelte @@ -2,51 +2,179 @@ import { untrack } from 'svelte'; import X from '@lucide/svelte/icons/x'; import Plus from '@lucide/svelte/icons/plus'; + import Search from '@lucide/svelte/icons/search'; + import ChevronUp from '@lucide/svelte/icons/chevron-up'; + import ChevronDown from '@lucide/svelte/icons/chevron-down'; + import UserPlus from '@lucide/svelte/icons/user-plus'; + import Check from '@lucide/svelte/icons/check'; + import { toasts } from './toast.svelte'; + import type { ContactCard } from '$lib/types'; - type Option = { id: number; name: string; role: string }; - let { name, options, value = [] }: { name: string; options: Option[]; value?: number[] } = $props(); + /** + * Ansprechpartner auswählen – einmal unter „Ansprechpartner“ angelegt, + * überall wiederverwendbar. Neue Personen lassen sich direkt hier anlegen. + */ + let { name, options, value = [] }: { name: string; options: ContactCard[]; value?: number[] } = $props(); + let all = $state(untrack(() => [...options])); let selected = $state(untrack(() => value)); - let adding = $state(''); + let open = $state(false); + let q = $state(''); + let creating = $state(false); + let draft = $state({ name: '', role: '', email: '', phone: '' }); + let saving = $state(false); + let announce = $state(''); - const byId = $derived(new Map(options.map((o) => [o.id, o]))); - const available = $derived(options.filter((o) => !selected.includes(o.id))); + const uid = $props.id(); + const byId = $derived(new Map(all.map((o) => [o.id, o]))); + const filtered = $derived( + all.filter((o) => [o.name, o.role, o.location].join(' ').toLowerCase().includes(q.trim().toLowerCase())) + ); + + function toggle(id: number, on: boolean) { + selected = on ? [...selected, id] : selected.filter((x) => x !== id); + announce = `${byId.get(id)?.name} ${on ? 'hinzugefügt' : 'entfernt'}`; + } + + function move(i: number, d: number) { + const next = [...selected]; + [next[i], next[i + d]] = [next[i + d], next[i]]; + selected = next; + announce = `${byId.get(next[i + d])?.name} auf Position ${i + d + 1} verschoben`; + } + + async function create() { + if (!draft.name.trim()) return; + saving = true; + const res = await fetch('/admin/api/ansprechpartner', { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify(draft) + }); + saving = false; + if (!res.ok) { + toasts.show((await res.json().catch(() => null))?.message ?? 'Anlegen fehlgeschlagen.', 'error'); + return; + } + const contact: ContactCard = await res.json(); + all = [...all, contact].sort((a, b) => a.name.localeCompare(b.name, 'de')); + selected = [...selected, contact.id]; + draft = { name: '', role: '', email: '', phone: '' }; + creating = false; + toasts.show(`${contact.name} angelegt und hinzugefügt`); + } + + const initialsOf = (n: string) => + n + .split(/\s+/) + .map((w) => w[0]) + .slice(0, 2) + .join(''); -
- Ansprechpartner +
+ Ansprechpartner +

{announce}

+ {#if selected.length} -
    - {#each selected as id (id)} +
      + {#each selected as id, i (id)} {@const c = byId.get(id)} {#if c} -
    1. - {c.name} - +
    2. + {#if c.photo} + + {:else} + + {/if} + + {c.name} + {#if c.role || c.location}{[c.role, c.location].filter(Boolean).join(' · ')}{/if} + + {#if selected.length > 1} + + + + + {/if} +
    3. {/if} {/each} -
+ {/if} - {#if available.length} -
- - + + + + {#if open} +
+ {#if all.length > 5} + + {/if} + + {#if filtered.length} +
    + {#each filtered as c (c.id)} + {@const on = selected.includes(c.id)} +
  • + +
  • + {/each} +
+ {:else} +

{all.length ? 'Niemand gefunden.' : 'Noch keine Ansprechpartner angelegt.'}

+ {/if} + + {#if creating} +
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+

Foto und Standort kannst du später unter „Ansprechpartner“ ergänzen.

+
+ + +
+
+ {:else} + + {/if}
- {:else if !options.length} -

Lege zuerst unter „Ansprechpartner“ Personen an.

{/if} -
+
diff --git a/src/lib/server/content.ts b/src/lib/server/content.ts index 7e9daa8..a2935b3 100644 --- a/src/lib/server/content.ts +++ b/src/lib/server/content.ts @@ -50,6 +50,12 @@ export function contactsByIds(ids: number[]): ContactCard[] { })); } +/** Alle Ansprechpartner für Auswahlfelder in der Redaktion (inkl. nicht gelisteter) */ +export function contactOptions() { + const rows = db.select({ id: contacts.id }).from(contacts).orderBy(asc(contacts.name)).all(); + return contactsByIds(rows.map((r) => r.id)); +} + export function listedContacts(): ContactCard[] { const rows = db .select({ id: contacts.id }) diff --git a/src/routes/admin/(panel)/ansprechpartner/[id]/+page.server.ts b/src/routes/admin/(panel)/ansprechpartner/[id]/+page.server.ts index 5da4e30..9c0a132 100644 --- a/src/routes/admin/(panel)/ansprechpartner/[id]/+page.server.ts +++ b/src/routes/admin/(panel)/ansprechpartner/[id]/+page.server.ts @@ -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, 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 = { diff --git a/src/routes/admin/(panel)/ansprechpartner/[id]/+page.svelte b/src/routes/admin/(panel)/ansprechpartner/[id]/+page.svelte index c9cdd35..914763e 100644 --- a/src/routes/admin/(panel)/ansprechpartner/[id]/+page.svelte +++ b/src/routes/admin/(panel)/ansprechpartner/[id]/+page.svelte @@ -42,6 +42,24 @@ + {#if !data.isNew} + + {#if data.usage.length} + + {:else} +

Noch bei keiner Seite oder Meldung eingetragen.

+ {/if} +
+ {/if} {/snippet} {/key} diff --git a/src/routes/admin/(panel)/meldungen/[id]/+page.server.ts b/src/routes/admin/(panel)/meldungen/[id]/+page.server.ts index 2aae072..e459728 100644 --- a/src/routes/admin/(panel)/meldungen/[id]/+page.server.ts +++ b/src/routes/admin/(panel)/meldungen/[id]/+page.server.ts @@ -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 { diff --git a/src/routes/admin/(panel)/seiten/[id]/+page.server.ts b/src/routes/admin/(panel)/seiten/[id]/+page.server.ts index b660260..20a3517 100644 --- a/src/routes/admin/(panel)/seiten/[id]/+page.server.ts +++ b/src/routes/admin/(panel)/seiten/[id]/+page.server.ts @@ -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() : descendantsOf(id); const parentOptions = pageTree() .filter((r) => !excluded.has(r.id)) diff --git a/src/routes/admin/api/ansprechpartner/+server.ts b/src/routes/admin/api/ansprechpartner/+server.ts new file mode 100644 index 0000000..2ee2cef --- /dev/null +++ b/src/routes/admin/api/ansprechpartner/+server.ts @@ -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]); +};