diff --git a/components/ClientPortal.tsx b/components/ClientPortal.tsx index 3b92207..de94550 100644 --- a/components/ClientPortal.tsx +++ b/components/ClientPortal.tsx @@ -141,18 +141,42 @@ export const ClientPortal: React.FC = ({ setIsTyping(false); }; - const submitTicket = (e: React.FormEvent) => { + const submitTicket = async (e: React.FormEvent) => { e.preventDefault(); + setIsUploading(true); const attachments: Attachment[] = []; - if (ticketFiles) { + + if (ticketFiles && ticketFiles.length > 0) { for (let i = 0; i < ticketFiles.length; i++) { const file = ticketFiles[i]; - attachments.push({ - id: `att-${Date.now()}-${i}`, - name: file.name, - type: file.type, - url: URL.createObjectURL(file) // Note: This is client-side only URL for preview, real implementation would upload here too or later - }); + + // Validation check before upload + const maxSize = (settings.features.maxFileSizeMb || 5) * 1024 * 1024; + if (file.size > maxSize) { + showToast(`File ${file.name} troppo grande. Max ${settings.features.maxFileSizeMb}MB`, 'error'); + continue; + } + + const formData = new FormData(); + formData.append('file', file); + + try { + const res = await fetch('/api/upload', { method: 'POST', body: formData }); + if(res.ok) { + const data = await res.json(); + attachments.push({ + id: data.id, + name: data.name, + url: data.url, + type: data.type + }); + } else { + showToast(`Errore caricamento ${file.name}`, 'error'); + } + } catch(err) { + console.error(err); + showToast(`Errore di rete caricamento ${file.name}`, 'error'); + } } } @@ -167,6 +191,7 @@ export const ClientPortal: React.FC = ({ setTicketForm({ subject: '', description: '', queue: queues.length > 0 ? queues[0].name : 'General' }); setTicketFiles(null); + setIsUploading(false); setActiveView('dashboard'); }; @@ -390,6 +415,20 @@ export const ClientPortal: React.FC = ({
{selectedTicket.description}
+ + {selectedTicket.attachments && selectedTicket.attachments.length > 0 && ( +
+

Allegati Iniziali

+
+ {selectedTicket.attachments.map(att => ( + + {att.name} + + ))} +
+
+ )} +

Messaggi

{selectedTicket.messages.map(m => ( @@ -440,12 +479,22 @@ export const ClientPortal: React.FC = ({
setTicketForm({...ticketForm, subject: e.target.value})} required />