// js/ui.js // --- IMPORTS --- import { apiCall } from './api.js'; import * as utils from './utils.js'; import { attachAuthFormListener, attachAdminDashboardListeners, attachEmployeeDashboardListeners, } from './main.js'; // --- DOM ELEMENT SELECTORS --- const mainViews = { auth: document.getElementById('auth-view'), employee: document.getElementById('employee-dashboard'), admin: document.getElementById('admin-dashboard'), archive: document.getElementById('admin-archive-view'), timeOffHistory: document.getElementById('admin-time-off-history-view') }; const navUserControls = document.getElementById('nav-user-controls'); const welcomeMessage = document.getElementById('welcome-message'); const modalContainer = document.getElementById('modal-container'); // --- MODULE-LEVEL STATE --- let employeeTimerInterval = null; let adminTimerIntervals = []; let allTimeEntries = []; let allUsers = []; // --- VIEW MANAGEMENT --- export function showView(viewName) { clearInterval(employeeTimerInterval); adminTimerIntervals.forEach(clearInterval); adminTimerIntervals = []; Object.keys(mainViews).forEach(v => mainViews[v].classList.toggle('hidden', v !== viewName)); } // --- MASTER UI UPDATE FUNCTION --- export function updateUI() { // This function was empty, it has been corrected. try { const storedUser = localStorage.getItem('user'); const authToken = localStorage.getItem('authToken'); const user = storedUser ? JSON.parse(storedUser) : null; if (authToken && user) { navUserControls.classList.remove('hidden'); welcomeMessage.textContent = `Welcome, ${user.username}`; if (user.role === 'admin') { renderAdminDashboard(); } else { renderEmployeeDashboard(); } } else { navUserControls.classList.add('hidden'); renderAuthView(); } } catch (error) { console.error("Corrupted session data. Clearing and reloading.", error); localStorage.clear(); window.location.reload(); } } // --- RENDER FUNCTIONS --- export function renderAuthView() { showView('auth'); mainViews.auth.innerHTML = `
${punchedIn ? 'Punched In' : 'Punched Out'}
${punchedIn ? 'Since:' : 'Last Punch:'} ${utils.formatDateTime(punchedIn ? last.punch_in_time : last?.punch_out_time)}
"${note.note_text}"
- ${note.admin_username} on ${utils.formatDate(note.created_at)}
You have no new notes.
'}${utils.formatDecimal(totalMilliseconds)}
| Dates | Reason | Status |
|---|---|---|
| ${utils.formatDate(r.start_date)} - ${utils.formatDate(r.end_date)} | ${r.reason || ''} | ${r.status} |
| No upcoming or pending requests. | ||
| In | Out | Duration (Hours) |
|---|---|---|
| ${utils.formatDateTime(e.punch_in_time)} | ${utils.formatDateTime(e.punch_out_time)} | ${e.status === 'in' ? 'Running...' : utils.formatDecimal(new Date(e.punch_out_time) - new Date(e.punch_in_time))} |
| No entries. | ||
| Employee | Dates | Reason | Actions |
|---|---|---|---|
| ${r.username} | ${utils.formatDate(r.start_date)} - ${utils.formatDate(r.end_date)} | ${r.reason||''} | |
| No pending requests. | |||
| Employee | In | Out | Duration (Hrs) | Archived On |
|---|---|---|---|---|
| ${e.username||'N/A'} | ${utils.formatDateTime(e.punch_in_time)} | ${utils.formatDateTime(e.punch_out_time)} | ${utils.formatDecimal(new Date(e.punch_out_time) - new Date(e.punch_in_time))} | ${utils.formatDateTime(e.archived_at)} |
| No archived entries found. | ||||
| Employee | Dates | Reason | Status |
|---|---|---|---|
| ${r.username} | ${utils.formatDate(r.start_date)} - ${utils.formatDate(r.end_date)} | ${r.reason||''} | ${r.status} |
| No history. | |||
| Dates | Reason | Status |
|---|---|---|
| ${utils.formatDate(r.start_date)} - ${utils.formatDate(r.end_date)} | ${r.reason || ''} | ${r.status} |
| No history found. | ||
"${note.note_text}"
- ${note.admin_username} on ${utils.formatDate(note.created_at)}
No notes found for this employee.
'; } } else { container.innerHTML = 'Could not load notes.
'; } } // This function ONLY updates the pending requests table. export function updatePendingRequestsList(requests) { const tableBody = document.querySelector('#tab-content-overview table tbody'); if (!tableBody) return; // Exit if the table isn't on the page if (requests.length === 0) { tableBody.innerHTML = '