);
};
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.`}
+