Update Documents.tsx

This commit is contained in:
2025-12-12 00:19:56 +01:00
committed by GitHub
parent 8edf0f4652
commit 52c81d6e15

View File

@@ -40,6 +40,8 @@ export const DocumentsPage: React.FC = () => {
try { try {
const condo = await CondoService.getActiveCondo(); const condo = await CondoService.getActiveCondo();
setActiveCondo(condo); setActiveCondo(condo);
// Verify method exists before calling to avoid crash if service is outdated
if (typeof CondoService.getDocuments === 'function') {
const docs = await CondoService.getDocuments(); const docs = await CondoService.getDocuments();
setDocuments(docs); setDocuments(docs);
@@ -47,6 +49,9 @@ export const DocumentsPage: React.FC = () => {
const tags = new Set<string>(); const tags = new Set<string>();
docs.forEach(d => d.tags.forEach(t => tags.add(t))); docs.forEach(d => d.tags.forEach(t => tags.add(t)));
setAllTags(Array.from(tags).sort()); setAllTags(Array.from(tags).sort());
} else {
console.error("CondoService.getDocuments is missing");
}
} catch(e) { console.error(e); } } catch(e) { console.error(e); }
finally { setLoading(false); } finally { setLoading(false); }
}; };
@@ -80,26 +85,35 @@ export const DocumentsPage: React.FC = () => {
setIsUploading(true); setIsUploading(true);
try { try {
// Convert FileReader to Promise to handle errors in the main try/catch block
const base64 = await new Promise<string>((resolve, reject) => {
const reader = new FileReader(); const reader = new FileReader();
reader.readAsDataURL(uploadForm.file); reader.readAsDataURL(uploadForm.file!);
reader.onload = async () => { reader.onload = () => resolve(reader.result as string);
const base64 = 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({ await CondoService.uploadDocument({
title: uploadForm.title, title: uploadForm.title,
description: uploadForm.description, description: uploadForm.description,
fileName: uploadForm.file!.name, fileName: uploadForm.file.name,
fileType: uploadForm.file!.type, fileType: uploadForm.file.type,
fileSize: uploadForm.file!.size, fileSize: uploadForm.file.size,
tags: uploadForm.tags, tags: uploadForm.tags,
fileData: base64 fileData: base64
}); });
setIsUploading(false); setIsUploading(false);
setShowUploadModal(false); setShowUploadModal(false);
setUploadForm({ title: '', description: '', tags: [], currentTagInput: '', file: null }); setUploadForm({ title: '', description: '', tags: [], currentTagInput: '', file: null });
loadData(); loadData();
};
} catch(e) { } catch(e) {
alert("Errore upload"); console.error("Upload error:", e);
alert("Errore durante l'upload. Riprova o contatta l'assistenza.");
setIsUploading(false); setIsUploading(false);
} }
}; };
@@ -204,7 +218,7 @@ export const DocumentsPage: React.FC = () => {
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 overflow-y-auto pb-20"> <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 overflow-y-auto pb-20">
{filteredDocs.length === 0 ? ( {filteredDocs.length === 0 ? (
<div className="col-span-full text-center p-12 text-slate-400 border border-dashed rounded-xl"> <div className="col-span-full text-center p-12 text-slate-400 border border-dashed rounded-xl">
Nessun documento trovato. {loading ? "Caricamento..." : "Nessun documento trovato."}
</div> </div>
) : ( ) : (
filteredDocs.map(doc => ( filteredDocs.map(doc => (
@@ -289,7 +303,7 @@ export const DocumentsPage: React.FC = () => {
</div> </div>
</div> </div>
<button type="submit" disabled={isUploading || !uploadForm.file} className="w-full bg-blue-600 text-white py-3 rounded-lg font-bold hover:bg-blue-700 disabled:opacity-50 mt-4"> <button type="submit" disabled={isUploading || !uploadForm.file} className="w-full bg-blue-600 text-white py-3 rounded-lg font-bold hover:bg-blue-700 disabled:opacity-50 mt-4 flex justify-center items-center gap-2">
{isUploading ? 'Caricamento...' : 'Salva Documento'} {isUploading ? 'Caricamento...' : 'Salva Documento'}
</button> </button>
</form> </form>