'use client'; import React, { useState, ChangeEvent, FormEvent, useEffect } from 'react'; import IconTrashLines from '../icon/icon-trash-lines'; import axios from 'axios'; import { useRouter } from 'next/navigation'; import { buildApiUrl } from '@/utils/BaseUrl.utils'; interface FormErrors { [key: string]: string; } interface EditEventFormProps { eventId: string | null; } const CreateEventGalleryForm: React.FC = ({ eventId }) => { const router = useRouter(); 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 => { if (link.includes("drive.google.com")) { const match = link.match(/\/d\/([a-zA-Z0-9_-]+)/) || link.match(/id=([a-zA-Z0-9_-]+)/); if (match && match[1]) { return `https://drive.google.com/uc?export=view&id=${match[1]}`; } } return link; }); }, [linkInput]); const handleLinkInputChange = (e: ChangeEvent) => { 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: '' })); } }; const handleImageDelete = (index: number) => { const lines = linkInput.split(/[\n,]+/).map(l => l.trim()).filter(l => l !== ''); lines.splice(index, 1); setLinkInput(lines.join('\n')); }; const validateForm = (): boolean => { const newErrors: FormErrors = {}; if (parsedLinks.length === 0) { newErrors.images = 'Please provide at least one image link'; } else if (parsedLinks.length > remainingSlots) { newErrors.images = `Only ${remainingSlots} more images can be added (20 total per event).`; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); if (!validateForm()) return; setLoading(true); try { // Step 1: Prepare body for bulk save const body = { eventid: Number(eventId), imageurl: parsedLinks }; console.log("Sending body:", body); // Step 3: Call bulk API const token = localStorage.getItem('token'); await axios.post( buildApiUrl('event-images/bulk'), body, { headers: { Authorization: `Bearer ${token}`, }, } ); router.push(`/event-gallery?eventid=${eventId}`); } catch (error: any) { console.error('Upload failed:', error); const message = error.response?.data?.message || 'Failed to upload images. Please try again.'; setErrors({ submit: message }); alert(message); } finally { setLoading(false); } }; return (

Upload Gallery Images

{/* Link Input */}