Refactor pages registry and router to enhance sidebar navigation and page rendering

This commit is contained in:
Javier Mengual 2026-01-16 21:55:40 +01:00
parent 90f2ba378b
commit 63d4b8bdb4
2 changed files with 52 additions and 23 deletions

View File

@ -1,34 +1,59 @@
import { renderDashboard } from './DashboardPage.js';
import { renderTestPage } from './test.js';
// Registry of all pages available in the application // Registry of all pages available in the application
export const pagesRegistry = [ export const pagesRegistry = [
{ {
route: 'dashboard', route: 'dashboard',
name: 'Dashboard', name: 'Dashboard',
icon: '🏠', icon: '🏠',
requiresAuth: true requiresAuth: true,
showInSidebar: true,
render: renderDashboard
}, },
{ {
route: 'test', route: 'test',
name: 'Test Page', name: 'Test Page',
icon: '🧪', icon: '🧪',
requiresAuth: true requiresAuth: true,
showInSidebar: true,
render: renderTestPage
}, },
{ {
route: 'invoices', route: 'invoices',
name: 'Facturas', name: 'Facturas',
icon: '📄', icon: '📄',
requiresAuth: true requiresAuth: true,
showInSidebar: true,
render: () => {
const div = document.createElement('div');
div.innerHTML = '<h1>Facturas</h1><p>Página en construcción...</p>';
return div;
}
}, },
{ {
route: 'clients', route: 'clients',
name: 'Clientes', name: 'Clientes',
icon: '👥', icon: '👥',
requiresAuth: true requiresAuth: true,
showInSidebar: true,
render: () => {
const div = document.createElement('div');
div.innerHTML = '<h1>Clientes</h1><p>Página en construcción...</p>';
return div;
}
}, },
{ {
route: 'settings', route: 'settings',
name: 'Configuración', name: 'Configuración',
icon: '⚙️', icon: '⚙️',
requiresAuth: true requiresAuth: true,
showInSidebar: true,
render: () => {
const div = document.createElement('div');
div.innerHTML = '<h1>Configuración</h1><p>Página en construcción...</p>';
return div;
}
} }
]; ];
@ -37,8 +62,11 @@ export function getAvailablePages(isAuthenticated) {
if (!isAuthenticated) { if (!isAuthenticated) {
return []; return [];
} }
// Filter pages based on authentication requirement // Filter pages based on authentication requirement and sidebar visibility
return pagesRegistry.filter(page => !page.requiresAuth || isAuthenticated); return pagesRegistry.filter(page => {
if (page.requiresAuth && !isAuthenticated) return false;
return page.showInSidebar;
});
} }
// Get page by route // Get page by route

View File

@ -1,9 +1,7 @@
import { auth } from './services/auth.js'; import { auth } from './services/auth.js';
import { renderLoginPage } from './pages/LoginPage.js'; import { renderLoginPage } from './pages/LoginPage.js';
import { renderDashboard } from './pages/DashboardPage.js';
import { renderTestPage } from './pages/test.js';
import { createSidebar } from './components/Sidebar.js'; import { createSidebar } from './components/Sidebar.js';
import { getAvailablePages } from './pages/pagesRegistry.js'; import { getAvailablePages, getPageByRoute } from './pages/pagesRegistry.js';
export function initRouter() { export function initRouter() {
const app = document.querySelector('#app'); const app = document.querySelector('#app');
@ -32,20 +30,23 @@ export function initRouter() {
const mainContent = document.createElement('main'); const mainContent = document.createElement('main');
mainContent.className = 'main-content'; mainContent.className = 'main-content';
let pageContent; // Get page from registry
switch (hash) { const page = getPageByRoute(route);
case '#dashboard':
pageContent = renderDashboard(); if (!page) {
break;
case '#test':
pageContent = renderTestPage();
break;
default:
// For unregistered routes, redirect to dashboard // For unregistered routes, redirect to dashboard
window.location.hash = '#dashboard'; window.location.hash = '#dashboard';
return; return;
} }
if (page.requiresAuth && !auth.checkAuth()) {
window.location.hash = '#login';
return;
}
// Render page content using registry
const pageContent = page.render();
mainContent.appendChild(pageContent); mainContent.appendChild(pageContent);
layout.appendChild(sidebar); layout.appendChild(sidebar);
layout.appendChild(mainContent); layout.appendChild(mainContent);