remember tab added

This commit is contained in:
chris 2025-08-10 09:39:52 -04:00
parent 4fd18e5ea3
commit bb7e1e7950

View File

@ -31,13 +31,9 @@ async function handleAuthSubmit(e) {
const res = await apiCall('/login', 'POST', { username, password });
if (res && res.success) {
// --- THIS IS THE FIX ---
// You were missing these two lines to save the data
localStorage.setItem('authToken', res.data.token);
localStorage.setItem('user', JSON.stringify(res.data.user));
// --- END OF FIX ---
initializeApp(); // Now this will find the data it needs
initializeApp();
} else {
showMessage(res?.data?.message || 'Login failed. Please check your credentials.', 'error');
}
@ -170,14 +166,12 @@ async function handleResetPassword(e) {
}
}
// This single handler uses event delegation for all buttons on the admin dashboard
function handleAdminDashboardClick(e) {
const target = e.target.closest('button'); // Find the closest button that was clicked
if (!target) return; // Ignore clicks that aren't on or inside a button
const target = e.target.closest('button');
if (!target) return;
const { id, userid, username, role, noteId } = target.dataset;
// Handle buttons by their ID
switch (target.id) {
case 'view-archives-btn':
renderArchiveView();
@ -193,8 +187,6 @@ function handleAdminDashboardClick(e) {
return;
}
// --- THIS IS THE UPDATED LOGIC ---
// It now calls the targeted update function instead of renderAdminDashboard()
if (target.classList.contains('approve-request-btn') || target.classList.contains('deny-request-btn')) {
const status = target.classList.contains('approve-request-btn') ? 'approved' : 'denied';
if (confirm(`Set this request to "${status}"?`)) {
@ -202,7 +194,6 @@ function handleAdminDashboardClick(e) {
.then(res => {
if (res.success) {
showMessage(`Request ${status}.`, 'success');
// After success, just fetch the new list of requests and update the table
apiCall('/admin/time-off-requests/pending').then(requestsRes => {
if (requestsRes.success) {
updatePendingRequestsList(requestsRes.data);
@ -211,11 +202,9 @@ function handleAdminDashboardClick(e) {
}
});
}
return; // Stop the function here
return;
}
// --- END OF UPDATED LOGIC ---
// Handle other buttons by their class name
if (target.classList.contains('edit-btn')) renderEditModal(id, handleEditSubmit);
if (target.classList.contains('delete-btn') && confirm('Delete this time entry?')) apiCall(`/admin/logs/${id}`, 'DELETE').then(res => res.success && renderAdminDashboard());
if (target.classList.contains('force-clock-out-btn') && confirm(`Force clock out ${username}?`)) apiCall('/admin/force-clock-out', 'POST', { userId: userid }).then(res => res.success && renderAdminDashboard());
@ -239,46 +228,32 @@ export function attachEmployeeDashboardListeners() {
}
export function attachAdminDashboardListeners() {
// This master listener handles all buttons
document.getElementById('admin-dashboard')?.addEventListener('click', handleAdminDashboardClick);
// Listeners for forms that need to prevent default submission
document.getElementById('create-user-form')?.addEventListener('submit', handleCreateUser);
document.getElementById('add-punch-form')?.addEventListener('submit', handleAddPunch);
document.getElementById('add-note-form')?.addEventListener('submit', handleAddNote);
// Make the tabs interactive
setupTabbedInterface();
}
// --- APP INITIALIZER ---
function initializeApp() {
console.log("A. Inside initializeApp()."); // Checkpoint A
authToken = localStorage.getItem('authToken');
const userString = localStorage.getItem('user');
user = userString ? JSON.parse(userString) : null;
console.log("B. Auth Token found:", !!authToken); // Checkpoint B
console.log("C. User data found:", user); // Checkpoint C
if (authToken && user) {
const userControls = document.getElementById('nav-user-controls');
userControls.classList.remove('hidden');
userControls.querySelector('#welcome-message').textContent = `Welcome, ${user.username}`;
if (user.role === 'admin') {
console.log("D. User is an admin, calling renderAdminDashboard()."); // Checkpoint D
renderAdminDashboard();
} else {
console.log("E. User is an employee, calling renderEmployeeDashboard()."); // Checkpoint E
renderEmployeeDashboard();
}
} else {
console.log("F. No user or token, calling renderAuthView()."); // Checkpoint F
document.getElementById('nav-user-controls').classList.add('hidden');
renderAuthView();
}
console.log("G. initializeApp() finished."); // Checkpoint G
}
// --- HELPERS ---
@ -293,6 +268,7 @@ function setupTabbedInterface() {
if (!clickedTab) return;
const tabTarget = clickedTab.dataset.tab;
lastAdminTab = tabTarget;
tabsContainer.querySelectorAll('.tab-btn').forEach(btn => {
btn.classList.remove('active-tab');
@ -306,27 +282,7 @@ function setupTabbedInterface() {
});
}
function setupTabbedInterface() {
const tabsContainer = document.getElementById('admin-tabs-nav');
const contentContainer = document.getElementById('admin-tabs-content');
if (!tabsContainer || !contentContainer) return;
tabsContainer.addEventListener('click', (e) => {
const clickedTab = e.target.closest('.tab-btn');
if (!clickedTab) return;
const tabTarget = clickedTab.dataset.tab;
// --- 2. ADD THIS LINE to update our variable ---
lastAdminTab = tabTarget;
// ... (the rest of the function stays the same) ...
});
}
// --- START THE APP ---
// Register the service worker for PWA functionality
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js')
@ -335,7 +291,6 @@ if ('serviceWorker' in navigator) {
});
}
// Attach global listeners that are always present on the page
document.getElementById('sign-out-btn').addEventListener('click', () => handleSignOut('You have been signed out.'));
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible' && localStorage.getItem('user')) {
@ -343,5 +298,4 @@ document.addEventListener('visibilitychange', () => {
}
});
// Initial call to start the app on page load
initializeApp();