'use client'; import React, { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Label } from '@/components/ui/label'; import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/alert-dialog'; import { MoreVertical, Plus } from 'lucide-react'; const ADDRESSER_KEY = 'myAppAddresser'; const RSVP_KEY = 'myAppRSVP'; // Default RSVP data if localStorage is empty function getDefaultRSVPs() { return []; } const RSVP = () => { // State for RSVPs with placeholder empty array to avoid hydration mismatch const [rsvps, setRSVPs] = useState([]); const [contacts, setContacts] = useState([]); const [isLoaded, setIsLoaded] = useState(false); // State for handling the edit/add modal const [modalOpen, setModalOpen] = useState(false); const [isEditing, setIsEditing] = useState(false); const [currentRSVP, setCurrentRSVP] = useState({ id: null, contactId: null, attending: null }); // State for delete confirmation dialog const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const [rsvpToDelete, setRSVPToDelete] = useState(null); // Load data from localStorage after component mounts on client useEffect(() => { try { // Load contacts const savedContacts = localStorage.getItem(ADDRESSER_KEY); if (savedContacts) { setContacts(JSON.parse(savedContacts)); } else { setContacts([]); } // Load RSVPs const savedRSVPs = localStorage.getItem(RSVP_KEY); if (savedRSVPs) { setRSVPs(JSON.parse(savedRSVPs)); } else { setRSVPs(getDefaultRSVPs()); } } catch (error) { console.error('Fejl ved læsning af data fra localStorage:', error); setRSVPs(getDefaultRSVPs()); setContacts([]); } setIsLoaded(true); }, []); // Save to localStorage whenever rsvps changes (but only after initial load) useEffect(() => { if (isLoaded) { localStorage.setItem(RSVP_KEY, JSON.stringify(rsvps)); } }, [rsvps, isLoaded]); // Handle opening the add modal const handleAdd = () => { setIsEditing(false); setCurrentRSVP({ id: null, contactId: null, attending: null }); setModalOpen(true); }; // Handle opening the edit modal const handleEdit = (rsvp) => { setIsEditing(true); setCurrentRSVP({ ...rsvp }); setModalOpen(true); }; // Handle contact selection const handleSelectContact = (value) => { setCurrentRSVP({ ...currentRSVP, contactId: parseInt(value) }); }; // Handle radio button change const handleAttendingChange = (value) => { setCurrentRSVP({ ...currentRSVP, attending: value === 'yes' }); }; // Handle saving a new or edited RSVP const handleSave = () => { if (!currentRSVP.contactId || currentRSVP.attending === null) { return; // Validation failed } if (isEditing) { // Update existing RSVP setRSVPs(rsvps.map(rsvp => rsvp.id === currentRSVP.id ? currentRSVP : rsvp )); } else { // Check if this contact already has an RSVP const existingRSVP = rsvps.find(r => r.contactId === currentRSVP.contactId); if (existingRSVP) { // Update the existing RSVP instead setRSVPs(rsvps.map(rsvp => rsvp.contactId === currentRSVP.contactId ? { ...rsvp, attending: currentRSVP.attending } : rsvp )); } else { // Add new RSVP const newId = rsvps.length > 0 ? Math.max(...rsvps.map(r => r.id)) + 1 : 1; setRSVPs([...rsvps, { ...currentRSVP, id: newId }]); } } setModalOpen(false); }; // Open delete confirmation dialog const openDeleteDialog = (rsvp) => { setRSVPToDelete(rsvp); setDeleteDialogOpen(true); }; // Handle deleting an RSVP const handleDelete = () => { if (rsvpToDelete) { setRSVPs(rsvps.filter(rsvp => rsvp.id !== rsvpToDelete.id)); setDeleteDialogOpen(false); setRSVPToDelete(null); } }; // Get contact name by ID const getContactName = (contactId) => { const contact = contacts.find(c => c.id === contactId); return contact ? contact.navn : 'Ukendt'; }; // Show loading state until client-side data is loaded if (!isLoaded) { return (
Indlæser RSVP liste...