190 lines
10 KiB
TypeScript
190 lines
10 KiB
TypeScript
// src/routes/AppRoutes.tsx
|
|
import React, { type JSX } from 'react';
|
|
import { BrowserRouter, Routes, Route, Navigate, Outlet } from 'react-router-dom';
|
|
import LoginPage from '../pages/LoginPage';
|
|
import HomePage from '../pages/HomePage';
|
|
import { useAuth } from '../contexts/AuthContext';
|
|
import MainLayout from '../layouts/MainLayout';
|
|
import { Typography } from '@mui/material';
|
|
|
|
// Distribución
|
|
import DistribucionIndexPage from '../pages/Distribucion/DistribucionIndexPage';
|
|
import GestionarCanillitasPage from '../pages/Distribucion/GestionarCanillitasPage';
|
|
import GestionarDistribuidoresPage from '../pages/Distribucion/GestionarDistribuidoresPage';
|
|
import GestionarPublicacionesPage from '../pages/Distribucion/GestionarPublicacionesPage';
|
|
import GestionarSeccionesPublicacionPage from '../pages/Distribucion/GestionarSeccionesPublicacionPage';
|
|
import GestionarPreciosPublicacionPage from '../pages/Distribucion/GestionarPreciosPublicacionPage';
|
|
import GestionarRecargosPublicacionPage from '../pages/Distribucion/GestionarRecargosPublicacionPage';
|
|
import GestionarPorcentajesPagoPage from '../pages/Distribucion/GestionarPorcentajesPagoPage';
|
|
import GestionarPorcMonCanillaPage from '../pages/Distribucion/GestionarPorcMonCanillaPage';
|
|
import GestionarOtrosDestinosPage from '../pages/Distribucion/GestionarOtrosDestinosPage';
|
|
import GestionarZonasPage from '../pages/Distribucion/GestionarZonasPage';
|
|
import GestionarEmpresasPage from '../pages/Distribucion/GestionarEmpresasPage';
|
|
import GestionarSalidasOtrosDestinosPage from '../pages/Distribucion/GestionarSalidasOtrosDestinosPage';
|
|
import GestionarEntradasSalidasDistPage from '../pages/Distribucion/GestionarEntradasSalidasDistPage';
|
|
import GestionarEntradasSalidasCanillaPage from '../pages/Distribucion/GestionarEntradasSalidasCanillaPage';
|
|
import GestionarControlDevolucionesPage from '../pages/Distribucion/GestionarControlDevolucionesPage';
|
|
|
|
// Impresión
|
|
import ImpresionIndexPage from '../pages/Impresion/ImpresionIndexPage';
|
|
import GestionarPlantasPage from '../pages/Impresion/GestionarPlantasPage';
|
|
import GestionarTiposBobinaPage from '../pages/Impresion/GestionarTiposBobinaPage';
|
|
import GestionarEstadosBobinaPage from '../pages/Impresion/GestionarEstadosBobinaPage';
|
|
import GestionarStockBobinasPage from '../pages/Impresion/GestionarStockBobinasPage';
|
|
import GestionarTiradasPage from '../pages/Impresion/GestionarTiradasPage';
|
|
|
|
// Contables
|
|
import ContablesIndexPage from '../pages/Contables/ContablesIndexPage';
|
|
import GestionarTiposPagoPage from '../pages/Contables/GestionarTiposPagoPage';
|
|
import GestionarPagosDistribuidorPage from '../pages/Contables/GestionarPagosDistribuidorPage';
|
|
import GestionarNotasCDPage from '../pages/Contables/GestionarNotasCDPage';
|
|
|
|
// Usuarios
|
|
import UsuariosIndexPage from '../pages/Usuarios/UsuariosIndexPage'; // Crear este componente
|
|
import GestionarPerfilesPage from '../pages/Usuarios/GestionarPerfilesPage';
|
|
import GestionarPermisosPage from '../pages/Usuarios/GestionarPermisosPage';
|
|
import AsignarPermisosAPerfilPage from '../pages/Usuarios/AsignarPermisosAPerfilPage';
|
|
import GestionarUsuariosPage from '../pages/Usuarios/GestionarUsuariosPage';
|
|
|
|
// Radios
|
|
import RadiosIndexPage from '../pages/Radios/RadiosIndexPage';
|
|
import GestionarRitmosPage from '../pages/Radios/GestionarRitmosPage';
|
|
import GestionarCancionesPage from '../pages/Radios/GestionarCancionesPage';
|
|
import GenerarListasRadioPage from '../pages/Radios/GenerarListasRadioPage';
|
|
|
|
// Auditorias
|
|
import GestionarAuditoriaUsuariosPage from '../pages/Usuarios/Auditoria/GestionarAuditoriaUsuariosPage';
|
|
|
|
// --- ProtectedRoute y PublicRoute SIN CAMBIOS ---
|
|
const ProtectedRoute: React.FC<{ children: JSX.Element }> = ({ children }) => {
|
|
const { isAuthenticated, isLoading } = useAuth();
|
|
// console.log("ProtectedRoute Check:", { path: window.location.pathname, isAuthenticated, isLoading });
|
|
if (isLoading) return null;
|
|
if (!isAuthenticated) {
|
|
// console.log("ProtectedRoute: Not authenticated, redirecting to /login");
|
|
return <Navigate to="/login" replace />;
|
|
}
|
|
// console.log("ProtectedRoute: Authenticated, rendering children");
|
|
return children;
|
|
};
|
|
const PublicRoute: React.FC<{ children: JSX.Element }> = ({ children }) => {
|
|
const { isAuthenticated, isLoading } = useAuth();
|
|
// console.log("PublicRoute Check:", { path: window.location.pathname, isAuthenticated, isLoading });
|
|
if (isLoading) return null;
|
|
if (isAuthenticated) {
|
|
// console.log("PublicRoute: Authenticated, redirecting to /");
|
|
return <Navigate to="/" replace />;
|
|
}
|
|
// console.log("PublicRoute: Not authenticated, rendering children");
|
|
return children;
|
|
};
|
|
// --- Fin Protected/Public ---
|
|
|
|
const MainLayoutWrapper: React.FC = () => (
|
|
<MainLayout>
|
|
<Outlet />
|
|
</MainLayout>
|
|
);
|
|
|
|
// Placeholder simple
|
|
const PlaceholderPage: React.FC<{ moduleName: string }> = ({ moduleName }) => (
|
|
<Typography variant="h5" sx={{ p: 2 }}>Página Principal del Módulo: {moduleName}</Typography>
|
|
);
|
|
|
|
const AppRoutes = () => {
|
|
return (
|
|
<BrowserRouter>
|
|
<Routes> {/* Un solo <Routes> de nivel superior */}
|
|
<Route path="/login" element={<PublicRoute><LoginPage /></PublicRoute>} />
|
|
|
|
{/* Rutas Protegidas que usan el MainLayout */}
|
|
<Route
|
|
path="/" // La ruta padre para todas las secciones protegidas
|
|
element={
|
|
<ProtectedRoute>
|
|
<MainLayoutWrapper />
|
|
</ProtectedRoute>
|
|
}
|
|
>
|
|
{/* Rutas hijas que se renderizarán en el Outlet de MainLayoutWrapper */}
|
|
<Route index element={<HomePage />} /> {/* Para la ruta exacta "/" */}
|
|
|
|
{/* Módulo de Distribución (anidado) */}
|
|
<Route path="distribucion" element={<DistribucionIndexPage />}>
|
|
<Route index element={<Navigate to="es-canillas" replace />} />
|
|
<Route path="es-canillas" element={<GestionarEntradasSalidasCanillaPage />} />
|
|
<Route path="control-devoluciones" element={<GestionarControlDevolucionesPage />} />
|
|
<Route path="es-distribuidores" element={<GestionarEntradasSalidasDistPage />} />
|
|
<Route path="salidas-otros-destinos" element={<GestionarSalidasOtrosDestinosPage />} />
|
|
<Route path="canillas" element={<GestionarCanillitasPage />} />
|
|
<Route path="distribuidores" element={<GestionarDistribuidoresPage />} />
|
|
<Route path="otros-destinos" element={<GestionarOtrosDestinosPage />} />
|
|
<Route path="zonas" element={<GestionarZonasPage />} />
|
|
<Route path="empresas" element={<GestionarEmpresasPage />} />
|
|
{/* Rutas para Publicaciones y sus detalles */}
|
|
<Route path="publicaciones" element={<Outlet />}> {/* Contenedor para sub-rutas de publicaciones */}
|
|
<Route index element={<GestionarPublicacionesPage />} /> {/* Lista de publicaciones */}
|
|
<Route path=":idPublicacion/precios" element={<GestionarPreciosPublicacionPage />} />
|
|
<Route path=":idPublicacion/recargos" element={<GestionarRecargosPublicacionPage />} />
|
|
<Route path=":idPublicacion/porcentajes-pago-dist" element={<GestionarPorcentajesPagoPage />} />
|
|
<Route path=":idPublicacion/porcentajes-mon-canilla" element={<GestionarPorcMonCanillaPage />} />
|
|
<Route path=":idPublicacion/secciones" element={<GestionarSeccionesPublicacionPage />} />
|
|
</Route>
|
|
</Route>
|
|
|
|
{/* Módulo Contable (anidado) */}
|
|
<Route path="contables" element={<ContablesIndexPage />}>
|
|
<Route index element={<Navigate to="tipos-pago" replace />} />
|
|
<Route path="tipos-pago" element={<GestionarTiposPagoPage />} />
|
|
<Route path="pagos-distribuidores" element={<GestionarPagosDistribuidorPage />} />
|
|
<Route path="notas-cd" element={<GestionarNotasCDPage />} />
|
|
</Route>
|
|
|
|
{/* Módulo de Impresión (anidado) */}
|
|
<Route path="impresion" element={<ImpresionIndexPage />}>
|
|
<Route index element={<Navigate to="plantas" replace />} />
|
|
<Route path="plantas" element={<GestionarPlantasPage />} />
|
|
<Route path="tipos-bobina" element={<GestionarTiposBobinaPage />} />
|
|
<Route path="estados-bobina" element={<GestionarEstadosBobinaPage />} />
|
|
<Route path="stock-bobinas" element={<GestionarStockBobinasPage />} />
|
|
<Route path="tiradas" element={<GestionarTiradasPage />} />
|
|
</Route>
|
|
|
|
{/* Otros Módulos Principales (estos son "finales", no tienen más hijos) */}
|
|
<Route path="reportes" element={<PlaceholderPage moduleName="Reportes" />} />
|
|
|
|
{/* Módulo de Radios (anidado) */}
|
|
<Route path="radios" element={<RadiosIndexPage />}>
|
|
<Route index element={<Navigate to="ritmos" replace />} />
|
|
<Route path="ritmos" element={<GestionarRitmosPage />} />
|
|
<Route path="canciones" element={<GestionarCancionesPage />} />
|
|
<Route path="generar-listas" element={<GenerarListasRadioPage />} />
|
|
</Route>
|
|
|
|
{/* Módulo de Usuarios (anidado) */}
|
|
<Route path="usuarios" element={<UsuariosIndexPage />}>
|
|
<Route index element={<Navigate to="perfiles" replace />} /> {/* Redirigir a la primera subpestaña */}
|
|
<Route path="perfiles" element={<GestionarPerfilesPage />} />
|
|
<Route path="permisos" element={<GestionarPermisosPage />} />
|
|
<Route path="perfiles/:idPerfil/permisos" element={<AsignarPermisosAPerfilPage />} />
|
|
<Route path="gestion-usuarios" element={<GestionarUsuariosPage />} />
|
|
<Route path="auditoria-usuarios" element={<GestionarAuditoriaUsuariosPage />} />
|
|
</Route>
|
|
|
|
{/* Ruta catch-all DENTRO del layout protegido */}
|
|
<Route path="*" element={<Navigate to="/" replace />} />
|
|
</Route> {/* Cierre de la ruta padre "/" */}
|
|
|
|
{/* Podrías tener un catch-all global aquí si una ruta no coincide EN ABSOLUTO,
|
|
pero el path="*" dentro de la ruta "/" ya debería manejar la mayoría de los casos
|
|
después de un login exitoso.
|
|
Si un usuario no autenticado intenta una ruta inválida, ProtectedRoute lo manda a /login.
|
|
*/}
|
|
{/* <Route path="*" element={<Navigate to="/login" replace />} /> */}
|
|
|
|
</Routes>
|
|
</BrowserRouter>
|
|
);
|
|
};
|
|
|
|
export default AppRoutes; |