Files
hotel777-manager/public/nav.js
2026-05-04 21:09:42 +05:00

52 lines
1.5 KiB
JavaScript

document.addEventListener('DOMContentLoaded', () => {
const currentPage = window.location.pathname.split('/').pop() || 'index.html';
const navHTML = `
<nav>
<a href="/index.html" data-page="index.html">Заявки</a>
<a href="/clients.html" data-page="clients.html">Клиенты</a>
<a href="/admin.html" data-page="admin.html">Панель управления</a>
<a id="logoutLink" href="#" style="display:none;">Выход</a>
</nav>
`;
const header = document.querySelector('header');
if (header) header.innerHTML += navHTML;
const pageMap = {
'': 'index.html',
'index.html': 'index.html',
'clients.html': 'clients.html',
'client.html': 'clients.html',
'admin.html': 'admin.html',
'login.html': 'login.html'
};
const activePage = pageMap[currentPage] || '';
document.querySelectorAll('nav a[data-page]').forEach(link => {
if (link.dataset.page === activePage) {
link.classList.add('active');
}
});
fetch('/api/me')
.then(r => r.json())
.then(data => {
if (data.isAdmin) {
document.getElementById('logoutLink').style.display = 'inline';
}
});
document.addEventListener('click', (e) => {
if (e.target.id === 'logoutLink') {
e.preventDefault();
fetch('/api/logout', { method: 'POST' })
.then(() => {
window.localStorage.removeItem('csrfToken');
window.location.href = '/login.html';
});
}
});
});