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

79 lines
2.8 KiB
TypeScript

import { redirect } from "next/navigation";
import { createAuftrag } from "./actions";
import { listOrte, listFahrzeuge } from "@/lib/auftraege/queries";
import { loadProfile } from "@/lib/auth/profile";
export default async function NeuerAuftragPage({
searchParams,
}: {
searchParams: Promise<{ error?: string; vorgaenger_auftrag_id?: string }>;
}) {
const profileResult = await loadProfile();
// Seitenebenen-Gate zusaetzlich zur INSERT-RLS-Policy auf `auftraege`
// (admin/disponent-only): ohne diesen Check wuerde die Seite das Formular
// auch Fahrern/Readonly anzeigen, obwohl ein Submit dann ohnehin an der
// RLS-Policy scheitern wuerde -- das ist verwirrend UX statt eines echten
// Sicherheitslecks, aber ein frueher Redirect ist die klarere Loesung.
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 } = await searchParams;
const [orte, fahrzeuge] = await Promise.all([listOrte(), listFahrzeuge()]);
return (
<main>
<h1>Neuen Auftrag anlegen</h1>
{error && <p role="alert">{error}</p>}
<form action={createAuftrag}>
{vorgaenger_auftrag_id && (
<input type="hidden" name="vorgaenger_auftrag_id" value={vorgaenger_auftrag_id} />
)}
<div>
<label htmlFor="abholort_id">Abholort</label>
<select id="abholort_id" name="abholort_id" required defaultValue="">
<option value="" disabled>
Bitte waehlen
</option>
{orte.map((ort) => (
<option key={ort.id} value={ort.id}>
{ort.adresse} ({ort.ansprechpartner})
</option>
))}
</select>
</div>
<div>
<label htmlFor="zielort_id">Zielort</label>
<select id="zielort_id" name="zielort_id" required defaultValue="">
<option value="" disabled>
Bitte waehlen
</option>
{orte.map((ort) => (
<option key={ort.id} value={ort.id}>
{ort.adresse} ({ort.ansprechpartner})
</option>
))}
</select>
</div>
<div>
<label htmlFor="fahrzeug_id">Fahrzeug</label>
<select id="fahrzeug_id" name="fahrzeug_id" required defaultValue="">
<option value="" disabled>
Bitte waehlen
</option>
{fahrzeuge.map((fahrzeug) => (
<option key={fahrzeug.id} value={fahrzeug.id}>
{fahrzeug.kennzeichen}
</option>
))}
</select>
</div>
<button type="submit">Auftrag anlegen</button>
</form>
</main>
);
}