'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 extractDriveFileId = (link: string): string | null => { const trimmed = link?.trim(); if (!trimmed) return null; const match = trimmed.match(/\/d\/([a-zA-Z0-9_-]+)/) || trimmed.match(/[?&]id=([a-zA-Z0-9_-]+)/) || trimmed.match(/\/open\?id=([a-zA-Z0-9_-]+)/); return match?.[1] ?? null; }; const buildDriveViewUrl = (id: string) => `https://drive.google.com/thumbnail?id=${id}&sz=w1000`; const buildDriveDownloadUrl = (id: string) => `https://drive.google.com/uc?export=download&id=${id}`; const buildDriveThumbnailUrl = (id: string) => `https://drive.google.com/thumbnail?id=${id}&sz=w1000`; const buildDriveViewAuthUrl = (id: string) => `https://drive.google.com/uc?export=view&id=${id}&authuser=0`; const normalizeDriveImageUrl = (link: string): string => { const trimmed = link?.trim() || ''; const id = extractDriveFileId(trimmed); if (!id) return trimmed; return buildDriveViewUrl(id); }; const getDriveFallbackUrl = (currentUrl: string): string | null => { const id = extractDriveFileId(currentUrl); if (!id) return null; if (currentUrl.includes('thumbnail?id=')) { return buildDriveViewAuthUrl(id); } if (currentUrl.includes('export=download')) { return buildDriveThumbnailUrl(id); } if (currentUrl.includes('export=view')) { return buildDriveDownloadUrl(id); } return buildDriveViewAuthUrl(id); }; 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((link) => link.trim()) .filter((link) => link !== '') .map(normalizeDriveImageUrl); }, [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 */}