"use client"; import { useId, useRef, useState } from "react"; import { uploadMediaAction } from "./actions"; import { errorClass, inputClass, labelClass, secondaryButton } from "./ui"; // Kies een eerder geüploade afbeelding of upload een nieuwe (max 10 MB) export function ImageField({ label, value, onChange, mediaImages, emptyLabel = "Geen afbeelding", }: { label: string; value: string; onChange: (path: string) => void; mediaImages: string[]; emptyLabel?: string; }) { const [uploaded, setUploaded] = useState([]); const [uploading, setUploading] = useState(false); const [error, setError] = useState(null); const fileInputRef = useRef(null); const fieldId = useId(); async function handleUpload() { const file = fileInputRef.current?.files?.[0]; if (!file) return; const formData = new FormData(); formData.append("file", file); setUploading(true); setError(null); try { const result = await uploadMediaAction(formData); if (result.error) setError(result.error); else if (result.path) { setUploaded((u) => [result.path!, ...u]); onChange(result.path); if (fileInputRef.current) fileInputRef.current.value = ""; } } finally { setUploading(false); } } // Ook een gekozen afbeelding die niet (meer) in de lijst staat blijft zichtbaar als optie const options = [...new Set([...(value ? [value] : []), ...uploaded, ...mediaImages])]; return (
{value && ( )}
{error &&

{error}

}
); }