'use client'; import { useState, useTransition } from 'react'; import { useRouter } from 'next/navigation'; import type { ScreeningDocument } from '@/lib/types/screening'; import { Loader2, Trash2, UploadCloud } from 'lucide-react'; import { cn } from '@/lib/utils'; const DOCUMENT_BUCKET = 'screening-documents'; const BASE_STORAGE_URL = `${process.env.NEXT_PUBLIC_SUPABASE_URL}/storage/v1/object/public/${DOCUMENT_BUCKET}`; const documentTypeOptions = [ 'verwijsbrief', 'verhuisbericht', 'indicatie', 'overig', ]; interface DocumentCardProps { patientId: string; screeningId: string; documents: ScreeningDocument[]; } export function DocumentCard({ patientId, screeningId, documents }: DocumentCardProps) { const router = useRouter(); const [file, setFile] = useState(null); const [documentType, setDocumentType] = useState('verwijsbrief'); const [error, setError] = useState(null); const [isUploading, startUpload] = useTransition(); const [deletingId, setDeletingId] = useState(null); const handleUpload = () => { if (!file) { setError('Selecteer eerst een bestand.'); return; } setError(null); startUpload(async () => { const formData = new FormData(); formData.append('file', file); formData.append('documentType', documentType); try { const response = await fetch(`/api/screenings/${screeningId}/documents`, { method: 'POST', body: formData, }); if (!response.ok) { const data = await response.json().catch(() => ({})); throw new Error(data.error || 'Upload mislukt'); } setFile(null); router.refresh(); } catch (err) { console.error(err); setError(err instanceof Error ? err.message : 'Upload mislukt'); } }); }; const handleDelete = async (documentId: string) => { setDeletingId(documentId); try { const response = await fetch( `/api/screenings/${screeningId}/documents/${documentId}`, { method: 'DELETE', } ); if (!response.ok) { const data = await response.json().catch(() => ({})); throw new Error(data.error || 'Verwijderen mislukt'); } router.refresh(); } catch (err) { console.error(err); setError(err instanceof Error ? err.message : 'Verwijderen mislukt'); } finally { setDeletingId(null); } }; return (

Documenten

Upload verwijsbrieven, indicaties en andere stukken.

{ const selected = event.target.files?.[0] ?? null; setFile(selected); setError(null); }} />
{error &&

{error}

} {file && (

Geselecteerd: {file.name} • {(file.size / 1024).toFixed(1)} KB

)}
{documents.length === 0 && (

Nog geen documenten geregistreerd.

)} {documents.map((doc) => { const publicUrl = `${BASE_STORAGE_URL}/${doc.file_path}`; return (

{doc.file_name}

{doc.document_type} • {(doc.file_size / 1024).toFixed(1)} KB •{' '} {doc.uploaded_by_name || 'Onbekend'}

Download
); })}
); }