fix main.js - dupe functions

This commit is contained in:
chris 2025-08-08 21:06:20 -04:00
parent 213f63a16d
commit e72e6b6511

View File

@ -5,7 +5,6 @@
import { apiCall } from './api.js'; import { apiCall } from './api.js';
import { showMessage } from './utils.js'; import { showMessage } from './utils.js';
import { import {
updateUI,
renderAuthView, renderAuthView,
renderAdminDashboard, renderAdminDashboard,
renderEmployeeDashboard, renderEmployeeDashboard,
@ -13,7 +12,7 @@ import {
renderChangePasswordModal, renderChangePasswordModal,
renderResetPasswordModal, renderResetPasswordModal,
renderRequestHistoryModal, renderRequestHistoryModal,
handleViewNotesClick, // This UI-specific handler is simple enough to live in ui.js handleViewNotesClick,
renderArchiveView, renderArchiveView,
renderTimeOffHistoryView renderTimeOffHistoryView
} from './ui.js'; } from './ui.js';
@ -195,19 +194,16 @@ export function attachEmployeeDashboardListeners() {
} }
export function attachAdminDashboardListeners() { export function attachAdminDashboardListeners() {
// Event delegation for all buttons // This master listener handles most buttons in the admin view via event delegation
document.getElementById('admin-dashboard').addEventListener('click', handleAdminDashboardClick); document.getElementById('admin-dashboard').addEventListener('click', handleAdminDashboardClick);
// Specific form handlers // Listeners for specific forms that need to prevent default submission behavior
document.getElementById('create-user-form').addEventListener('submit', handleCreateUser); document.getElementById('create-user-form').addEventListener('submit', handleCreateUser);
document.getElementById('add-punch-form').addEventListener('submit', handleAddPunch); document.getElementById('add-punch-form').addEventListener('submit', handleAddPunch);
document.getElementById('add-note-form').addEventListener('submit', handleAddNote); document.getElementById('add-note-form').addEventListener('submit', handleAddNote);
// Other top-level buttons // Call the function to make the new tabs work
document.getElementById('archive-btn').addEventListener('click', handleArchive); setupTabbedInterface();
document.getElementById('view-archives-btn').addEventListener('click', renderArchiveView);
document.getElementById('view-time-off-history-btn').addEventListener('click', renderTimeOffHistoryView);
document.getElementById('view-notes-btn').addEventListener('click', handleViewNotesClick);
} }
// --- APP INITIALIZER --- // --- APP INITIALIZER ---
@ -231,26 +227,28 @@ function initializeApp() {
} }
} }
// --- HELPERS ---
// This function handles the logic for switching between tabs
function setupTabbedInterface() { function setupTabbedInterface() {
const tabsContainer = document.getElementById('admin-tabs-nav'); const tabsContainer = document.getElementById('admin-tabs-nav');
const contentContainer = document.getElementById('admin-tabs-content'); const contentContainer = document.getElementById('admin-tabs-content');
if (!tabsContainer) return; if (!tabsContainer || !contentContainer) return; // Exit if the tab elements aren't on the page
// Use event delegation on the tab navigation container
tabsContainer.addEventListener('click', (e) => { tabsContainer.addEventListener('click', (e) => {
const clickedTab = e.target.closest('.tab-btn'); const clickedTab = e.target.closest('.tab-btn');
if (!clickedTab) return; if (!clickedTab) return; // Ignore clicks that aren't on a tab button
// Get the target tab's identifier
const tabTarget = clickedTab.dataset.tab; const tabTarget = clickedTab.dataset.tab;
// Update the tab buttons' active state // Update the active state on tab buttons
tabsContainer.querySelectorAll('.tab-btn').forEach(btn => { tabsContainer.querySelectorAll('.tab-btn').forEach(btn => {
btn.classList.remove('active-tab'); btn.classList.remove('active-tab');
}); });
clickedTab.classList.add('active-tab'); clickedTab.classList.add('active-tab');
// Update the visible content panel // Show the correct content panel and hide the others
contentContainer.querySelectorAll('[id^="tab-content-"]').forEach(panel => { contentContainer.querySelectorAll('[id^="tab-content-"]').forEach(panel => {
panel.classList.add('hidden'); panel.classList.add('hidden');
}); });
@ -258,25 +256,8 @@ function setupTabbedInterface() {
}); });
} }
// NOW, UPDATE your existing attachAdminDashboardListeners function to call this new function.
export function attachAdminDashboardListeners() {
// Event delegation for all buttons
document.getElementById('admin-dashboard').addEventListener('click', handleAdminDashboardClick);
// Specific form handlers
document.getElementById('create-user-form').addEventListener('submit', handleCreateUser);
document.getElementById('add-punch-form').addEventListener('submit', handleAddPunch);
document.getElementById('add-note-form').addEventListener('submit', handleAddNote);
// ... Any other listeners you have here ...
// ADD THIS LINE at the end of the function:
setupTabbedInterface();
}
// --- START THE APP --- // --- START THE APP ---
// Attach global listeners that are always present. // Attach global listeners that are always present on the page.
document.getElementById('sign-out-btn').addEventListener('click', () => handleSignOut('You have been signed out.')); document.getElementById('sign-out-btn').addEventListener('click', () => handleSignOut('You have been signed out.'));
document.addEventListener('visibilitychange', () => { document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible' && localStorage.getItem('user')) { if (document.visibilityState === 'visible' && localStorage.getItem('user')) {