1
0
Fork 0

feat: add inline Ort quick-add to Auftrag-anlegen page

This commit is contained in:
Jonny 2026-07-07 08:47:42 +02:00
parent 08184d2166
commit 69c22f9afe
2 changed files with 178 additions and 6 deletions

View File

@ -2,6 +2,8 @@
import { redirect } from "next/navigation";
import { createClient } from "@/lib/supabase/server";
import { validateOrt } from "@/lib/orte-fahrzeuge/validate-ort";
import { insertOrt } from "@/lib/orte-fahrzeuge/mutations";
export async function createAuftrag(formData: FormData): Promise<void> {
const abholortId = formData.get("abholort_id");
@ -43,3 +45,56 @@ export async function createAuftrag(formData: FormData): Promise<void> {
redirect("/auftraege");
}
export async function createOrtUndAuswaehlen(ziel: "abholort" | "zielort", formData: FormData): Promise<void> {
const vorgaengerAuftragId = formData.get("vorgaenger_auftrag_id");
const abholortId = formData.get("abholort_id");
const zielortId = formData.get("zielort_id");
const fahrzeugId = formData.get("fahrzeug_id");
const ruecksprungParams = new URLSearchParams();
if (typeof vorgaengerAuftragId === "string" && vorgaengerAuftragId) {
ruecksprungParams.set("vorgaenger_auftrag_id", vorgaengerAuftragId);
}
if (typeof abholortId === "string" && abholortId) {
ruecksprungParams.set("abholort_id", abholortId);
}
if (typeof zielortId === "string" && zielortId) {
ruecksprungParams.set("zielort_id", zielortId);
}
if (typeof fahrzeugId === "string" && fahrzeugId) {
ruecksprungParams.set("fahrzeug_id", fahrzeugId);
}
const validation = validateOrt({
adresse: formData.get("adresse"),
ansprechpartner: formData.get("ansprechpartner"),
});
if (!validation.valid) {
ruecksprungParams.set("zeige_ort_formular", ziel);
ruecksprungParams.set("error", validation.error);
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
}
const supabase = await createClient();
const {
data: { user },
} = await supabase.auth.getUser();
if (!user) {
redirect("/login");
}
const result = await insertOrt({
adresse: validation.adresse,
ansprechpartner: validation.ansprechpartner,
createdBy: user.id,
});
if (!result.success) {
ruecksprungParams.set("zeige_ort_formular", ziel);
ruecksprungParams.set("error", result.error);
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
}
ruecksprungParams.set(ziel === "abholort" ? "abholort_id" : "zielort_id", result.id);
redirect(`/auftraege/neu?${ruecksprungParams.toString()}`);
}

View File

@ -1,5 +1,6 @@
import Link from "next/link";
import { redirect } from "next/navigation";
import { createAuftrag } from "./actions";
import { createAuftrag, createOrtUndAuswaehlen } from "./actions";
import { listOrte, listFahrzeuge } from "@/lib/auftraege/queries";
import { loadProfile } from "@/lib/auth/profile";
import { Card } from "@/components/ui/Card";
@ -7,10 +8,29 @@ import { Button } from "@/components/ui/Button";
import { Alert } from "@/components/ui/Alert";
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
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()}`;
}
export default async function NeuerAuftragPage({
searchParams,
}: {
searchParams: Promise<{ error?: string; vorgaenger_auftrag_id?: string }>;
searchParams: Promise<{
error?: string;
vorgaenger_auftrag_id?: string;
abholort_id?: string;
zielort_id?: string;
fahrzeug_id?: string;
zeige_ort_formular?: string;
}>;
}) {
const profileResult = await loadProfile();
// Seitenebenen-Gate zusaetzlich zur INSERT-RLS-Policy auf `auftraege`
@ -25,9 +45,13 @@ export default async function NeuerAuftragPage({
redirect(`/auftraege?error=${encodeURIComponent("Nur Disponent/Admin duerfen Auftraege anlegen.")}`);
}
const { error, vorgaenger_auftrag_id } = await searchParams;
const { error, vorgaenger_auftrag_id, abholort_id, zielort_id, fahrzeug_id, zeige_ort_formular } =
await searchParams;
const [orte, fahrzeuge] = await Promise.all([listOrte(), listFahrzeuge()]);
const createOrtFuerAbholortAction = createOrtUndAuswaehlen.bind(null, "abholort");
const createOrtFuerZielortAction = createOrtUndAuswaehlen.bind(null, "zielort");
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>
@ -36,13 +60,72 @@ export default async function NeuerAuftragPage({
<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>
<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>
<Button type="submit">Zielort 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="" className={FIELD_CLASSES}>
<select
id="abholort_id"
name="abholort_id"
required
defaultValue={abholort_id ?? ""}
className={FIELD_CLASSES}
>
<option value="" disabled>
Bitte waehlen
</option>
@ -53,8 +136,25 @@ export default async function NeuerAuftragPage({
))}
</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="" className={FIELD_CLASSES}>
<select
id="zielort_id"
name="zielort_id"
required
defaultValue={zielort_id ?? ""}
className={FIELD_CLASSES}
>
<option value="" disabled>
Bitte waehlen
</option>
@ -65,8 +165,25 @@ export default async function NeuerAuftragPage({
))}
</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="" className={FIELD_CLASSES}>
<select
id="fahrzeug_id"
name="fahrzeug_id"
required
defaultValue={fahrzeug_id ?? ""}
className={FIELD_CLASSES}
>
<option value="" disabled>
Bitte waehlen
</option>