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

@ -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>
);
}