diff --git a/app/(defaults)/event-gallery-full/page.tsx b/app/(defaults)/event-gallery-full/page.tsx index 157f5e4..df4fcda 100644 --- a/app/(defaults)/event-gallery-full/page.tsx +++ b/app/(defaults)/event-gallery-full/page.tsx @@ -1,6 +1,6 @@ 'use client' import React from 'react'; -import FullEventGallery from '@/components/gallery/FullEventGallery'; +import ListOfEventsGallery from '@/components/gallery/ListOfEventGallery'; import { useSearchParams } from 'next/navigation'; const EventGalleryFullPage = () => { @@ -9,7 +9,7 @@ const EventGalleryFullPage = () => { return (
- +
); }; diff --git a/components/gallery/CreateEventGalleryForm.tsx b/components/gallery/CreateEventGalleryForm.tsx index 82bc0b1..2ca97fc 100644 --- a/components/gallery/CreateEventGalleryForm.tsx +++ b/components/gallery/CreateEventGalleryForm.tsx @@ -1,5 +1,5 @@ 'use client'; -import React, { useState, ChangeEvent, FormEvent } from 'react'; +import React, { useState, ChangeEvent, FormEvent, useEffect } from 'react'; import IconTrashLines from '../icon/icon-trash-lines'; import axios from 'axios'; import { useRouter } from 'next/navigation'; @@ -18,6 +18,33 @@ const CreateEventGalleryForm: React.FC = ({ eventId }) => { const [linkInput, setLinkInput] = useState(''); const [loading, setLoading] = useState(false); const [errors, setErrors] = useState({}); + const [existingImagesCount, setExistingImagesCount] = useState(0); + const [fetchingImages, setFetchingImages] = useState(true); + + useEffect(() => { + if (eventId) { + getEventGallery(); + } else { + setFetchingImages(false); + } + }, [eventId]); + + const getEventGallery = async () => { + try { + const token = localStorage.getItem("token"); + const res = await axios.get(buildApiUrl(`event-images/event/${eventId}`), { + headers: { Authorization: `Bearer ${token}` }, + }); + const images = res.data?.data || []; + setExistingImagesCount(images.length); + } catch (error) { + console.error('error fetching existing images', error); + } finally { + setFetchingImages(false); + } + }; + + const remainingSlots = Math.max(0, 20 - existingImagesCount); const parsedLinks = React.useMemo(() => { return linkInput.split(/[\n,]+/).map(l => l.trim()).filter(l => l !== '').map(link => { @@ -32,9 +59,11 @@ const CreateEventGalleryForm: React.FC = ({ eventId }) => { }, [linkInput]); const handleLinkInputChange = (e: ChangeEvent) => { - setLinkInput(e.target.value); - if (parsedLinks.length > 20) { - setErrors({ images: 'You can only add up to 20 images at a time' }); + const newValue = e.target.value; + setLinkInput(newValue); + const currentCount = newValue.split(/[\n,]+/).map(l => l.trim()).filter(l => l !== '').length; + if (currentCount > remainingSlots) { + setErrors({ images: `You can only add up to ${remainingSlots} more images (20 total per event).` }); } else { setErrors(prev => ({ ...prev, images: '' })); } @@ -51,8 +80,8 @@ const CreateEventGalleryForm: React.FC = ({ eventId }) => { if (parsedLinks.length === 0) { newErrors.images = 'Please provide at least one image link'; - } else if (parsedLinks.length > 20) { - newErrors.images = 'Only 20 images can be added at a time'; + } else if (parsedLinks.length > remainingSlots) { + newErrors.images = `Only ${remainingSlots} more images can be added (20 total per event).`; } setErrors(newErrors); @@ -109,10 +138,17 @@ const CreateEventGalleryForm: React.FC = ({ eventId }) => { placeholder="Paste your Google Drive links here, one per line..." value={linkInput} onChange={handleLinkInputChange} - className="w-full border rounded px-3 py-2" + disabled={remainingSlots === 0} + className="w-full border rounded px-3 py-2 disabled:bg-gray-100 disabled:cursor-not-allowed dark:disabled:bg-gray-800" /> {errors.images &&

{errors.images}

} -

You can add up to 20 images.

+

+ {fetchingImages + ? 'Checking current image count...' + : remainingSlots > 0 + ? `You can add up to ${remainingSlots} more images (currently ${existingImagesCount}/20).` + : `You have reached the maximum of 20 images for this event.`} +

{/* Previews */} @@ -141,13 +177,20 @@ const CreateEventGalleryForm: React.FC = ({ eventId }) => { )} {/* Submit */} -
+
+
@@ -155,3 +198,4 @@ const CreateEventGalleryForm: React.FC = ({ eventId }) => { }; export default CreateEventGalleryForm; + diff --git a/components/gallery/ListOfEventGallery.tsx b/components/gallery/ListOfEventGallery.tsx index 6dbeb21..5d72807 100644 --- a/components/gallery/ListOfEventGallery.tsx +++ b/components/gallery/ListOfEventGallery.tsx @@ -57,7 +57,7 @@ const ListOfEventsGallery: React.FC = ({ eventId }) => { }, }); const formatted: GalleryImage[] = res.data?.data?.map((img: any) => { - console.log("Image URL:", img.imageurl); + // console.log("Image URL:", img.imageurl); return { id: img.id, @@ -82,7 +82,9 @@ const ListOfEventsGallery: React.FC = ({ eventId }) => { id: item.id, sort_order: index, })); + console.log("Sending reorder payload:", images); const token = localStorage.getItem("token"); + await axios.put(buildApiUrl('event-images/reorder'), { images }, { @@ -161,13 +163,22 @@ const ListOfEventsGallery: React.FC = ({ eventId }) => { {/* Header */}
Gallery
- +
+ + +
{/* Gallery Grid */}