-
-
+
+
+
@@ -65,9 +64,8 @@
const formatDuration = (ms) => { if (!ms || ms < 0) return '00:00:00'; const s = Math.floor(ms / 1000); const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}:${String(s % 60).padStart(2, '0')}`; };
// --- API Calls ---
- // MODIFIED: This now points to the full server address, including the port.
const API_BASE_URL = '/api';
- async function apiCall(endpoint, method = 'GET', body = null) {
+ async function apiCall(endpoint, method = 'GET', body = null) {
const headers = { 'Content-Type': 'application/json' };
if (authToken) headers['Authorization'] = `Bearer ${authToken}`;
try {
@@ -86,14 +84,23 @@
// --- UI Rendering ---
function updateUI() {
- if (authToken && user) {
- navUserControls.classList.remove('hidden');
- welcomeMessage.textContent = `Welcome, ${user.username}`;
- user.role === 'admin' ? (showView('admin'), renderAdminDashboard()) : (showView('employee'), renderEmployeeDashboard());
- } else {
- navUserControls.classList.add('hidden');
- showView('auth');
- renderAuthView();
+ try {
+ const storedUser = localStorage.getItem('user');
+ user = storedUser ? JSON.parse(storedUser) : null;
+ authToken = localStorage.getItem('authToken');
+
+ if (authToken && user) {
+ navUserControls.classList.remove('hidden');
+ welcomeMessage.textContent = `Welcome, ${user.username}`;
+ user.role === 'admin' ? (showView('admin'), renderAdminDashboard()) : (showView('employee'), renderEmployeeDashboard());
+ } else {
+ navUserControls.classList.add('hidden');
+ showView('auth');
+ renderAuthView();
+ }
+ } catch (error) {
+ console.error("Error in updateUI:", error);
+ handleSignOut("There was an error loading your session.");
}
}
@@ -160,7 +167,20 @@
mainViews.admin.innerHTML = `
Admin Dashboard
-
+
+
+
Currently Punched In
+
+
+
Pending Time Off Requests
| Employee | Dates | Reason | Actions |
${pendingRequests.map(r => `| ${r.username} | ${formatDate(r.start_date)} - ${formatDate(r.end_date)} | ${r.reason||''} | |
`).join('') || '| No pending requests. |
'}
Hours by Employee
| Employee | Total Hours |
${Object.keys(employeeTotals).map(u => `| ${u} | ${formatDecimal(employeeTotals[u])} |
`).join('')}
Detailed Logs
| Employee | In | Out | Duration | Actions |
${allTimeEntries.map(e => `| ${e.username||'N/A'} | ${formatDateTime(e.punch_in_time)} | ${formatDateTime(e.punch_out_time)} | ${formatDecimal(new Date(e.punch_out_time) - new Date(e.punch_in_time))} | |
`).join('')}
@@ -250,7 +270,7 @@
signOutBtn.addEventListener('click', () => handleSignOut());
updateUI();
console.log("Attempting to register service worker...");
- // Add the registration code here, inside the same script
+
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js')
@@ -263,7 +283,6 @@
});
}
-
-