// js/ui.js // --- IMPORTS --- // Imports functions for server calls, utility formatting, and attaching event listeners. 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 --- // These are specific to the UI and manage running timers. let employeeTimerInterval = null; let adminTimerIntervals = []; // This state is populated by renderAdminDashboard and used by its helper functions like renderEditModal 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() { try { const storedUser = localStorage.getItem('user'); const authToken = localStorage.getItem('authToken'); const user = storedUser ? JSON.parse(storedUser) : null; if (authToken && user) { // ... (rest of the if block is fine) } else { // ... (rest of the else block is fine) } } catch (error) { // This is the corrected catch block console.error("Corrupted session data. Clearing and reloading.", error); localStorage.clear(); window.location.reload(); // Reload the page to a clean state } } // --- 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 | Total Hours |
|---|---|
| ${username} | ${utils.formatDecimal(totalMs)} |
| No data. | |
| Employee | In | Out | Duration | Actions |
|---|---|---|---|---|
| ${e.username||'N/A'} | ${utils.formatDateTime(e.punch_in_time)} | ${utils.formatDateTime(e.punch_out_time)} | ${e.punch_out_time ? utils.formatDecimal(new Date(e.punch_out_time) - new Date(e.punch_in_time)) + ' hrs' : '...'} |
| Username | Role | Actions |
|---|---|---|
| ${u.username} | ${u.role} | ${u.isPrimary ? `Primary Admin` : `${u.username !== user.username ? `` : ''}`} |
| 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. | |||
"${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.
'; } }