"use client"; import React, { useRef, useState } from "react"; import { useAuth } from "@/hooks/useAuth"; import { apiFetch } from "@/lib/api"; export function FileUploader({ onUploaded }: { onUploaded: (url: string) => void }) { const { token } = useAuth(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const inputRef = useRef(null); async function upload(file: File) { if (!token) { setError("Please login"); return; } setLoading(true); setError(null); try { const fd = new FormData(); fd.append("image", file); const data = await apiFetch<{ url: string }>(`/api/uploads/event-image`, { method: "POST", body: fd, authToken: token, }); onUploaded(data.url); } catch (e: any) { setError(e?.message || "Upload failed"); } finally { setLoading(false); } } return (
{ const f = e.target.files?.[0]; if (f) upload(f); }} /> {loading &&

Uploading...

} {error &&

{error}

}
); }