|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useState, useEffect, useCallback } from 'react' |
| 4 | +import { useRouter, useParams } from 'next/navigation' |
| 5 | +import { useCompanyContext } from '@/contexts/CompanyContext' |
| 6 | +import { EventForm } from '@/components/dashboard/EventForm' |
| 7 | +import { ArrowLeft, Trash2 } from 'lucide-react' |
| 8 | +import { Button } from '@/components/ui/button' |
| 9 | +import Link from 'next/link' |
| 10 | +import { Event } from '@/types/events' |
| 11 | +import { toast } from 'sonner' |
| 12 | +import { |
| 13 | + AlertDialog, |
| 14 | + AlertDialogAction, |
| 15 | + AlertDialogCancel, |
| 16 | + AlertDialogContent, |
| 17 | + AlertDialogDescription, |
| 18 | + AlertDialogFooter, |
| 19 | + AlertDialogHeader, |
| 20 | + AlertDialogTitle, |
| 21 | + AlertDialogTrigger, |
| 22 | +} from '@/components/ui/alert-dialog' |
| 23 | + |
| 24 | +export default function EditEventPage() { |
| 25 | + const router = useRouter() |
| 26 | + const params = useParams() |
| 27 | + const slug = params.slug as string |
| 28 | + const { currentCompany, loading: companyLoading } = useCompanyContext() |
| 29 | + const [event, setEvent] = useState<Event | null>(null) |
| 30 | + const [loading, setLoading] = useState(true) |
| 31 | + const [deleting, setDeleting] = useState(false) |
| 32 | + |
| 33 | + const fetchEvent = useCallback(async () => { |
| 34 | + try { |
| 35 | + setLoading(true) |
| 36 | + const response = await fetch(`/api/events/${slug}`) |
| 37 | + |
| 38 | + if (!response.ok) { |
| 39 | + throw new Error('Failed to fetch event') |
| 40 | + } |
| 41 | + |
| 42 | + const data = await response.json() |
| 43 | + setEvent(data.event) |
| 44 | + } catch (error) { |
| 45 | + console.error('Error fetching event:', error) |
| 46 | + toast.error('Failed to load event') |
| 47 | + router.push('/dashboard/company/events') |
| 48 | + } finally { |
| 49 | + setLoading(false) |
| 50 | + } |
| 51 | + }, [slug, router]) |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + if (slug) { |
| 55 | + fetchEvent() |
| 56 | + } |
| 57 | + }, [slug, fetchEvent]) |
| 58 | + |
| 59 | + const handleSuccess = (updatedEvent: Event) => { |
| 60 | + setEvent(updatedEvent) |
| 61 | + toast.success('Event updated successfully!') |
| 62 | + } |
| 63 | + |
| 64 | + const handleDelete = async () => { |
| 65 | + try { |
| 66 | + setDeleting(true) |
| 67 | + const response = await fetch(`/api/events/${slug}`, { |
| 68 | + method: 'DELETE', |
| 69 | + }) |
| 70 | + |
| 71 | + if (!response.ok) { |
| 72 | + throw new Error('Failed to delete event') |
| 73 | + } |
| 74 | + |
| 75 | + toast.success('Event deleted successfully!') |
| 76 | + router.push('/dashboard/company/events') |
| 77 | + } catch (error) { |
| 78 | + console.error('Error deleting event:', error) |
| 79 | + toast.error('Failed to delete event') |
| 80 | + } finally { |
| 81 | + setDeleting(false) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (loading || companyLoading || !currentCompany) { |
| 86 | + return ( |
| 87 | + <div className="flex items-center justify-center min-h-[400px]"> |
| 88 | + <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div> |
| 89 | + </div> |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + if (!event) { |
| 94 | + return ( |
| 95 | + <div className="flex items-center justify-center min-h-[400px]"> |
| 96 | + <div className="text-center"> |
| 97 | + <h2 className="text-2xl font-bold mb-2">Event not found</h2> |
| 98 | + <p className="text-muted-foreground mb-4"> |
| 99 | + The event you're looking for doesn't exist or you don't have access to it. |
| 100 | + </p> |
| 101 | + <Link href="/dashboard/company/events"> |
| 102 | + <Button>Back to Events</Button> |
| 103 | + </Link> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + ) |
| 107 | + } |
| 108 | + |
| 109 | + return ( |
| 110 | + <div className="space-y-6"> |
| 111 | + {/* Header */} |
| 112 | + <div className="flex items-center justify-between"> |
| 113 | + <div className="flex items-center gap-4"> |
| 114 | + <Link href="/dashboard/company/events"> |
| 115 | + <Button variant="outline" size="sm"> |
| 116 | + <ArrowLeft className="h-4 w-4 mr-2" /> |
| 117 | + Back to Events |
| 118 | + </Button> |
| 119 | + </Link> |
| 120 | + <div> |
| 121 | + <h1 className="text-3xl font-bold tracking-tight">Edit Event</h1> |
| 122 | + <p className="text-muted-foreground mt-1"> |
| 123 | + Update your event details |
| 124 | + </p> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + |
| 128 | + {/* Delete Button */} |
| 129 | + <AlertDialog> |
| 130 | + <AlertDialogTrigger asChild> |
| 131 | + <Button variant="outline" className="text-red-600 border-red-600 hover:bg-red-50"> |
| 132 | + <Trash2 className="h-4 w-4 mr-2" /> |
| 133 | + Delete Event |
| 134 | + </Button> |
| 135 | + </AlertDialogTrigger> |
| 136 | + <AlertDialogContent> |
| 137 | + <AlertDialogHeader> |
| 138 | + <AlertDialogTitle>Are you sure?</AlertDialogTitle> |
| 139 | + <AlertDialogDescription> |
| 140 | + This action cannot be undone. This will permanently delete the event |
| 141 | + "{event.title}" and all associated data. |
| 142 | + </AlertDialogDescription> |
| 143 | + </AlertDialogHeader> |
| 144 | + <AlertDialogFooter> |
| 145 | + <AlertDialogCancel>Cancel</AlertDialogCancel> |
| 146 | + <AlertDialogAction |
| 147 | + onClick={handleDelete} |
| 148 | + disabled={deleting} |
| 149 | + className="bg-red-600 hover:bg-red-700" |
| 150 | + > |
| 151 | + {deleting ? 'Deleting...' : 'Delete Event'} |
| 152 | + </AlertDialogAction> |
| 153 | + </AlertDialogFooter> |
| 154 | + </AlertDialogContent> |
| 155 | + </AlertDialog> |
| 156 | + </div> |
| 157 | + |
| 158 | + {/* Event Form */} |
| 159 | + <EventForm |
| 160 | + company={currentCompany} |
| 161 | + event={event} |
| 162 | + mode="edit" |
| 163 | + onSuccess={handleSuccess} |
| 164 | + /> |
| 165 | + </div> |
| 166 | + ) |
| 167 | +} |
0 commit comments