Files
vitfit-r2/V2/app/public/settings.html
T
2026-06-16 07:17:49 -04:00

268 lines
13 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Settings — 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">
<h1 class="page-title">Settings</h1>
<div class="tabs" style="margin-bottom:var(--space-6);">
<button class="tab active" data-tab="org">Organisation</button>
<button class="tab" data-tab="branding">Branding</button>
<button class="tab" data-tab="users">Users</button>
</div>
<!-- Org tab -->
<div id="tab-org">
<div class="card" style="max-width:560px;">
<h2 class="card-title" style="margin-bottom:var(--space-6);">Organisation Details</h2>
<form id="org-form" novalidate>
<div class="form-group">
<label class="form-label" for="org-name">Name</label>
<input class="form-input" type="text" id="org-name" maxlength="120">
</div>
<div class="form-group">
<label class="form-label" for="org-timezone">Timezone</label>
<input class="form-input" type="text" id="org-timezone" placeholder="e.g. America/Chicago">
</div>
<div id="org-error" class="form-error" style="display:none;"></div>
<button class="btn btn-primary" type="submit" id="org-btn">Save</button>
</form>
</div>
</div>
<!-- Branding tab -->
<div id="tab-branding" style="display:none;">
<div class="card" style="max-width:560px;">
<h2 class="card-title" style="margin-bottom:var(--space-6);">Branding</h2>
<form id="branding-form" novalidate>
<div class="form-group">
<label class="form-label" for="app-name-input">App Name</label>
<input class="form-input" type="text" id="app-name-input" maxlength="60">
</div>
<div class="form-group">
<label class="form-label" for="primary-color">Primary Color</label>
<div class="input-row">
<input class="form-input" type="text" id="primary-color" placeholder="#3a86ff" maxlength="7" pattern="^#[0-9A-Fa-f]{6}$">
<input type="color" id="primary-color-pick" style="width:44px;height:44px;border:none;cursor:pointer;">
</div>
</div>
<div class="form-group">
<label class="form-label" for="logo-file">Logo (PNG/JPG, max 2MB)</label>
<input class="form-input" type="file" id="logo-file" accept="image/*">
<img id="logo-preview" style="display:none;max-height:60px;margin-top:var(--space-2);">
</div>
<div id="branding-error" class="form-error" style="display:none;"></div>
<button class="btn btn-primary" type="submit" id="branding-btn">Save Branding</button>
</form>
</div>
</div>
<!-- Users tab -->
<div id="tab-users" style="display:none;">
<div class="page-header" style="margin-bottom:var(--space-4);">
<h2 class="card-title">Users</h2>
<button class="btn btn-primary btn-sm" id="invite-btn">Invite User</button>
</div>
<div id="user-list">
<div class="empty-state"><p class="empty-state-text">Loading…</p></div>
</div>
<div class="modal-overlay" id="invite-modal" style="display:none;">
<div class="modal">
<div class="modal-header">
<h2 class="modal-title">Invite User</h2>
<button class="modal-close" id="invite-modal-close">&times;</button>
</div>
<div class="form-group">
<label class="form-label" for="invite-name">Name</label>
<input class="form-input" type="text" id="invite-name" maxlength="80">
</div>
<div class="form-group">
<label class="form-label" for="invite-email">Email</label>
<input class="form-input" type="email" id="invite-email">
</div>
<div class="form-group">
<label class="form-label" for="invite-role">Role</label>
<select class="form-select" id="invite-role">
<option value="Coach">Coach</option>
<option value="Client">Client</option>
</select>
</div>
<div class="form-group">
<label class="form-label" for="invite-password">Temporary Password</label>
<input class="form-input" type="text" id="invite-password" autocomplete="off">
</div>
<div id="invite-error" class="form-error" style="display:none;"></div>
<button class="btn btn-primary" id="invite-confirm-btn">Create User</button>
</div>
</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>
async function loadUsers() {
const el = document.getElementById('user-list');
try {
const res = await Api.get('/api/v1/users');
if (!res.ok) throw new Error();
const { data } = await res.json();
el.innerHTML = `<table class="table"><thead><tr><th>Name</th><th>Email</th><th>Role</th><th>Status</th><th></th></tr></thead><tbody>
${data.map(u => `<tr>
<td>${u.name}</td>
<td>${u.email}</td>
<td>${u.role}</td>
<td><span class="badge badge-${u.isActive ? 'success' : 'danger'}">${u.isActive ? 'Active' : 'Inactive'}</span></td>
<td class="table-actions">
${u.isActive
? `<button class="btn btn-danger btn-sm" data-action="toggle" data-id="${u.id}" data-active="false">Deactivate</button>`
: `<button class="btn btn-secondary btn-sm" data-action="toggle" data-id="${u.id}" data-active="true">Activate</button>`}
</td>
</tr>`).join('')}
</tbody></table>`;
} catch { el.innerHTML = '<div class="empty-state"><p class="empty-state-text">Failed to load.</p></div>'; }
}
async function toggleUser(id, active) {
try {
const res = await Api.patch(`/api/v1/users/${id}`, { isActive: active });
if (res.ok) { toast(active ? 'User activated.' : 'User deactivated.', 'success'); loadUsers(); }
else { const b = await res.json(); toast(b.message || 'Failed.', 'error'); }
} catch { toast('Network error.', 'error'); }
}
document.addEventListener('DOMContentLoaded', async () => {
await Page.init(['Owner']);
document.getElementById('user-list').addEventListener('click', (e) => {
const btn = e.target.closest('button[data-action="toggle"]');
if (btn) toggleUser(parseInt(btn.dataset.id, 10), btn.dataset.active === 'true');
});
// Tab switching
document.querySelectorAll('.tab').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
btn.classList.add('active');
['org', 'branding', 'users'].forEach(t => {
document.getElementById(`tab-${t}`).style.display = btn.dataset.tab === t ? '' : 'none';
});
if (btn.dataset.tab === 'users') loadUsers();
});
});
// Load org data
try {
const res = await Api.get('/api/v1/org');
if (res.ok) {
const { data } = await res.json();
document.getElementById('org-name').value = data.name || '';
document.getElementById('org-timezone').value = data.timezone || '';
}
} catch {}
document.getElementById('org-form').addEventListener('submit', async (e) => {
e.preventDefault();
const errEl = document.getElementById('org-error'); errEl.style.display = 'none';
const btn = document.getElementById('org-btn'); btn.disabled = true; btn.textContent = 'Saving…';
try {
const res = await Api.patch('/api/v1/org', { name: document.getElementById('org-name').value.trim(), timezone: document.getElementById('org-timezone').value.trim() });
if (res.ok) toast('Saved.', 'success');
else { const b = await res.json(); errEl.textContent = b.message || 'Failed.'; errEl.style.display = 'block'; }
} catch { errEl.textContent = 'Network error.'; errEl.style.display = 'block'; }
finally { btn.disabled = false; btn.textContent = 'Save'; }
});
// Load branding
try {
const res = await Api.get('/api/v1/org/branding');
if (res.ok) {
const { data } = await res.json();
document.getElementById('app-name-input').value = data.appName || '';
if (data.primaryColor) { document.getElementById('primary-color').value = data.primaryColor; document.getElementById('primary-color-pick').value = data.primaryColor; }
if (data.logoPath) { const img = document.getElementById('logo-preview'); img.src = data.logoPath; img.style.display = ''; }
}
} catch {}
document.getElementById('primary-color-pick').addEventListener('input', (e) => {
document.getElementById('primary-color').value = e.target.value;
});
document.getElementById('primary-color').addEventListener('input', (e) => {
if (/^#[0-9A-Fa-f]{6}$/.test(e.target.value)) document.getElementById('primary-color-pick').value = e.target.value;
});
document.getElementById('branding-form').addEventListener('submit', async (e) => {
e.preventDefault();
const errEl = document.getElementById('branding-error'); errEl.style.display = 'none';
const btn = document.getElementById('branding-btn'); btn.disabled = true; btn.textContent = 'Saving…';
const logoFile = document.getElementById('logo-file').files[0];
try {
if (logoFile) {
const fd = new FormData();
fd.append('logo', logoFile);
fd.append('appName', document.getElementById('app-name-input').value.trim());
fd.append('primaryColor', document.getElementById('primary-color').value.trim());
const res = await Api.upload('/api/v1/org/branding/logo', fd);
if (!res.ok) { const b = await res.json(); errEl.textContent = b.message || 'Failed.'; errEl.style.display = 'block'; return; }
} else {
const res = await Api.patch('/api/v1/org/branding', {
appName: document.getElementById('app-name-input').value.trim(),
primaryColor: document.getElementById('primary-color').value.trim() || undefined,
});
if (!res.ok) { const b = await res.json(); errEl.textContent = b.message || 'Failed.'; errEl.style.display = 'block'; return; }
}
toast('Branding updated.', 'success');
Branding.load();
} catch { errEl.textContent = 'Network error.'; errEl.style.display = 'block'; }
finally { btn.disabled = false; btn.textContent = 'Save Branding'; }
});
// Users tab
document.getElementById('invite-btn').addEventListener('click', () => {
document.getElementById('invite-modal').style.display = 'flex';
});
document.getElementById('invite-modal-close').addEventListener('click', () => {
document.getElementById('invite-modal').style.display = 'none';
});
document.getElementById('invite-confirm-btn').addEventListener('click', async () => {
const errEl = document.getElementById('invite-error'); errEl.style.display = 'none';
const name = document.getElementById('invite-name').value.trim();
const email = document.getElementById('invite-email').value.trim();
const role = document.getElementById('invite-role').value;
const password = document.getElementById('invite-password').value;
if (!name || !email || !password) { errEl.textContent = 'All fields required.'; errEl.style.display = 'block'; return; }
try {
const res = await Api.post('/api/v1/users', { name, email, role, password });
if (res.ok) {
toast('User created.', 'success');
document.getElementById('invite-modal').style.display = 'none';
loadUsers();
} else { const b = await res.json(); errEl.textContent = b.message || 'Failed.'; errEl.style.display = 'block'; }
} catch { errEl.textContent = 'Network error.'; errEl.style.display = 'block'; }
});
});
</script>
</body>
</html>