import { Link, useLoaderData, useNavigate, redirect } from "react-router"; export const handle = { breadcrumbs: (data: { company: { id: string; name: string } }) => [ { label: "Mandanten", href: "/companies" }, { label: data.company.name, href: `/companies/${data.company.id}` }, { label: "Rechnungen", href: `/companies/${data.company.id}/invoices` }, { label: "Neue Rechnung" }, ], }; import { requireUser } from "@/session.server"; import prisma from "@/lib/prisma"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { InvoiceForm } from "@/components/invoice/invoice-form"; import { ChevronLeft } from "lucide-react"; export async function loader({ request, params }: { request: Request; params: { id: string } }) { const user = await requireUser(request); const { id } = params; const company = await prisma.company.findFirst({ where: { id, userId: user.id }, }); if (!company) throw new Response("Not Found", { status: 404 }); const customers = await prisma.customer.findMany({ where: { companyId: id }, orderBy: { name: "asc" }, select: { id: true, name: true }, }); if (customers.length === 0) { throw redirect(`/companies/${id}/customers`); } return { company, customers }; } export default function NewInvoicePage() { const { company, customers } = useLoaderData(); const navigate = useNavigate(); async function handleSubmit(data: Record) { const res = await fetch("/api/invoices", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }); if (res.ok) { const invoice = await res.json(); navigate(`/companies/${company.id}/invoices/${invoice.id}`); } else { alert("Fehler beim Erstellen der Rechnung."); } } return (
Zurück zu Rechnungen

Neue Rechnung

Für Mandant: {company.name}

Rechnungsdaten
); }