"use client"; import { useState, useEffect } from "react"; import { useForm } from "react-hook-form"; import { X, CalendarIcon, Clock } from "lucide-react"; import toast from "react-hot-toast"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { getStationsAPI } from "(app)/_querys/stations"; import { createBookingAPI } from "(app)/_querys/bookings"; interface Station { id: number; bosCallsign: string; bosCallsignShort: string; locationState: string; operator: string; aircraft: string; } interface NewBookingFormData { type: "STATION" | "LST_01" | "LST_02" | "LST_03" | "LST_04"; stationId?: number; startTime: string; endTime: string; title?: string; description?: string; } interface NewBookingModalProps { isOpen: boolean; onClose: () => void; onBookingCreated: () => void; userPermissions: string[]; } export const NewBookingModal = ({ isOpen, onClose, onBookingCreated, userPermissions, }: NewBookingModalProps) => { const [submitting, setSubmitting] = useState(false); const queryClient = useQueryClient(); const { data: stations, isLoading: isLoadingStations } = useQuery({ queryKey: ["stations"], queryFn: () => getStationsAPI({}), }); const { mutate: createBooking, isPending: isCreateBookingLoading } = useMutation({ mutationKey: ["createBooking"], mutationFn: createBookingAPI, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["bookings"] }); onBookingCreated(); }, }); const { register, handleSubmit, watch, setValue, reset, formState: { errors }, } = useForm(); const selectedType = watch("type"); const hasDISPOPermission = userPermissions.includes("DISPO"); // Reset form when modal opens useEffect(() => { if (isOpen) { reset(); // Set default datetime to current hour const now = new Date(); const currentHour = new Date( now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), 0, 0, ); const nextHour = new Date(currentHour.getTime() + 60 * 60 * 1000); setValue("startTime", currentHour.toISOString().slice(0, 16)); setValue("endTime", nextHour.toISOString().slice(0, 16)); } }, [isOpen, reset, setValue]); const onSubmit = async (data: NewBookingFormData) => { setSubmitting(true); try { // Validate that end time is after start time if (new Date(data.endTime) <= new Date(data.startTime)) { toast.error("Endzeit muss nach der Startzeit liegen"); return; } const response = await createBooking(data); console.log("Booking created:", response); toast.success("Buchung erfolgreich erstellt!"); onBookingCreated(); onClose(); } catch (error) { console.error("Error creating booking:", error); toast.error("Fehler beim Erstellen der Buchung"); } finally { setSubmitting(false); } }; if (!isOpen) return null; return (
{/* Header */}

Neue Buchung erstellen

{/* Form */}
{/* Resource Type Selection */}
{errors.type && ( )}
{/* Station Selection (only if STATION type is selected) */} {selectedType === "STATION" && (
{isLoadingStations ? (
) : ( )} {errors.stationId && ( )}
)} {/* Time Selection */}
{errors.startTime && ( )}
{errors.endTime && ( )}
{/* Actions */}
); };