From d825cb5f4139a02ff4c98f19b71eb6e7079e3a40 Mon Sep 17 00:00:00 2001 From: Kevin Rosales Date: Tue, 5 Aug 2025 16:20:50 -0600 Subject: [PATCH] Se modifico formulario de actualizazcion --- .env | 6 +- src/fetchWithAuth.js | 136 +++++++++++++++++++++++++++++++++++++++++++ src/pages/Vucem.jsx | 6 +- 3 files changed, 142 insertions(+), 6 deletions(-) diff --git a/.env b/.env index 6e3cec2..bae8a6f 100644 --- a/.env +++ b/.env @@ -1,4 +1,4 @@ -DEBUG_MODE=false +VITE_DEBUG_MODE=false -VITE_EFC_API_URL=https://api.efc-aduanasoft.com/api/v1 -VITE_EFC_MICROSERVICE_URL=https://api.efc-aduanasoft.com/microservice/api/v1 +VITE_EFC_API_URL=http://192.168.1.195:8000/api/v1 +VITE_EFC_MICROSERVICE_URL=http://192.168.1.195:8001/api/v1 diff --git a/src/fetchWithAuth.js b/src/fetchWithAuth.js index c726a8b..5c98fb2 100644 --- a/src/fetchWithAuth.js +++ b/src/fetchWithAuth.js @@ -289,4 +289,140 @@ export const postFormDataWithAuth = async (url, formData) => { } }; +// Función para hacer peticiones PUT con FormData (para archivos) +export const putFormDataWithAuth = async (url, formData) => { + let token = localStorage.getItem('access'); + + const options = { + method: 'PUT', + headers: { + ...(token && { 'Authorization': `Bearer ${token}` }) + }, + body: formData + }; + + try { + let response = await fetch(url, options); + + if (response.status === 401) { + if (!isRefreshing) { + isRefreshing = true; + + try { + const newToken = await refreshToken(); + processQueue(null, newToken); + + options.headers['Authorization'] = `Bearer ${newToken}`; + response = await fetch(url, options); + + } catch (refreshError) { + processQueue(refreshError, null); + throw refreshError; + } finally { + isRefreshing = false; + } + } else { + // Ya hay un refresh en proceso, agregar esta petición a la cola + return new Promise((resolve, reject) => { + failedQueue.push({ + resolve: (token) => { + options.headers['Authorization'] = `Bearer ${token}`; + fetch(url, options) + .then(resolve) + .catch(reject); + }, + reject + }); + }); + } + } + + if (response.status === 401) { + localStorage.removeItem('access'); + localStorage.removeItem('refresh'); + localStorage.removeItem('user_id'); + localStorage.removeItem('user_is_importador'); + + setTimeout(() => { + window.location.href = '/login'; + }, 1000); + + throw new Error('Session expired'); + } + + return response; + + } catch (error) { + throw error; + } +}; + +// Función para hacer peticiones PATCH con FormData (para archivos) +export const patchFormDataWithAuth = async (url, formData) => { + let token = localStorage.getItem('access'); + + const options = { + method: 'PATCH', + headers: { + ...(token && { 'Authorization': `Bearer ${token}` }) + }, + body: formData + }; + + try { + let response = await fetch(url, options); + + if (response.status === 401) { + if (!isRefreshing) { + isRefreshing = true; + + try { + const newToken = await refreshToken(); + processQueue(null, newToken); + + options.headers['Authorization'] = `Bearer ${newToken}`; + response = await fetch(url, options); + + } catch (refreshError) { + processQueue(refreshError, null); + throw refreshError; + } finally { + isRefreshing = false; + } + } else { + // Ya hay un refresh en proceso, agregar esta petición a la cola + return new Promise((resolve, reject) => { + failedQueue.push({ + resolve: (token) => { + options.headers['Authorization'] = `Bearer ${token}`; + fetch(url, options) + .then(resolve) + .catch(reject); + }, + reject + }); + }); + } + } + + if (response.status === 401) { + localStorage.removeItem('access'); + localStorage.removeItem('refresh'); + localStorage.removeItem('user_id'); + localStorage.removeItem('user_is_importador'); + + setTimeout(() => { + window.location.href = '/login'; + }, 1000); + + throw new Error('Session expired'); + } + + return response; + + } catch (error) { + throw error; + } +}; + export default fetchWithAuth; diff --git a/src/pages/Vucem.jsx b/src/pages/Vucem.jsx index 047e0a4..6548243 100644 --- a/src/pages/Vucem.jsx +++ b/src/pages/Vucem.jsx @@ -1,5 +1,5 @@ import React, { useEffect, useState } from 'react'; -import { fetchWithAuth, postWithAuth, putWithAuth, deleteWithAuth } from '../fetchWithAuth'; +import { fetchWithAuth, postWithAuth, putWithAuth, deleteWithAuth, putFormDataWithAuth, postFormDataWithAuth } from '../fetchWithAuth'; const API_URL = import.meta.env.VITE_EFC_API_URL; export default function Vucem() { @@ -931,7 +931,7 @@ export default function Vucem() { formData.append('acuseedocument', form.acuseedocument); formData.append('is_active', form.is_active); try { - const res = await postWithAuth(`${API_URL}/vucem/vucem/`, formData); + const res = await postFormDataWithAuth(`${API_URL}/vucem/vucem/`, formData); if (!res.ok) throw new Error('Error al crear VUCEM'); await fetchVucem(); closeModals(); @@ -1095,7 +1095,7 @@ export default function Vucem() { formData.append('acuseedocument', form.acuseedocument); formData.append('is_active', form.is_active); try { - const res = await putWithAuth(`${API_URL}/vucem/vucem/${editVucem.id}/`, formData); + const res = await putFormDataWithAuth(`${API_URL}/vucem/vucem/${editVucem.id}/`, formData); if (!res.ok) throw new Error('Error al actualizar VUCEM'); await fetchVucem(); closeModals();