feat: Enhance document management with a first draft of upload and download functionalities (needs revision)

This commit is contained in:
2026-01-12 12:42:33 +01:00
parent 6e5b9cde68
commit 325bfbe19f
7 changed files with 276 additions and 53 deletions

View File

@ -1,5 +1,6 @@
import { Download, FileText, MapPin, Plus, Search, CalendarIcon } from 'lucide-react-native';
import { ArrowLeft, Download, FileText, MapPin, Plus, Search, CalendarIcon } from 'lucide-react-native';
import React, { useEffect, useState } from 'react';
import { useRouter } from 'expo-router';
import { RangePickerModal } from '@/components/RangePickerModal';
import { Alert, RefreshControl, ScrollView, Text, TextInput, TouchableOpacity, View } from 'react-native';
import { DocumentItem } from '@/types/types';
@ -8,8 +9,10 @@ import dayjs from 'dayjs';
import LoadingScreen from '@/components/LoadingScreen';
import { formatTimestamp, parseTimestamp } from '@/utils/dateTime';
import AddDocumentModal from '@/components/AddDocumentModal';
import { downloadAndShareDocument, downloadDocumentByUrl, downloadDocumentLegacy, uploadDocument } from '@/utils/documentUtils';
export default function DocumentsScreen() {
const router = useRouter();
const [documents, setDocuments] = useState<DocumentItem[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [refreshing, setRefreshing] = useState(false);
@ -77,25 +80,9 @@ export default function DocumentsScreen() {
// Gestione Caricamento Documento
const handleUploadDocument = async (file: any, customTitle?: string) => {
console.log('Caricamento documento:', file, 'con titolo personalizzato:', customTitle);
setIsUploading(true);
try {
// const formData = new FormData();
// formData.append('file', {
// uri: file.uri,
// name: file.name,
// type: file.mimeType || 'application/octet-stream',
// } as any);
// if (customTitle) {
// formData.append('title', customTitle);
// }
// const response = await api.post('/attachment/upload', formData, {
// headers: {
// 'Content-Type': 'multipart/form-data',
// },
// });
const response = await uploadDocument(file, null, customTitle);
// console.log('Risposta caricamento:', response.data);
// Alert.alert('Successo', 'Documento caricato con successo!');
// setShowUploadModal(false);
@ -108,6 +95,16 @@ export default function DocumentsScreen() {
}
}
// Gestione Download e Condivisione Documento
const handleDownloadAndShare = async (mimetype: string, fileName: string, fileUrl: string) => {
try {
await downloadAndShareDocument(mimetype, fileName, fileUrl);
} catch (error) {
console.error('Errore nel download/condivisione del documento:', error);
Alert.alert('Errore', 'Impossibile scaricare/condividere il documento. Riprova più tardi.');
}
};
if (isLoading && !refreshing) {
return (
<LoadingScreen />
@ -117,10 +114,14 @@ export default function DocumentsScreen() {
return (
<View className="flex-1 bg-gray-50">
{/* Header */}
{/* TODO: Aggiungi torna indietro */}
<View className="bg-white p-6 pt-16 shadow-sm border-b border-gray-100">
<Text className="text-3xl font-bold text-gray-800 mb-1">Documenti</Text>
<Text className="text-base text-gray-500">Gestisci i tuoi documenti</Text>
<View className="flex-row items-center gap-4 bg-white p-6 pt-16 shadow-sm border-b border-gray-100">
<TouchableOpacity onPress={() => router.back()} className="p-2 -ml-2 rounded-full active:bg-gray-100">
<ArrowLeft size={24} color="#374151" />
</TouchableOpacity>
<View className="flex-1">
<Text className="text-3xl font-bold text-gray-800">Documenti</Text>
<Text className="text-base text-gray-500">Gestisci i tuoi documenti</Text>
</View>
</View>
<View className="p-5 gap-6 flex-1">
@ -179,7 +180,9 @@ export default function DocumentsScreen() {
</View>
</View>
</View>
<TouchableOpacity className="p-4 bg-gray-50 rounded-2xl active:bg-gray-100">
<TouchableOpacity
onPress={() => downloadDocumentLegacy(doc.mimetype, doc.title, doc.url)} // downloadDocumentByUrl(doc.url, doc.title) handleDownloadAndShare(doc.mimetype, doc.title, doc.url)
className="p-4 bg-gray-50 rounded-2xl active:bg-gray-100">
<Download size={24} color="#6b7280" />
</TouchableOpacity>
</View>

View File

@ -8,6 +8,8 @@ import LoadingScreen from '@/components/LoadingScreen';
import api from '@/utils/api';
import dayjs from 'dayjs';
import { formatTimestamp, parseTimestamp } from '@/utils/dateTime';
import { downloadAndShareDocument, uploadDocument } from '@/utils/documentUtils';
import AddDocumentModal from '@/components/AddDocumentModal';
export default function SiteDocumentsScreen() {
const router = useRouter();
@ -17,6 +19,9 @@ export default function SiteDocumentsScreen() {
const [isLoading, setIsLoading] = useState(true);
const [refreshing, setRefreshing] = useState(false);
const [showUploadModal, setShowUploadModal] = useState(false);
const [isUploading, setIsUploading] = useState(false);
// Fetch dei documenti del cantiere
const fetchSiteDocuments = useCallback(async (siteId: number, isRefreshing = false) => {
try {
@ -88,6 +93,33 @@ export default function SiteDocumentsScreen() {
return true;
});
// Gestione Download e Condivisione Documento
const handleDownloadAndShare = async (mimetype: string, fileName: string, fileUrl: string) => {
try {
await downloadAndShareDocument(mimetype, fileName, fileUrl);
} catch (error) {
console.error('Errore nel download/condivisione del documento:', error);
Alert.alert('Errore', 'Impossibile scaricare il documento. Riprova più tardi.');
}
};
// Gestione Caricamento Documento
const handleUploadDocument = async (file: any, customTitle?: string) => {
setIsUploading(true);
try {
const response = await uploadDocument(file, Number(params.id), customTitle);
// console.log('Risposta caricamento:', response.data);
// Alert.alert('Successo', 'Documento caricato con successo!');
// setShowUploadModal(false);
// fetchSiteDocuments(Number(params.id)); // Ricarica la lista dei documenti
} catch (error) {
console.error('Errore nel caricamento del documento:', error);
Alert.alert('Errore', 'Impossibile caricare il documento. Riprova più tardi.');
} finally {
setIsUploading(false);
}
}
if (!site || (isLoading && !refreshing)) {
return (
<LoadingScreen />
@ -175,7 +207,9 @@ export default function SiteDocumentsScreen() {
<Text className="text-sm text-gray-400 font-medium">{formatTimestamp(doc.updated_at)}</Text>
</View>
</View>
<TouchableOpacity className="p-4 bg-gray-50 rounded-2xl active:bg-gray-100">
<TouchableOpacity
onPress={() => handleDownloadAndShare(doc.mimetype, doc.title, doc.url)}
className="p-4 bg-gray-50 rounded-2xl active:bg-gray-100">
<Download size={24} color="#6b7280" />
</TouchableOpacity>
</View>
@ -189,11 +223,19 @@ export default function SiteDocumentsScreen() {
{/* FAB (Spostato qui) */}
<TouchableOpacity
onPress={() => alert(`Aggiungi doc a ${site.name}`)}
onPress={() => setShowUploadModal(true)}
className="absolute bottom-8 right-6 w-16 h-16 bg-[#099499] rounded-full shadow-lg items-center justify-center active:scale-90"
>
<Plus size={32} color="white" />
</TouchableOpacity>
{/* Modale Caricamento Documento */}
<AddDocumentModal
visible={showUploadModal}
onClose={() => setShowUploadModal(false)}
onUpload={handleUploadDocument}
isUploading={isUploading}
/>
</View>
);
}