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,13 +40,18 @@ export const DocumentsPage: React.FC = () => {
try { try {
const condo = await CondoService.getActiveCondo(); const condo = await CondoService.getActiveCondo();
setActiveCondo(condo); setActiveCondo(condo);
const docs = await CondoService.getDocuments(); // Verify method exists before calling to avoid crash if service is outdated
setDocuments(docs); if (typeof CondoService.getDocuments === 'function') {
const docs = await CondoService.getDocuments();
// Extract unique tags setDocuments(docs);
const tags = new Set<string>();
docs.forEach(d => d.tags.forEach(t => tags.add(t))); // Extract unique tags
setAllTags(Array.from(tags).sort()); const tags = new Set<string>();
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); } } 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 {
const reader = new FileReader(); // Convert FileReader to Promise to handle errors in the main try/catch block
reader.readAsDataURL(uploadForm.file); const base64 = await new Promise<string>((resolve, reject) => {
reader.onload = async () => { const reader = new FileReader();
const base64 = reader.result as string; reader.readAsDataURL(uploadForm.file!);
await CondoService.uploadDocument({ reader.onload = () => resolve(reader.result as string);
title: uploadForm.title, reader.onerror = error => reject(error);
description: uploadForm.description, });
fileName: uploadForm.file!.name,
fileType: uploadForm.file!.type, if (typeof CondoService.uploadDocument !== 'function') {
fileSize: uploadForm.file!.size, throw new Error("Il metodo uploadDocument non è disponibile nel servizio.");
tags: uploadForm.tags, }
fileData: base64
}); await CondoService.uploadDocument({
setIsUploading(false); title: uploadForm.title,
setShowUploadModal(false); description: uploadForm.description,
setUploadForm({ title: '', description: '', tags: [], currentTagInput: '', file: null }); fileName: uploadForm.file.name,
loadData(); 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) { } 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>