forked from fahrPool/fahrPool
feat: add auftrag creation page and server action
This commit is contained in:
parent
36e65cad48
commit
e85e9ce993
|
|
@ -0,0 +1,45 @@
|
||||||
|
"use server";
|
||||||
|
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
import { createClient } from "@/lib/supabase/server";
|
||||||
|
|
||||||
|
export async function createAuftrag(formData: FormData): Promise<void> {
|
||||||
|
const abholortId = formData.get("abholort_id");
|
||||||
|
const zielortId = formData.get("zielort_id");
|
||||||
|
const fahrzeugId = formData.get("fahrzeug_id");
|
||||||
|
const vorgaengerAuftragId = formData.get("vorgaenger_auftrag_id");
|
||||||
|
|
||||||
|
if (typeof abholortId !== "string" || !abholortId) {
|
||||||
|
redirect(`/auftraege/neu?error=${encodeURIComponent("Bitte einen Abholort auswaehlen.")}`);
|
||||||
|
}
|
||||||
|
if (typeof zielortId !== "string" || !zielortId) {
|
||||||
|
redirect(`/auftraege/neu?error=${encodeURIComponent("Bitte einen Zielort auswaehlen.")}`);
|
||||||
|
}
|
||||||
|
if (typeof fahrzeugId !== "string" || !fahrzeugId) {
|
||||||
|
redirect(`/auftraege/neu?error=${encodeURIComponent("Bitte ein Fahrzeug auswaehlen.")}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const supabase = await createClient();
|
||||||
|
const {
|
||||||
|
data: { user },
|
||||||
|
} = await supabase.auth.getUser();
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
redirect("/login");
|
||||||
|
}
|
||||||
|
|
||||||
|
const { error } = await supabase.from("auftraege").insert({
|
||||||
|
abholort_id: abholortId,
|
||||||
|
zielort_id: zielortId,
|
||||||
|
fahrzeug_id: fahrzeugId,
|
||||||
|
erstellt_von: user.id,
|
||||||
|
vorgaenger_auftrag_id:
|
||||||
|
typeof vorgaengerAuftragId === "string" && vorgaengerAuftragId ? vorgaengerAuftragId : null,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
redirect(`/auftraege/neu?error=${encodeURIComponent("Auftrag konnte nicht angelegt werden.")}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect("/auftraege");
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
import { createAuftrag } from "./actions";
|
||||||
|
import { listOrte, listFahrzeuge } from "@/lib/auftraege/queries";
|
||||||
|
|
||||||
|
export default async function NeuerAuftragPage({
|
||||||
|
searchParams,
|
||||||
|
}: {
|
||||||
|
searchParams: Promise<{ error?: string; vorgaenger_auftrag_id?: string }>;
|
||||||
|
}) {
|
||||||
|
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>
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue