import DocumentListCard from '@/components/DocumentListCard'; import LoadingScreen from '@/components/LoadingScreen'; import { DocumentItem } from '@/types/types'; import api from '@/utils/api'; import * as DocumentPicker from 'expo-document-picker'; import { useLocalSearchParams, useRouter } from 'expo-router'; import { StatusBar } from 'expo-status-bar'; import { ChevronLeft, AlertCircle } from 'lucide-react-native'; import { useAlert } from '@/components/AlertComponent'; import { uploadDocument } from '@/utils/documentUtils'; import React, { useCallback, useEffect, useState } from 'react'; import { ActivityIndicator, RefreshControl, ScrollView, Text, TouchableOpacity, View } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; export default function ConstructionSiteDocumentsScreen() { const { id, name } = useLocalSearchParams(); const router = useRouter(); const alert = useAlert(); const [documents, setDocuments] = useState([]); const [isLoading, setIsLoading] = useState(true); const [isRefreshing, setIsRefreshing] = useState(false); const [isUploading, setIsUploading] = useState(false); const fetchDocuments = useCallback(async () => { try { const response = await api.get(`/construction-site/get-attachments?id=${id}`); if (response.data?.success) { setDocuments(response.data.result || []); } } catch (error) { console.error('Errore nel recupero dei documenti:', error); alert.showAlert('error', 'Errore', 'Impossibile recuperare la lista dei documenti.'); } finally { setIsLoading(false); setIsRefreshing(false); } }, [id]); useEffect(() => { if (id) { fetchDocuments(); } }, [id, fetchDocuments]); const onRefresh = () => { setIsRefreshing(true); fetchDocuments(); }; const handleUpload = async () => { try { const result = await DocumentPicker.getDocumentAsync({ type: '*/*', copyToCacheDirectory: true, multiple: false, }); if (result.canceled || !result.assets || result.assets.length === 0) { return; } const file = result.assets[0]; setIsUploading(true); await uploadDocument(file, { endpoint: '/construction-site/upload-attachment', fileKey: 'files', extraData: { model_classname: 'ConstructionSite', model_id: id as string, method: 'put', name: file.name, type: file.mimeType || 'application/octet-stream' } }); alert.showAlert('success', 'Successo', 'Documento caricato con successo.'); onRefresh(); // reload list } catch (error: any) { console.error('Errore durante l\'upload:', error); alert.showAlert('error', 'Errore', error.message || 'Si รจ verificato un errore durante l\'upload del documento.'); } finally { setIsUploading(false); } }; if (isLoading && !isRefreshing) { return ; } return ( {/* Header */} router.back()} className="p-2 -ml-2 rounded-full active:bg-white/20 w-12 items-center justify-center" > {name || 'Allegati Cantiere'} {/* Content */} {isUploading && ( Caricamento in corso... )} } > Documenti {isUploading ? ( ) : ( Aggiungi )} {documents.length > 0 ? ( {documents.map((item, index) => ( ))} ) : ( Nessun documento caricato per questo cantiere. )} ); }