import { renderDashboard } from './DashboardPage.js';
import { renderFacturasPage } from './Facturas.js';
import { renderCreateInvoicePage } from './CreateInvoicePage.js';
import { renderClientesPage } from './ClientesPage.js';
import { authFetch } from '../services/auth.js';
import { showConfirmDialog } from '../components/ConfirmDialog.js';
import { showToast } from '../components/Toast.js';
// Registry of all pages available in the application
export const pagesRegistry = [
{
route: 'dashboard',
name: 'Dashboard',
icon: '',
requiresAuth: true,
showInSidebar: true,
render: renderDashboard
},
{
route: 'invoices',
name: 'Facturas',
icon: '',
requiresAuth: true,
showInSidebar: true,
render: renderFacturasPage
},
{
route: 'create-invoice',
name: 'Nueva Factura',
icon: '+',
requiresAuth: true,
showInSidebar: false, // No mostrar en sidebar
render: renderCreateInvoicePage
},
{
route: 'clients',
name: 'Clientes',
icon: '',
requiresAuth: true,
showInSidebar: true,
render: renderClientesPage
},
{
route: 'settings',
name: 'Configuración',
icon: '',
requiresAuth: true,
showInSidebar: true,
render: () => {
const div = document.createElement('div');
div.className = 'settings-page';
const apiUrl = import.meta.env.VITE_API_BASE_URL || 'No configurada';
div.innerHTML = /*html*/`
Sesión
Estado:
Activa
Token:
${localStorage.getItem('token') ? '••••••••' + localStorage.getItem('token').slice(-8) : 'No disponible'}
Conexión API
URL:
${apiUrl}
Estado:
Comprobando...
Acerca de
Aplicación:
Doli App
Versión:
1.0.0
Backend:
Dolibarr BFF
`;
// Logout
div.querySelector('#btn-logout').addEventListener('click', async () => {
const confirmed = await showConfirmDialog({
title: '¿Cerrar sesión?',
message: 'Se cerrará tu sesión y tendrás que volver a iniciar sesión.',
confirmText: 'Cerrar sesión',
cancelText: 'Cancelar',
variant: 'warning'
});
if (confirmed) {
localStorage.removeItem('token');
window.location.hash = '#login';
window.location.reload();
}
});
// Check API
async function checkApi() {
const statusEl = div.querySelector('#api-status');
statusEl.textContent = 'Comprobando...';
statusEl.className = 'settings-api-status';
try {
const token = localStorage.getItem('token');
const res = await authFetch(`${apiUrl}/api/Invoices?limit=1`, {
headers: { 'Authorization': `Bearer ${token}` }
});
if (res.ok) {
statusEl.textContent = '✓ Conectado';
statusEl.classList.add('api-ok');
} else {
statusEl.textContent = '✗ Error ' + res.status;
statusEl.classList.add('api-error');
}
} catch {
statusEl.textContent = '✗ Sin conexión';
statusEl.classList.add('api-error');
}
}
div.querySelector('#btn-check-api').addEventListener('click', checkApi);
checkApi();
return div;
}
}
];
// Get pages that the user can access
export function getAvailablePages(isAuthenticated) {
if (!isAuthenticated) {
return [];
}
// Filter pages based on authentication requirement and sidebar visibility
return pagesRegistry.filter(page => {
if (page.requiresAuth && !isAuthenticated) return false;
return page.showInSidebar;
});
}
// Get page by route
export function getPageByRoute(route) {
return pagesRegistry.find(page => page.route === route);
}