Refactor pages registry and router to enhance sidebar navigation and page rendering
This commit is contained in:
parent
90f2ba378b
commit
63d4b8bdb4
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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');
|
||||||
|
|
@ -27,25 +25,28 @@ export function initRouter() {
|
||||||
// Create sidebar
|
// Create sidebar
|
||||||
const pages = getAvailablePages(true);
|
const pages = getAvailablePages(true);
|
||||||
const sidebar = createSidebar(pages, route);
|
const sidebar = createSidebar(pages, route);
|
||||||
|
|
||||||
// Create main content area
|
// Create main content area
|
||||||
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;
|
// For unregistered routes, redirect to dashboard
|
||||||
case '#test':
|
window.location.hash = '#dashboard';
|
||||||
pageContent = renderTestPage();
|
return;
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// For unregistered routes, redirect to dashboard
|
|
||||||
window.location.hash = '#dashboard';
|
|
||||||
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);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue