import React from "react"; import { base44 } from "@/api/base44Client"; import { useMutation, useQueryClient, useQuery } from "@tanstack/react-query"; import { useNavigate } from "react-router-dom"; import { createPageUrl } from "@/utils"; import EventFormWizard from "@/components/events/EventFormWizard"; import { useToast } from "@/components/ui/use-toast"; import { Button } from "@/components/ui/button"; import { X, AlertTriangle } from "lucide-react"; import { detectAllConflicts, ConflictAlert } from "@/components/scheduling/ConflictDetection"; import { Card, CardContent } from "@/components/ui/card"; export default function CreateEvent() { const navigate = useNavigate(); const queryClient = useQueryClient(); const { toast } = useToast(); const [pendingEvent, setPendingEvent] = React.useState(null); const [showConflictWarning, setShowConflictWarning] = React.useState(false); const { data: currentUser } = useQuery({ queryKey: ['current-user-create-event'], queryFn: () => base44.auth.me(), }); const { data: allEvents = [] } = useQuery({ queryKey: ['events-for-conflict-check'], queryFn: () => base44.entities.Event.list(), initialData: [], }); const createEventMutation = useMutation({ mutationFn: (eventData) => base44.entities.Event.create(eventData), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['events'] }); queryClient.invalidateQueries({ queryKey: ['client-events'] }); toast({ title: "✅ Event Created", description: "Your event has been created successfully.", }); navigate(createPageUrl("Events")); }, onError: (error) => { toast({ title: "❌ Failed to Create Event", description: error.message || "There was an error creating the event.", variant: "destructive", }); }, }); const handleSubmit = (eventData) => { // Detect conflicts before creating const conflicts = detectAllConflicts(eventData, allEvents); if (conflicts.length > 0) { setPendingEvent({ ...eventData, detected_conflicts: conflicts }); setShowConflictWarning(true); } else { createEventMutation.mutate(eventData); } }; const handleConfirmWithConflicts = () => { if (pendingEvent) { createEventMutation.mutate(pendingEvent); setShowConflictWarning(false); setPendingEvent(null); } }; const handleCancelConflicts = () => { setShowConflictWarning(false); setPendingEvent(null); }; return (
{/* Header */}

Create Standard Order

Fill out the details for your planned event

{/* Conflict Warning Modal */} {showConflictWarning && pendingEvent && (

Scheduling Conflicts Detected

This event has {pendingEvent.detected_conflicts.length} potential conflict{pendingEvent.detected_conflicts.length !== 1 ? 's' : ''} with existing bookings. Review the conflicts below and decide how to proceed.

)} navigate(createPageUrl("ClientDashboard"))} />
); }