'use client'; import React, { useState, ChangeEvent, FormEvent } 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 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/thumbnail?id=${match[1]}&sz=w1000-h1000`; } } return link; }); }, [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' }); } 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 > 20) { newErrors.images = 'Only 20 images can be added at a time'; } 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 */}