From 52c81d6e15e4ff96761aa06d935292dc11c9df78 Mon Sep 17 00:00:00 2001 From: frakarr Date: Fri, 12 Dec 2025 00:19:56 +0100 Subject: [PATCH] Update Documents.tsx --- pages/Documents.tsx | 70 +++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/pages/Documents.tsx b/pages/Documents.tsx index d5c352d..c4aa629 100644 --- a/pages/Documents.tsx +++ b/pages/Documents.tsx @@ -40,13 +40,18 @@ export const DocumentsPage: React.FC = () => { try { const condo = await CondoService.getActiveCondo(); setActiveCondo(condo); - const docs = await CondoService.getDocuments(); - setDocuments(docs); - - // Extract unique tags - const tags = new Set(); - docs.forEach(d => d.tags.forEach(t => tags.add(t))); - setAllTags(Array.from(tags).sort()); + // Verify method exists before calling to avoid crash if service is outdated + if (typeof CondoService.getDocuments === 'function') { + const docs = await CondoService.getDocuments(); + setDocuments(docs); + + // Extract unique tags + const tags = new Set(); + docs.forEach(d => d.tags.forEach(t => tags.add(t))); + setAllTags(Array.from(tags).sort()); + } else { + console.error("CondoService.getDocuments is missing"); + } } catch(e) { console.error(e); } finally { setLoading(false); } }; @@ -80,26 +85,35 @@ export const DocumentsPage: React.FC = () => { setIsUploading(true); try { - const reader = new FileReader(); - reader.readAsDataURL(uploadForm.file); - reader.onload = async () => { - const base64 = reader.result as string; - await CondoService.uploadDocument({ - title: uploadForm.title, - description: uploadForm.description, - fileName: uploadForm.file!.name, - fileType: uploadForm.file!.type, - fileSize: uploadForm.file!.size, - tags: uploadForm.tags, - fileData: base64 - }); - setIsUploading(false); - setShowUploadModal(false); - setUploadForm({ title: '', description: '', tags: [], currentTagInput: '', file: null }); - loadData(); - }; + // Convert FileReader to Promise to handle errors in the main try/catch block + const base64 = await new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.readAsDataURL(uploadForm.file!); + reader.onload = () => resolve(reader.result as string); + reader.onerror = error => reject(error); + }); + + if (typeof CondoService.uploadDocument !== 'function') { + throw new Error("Il metodo uploadDocument non รจ disponibile nel servizio."); + } + + await CondoService.uploadDocument({ + title: uploadForm.title, + description: uploadForm.description, + fileName: uploadForm.file.name, + fileType: uploadForm.file.type, + fileSize: uploadForm.file.size, + tags: uploadForm.tags, + fileData: base64 + }); + + setIsUploading(false); + setShowUploadModal(false); + setUploadForm({ title: '', description: '', tags: [], currentTagInput: '', file: null }); + loadData(); } catch(e) { - alert("Errore upload"); + console.error("Upload error:", e); + alert("Errore durante l'upload. Riprova o contatta l'assistenza."); setIsUploading(false); } }; @@ -204,7 +218,7 @@ export const DocumentsPage: React.FC = () => {
{filteredDocs.length === 0 ? (
- Nessun documento trovato. + {loading ? "Caricamento..." : "Nessun documento trovato."}
) : ( filteredDocs.map(doc => ( @@ -289,7 +303,7 @@ export const DocumentsPage: React.FC = () => {
-