import React, { useState, useEffect, useCallback } from 'react'; import { supabase } from '../supabase'; import { SpecialEvent } from '../types'; interface CreateSpecialEventsViewProps { onShowToast: (msg: string, type: 'success' | 'delete') => void; } const CreateSpecialEventsView: React.FC = ({ onShowToast }) => { const [specialEvents, setSpecialEvents] = useState([]); const [isLoading, setIsLoading] = useState(true); const [isSubmitting, setIsSubmitting] = useState(false); const [editingEvent, setEditingEvent] = useState(null); const [formData, setFormData] = useState({ title: '', description: '', link: '' }); const fetchSpecialEvents = useCallback(async () => { setIsLoading(true); const { data, error } = await supabase .from('special_events') .select('*') .order('created_at', { ascending: false }); if (!error && data) { setSpecialEvents(data); } setIsLoading(false); }, []); useEffect(() => { fetchSpecialEvents(); }, [fetchSpecialEvents]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsSubmitting(true); try { const { data: { user } } = await supabase.auth.getUser(); if (!user) throw new Error("User not authenticated"); if (editingEvent) { const { error } = await supabase .from('special_events') .update({ title: formData.title, description: formData.description, link: formData.link }) .eq('id', editingEvent.id); if (error) throw error; onShowToast("Session updated! Awaiting Review.", "success"); } else { const { error } = await supabase .from('special_events') .insert({ title: formData.title, description: formData.description, link: formData.link, created_by: user.id, verification_status: 'PENDING' }); if (error) throw error; onShowToast("Session Broadcasted! Security check initiated.", "success"); } setFormData({ title: '', description: '', link: '' }); setEditingEvent(null); fetchSpecialEvents(); } catch (err: any) { console.error("Error saving special event:", err); alert(`Error: ${err.message}`); } finally { setIsSubmitting(false); } }; const handleEdit = (event: SpecialEvent) => { setEditingEvent(event); setFormData({ title: event.title, description: event.description || '', link: event.link }); // Scroll to form window.scrollTo({ top: 0, behavior: 'smooth' }); }; const handleDelete = async (id: string) => { if (!confirm("Are you sure you want to delete this special event?")) return; try { const { error } = await supabase .from('special_events') .delete() .eq('id', id); if (error) throw error; onShowToast("Special event deleted successfully!", "delete"); fetchSpecialEvents(); } catch (err: any) { console.error("Error deleting special event:", err); alert(`Error: ${err.message}`); } }; return (
{/* Creation Form */}

{editingEvent ? 'Edit Portal' : 'Create Portal'}

External Link Broadcaster

setFormData({ ...formData, title: e.target.value })} />
setFormData({ ...formData, link: e.target.value })} />