Se soluciono autenticacion
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import React, { useEffect, useState, useLayoutEffect, useRef } from 'react';
|
||||
import SuccessModal from '../components/SuccessModal.jsx';
|
||||
import { fetchWithAuth, postWithAuth } from '../fetchWithAuth';
|
||||
// Animación fade-in/slide-up para bloques
|
||||
const fadeInSlideUp = `@keyframes fadein-slideup { 0% { opacity: 0; transform: translateY(40px); } 100% { opacity: 1; transform: translateY(0); } }`;
|
||||
if (typeof document !== 'undefined' && !document.getElementById('fadein-slideup-documents')) {
|
||||
@@ -17,35 +18,28 @@ const API_URL = import.meta.env.VITE_EFC_API_URL;
|
||||
|
||||
// Descarga individual
|
||||
const downloadFile = async (id, filename = 'archivo', setSuccess, setError, showMessage) => {
|
||||
const token = localStorage.getItem('access');
|
||||
const res = await fetch(`${API_URL}/record/documents/descargar/${id}/`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
if (res.status === 401) {
|
||||
showMessage('Tu sesión ha expirado, por favor inicia sesión de nuevo.', 'error');
|
||||
localStorage.removeItem('access');
|
||||
localStorage.removeItem('refresh');
|
||||
setTimeout(() => {
|
||||
window.location.href = '/login';
|
||||
}, 2000);
|
||||
return;
|
||||
try {
|
||||
const res = await fetchWithAuth(`${API_URL}/record/documents/descargar/${id}/`);
|
||||
|
||||
if (!res.ok) {
|
||||
alert('No autorizado o error en la descarga');
|
||||
return;
|
||||
}
|
||||
|
||||
const blob = await res.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
window.URL.revokeObjectURL(url);
|
||||
if (setSuccess) setSuccess('Descarga exitosa');
|
||||
} catch (error) {
|
||||
console.error('Error downloading file:', error);
|
||||
showMessage('Error al descargar el archivo', 'error');
|
||||
}
|
||||
if (!res.ok) {
|
||||
alert('No autorizado o error en la descarga');
|
||||
return;
|
||||
}
|
||||
const blob = await res.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
window.URL.revokeObjectURL(url);
|
||||
if (setSuccess) setSuccess('Descarga exitosa');
|
||||
};
|
||||
|
||||
// Descarga masiva (bulk)
|
||||
@@ -54,38 +48,32 @@ const downloadBulkZip = async (ids, showMessage, setSuccess, nombreZip = 'docume
|
||||
showMessage('Selecciona al menos un documento.', 'error');
|
||||
return;
|
||||
}
|
||||
const token = localStorage.getItem('access');
|
||||
const res = await fetch(`${API_URL}/record/documents/bulk-download/`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ document_ids: ids, pedimento_nombre: nombreZip }),
|
||||
});
|
||||
if (res.status === 401) {
|
||||
showMessage('Tu sesión ha expirado, por favor inicia sesión de nuevo.', 'error');
|
||||
localStorage.removeItem('access');
|
||||
localStorage.removeItem('refresh');
|
||||
setTimeout(() => {
|
||||
window.location.href = '/login';
|
||||
}, 2000);
|
||||
return;
|
||||
|
||||
try {
|
||||
const res = await postWithAuth(`${API_URL}/record/documents/bulk-download/`, {
|
||||
document_ids: ids,
|
||||
pedimento_nombre: nombreZip
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
showMessage('No autorizado o error en la descarga masiva', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
const blob = await res.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `${nombreZip || 'documentos'}.zip`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
window.URL.revokeObjectURL(url);
|
||||
if (setSuccess) setSuccess('Descarga(s) completada(s)');
|
||||
} catch (error) {
|
||||
console.error('Error in bulk download:', error);
|
||||
showMessage('Error en la descarga masiva', 'error');
|
||||
}
|
||||
if (!res.ok) {
|
||||
showMessage('No autorizado o error en la descarga masiva', 'error');
|
||||
return;
|
||||
}
|
||||
const blob = await res.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `${nombreZip || 'documentos'}.zip`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
window.URL.revokeObjectURL(url);
|
||||
if (setSuccess) setSuccess('Descarga(s) completada(s)');
|
||||
};
|
||||
|
||||
export default function Documents() {
|
||||
@@ -128,8 +116,7 @@ export default function Documents() {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const token = localStorage.getItem('access');
|
||||
const data = await fetchPedimentoDocuments(token, '', currentPage, itemsPerPage, {
|
||||
const data = await fetchPedimentoDocuments(currentPage, itemsPerPage, {
|
||||
pedimento_numero: pedimentoNumeroFilter,
|
||||
extension: extensionFilter,
|
||||
document_type: documentTypeFilter,
|
||||
|
||||
Reference in New Issue
Block a user