Files
crosscode-platform/apps/events/frontend/src/components/forms/FileUploader.tsx
T
joshua fdd2d241b4 Initial commit
Next.js + Express event management app for Hope Family Church.
2026-07-23 15:26:47 +02:00

50 lines
1.3 KiB
TypeScript

"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<string | null>(null);
const inputRef = useRef<HTMLInputElement | null>(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 (
<div>
<input
ref={inputRef}
type="file"
accept="image/*"
className="block w-full text-sm"
onChange={(e) => {
const f = e.target.files?.[0];
if (f) upload(f);
}}
/>
{loading && <p className="text-xs text-gray-600 mt-1">Uploading...</p>}
{error && <p className="text-xs text-red-600 mt-1">{error}</p>}
</div>
);
}