/** * Универсальный компонент загрузки файлов. * Используется для нарядов на ТО, дефектов, чек-листов. */ 'use client'; import { useState, useRef } from 'react'; interface Props { ownerKind: string; // 'work_order' | 'defect' | 'checklist' | 'aircraft' ownerId: string; onUploaded?: (att: any) => void; } export default function AttachmentUpload({ ownerKind, ownerId, onUploaded }: Props) { const [uploading, setUploading] = useState(false); const [files, setFiles] = useState([]); const inputRef = useRef(null); const handleUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; setUploading(true); try { const fd = new FormData(); fd.append('file', file); const res = await fetch(`/api/v1/attachments/${ownerKind}/${ownerId}`, { method: 'POST', body: fd }); const att = await res.json(); if (att.id) { setFiles(prev => [...prev, att]); onUploaded?.(att); } } catch (err) { console.error('Upload failed:', err); } finally { setUploading(false); if (inputRef.current) inputRef.current.value = ''; } }; return (
{files.length > 0 && (
{files.map((f: any) => (
📄 {f.filename || f.file_name}
))}
)}
); }