import { Link, useLoaderData } from "react-router"; import { requireAdmin } from "@/session.server"; import prisma from "@/lib/prisma.server"; import { Badge } from "@/components/ui/badge"; import { Building2, Archive, Trash2 } from "lucide-react"; import { useState } from "react"; export async function loader({ request }: { request: Request }) { await requireAdmin(request); const companies = await prisma.company.findMany({ include: { user: { select: { id: true, name: true, email: true } }, _count: { select: { invoices: true, customers: true } }, }, orderBy: [{ archived: "asc" }, { name: "asc" }], }); return { companies: companies.map((c) => ({ ...c, archivedAt: c.archivedAt?.toISOString() ?? null, })), }; } export default function AdminMandanten() { const { companies } = useLoaderData(); const active = companies.filter((c) => !c.archived); const archived = companies.filter((c) => c.archived); return (

Alle Mandanten

{companies.length} Mandanten gesamt · {active.length} aktiv · {archived.length} archiviert

{archived.length > 0 && (
)}
); } type Company = { id: string; name: string; legalForm: string | null; city: string; email: string | null; archived: boolean; user: { id: string; name: string; email: string }; _count: { invoices: number; customers: number }; }; function MandantenTabelle({ companies, title, archived = false, }: { companies: Company[]; title: string; archived?: boolean; }) { const [deleteConfirm, setDeleteConfirm] = useState(null); const [isDeleting, setIsDeleting] = useState(false); const handleDelete = async (companyId: string, companyName: string) => { if (deleteConfirm !== companyId) { setDeleteConfirm(companyId); return; } setIsDeleting(true); try { const response = await fetch(`/api/admin/companies/${companyId}/delete`, { method: "DELETE", }); if (response.ok) { // Reload the page to refresh the list window.location.reload(); } else { const error = await response.json(); alert(`Fehler beim Löschen: ${error.error || response.statusText}`); setDeleteConfirm(null); } } catch (error) { alert(`Fehler beim Löschen: ${error}`); setDeleteConfirm(null); } finally { setIsDeleting(false); } }; if (companies.length === 0) return null; return (

{title}

{companies.map((company) => ( ))}
Mandant Ort Benutzer Rechnungen Kunden
{company.name} {archived && ( )}
{company.legalForm && (
{company.legalForm}
)}
{company.city}
{company.user.name}
{company.user.email}
{company._count.invoices} {company._count.customers} Öffnen →
); }