Update Documents.tsx
This commit is contained in:
@@ -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<string>();
|
||||
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<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); }
|
||||
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<string>((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 = () => {
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 overflow-y-auto pb-20">
|
||||
{filteredDocs.length === 0 ? (
|
||||
<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>
|
||||
) : (
|
||||
filteredDocs.map(doc => (
|
||||
@@ -289,7 +303,7 @@ export const DocumentsPage: React.FC = () => {
|
||||
</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'}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user