Feat: Cambios Varios
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import axios from 'axios';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_URL, // Usa la variable de entorno
|
||||
baseURL: import.meta.env.VITE_API_URL,
|
||||
});
|
||||
|
||||
// 1. Interceptor de Solicitud (Request): Pega el token antes de salir
|
||||
api.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
@@ -12,4 +13,30 @@ api.interceptors.request.use((config) => {
|
||||
return config;
|
||||
});
|
||||
|
||||
// 2. Interceptor de Respuesta (Response): Atrapa errores al volver
|
||||
api.interceptors.response.use(
|
||||
(response) => {
|
||||
// Si la respuesta es exitosa (200-299), la dejamos pasar
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
// Si hay un error de respuesta
|
||||
if (error.response && error.response.status === 401) {
|
||||
console.warn("Sesión expirada o no autorizada. Redirigiendo al login...");
|
||||
|
||||
// Borramos el token vencido/inválido
|
||||
localStorage.removeItem('token');
|
||||
|
||||
// Forzamos la redirección al Login
|
||||
// Usamos window.location.href para asegurar una recarga limpia fuera del Router de React si es necesario
|
||||
if (window.location.pathname !== '/login') {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
}
|
||||
|
||||
// Rechazamos la promesa para que el componente sepa que hubo error (si necesita mostrar alerta)
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default api;
|
||||
12
frontend/admin-panel/src/services/clientService.ts
Normal file
12
frontend/admin-panel/src/services/clientService.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import api from './api';
|
||||
|
||||
export const clientService = {
|
||||
getAll: async () => {
|
||||
const res = await api.get('/clients');
|
||||
return res.data;
|
||||
},
|
||||
getHistory: async (id: number) => {
|
||||
const res = await api.get(`/clients/${id}/history`);
|
||||
return res.data;
|
||||
}
|
||||
};
|
||||
19
frontend/admin-panel/src/services/dashboardService.ts
Normal file
19
frontend/admin-panel/src/services/dashboardService.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import api from './api';
|
||||
|
||||
export interface DashboardData {
|
||||
revenueToday: number;
|
||||
adsToday: number;
|
||||
ticketAverage: number;
|
||||
paperOccupation: number;
|
||||
weeklyTrend: { day: string; amount: number }[];
|
||||
channelMix: { name: string; value: number }[];
|
||||
}
|
||||
|
||||
export const dashboardService = {
|
||||
getStats: async (from?: string, to?: string): Promise<DashboardData> => {
|
||||
const response = await api.get<DashboardData>('/reports/dashboard', {
|
||||
params: { from, to }
|
||||
});
|
||||
return response.data;
|
||||
}
|
||||
};
|
||||
12
frontend/admin-panel/src/services/listingService.ts
Normal file
12
frontend/admin-panel/src/services/listingService.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import api from './api';
|
||||
|
||||
export const listingService = {
|
||||
getPendingCount: async (): Promise<number> => {
|
||||
const response = await api.get<number>('/listings/pending/count');
|
||||
return response.data;
|
||||
},
|
||||
getById: async (id: number) => {
|
||||
const res = await api.get(`/listings/${id}`);
|
||||
return res.data;
|
||||
},
|
||||
};
|
||||
32
frontend/admin-panel/src/services/reportService.ts
Normal file
32
frontend/admin-panel/src/services/reportService.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import api from './api';
|
||||
|
||||
export interface CategorySales {
|
||||
categoryId: number;
|
||||
categoryName: string;
|
||||
totalSales: number;
|
||||
adCount: number;
|
||||
percentage: number;
|
||||
}
|
||||
|
||||
export const reportService = {
|
||||
getSalesByCategory: async (from?: string, to?: string): Promise<CategorySales[]> => {
|
||||
const response = await api.get<CategorySales[]>('/reports/sales-by-category', {
|
||||
params: { from, to }
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
exportCierre: async (from: string, to: string, userId?: number) => {
|
||||
const response = await api.get(`/reports/export-cierre`, {
|
||||
params: { from, to, userId },
|
||||
responseType: 'blob'
|
||||
});
|
||||
|
||||
const url = window.URL.createObjectURL(new Blob([response.data], { type: 'application/pdf' }));
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.setAttribute('download', userId ? `Caja_Cajero_${userId}.pdf` : `Cierre_Global.pdf`);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user