'use client'; import React, { useEffect, useState } from 'react'; import Lightbox from 'yet-another-react-lightbox'; import Captions from 'yet-another-react-lightbox/plugins/captions'; import Zoom from 'yet-another-react-lightbox/plugins/zoom'; import 'yet-another-react-lightbox/styles.css'; import 'yet-another-react-lightbox/plugins/captions.css'; import axios from 'axios'; import { useRouter } from 'next/navigation'; import IconTrashLines from '../icon/icon-trash-lines'; import Swal from 'sweetalert2'; interface EditEventFormProps { eventId: string | null; } const ListOfEventsGallery: React.FC = ({ eventId }) => { const router = useRouter(); const [isOpen, setIsOpen] = useState(false); const [photoIndex, setPhotoIndex] = useState(0); const [eventImages, setEventImages] = useState([]); useEffect(() => { if (eventId) { getEventGallery(); } }, [eventId]); const getEventGallery = async () => { try { const res = await axios.get(`https://api.tamilculturewaterloo.org/api/event-images/event/${eventId}`); const formatted = res.data?.data?.map((img: any) => ({ id: img.id, // ensure ID is preserved for deletion src: img.imageurl, title: img?.title || '', description: img?.description || '', })); setEventImages(formatted || []); } catch (error) { console.log("error", error); } }; const showAlert = async (e: React.MouseEvent, item: any) => { e.stopPropagation(); // ✅ Prevents triggering the parent onClick (lightbox) if (isOpen) setIsOpen(false); // optional: close lightbox if it's open Swal.fire({ icon: 'warning', title: 'Are you sure?', text: "You won't be able to revert this!", showCancelButton: true, confirmButtonText: 'Delete', padding: '2em', customClass: { popup: 'sweet-alerts' }, }).then(async (result) => { if (result.isConfirmed) { try { await axios.delete(`https://api.tamilculturewaterloo.org/api/event-images/${item.id}`); Swal.fire({ title: 'Deleted!', text: 'Your file has been deleted.', icon: 'success', customClass: { popup: 'sweet-alerts' }, }); getEventGallery(); // refresh after delete } catch (error) { Swal.fire({ title: 'Error!', text: 'Failed to delete the file.', icon: 'error', customClass: { popup: 'sweet-alerts' }, }); } } }); }; return (
Gallery
{eventImages.map((item, index) => (
{ setPhotoIndex(index); setIsOpen(true); }} > {/* Delete button (top-right corner) */}
{`gallery-${index}`}
))}
setIsOpen(false)} slides={eventImages} index={photoIndex} plugins={[Captions, Zoom]} />
); }; export default ListOfEventsGallery;