Files
vitfit-r2/V2/app/public/timers.html
T
2026-06-30 11:49:26 -04:00

173 lines
6.9 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Timers — VitFit</title>
<link rel="manifest" href="/manifest.json">
<link rel="stylesheet" href="/styles/base.css">
<link rel="stylesheet" href="/styles/layout.css">
<link rel="stylesheet" href="/styles/components.css">
</head>
<body>
<header class="banner">
<button class="banner-hamburger" id="nav-hamburger" aria-label="Menu">&#9776;</button>
<img id="banner-logo" alt="Logo" style="display:none;height:36px;">
<span id="app-name" class="banner-title">VitFit</span>
</header>
<div class="drawer-scrim" id="drawer-scrim"></div>
<nav class="nav-drawer" id="nav-drawer"><ul class="nav-list" id="nav-list"></ul></nav>
<main class="main-content">
<div style="text-align:center; margin-bottom:var(--space-6);">
<h1 class="page-title" style="margin-bottom:var(--space-3);">Timers</h1>
<a href="/create-timer.html" class="btn btn-primary">Create Timer</a>
</div>
<!-- Active session banner -->
<div id="active-session-banner" style="display:none;" class="warning-banner">
<span id="active-session-label">Timer running</span>
<div class="flex gap-3">
<a id="active-session-display-link" href="#" class="btn btn-secondary btn-sm" target="_blank">Open Display</a>
<button id="stop-timer-btn" class="btn btn-danger btn-sm">Stop Timer</button>
</div>
</div>
<div id="timer-list">
<div class="empty-state"><p class="empty-state-text">Loading…</p></div>
</div>
</main>
<div id="session-expired-overlay" class="session-expired-overlay">
<div class="session-expired-box"><p>Your session has expired.</p><a href="/" class="btn btn-primary">Sign In Again</a></div>
</div>
<script src="/js/core.js"></script>
<script>
let _activeSession = null;
async function loadActiveSession() {
try {
const res = await Api.get('/api/v1/timers/session/active');
if (res.ok) {
const { data } = await res.json();
_activeSession = data;
const banner = document.getElementById('active-session-banner');
if (data) {
document.getElementById('active-session-label').textContent = `Running: ${data.timerName || 'Timer'}`;
document.getElementById('active-session-display-link').href = `/timer-display.html?session=${data.sessionId}`;
banner.style.display = 'flex';
} else {
banner.style.display = 'none';
}
}
} catch {}
}
function formatDate(d) {
if (!d) return '—';
const dt = new Date(d);
return dt.toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' });
}
function timerSummary(t) {
const parts = [];
if (t.exerciseCount) parts.push(`${t.exerciseCount} exercises`);
if (t.setCount) parts.push(`${t.setCount} sets`);
if (t.setTime) parts.push(`${t.setTime}s work`);
if (t.restTime) parts.push(`${t.restTime}s rest`);
if (t.lapCount > 1) parts.push(`${t.lapCount} laps`);
return parts.join(' · ');
}
async function loadTimers() {
const el = document.getElementById('timer-list');
try {
const res = await Api.get('/api/v1/timers');
if (!res.ok) { el.innerHTML = '<div class="empty-state"><p class="empty-state-text">Failed to load.</p></div>'; return; }
const { data } = await res.json();
if (!data.length) {
el.innerHTML = '<div class="empty-state"><p class="empty-state-text">No timers yet.</p><a href="/create-timer.html" class="btn btn-primary">Create one</a></div>';
return;
}
el.innerHTML = `<table class="table"><thead><tr>
<th>Name</th><th>Mode</th><th>Date</th><th>Config</th><th></th>
</tr></thead><tbody>
${data.map(t => `<tr>
<td>${t.name}</td>
<td>${t.timerMode ? t.timerMode.charAt(0).toUpperCase() + t.timerMode.slice(1) : '—'}</td>
<td>${formatDate(t.workoutDate)}</td>
<td>${timerSummary(t)}</td>
<td class="table-actions">
<button class="btn btn-primary btn-sm" data-action="launch" data-id="${t.id}" data-name="${t.name.replace(/"/g, '&quot;')}">Launch</button>
<button class="btn btn-danger btn-sm" data-action="delete" data-id="${t.id}" data-name="${t.name.replace(/"/g, '&quot;')}">Delete</button>
</td>
</tr>`).join('')}
</tbody></table>`;
} catch {
el.innerHTML = '<div class="empty-state"><p class="empty-state-text">Error loading timers.</p></div>';
}
}
async function launchTimer(id, name) {
if (_activeSession) {
if (!confirm('A timer is already running. Stop it and launch this one?')) return;
await Api.del('/api/v1/timers/session').catch(() => {});
}
try {
const res = await Api.post(`/api/v1/timers/${id}/launch`, {});
if (res.ok) {
const { data } = await res.json();
toast(`${name} launched!`, 'success');
window.open(`/timer-display.html?session=${data.sessionId}`, '_blank');
loadActiveSession();
} else {
const b = await res.json();
toast(b.message || 'Failed to launch.', 'error');
}
} catch { toast('Network error.', 'error'); }
}
async function deleteTimer(id, name) {
if (!confirm(`Delete "${name}"? This cannot be undone.`)) return;
try {
const res = await Api.del(`/api/v1/timers/${id}`);
if (res.ok || res.status === 204) {
toast(`${name} deleted.`, 'success');
loadTimers();
} else {
const b = await res.json();
toast(b.message || 'Failed to delete.', 'error');
}
} catch { toast('Network error.', 'error'); }
}
document.addEventListener('DOMContentLoaded', async () => {
await Page.init(['Owner', 'Coach']);
document.getElementById('timer-list').addEventListener('click', (e) => {
const btn = e.target.closest('button[data-action]');
if (!btn) return;
const id = parseInt(btn.dataset.id, 10);
const name = btn.dataset.name;
if (btn.dataset.action === 'launch') launchTimer(id, name);
if (btn.dataset.action === 'delete') deleteTimer(id, name);
});
loadActiveSession();
loadTimers();
document.getElementById('stop-timer-btn').addEventListener('click', async () => {
if (!confirm('Stop the running timer?')) return;
try {
const res = await Api.del('/api/v1/timers/session');
if (res.ok || res.status === 204) { toast('Timer stopped.', 'success'); loadActiveSession(); }
else { const b = await res.json(); toast(b.message || 'Failed.', 'error'); }
} catch { toast('Network error.', 'error'); }
});
});
</script>
</body>
</html>