forked from fahrPool/fahrPool
feat: Zeitplanung-Auswahl beim Anlegen eines Auftrags
Neuer ZeitplanungFeld-Client-Komponente zeigt Datum/Uhrzeit-Feld nur fuer die Zeitplanungstypen "am"/"ab" an und wird auf der Auftrag- Neu-Seite eingebunden. createAuftrag validiert und speichert zeitplanung_typ/zeitplanung_zeitpunkt, mit serverseitiger Pruefung vor dem Insert.
This commit is contained in:
parent
343f7a3aac
commit
0863d5d9b9
|
|
@ -6,6 +6,7 @@ import { validateOrt } from "@/lib/orte-fahrzeuge/validate-ort";
|
|||
import { validateFahrzeug } from "@/lib/orte-fahrzeuge/validate-fahrzeug";
|
||||
import { insertOrt, insertFahrzeug } from "@/lib/orte-fahrzeuge/mutations";
|
||||
import { loadProfile } from "@/lib/auth/profile";
|
||||
import { ZEITPLANUNG_TYPEN, type ZeitplanungTyp } from "@/lib/auftraege/zeitplanung";
|
||||
|
||||
export async function createAuftrag(formData: FormData): Promise<void> {
|
||||
const abholortId = formData.get("abholort_id");
|
||||
|
|
@ -16,6 +17,21 @@ export async function createAuftrag(formData: FormData): Promise<void> {
|
|||
const auftragsnummer =
|
||||
typeof auftragsnummerRaw === "string" && auftragsnummerRaw.trim() ? auftragsnummerRaw.trim() : null;
|
||||
|
||||
const zeitplanungTypRaw = formData.get("zeitplanung_typ");
|
||||
const zeitplanungTyp = (ZEITPLANUNG_TYPEN as readonly string[]).includes(zeitplanungTypRaw as string)
|
||||
? (zeitplanungTypRaw as ZeitplanungTyp)
|
||||
: "ab_sofort";
|
||||
|
||||
let zeitplanungZeitpunkt: string | null = null;
|
||||
if (zeitplanungTyp === "am" || zeitplanungTyp === "ab") {
|
||||
const zeitpunktRaw = formData.get("zeitplanung_zeitpunkt");
|
||||
const geparst = typeof zeitpunktRaw === "string" && zeitpunktRaw ? new Date(zeitpunktRaw) : null;
|
||||
if (!geparst || Number.isNaN(geparst.getTime())) {
|
||||
redirect(`/auftraege/neu?error=${encodeURIComponent("Bitte Datum und Uhrzeit fuer die Zeitplanung angeben.")}`);
|
||||
}
|
||||
zeitplanungZeitpunkt = geparst.toISOString();
|
||||
}
|
||||
|
||||
if (typeof abholortId !== "string" || !abholortId) {
|
||||
redirect(`/auftraege/neu?error=${encodeURIComponent("Bitte einen Abholort auswaehlen.")}`);
|
||||
}
|
||||
|
|
@ -41,6 +57,8 @@ export async function createAuftrag(formData: FormData): Promise<void> {
|
|||
fahrzeug_id: fahrzeugId,
|
||||
erstellt_von: user.id,
|
||||
auftragsnummer,
|
||||
zeitplanung_typ: zeitplanungTyp,
|
||||
zeitplanung_zeitpunkt: zeitplanungZeitpunkt,
|
||||
vorgaenger_auftrag_id:
|
||||
typeof vorgaengerAuftragId === "string" && vorgaengerAuftragId ? vorgaengerAuftragId : null,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ 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 { ZeitplanungFeld } from "@/components/auftraege/ZeitplanungFeld";
|
||||
import { berechneVorbefuelltenZeitpunkt } from "@/lib/auftraege/zeitplanung";
|
||||
|
||||
function buildOrtFormularUrl(
|
||||
ziel: "abholort" | "zielort",
|
||||
|
|
@ -66,6 +68,7 @@ export default async function NeuerAuftragPage({
|
|||
|
||||
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">
|
||||
|
|
@ -261,6 +264,8 @@ export default async function NeuerAuftragPage({
|
|||
+ Neues Fahrzeug
|
||||
</Link>
|
||||
|
||||
<ZeitplanungFeld vorbefuelltesDatum={vorbefuelltesDatum} />
|
||||
|
||||
<FormField label="Auftragsnummer (optional, wird sonst automatisch vergeben)" htmlFor="auftragsnummer">
|
||||
<input id="auftragsnummer" name="auftragsnummer" type="text" className={FIELD_CLASSES} />
|
||||
</FormField>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
|
||||
import { ZEITPLANUNG_TYPEN, type ZeitplanungTyp } from "@/lib/auftraege/zeitplanung";
|
||||
|
||||
const LABELS: Record<ZeitplanungTyp, string> = {
|
||||
ab_sofort: "Ab sofort",
|
||||
am: "Am (fester Termin)",
|
||||
ab: "Ab (frühestens ab)",
|
||||
diese_woche: "Diese Woche",
|
||||
sobald_moeglich: "Sobald als möglich",
|
||||
};
|
||||
|
||||
export function ZeitplanungFeld({ vorbefuelltesDatum }: { vorbefuelltesDatum: string }) {
|
||||
const [typ, setTyp] = useState<ZeitplanungTyp>("ab_sofort");
|
||||
const zeigeZeitpunkt = typ === "am" || typ === "ab";
|
||||
|
||||
return (
|
||||
<>
|
||||
<FormField label="Zeitplanung" htmlFor="zeitplanung_typ">
|
||||
<select
|
||||
id="zeitplanung_typ"
|
||||
name="zeitplanung_typ"
|
||||
required
|
||||
value={typ}
|
||||
onChange={(event) => setTyp(event.target.value as ZeitplanungTyp)}
|
||||
className={FIELD_CLASSES}
|
||||
>
|
||||
{ZEITPLANUNG_TYPEN.map((wert) => (
|
||||
<option key={wert} value={wert}>
|
||||
{LABELS[wert]}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</FormField>
|
||||
{zeigeZeitpunkt && (
|
||||
<FormField label="Datum und Uhrzeit" htmlFor="zeitplanung_zeitpunkt">
|
||||
<input
|
||||
id="zeitplanung_zeitpunkt"
|
||||
name="zeitplanung_zeitpunkt"
|
||||
type="datetime-local"
|
||||
required
|
||||
defaultValue={vorbefuelltesDatum}
|
||||
className={FIELD_CLASSES}
|
||||
/>
|
||||
</FormField>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue