forked from fahrPool/fahrPool
210 lines
8.9 KiB
TypeScript
210 lines
8.9 KiB
TypeScript
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 { 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 { AuftragStammFelder } from "@/components/auftraege/AuftragStammFelder";
|
|
import { FinFeld } from "@/components/fahrzeuge/FinFeld";
|
|
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} />
|
|
)}
|
|
<AuftragStammFelder
|
|
orte={orte}
|
|
fahrzeuge={fahrzeuge}
|
|
werte={{ abholortId: abholort_id, zielortId: zielort_id, fahrzeugId: fahrzeug_id }}
|
|
vorbefuelltesDatum={vorbefuelltesDatum}
|
|
neuLinks={{
|
|
abholort: buildOrtFormularUrl("abholort", {
|
|
vorgaengerAuftragId: vorgaenger_auftrag_id,
|
|
zielortId: zielort_id,
|
|
fahrzeugId: fahrzeug_id,
|
|
}),
|
|
zielort: buildOrtFormularUrl("zielort", {
|
|
vorgaengerAuftragId: vorgaenger_auftrag_id,
|
|
abholortId: abholort_id,
|
|
fahrzeugId: fahrzeug_id,
|
|
}),
|
|
fahrzeug: buildFahrzeugFormularUrl({
|
|
vorgaengerAuftragId: vorgaenger_auftrag_id,
|
|
abholortId: abholort_id,
|
|
zielortId: zielort_id,
|
|
}),
|
|
}}
|
|
nummerLabel="Auftragsnummer (optional, wird sonst automatisch vergeben)"
|
|
/>
|
|
<Button type="submit">Auftrag anlegen</Button>
|
|
</form>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|