'use client'; import React, { useEffect, useRef, 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 IconMenu from '../icon/icon-menu'; import Swal from 'sweetalert2'; import { buildApiUrl } from '@/utils/BaseUrl.utils'; import { ReactSortable } from 'react-sortablejs'; import Sortable, { Swap } from 'sortablejs'; if (typeof window !== 'undefined') { Sortable.mount(new Swap()); } interface GalleryImage { id: number; src: string; title: string; description: string; } 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([]); const latestImagesRef = useRef([]); useEffect(() => { latestImagesRef.current = eventImages; }, [eventImages]); useEffect(() => { if (eventId) { getEventGallery(); } }, [eventId]); const getEventGallery = async () => { try { const res = await axios.get(buildApiUrl(`event-images/event/${eventId}`)); const formatted: GalleryImage[] = res.data?.data?.map((img: any) => ({ id: img.id, src: img.imageurl, title: img?.title || '', description: img?.description || '', })) || []; setEventImages(formatted); } catch (error) { console.log('error', error); } }; const handleReorder = (newList: GalleryImage[]) => { setEventImages(newList); }; const saveNewOrder = async (list: GalleryImage[]) => { try { const images = list.map((item, index) => ({ id: item.id, sort_order: index, })); await axios.put(buildApiUrl('event-images/reorder'), { images }); Swal.fire({ title: 'Saved!', text: 'Image order saved successfully.', icon: 'success', toast: true, position: 'top-end', showConfirmButton: false, timer: 2000, }); } catch (error: any) { console.error('Failed to save order:', error); const errMsg = error.response?.data?.message || error.message || 'Unknown error'; Swal.fire({ title: 'Error!', text: `Failed to save the new order: ${errMsg}`, icon: 'error', toast: true, position: 'top-end', showConfirmButton: false, timer: 5000, }); } }; const showAlert = async (e: React.MouseEvent, item: GalleryImage) => { e.stopPropagation(); if (isOpen) setIsOpen(false); 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(buildApiUrl(`event-images/${item.id}`)); Swal.fire({ title: 'Deleted!', text: 'Your file has been deleted.', icon: 'success', customClass: { popup: 'sweet-alerts' }, }); getEventGallery(); } catch (error) { Swal.fire({ title: 'Error!', text: 'Failed to delete the file.', icon: 'error', customClass: { popup: 'sweet-alerts' }, }); } } }); }; return (
{/* Header */}
Gallery
{/* Gallery Grid */} {eventImages.length === 0 ? (

No images yet

Click "Upload Images" to add some!

) : ( <>

Drag the ☰ icon to reorder images

saveNewOrder(latestImagesRef.current)} animation={200} handle=".drag-handle" swap={true} swapClass="swap-highlight" className="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4 items-start" > {eventImages.map((item, index) => (
{ setPhotoIndex(index); setIsOpen(true); }} > {/* Drag Handle */}
{/* Delete Button */}
{`gallery-${index}`} e.preventDefault()} className="w-full h-auto" />
))}
)} setIsOpen(false)} slides={eventImages} index={photoIndex} plugins={[Captions, Zoom]} />
); }; export default ListOfEventsGallery;