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

79 lines
3.0 KiB
TypeScript

import Link from "next/link";
import { notFound, redirect } from "next/navigation";
import { aktualisiereAuftrag } from "./actions";
import { listFahrzeuge, listOrte, loadAuftrag } from "@/lib/auftraege/queries";
import { loadProfile } from "@/lib/auth/profile";
import { kannAuftraegeVerwalten } from "@/lib/auth/roles";
import { istBearbeitbar, NICHT_BEARBEITBAR_MELDUNG } from "@/lib/auftraege/validate-auftrag";
import { alsDatetimeLocal, berechneVorbefuelltenZeitpunkt } from "@/lib/auftraege/zeitplanung";
import { AuftragStammFelder } from "@/components/auftraege/AuftragStammFelder";
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 AuftragBearbeitenPage({
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 bearbeiten.")}`);
}
const [auftrag, orte, fahrzeuge] = await Promise.all([loadAuftrag(id), listOrte(), listFahrzeuge()]);
if (!auftrag) {
notFound();
}
if (!istBearbeitbar(auftrag.status)) {
redirect(`/auftraege/${id}?error=${encodeURIComponent(NICHT_BEARBEITBAR_MELDUNG)}`);
}
const aktion = aktualisiereAuftrag.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 bearbeiten</h1>
<p className="mb-6 text-sm text-asphalt-grey/70">{auftrag.auftragsnummer}</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,
fahrzeugId: auftrag.fahrzeug.id,
auftragsnummer: auftrag.auftragsnummer,
zeitplanungTyp: auftrag.zeitplanung_typ,
zeitplanungZeitpunkt: auftrag.zeitplanung_zeitpunkt
? alsDatetimeLocal(auftrag.zeitplanung_zeitpunkt)
: undefined,
}}
vorbefuelltesDatum={berechneVorbefuelltenZeitpunkt()}
neuLinks={null}
nummerLabel="Auftragsnummer"
/>
<div className="flex flex-col gap-3 sm:flex-row">
<Button type="submit">Speichern</Button>
<Link href={`/auftraege/${auftrag.id}`} className={buttonClasses("secondary", true)}>
Abbrechen
</Link>
</div>
</NetzSicheresFormular>
</Card>
</div>
);
}