Se modifico formulario de actualizazcion
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user