/** * FEAT-2: Becken-Detailseite — Stammdaten, BELEGUNG (welche Tiere wohnen * aktuell hier, mit Farbschlag + Link zur Rennmaus), Löschen mit * Bestätigung; Konflikt (Becken nicht leer) wird deutsch gemeldet. */ import { useMemo, useState } from 'react' import { Link, useNavigate, useParams } from 'react-router-dom' import { de } from '../strings/de' import { ApiError } from '../api/client' import { deleteEnclosure, getEnclosure } from '../api/enclosures' import { listGerbils } from '../api/gerbils' import { listColorVarieties } from '../api/lookups' import { condition } from '../api/gridify' import { useApi, useMutation } from '../hooks/useApi' export default function BeckenDetailPage() { const t = de.pages.becken const { id = '' } = useParams() const navigate = useNavigate() const [deleteError, setDeleteError] = useState(null) const enclosure = useApi(() => getEnclosure(id), [id]) // Belegung: alle Tiere, deren aktuelles Becken dieses ist. const occupants = useApi( () => listGerbils({ filter: condition({ field: 'enclosureId', op: '==', value: id }), orderBy: 'name', page: 1, pageSize: 500, }), [id], ) const colorVarieties = useApi(() => listColorVarieties(), []) const colorNameById = useMemo( () => new Map((colorVarieties.data ?? []).map((c) => [c.id, c.name])), [colorVarieties.data], ) const removal = useMutation(() => deleteEnclosure(id)) async function onDelete() { if (!window.confirm(t.delete.confirmMessage)) return setDeleteError(null) const result = await removal.run() if (result.ok) { navigate('/gehege') } else if (result.cause instanceof ApiError && result.cause.status === 409) { // Backend meldet Konflikt, wenn noch Tiere im Becken wohnen. setDeleteError(t.delete.conflict) } else { setDeleteError(result.error) } } if (enclosure.loading) return

{de.common.loading}

if (enclosure.error || !enclosure.data) { return (

{enclosure.error ?? t.detail.notFound}

{t.detail.back}
) } const e = enclosure.data const animals = occupants.data?.items ?? [] const count = occupants.data?.totalCount ?? 0 return (

{e.name}

{occupants.data && (

{count} {count === 1 ? t.occupancy.countOne : t.occupancy.countMany}

)}
{t.detail.edit} {t.detail.back}
{deleteError &&
{deleteError}
} {e.notes && (
{t.fields.notes}
{e.notes}
)}

{t.occupancy.title}

{occupants.loading &&

{de.common.loading}

} {occupants.error && (
{occupants.error}
)} {!occupants.loading && !occupants.error && animals.length === 0 && (

{t.occupancy.empty}

)} {animals.length > 0 && ( )}
) }