forked from fahrPool/fahrPool
86 lines
3.4 KiB
TypeScript
86 lines
3.4 KiB
TypeScript
import { redirect } from "next/navigation";
|
|
import { createAuftrag } 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";
|
|
|
|
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 verwirrende 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 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>
|
|
)}
|
|
<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}>
|
|
<option value="" disabled>
|
|
Bitte waehlen
|
|
</option>
|
|
{orte.map((ort) => (
|
|
<option key={ort.id} value={ort.id}>
|
|
{ort.adresse} ({ort.ansprechpartner})
|
|
</option>
|
|
))}
|
|
</select>
|
|
</FormField>
|
|
<FormField label="Zielort" htmlFor="zielort_id">
|
|
<select id="zielort_id" name="zielort_id" required defaultValue="" 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>
|
|
<FormField label="Fahrzeug" htmlFor="fahrzeug_id">
|
|
<select id="fahrzeug_id" name="fahrzeug_id" required defaultValue="" className={FIELD_CLASSES}>
|
|
<option value="" disabled>
|
|
Bitte waehlen
|
|
</option>
|
|
{fahrzeuge.map((fahrzeug) => (
|
|
<option key={fahrzeug.id} value={fahrzeug.id}>
|
|
{fahrzeug.kennzeichen}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</FormField>
|
|
<Button type="submit">Auftrag anlegen</Button>
|
|
</form>
|
|
</Card>
|
|
</main>
|
|
);
|
|
}
|