1
0
Fork 0
fahrPool/web/app/auftraege/neu/page.tsx

280 lines
11 KiB
TypeScript

import Link from "next/link";
import { FinFeld } from "@/components/fahrzeuge/FinFeld";
import { redirect } from "next/navigation";
import { createAuftrag, createOrtUndAuswaehlen, createFahrzeugUndAuswaehlen } from "./actions";
import { listOrte, listFahrzeuge } from "@/lib/auftraege/queries";
import { loadProfile } from "@/lib/auth/profile";
import { fahrzeugBezeichnung } from "@/lib/orte-fahrzeuge/fahrzeug-bezeichnung";
import { Card } from "@/components/ui/Card";
import { Button } from "@/components/ui/Button";
import { Alert } from "@/components/ui/Alert";
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
import { ZeitplanungFeld } from "@/components/auftraege/ZeitplanungFeld";
import { berechneVorbefuelltenZeitpunkt } from "@/lib/auftraege/zeitplanung";
function buildOrtFormularUrl(
ziel: "abholort" | "zielort",
params: { vorgaengerAuftragId?: string; abholortId?: string; zielortId?: string; fahrzeugId?: string }
): string {
const query = new URLSearchParams({ zeige_ort_formular: ziel });
if (params.vorgaengerAuftragId) query.set("vorgaenger_auftrag_id", params.vorgaengerAuftragId);
if (params.abholortId) query.set("abholort_id", params.abholortId);
if (params.zielortId) query.set("zielort_id", params.zielortId);
if (params.fahrzeugId) query.set("fahrzeug_id", params.fahrzeugId);
return `/auftraege/neu?${query.toString()}`;
}
function buildFahrzeugFormularUrl(params: {
vorgaengerAuftragId?: string;
abholortId?: string;
zielortId?: string;
}): string {
const query = new URLSearchParams({ zeige_fahrzeug_formular: "1" });
if (params.vorgaengerAuftragId) query.set("vorgaenger_auftrag_id", params.vorgaengerAuftragId);
if (params.abholortId) query.set("abholort_id", params.abholortId);
if (params.zielortId) query.set("zielort_id", params.zielortId);
return `/auftraege/neu?${query.toString()}`;
}
export default async function NeuerAuftragPage({
searchParams,
}: {
searchParams: Promise<{
error?: string;
vorgaenger_auftrag_id?: string;
abholort_id?: string;
zielort_id?: string;
fahrzeug_id?: string;
zeige_ort_formular?: string;
zeige_fahrzeug_formular?: string;
}>;
}) {
const profileResult = await loadProfile();
if (
profileResult.status !== "ok" ||
(profileResult.profile.role !== "admin" && profileResult.profile.role !== "disponent")
) {
redirect(`/auftraege?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege anlegen.")}`);
}
const {
error,
vorgaenger_auftrag_id,
abholort_id,
zielort_id,
fahrzeug_id,
zeige_ort_formular,
zeige_fahrzeug_formular,
} = await searchParams;
const [orte, fahrzeuge] = await Promise.all([listOrte(), listFahrzeuge()]);
const createOrtFuerAbholortAction = createOrtUndAuswaehlen.bind(null, "abholort");
const createOrtFuerZielortAction = createOrtUndAuswaehlen.bind(null, "zielort");
const vorbefuelltesDatum = berechneVorbefuelltenZeitpunkt();
return (
<div className="mx-auto max-w-xl p-4 md:p-8">
<h1 className="mb-6 text-xl font-semibold text-asphalt-grey">Neuen Auftrag anlegen</h1>
{error && (
<div className="mb-6">
<Alert>{error}</Alert>
</div>
)}
{zeige_ort_formular === "abholort" && (
<Card className="mb-6">
<form action={createOrtFuerAbholortAction}>
<h2 className="mb-4 text-lg font-semibold text-asphalt-grey">Neuen Abholort anlegen</h2>
{vorgaenger_auftrag_id && (
<input type="hidden" name="vorgaenger_auftrag_id" value={vorgaenger_auftrag_id} />
)}
{zielort_id && <input type="hidden" name="zielort_id" value={zielort_id} />}
{fahrzeug_id && <input type="hidden" name="fahrzeug_id" value={fahrzeug_id} />}
<FormField label="Adresse" htmlFor="neuer_abholort_adresse">
<input id="neuer_abholort_adresse" name="adresse" type="text" required className={FIELD_CLASSES} />
</FormField>
<FormField label="Ansprechpartner" htmlFor="neuer_abholort_ansprechpartner">
<input
id="neuer_abholort_ansprechpartner"
name="ansprechpartner"
type="text"
required
className={FIELD_CLASSES}
/>
</FormField>
<FormField label="Telefon (optional)" htmlFor="neuer_abholort_telefon">
<input id="neuer_abholort_telefon" name="telefon" type="tel" className={FIELD_CLASSES} />
</FormField>
<FormField label="E-Mail (optional)" htmlFor="neuer_abholort_email">
<input id="neuer_abholort_email" name="email" type="email" className={FIELD_CLASSES} />
</FormField>
<Button type="submit">Abholort anlegen</Button>
</form>
</Card>
)}
{zeige_ort_formular === "zielort" && (
<Card className="mb-6">
<form action={createOrtFuerZielortAction}>
<h2 className="mb-4 text-lg font-semibold text-asphalt-grey">Neuen Zielort anlegen</h2>
{vorgaenger_auftrag_id && (
<input type="hidden" name="vorgaenger_auftrag_id" value={vorgaenger_auftrag_id} />
)}
{abholort_id && <input type="hidden" name="abholort_id" value={abholort_id} />}
{fahrzeug_id && <input type="hidden" name="fahrzeug_id" value={fahrzeug_id} />}
<FormField label="Adresse" htmlFor="neuer_zielort_adresse">
<input id="neuer_zielort_adresse" name="adresse" type="text" required className={FIELD_CLASSES} />
</FormField>
<FormField label="Ansprechpartner" htmlFor="neuer_zielort_ansprechpartner">
<input
id="neuer_zielort_ansprechpartner"
name="ansprechpartner"
type="text"
required
className={FIELD_CLASSES}
/>
</FormField>
<FormField label="Telefon (optional)" htmlFor="neuer_zielort_telefon">
<input id="neuer_zielort_telefon" name="telefon" type="tel" className={FIELD_CLASSES} />
</FormField>
<FormField label="E-Mail (optional)" htmlFor="neuer_zielort_email">
<input id="neuer_zielort_email" name="email" type="email" className={FIELD_CLASSES} />
</FormField>
<Button type="submit">Zielort anlegen</Button>
</form>
</Card>
)}
{zeige_fahrzeug_formular === "1" && (
<Card className="mb-6">
<form action={createFahrzeugUndAuswaehlen}>
<h2 className="mb-4 text-lg font-semibold text-asphalt-grey">Neues Fahrzeug anlegen</h2>
{vorgaenger_auftrag_id && (
<input type="hidden" name="vorgaenger_auftrag_id" value={vorgaenger_auftrag_id} />
)}
{abholort_id && <input type="hidden" name="abholort_id" value={abholort_id} />}
{zielort_id && <input type="hidden" name="zielort_id" value={zielort_id} />}
<FormField label="Kennzeichen (oder FIN)" htmlFor="neues_fahrzeug_kennzeichen">
<input
id="neues_fahrzeug_kennzeichen"
name="kennzeichen"
type="text"
className={FIELD_CLASSES}
/>
</FormField>
<FinFeld id="neues_fahrzeug_fin" />
<FormField label="Marke (optional)" htmlFor="neues_fahrzeug_marke">
<input id="neues_fahrzeug_marke" name="marke" type="text" className={FIELD_CLASSES} />
</FormField>
<FormField label="Modell (optional)" htmlFor="neues_fahrzeug_modell">
<input id="neues_fahrzeug_modell" name="modell" type="text" className={FIELD_CLASSES} />
</FormField>
<Button type="submit">Fahrzeug anlegen</Button>
</form>
</Card>
)}
<Card>
<form action={createAuftrag}>
{vorgaenger_auftrag_id && (
<input type="hidden" name="vorgaenger_auftrag_id" value={vorgaenger_auftrag_id} />
)}
<FormField label="Abholort" htmlFor="abholort_id">
<select
id="abholort_id"
name="abholort_id"
required
defaultValue={abholort_id ?? ""}
className={FIELD_CLASSES}
>
<option value="" disabled>
Bitte waehlen
</option>
{orte.map((ort) => (
<option key={ort.id} value={ort.id}>
{ort.adresse} ({ort.ansprechpartner})
</option>
))}
</select>
</FormField>
<Link
href={buildOrtFormularUrl("abholort", {
vorgaengerAuftragId: vorgaenger_auftrag_id,
zielortId: zielort_id,
fahrzeugId: fahrzeug_id,
})}
className="mb-4 inline-block text-sm text-performance-blue hover:underline"
>
+ Neuer Abholort
</Link>
<FormField label="Zielort" htmlFor="zielort_id">
<select
id="zielort_id"
name="zielort_id"
required
defaultValue={zielort_id ?? ""}
className={FIELD_CLASSES}
>
<option value="" disabled>
Bitte waehlen
</option>
{orte.map((ort) => (
<option key={ort.id} value={ort.id}>
{ort.adresse} ({ort.ansprechpartner})
</option>
))}
</select>
</FormField>
<Link
href={buildOrtFormularUrl("zielort", {
vorgaengerAuftragId: vorgaenger_auftrag_id,
abholortId: abholort_id,
fahrzeugId: fahrzeug_id,
})}
className="mb-4 inline-block text-sm text-performance-blue hover:underline"
>
+ Neuer Zielort
</Link>
<FormField label="Fahrzeug" htmlFor="fahrzeug_id">
<select
id="fahrzeug_id"
name="fahrzeug_id"
required
defaultValue={fahrzeug_id ?? ""}
className={FIELD_CLASSES}
>
<option value="" disabled>
Bitte waehlen
</option>
{fahrzeuge.map((fahrzeug) => (
<option key={fahrzeug.id} value={fahrzeug.id}>
{fahrzeugBezeichnung(fahrzeug)}
</option>
))}
</select>
</FormField>
<Link
href={buildFahrzeugFormularUrl({
vorgaengerAuftragId: vorgaenger_auftrag_id,
abholortId: abholort_id,
zielortId: zielort_id,
})}
className="mb-4 inline-block text-sm text-performance-blue hover:underline"
>
+ Neues Fahrzeug
</Link>
<ZeitplanungFeld vorbefuelltesDatum={vorbefuelltesDatum} />
<FormField label="Auftragsnummer (optional, wird sonst automatisch vergeben)" htmlFor="auftragsnummer">
<input id="auftragsnummer" name="auftragsnummer" type="text" className={FIELD_CLASSES} />
</FormField>
<Button type="submit">Auftrag anlegen</Button>
</form>
</Card>
</div>
);
}