1
0
Fork 0
fahrPool/web/app/auftraege/[id]/klonen/page.tsx

79 lines
3.1 KiB
TypeScript

import { notFound, redirect } from "next/navigation";
import Link from "next/link";
import { loadAuftrag, listOrte, listFahrzeuge } from "@/lib/auftraege/queries";
import { alsDatetimeLocal, berechneVorbefuelltenZeitpunkt } from "@/lib/auftraege/zeitplanung";
import { loadProfile } from "@/lib/auth/profile";
import { kannAuftraegeVerwalten } from "@/lib/auth/roles";
import { auftragKlonen } from "./actions";
import { AuftragStammFelder } from "@/components/auftraege/AuftragStammFelder";
import { FahrzeugMehrfachAuswahl } from "@/components/auftraege/FahrzeugMehrfachAuswahl";
import { NetzSicheresFormular } from "@/components/pwa/NetzSicheresFormular";
import { Card } from "@/components/ui/Card";
import { Button, buttonClasses } from "@/components/ui/Button";
import { Alert } from "@/components/ui/Alert";
export default async function AuftragKlonenPage({
params,
searchParams,
}: {
params: Promise<{ id: string }>;
searchParams: Promise<{ error?: string }>;
}) {
const { id } = await params;
const { error } = await searchParams;
const profileResult = await loadProfile();
if (profileResult.status !== "ok" || !kannAuftraegeVerwalten(profileResult.profile.role)) {
redirect(`/auftraege/${id}?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege klonen.")}`);
}
const [auftrag, orte, fahrzeuge] = await Promise.all([loadAuftrag(id), listOrte(), listFahrzeuge()]);
if (!auftrag) {
notFound();
}
const aktion = auftragKlonen.bind(null, auftrag.id);
return (
<div className="mx-auto max-w-xl p-4 md:p-8">
<h1 className="mb-1 text-xl font-semibold text-asphalt-grey">Auftrag klonen</h1>
<p className="mb-6 text-sm text-asphalt-grey/70">
Vorlage {auftrag.auftragsnummer}: {auftrag.abholort.adresse} → {auftrag.zielort.adresse}
</p>
{error && (
<div className="mb-6">
<Alert>{error}</Alert>
</div>
)}
<Card>
<NetzSicheresFormular action={aktion}>
<AuftragStammFelder
orte={orte}
fahrzeuge={fahrzeuge}
werte={{
abholortId: auftrag.abholort.id,
zielortId: auftrag.zielort.id,
zeitplanungTyp: auftrag.zeitplanung_typ,
zeitplanungZeitpunkt: auftrag.zeitplanung_zeitpunkt
? alsDatetimeLocal(auftrag.zeitplanung_zeitpunkt)
: undefined,
}}
vorbefuelltesDatum={berechneVorbefuelltenZeitpunkt()}
neuLinks={null}
nummerLabel="Auftragsnummer"
ohneFahrzeugUndNummer
/>
<FahrzeugMehrfachAuswahl fahrzeuge={fahrzeuge} />
<p className="mb-4 text-sm text-asphalt-grey/70">Je angehaktem Fahrzeug wird ein eigener Auftrag angelegt.</p>
<div className="flex flex-col gap-3 sm:flex-row">
<Button type="submit">Auftraege anlegen</Button>
<Link href={`/auftraege/${auftrag.id}`} className={buttonClasses("secondary", true)}>
Abbrechen
</Link>
</div>
</NetzSicheresFormular>
</Card>
</div>
);
}