643 lines
28 KiB
HTML
643 lines
28 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<link rel="icon" type="image/x-icon" href="favicon.ico">
|
|
<title>Users — Z-AMPP</title>
|
|
<link rel="stylesheet" href="/dist/app.css">
|
|
<style>
|
|
body { margin: 0; }
|
|
|
|
/* Local-only tabs styling — JS toggles .tab.active and .tab-content.active,
|
|
so we keep those class names and just retheme. */
|
|
.users-tabs {
|
|
display: flex;
|
|
gap: 4px;
|
|
padding: 0 20px;
|
|
background: var(--bg-deep);
|
|
border-bottom: 1px solid var(--border-faint);
|
|
flex-shrink: 0;
|
|
}
|
|
.users-tabs .tab {
|
|
background: transparent;
|
|
border: none;
|
|
color: var(--text-tertiary);
|
|
font: 500 12px/1 var(--font);
|
|
letter-spacing: 0.04em;
|
|
padding: 10px 14px;
|
|
cursor: pointer;
|
|
border-bottom: 2px solid transparent;
|
|
margin-bottom: -1px;
|
|
}
|
|
.users-tabs .tab:hover { color: var(--text-secondary); }
|
|
.users-tabs .tab.active {
|
|
color: var(--accent-bright);
|
|
border-bottom-color: var(--accent);
|
|
}
|
|
|
|
.tab-content { display: none; }
|
|
.tab-content.active { display: flex; flex-direction: column; flex: 1; overflow: hidden; }
|
|
|
|
.section-toolbar {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
margin-bottom: 12px;
|
|
}
|
|
.section-title {
|
|
font: 500 14px/1.2 var(--font);
|
|
color: var(--text-primary);
|
|
}
|
|
|
|
.wd-list-row.users-row {
|
|
display: grid;
|
|
grid-template-columns: minmax(140px, 1fr) minmax(140px, 1.2fr) minmax(80px, 0.6fr) minmax(80px, 0.6fr) minmax(80px, 0.6fr) auto;
|
|
gap: 14px;
|
|
align-items: center;
|
|
}
|
|
.wd-list-row.users-row.header {
|
|
font: 600 10px/1 var(--font);
|
|
letter-spacing: 0.08em;
|
|
text-transform: uppercase;
|
|
color: var(--text-tertiary);
|
|
}
|
|
|
|
.wd-list-row.groups-row {
|
|
display: grid;
|
|
grid-template-columns: minmax(140px, 1fr) minmax(180px, 1.5fr) minmax(80px, 0.6fr) minmax(80px, 0.6fr) auto;
|
|
gap: 14px;
|
|
align-items: center;
|
|
}
|
|
.wd-list-row.groups-row.header {
|
|
font: 600 10px/1 var(--font);
|
|
letter-spacing: 0.08em;
|
|
text-transform: uppercase;
|
|
color: var(--text-tertiary);
|
|
}
|
|
|
|
.empty-row {
|
|
padding: 48px 12px;
|
|
text-align: center;
|
|
color: var(--text-tertiary);
|
|
font-size: 13px;
|
|
}
|
|
|
|
.text-tertiary { color: var(--text-tertiary); }
|
|
.text-secondary { color: var(--text-secondary); }
|
|
.text-xs { font-size: 11px; }
|
|
.text-sm { font-size: 12px; }
|
|
|
|
.member-chips {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: 4px;
|
|
margin-top: 6px;
|
|
}
|
|
.member-chip {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 4px;
|
|
padding: 2px 8px 2px 6px;
|
|
border-radius: 100px;
|
|
font-size: 11px;
|
|
background: var(--bg-surface);
|
|
border: 1px solid var(--border-faint);
|
|
color: var(--text-secondary);
|
|
}
|
|
.member-chip button {
|
|
background: none;
|
|
border: none;
|
|
padding: 0;
|
|
color: var(--text-tertiary);
|
|
cursor: pointer;
|
|
line-height: 1;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
.member-chip button:hover { color: var(--signal-bad); }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="wd-shell" style="display:flex;min-height:100vh;">
|
|
|
|
<!-- Sidebar -->
|
|
<nav class="wd-sidebar" aria-label="Main navigation">
|
|
<div class="wd-sidebar-header">
|
|
<img src="img/dragon-logo.png?v=1" alt="Wild Dragon" style="width:18px;height:18px;">
|
|
<span class="wd-sidebar-brand">Z-AMPP</span>
|
|
</div>
|
|
<div class="wd-sidebar-nav">
|
|
<a href="home.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M2 7l6-5 6 5v7a1 1 0 0 1-1 1h-3v-5H6v5H3a1 1 0 0 1-1-1z"/></svg>
|
|
Home
|
|
</a>
|
|
<a href="index.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="1" y="1" width="6" height="6" rx="1"/><rect x="9" y="1" width="6" height="6" rx="1"/><rect x="1" y="9" width="6" height="6" rx="1"/><rect x="9" y="9" width="6" height="6" rx="1"/></svg>
|
|
Library
|
|
</a>
|
|
<a href="projects.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M1 4a1 1 0 0 1 1-1h4l2 2h5a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1z"/></svg>
|
|
Projects
|
|
</a>
|
|
<a href="upload.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M8 11V3M5 6l3-3 3 3"/><path d="M2 13h12"/></svg>
|
|
Ingest
|
|
</a>
|
|
<a href="recorders.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="1" y="4" width="10" height="8" rx="1"/><path d="M11 7l4-2v6l-4-2"/></svg>
|
|
Recorders
|
|
</a>
|
|
<a href="capture.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="8" cy="8" r="3"/><circle cx="8" cy="8" r="6.5"/></svg>
|
|
Capture
|
|
</a>
|
|
<a href="jobs.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M2 4h12M2 8h8M2 12h5"/></svg>
|
|
Jobs
|
|
</a>
|
|
<a href="editor.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M2 13l1.5-3.5L11 2l3 3-7.5 7.5L3 14zM10 3l3 3"/></svg>
|
|
Editor
|
|
</a>
|
|
<div class="wd-sidebar-section">Admin</div>
|
|
<a href="users.html" class="wd-nav-item is-active">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="6" cy="5" r="2.5"/><path d="M1 13c0-2.8 2.2-5 5-5s5 2.2 5 5"/><circle cx="12" cy="5" r="2"/><path d="M15 12c0-1.9-1.3-3.5-3-4"/></svg>
|
|
Users
|
|
</a>
|
|
<a href="tokens.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="6" cy="10" r="3.5"/><path d="M8.7 7.3L13 3M11.5 3.5l1.5 1.5M13.5 2.5l1 1"/></svg>
|
|
Tokens
|
|
</a>
|
|
<a href="containers.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><rect x="1" y="5" width="14" height="4" rx="1"/><rect x="1" y="10" width="14" height="4" rx="1"/><path d="M4 7h1M4 12h1"/></svg>
|
|
Containers
|
|
</a>
|
|
<a href="cluster.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="8" cy="8" r="2"/><circle cx="2" cy="3" r="1.5"/><circle cx="14" cy="3" r="1.5"/><circle cx="2" cy="13" r="1.5"/><circle cx="14" cy="13" r="1.5"/><path d="M3.1 4.1L6.5 6.5M12.9 4.1L9.5 6.5M3.1 11.9L6.5 9.5M12.9 11.9L9.5 9.5"/></svg>
|
|
Cluster
|
|
</a>
|
|
<a href="settings.html" class="wd-nav-item">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="8" cy="8" r="2.5"/><path d="M8 1v1.5M8 13.5V15M1 8h1.5M13.5 8H15M2.9 2.9l1.1 1.1M12 12l1.1 1.1M2.9 13.1L4 12M12 4l1.1-1.1"/></svg>
|
|
Settings
|
|
</a>
|
|
</div>
|
|
<div class="wd-sidebar-footer">
|
|
<div class="wd-sidebar-user">
|
|
<div class="wd-sidebar-user-avatar" id="userAvatar">?</div>
|
|
<div class="wd-sidebar-user-info">
|
|
<div class="wd-sidebar-user-name" id="userName">—</div>
|
|
<div class="wd-sidebar-user-role" id="userRole"></div>
|
|
</div>
|
|
<button class="wd-btn wd-btn--ghost wd-btn--sm wd-btn--icon wd-sidebar-user-logout" id="logoutBtn" title="Sign out">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" width="12" height="12"><path d="M10 8H3M6 5l-3 3 3 3"/><path d="M7 3h5a1 1 0 0 1 1 1v8a1 1 0 0 1-1 1H7"/></svg>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<!-- Main -->
|
|
<div style="flex:1;display:flex;flex-direction:column;">
|
|
<header class="wd-topbar">
|
|
<div class="wd-topbar-left">
|
|
<nav class="wd-breadcrumb"><span class="wd-breadcrumb-crumb">Users & Groups</span></nav>
|
|
</div>
|
|
<div class="wd-topbar-right">
|
|
<button class="wd-btn wd-btn--primary wd-btn--sm" onclick="openUserPanel()">New user</button>
|
|
</div>
|
|
</header>
|
|
|
|
<!-- Tabs -->
|
|
<div class="users-tabs">
|
|
<button class="tab active" onclick="switchTab('users',this)">Users</button>
|
|
<button class="tab" onclick="switchTab('groups',this)">Groups</button>
|
|
</div>
|
|
|
|
<!-- Users tab -->
|
|
<div class="tab-content active" id="tab-users">
|
|
<main style="flex:1;padding:20px 20px 32px;overflow:auto;">
|
|
<div class="section-toolbar">
|
|
<span class="section-title" id="userCount">Users</span>
|
|
<button class="wd-btn wd-btn--primary wd-btn--sm" onclick="openUserPanel()">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" width="12" height="12"><path d="M8 2v12M2 8h12"/></svg>
|
|
New user
|
|
</button>
|
|
</div>
|
|
<div class="wd-list" id="usersTable">
|
|
<div class="wd-list-row users-row header">
|
|
<span>Username</span>
|
|
<span>Display name</span>
|
|
<span>Role</span>
|
|
<span>Groups</span>
|
|
<span>Created</span>
|
|
<span style="text-align:right">Actions</span>
|
|
</div>
|
|
<div id="usersTbody">
|
|
<div class="empty-row">Loading…</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
|
|
<!-- Groups tab -->
|
|
<div class="tab-content" id="tab-groups">
|
|
<main style="flex:1;padding:20px 20px 32px;overflow:auto;">
|
|
<div class="section-toolbar">
|
|
<span class="section-title" id="groupCount">Groups</span>
|
|
<button class="wd-btn wd-btn--primary wd-btn--sm" onclick="openGroupPanel()">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" width="12" height="12"><path d="M8 2v12M2 8h12"/></svg>
|
|
New group
|
|
</button>
|
|
</div>
|
|
<div class="wd-list" id="groupsTable">
|
|
<div class="wd-list-row groups-row header">
|
|
<span>Name</span>
|
|
<span>Description</span>
|
|
<span>Members</span>
|
|
<span>Created</span>
|
|
<span style="text-align:right">Actions</span>
|
|
</div>
|
|
<div id="groupsTbody">
|
|
<div class="empty-row">Loading…</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- User slide panel -->
|
|
<div class="wd-slide-panel-overlay" id="userOverlay" onclick="closeUserPanel()"></div>
|
|
<div class="wd-slide-panel" id="userPanel">
|
|
<div class="wd-slide-panel-header">
|
|
<span class="wd-slide-panel-title" id="userPanelTitle">New user</span>
|
|
<button class="wd-btn wd-btn--ghost wd-btn--sm wd-btn--icon" onclick="closeUserPanel()" aria-label="Close">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" width="12" height="12"><path d="M3 3l10 10M13 3L3 13"/></svg>
|
|
</button>
|
|
</div>
|
|
<div class="wd-slide-panel-body">
|
|
<input type="hidden" id="editUserId">
|
|
<div class="wd-form-group">
|
|
<label class="wd-label" for="uUsername">Username</label>
|
|
<input class="wd-input" type="text" id="uUsername" placeholder="e.g. jsmith">
|
|
</div>
|
|
<div class="wd-form-group">
|
|
<label class="wd-label" for="uDisplayName">Display name</label>
|
|
<input class="wd-input" type="text" id="uDisplayName" placeholder="e.g. Jane Smith">
|
|
</div>
|
|
<div class="wd-form-group">
|
|
<label class="wd-label" for="uRole">Role</label>
|
|
<select class="wd-select" id="uRole">
|
|
<option value="editor">Editor</option>
|
|
<option value="viewer">Viewer</option>
|
|
<option value="admin">Admin</option>
|
|
</select>
|
|
</div>
|
|
<div class="wd-form-group">
|
|
<label class="wd-label" for="uPassword" id="uPasswordLabel">Password</label>
|
|
<input class="wd-input" type="password" id="uPassword" placeholder="Min 8 characters">
|
|
<div class="wd-hint" id="uPasswordHint"></div>
|
|
</div>
|
|
</div>
|
|
<div class="wd-slide-panel-footer">
|
|
<button class="wd-btn wd-btn--ghost wd-btn--md" onclick="closeUserPanel()">Cancel</button>
|
|
<button class="wd-btn wd-btn--primary wd-btn--md" id="saveUserBtn" onclick="saveUser()">Create user</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Group slide panel -->
|
|
<div class="wd-slide-panel-overlay" id="groupOverlay" onclick="closeGroupPanel()"></div>
|
|
<div class="wd-slide-panel" id="groupPanel">
|
|
<div class="wd-slide-panel-header">
|
|
<span class="wd-slide-panel-title" id="groupPanelTitle">New group</span>
|
|
<button class="wd-btn wd-btn--ghost wd-btn--sm wd-btn--icon" onclick="closeGroupPanel()" aria-label="Close">
|
|
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" width="12" height="12"><path d="M3 3l10 10M13 3L3 13"/></svg>
|
|
</button>
|
|
</div>
|
|
<div class="wd-slide-panel-body">
|
|
<input type="hidden" id="editGroupId">
|
|
<div class="wd-form-group">
|
|
<label class="wd-label" for="gName">Group name</label>
|
|
<input class="wd-input" type="text" id="gName" placeholder="e.g. News Team">
|
|
</div>
|
|
<div class="wd-form-group">
|
|
<label class="wd-label" for="gDescription">Description</label>
|
|
<textarea class="wd-textarea" id="gDescription" rows="2" placeholder="Optional description"></textarea>
|
|
</div>
|
|
<div class="wd-form-group" id="gMembersSection" style="display:none;">
|
|
<label class="wd-label">Members</label>
|
|
<div class="member-chips" id="memberChips"></div>
|
|
<select class="wd-select" id="addMemberSelect" style="margin-top:8px;">
|
|
<option value="">Add member…</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="wd-slide-panel-footer">
|
|
<button class="wd-btn wd-btn--ghost wd-btn--md" onclick="closeGroupPanel()">Cancel</button>
|
|
<button class="wd-btn wd-btn--primary wd-btn--md" id="saveGroupBtn" onclick="saveGroup()">Create group</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="wd-toast-container" id="toastContainer" aria-live="polite"></div>
|
|
|
|
<script src="js/api.js"></script>
|
|
<script>
|
|
let allUsers = [], allGroups = [], currentGroupMembers = [];
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
loadUsers();
|
|
loadGroups();
|
|
});
|
|
|
|
// ── Tabs ──────────────────────────────────────────────────
|
|
function switchTab(name, btn) {
|
|
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
|
|
document.querySelectorAll('.tab-content').forEach(c => c.classList.remove('active'));
|
|
btn.classList.add('active');
|
|
document.getElementById('tab-' + name).classList.add('active');
|
|
}
|
|
|
|
// ── Load users ────────────────────────────────────────────
|
|
async function loadUsers() {
|
|
const r = await getUsers();
|
|
if (!r.success) { toast('Failed to load users', r.error, 'error'); return; }
|
|
allUsers = r.data;
|
|
renderUsers();
|
|
}
|
|
|
|
function renderUsers() {
|
|
const tbody = document.getElementById('usersTbody');
|
|
document.getElementById('userCount').textContent = `${allUsers.length} user${allUsers.length !== 1 ? 's' : ''}`;
|
|
if (!allUsers.length) {
|
|
tbody.innerHTML = `<div class="empty-row">No users yet</div>`;
|
|
return;
|
|
}
|
|
const roleBadge = (role) => {
|
|
const mod = role === 'admin' ? 'wd-badge--good'
|
|
: role === 'editor' ? 'wd-badge--info'
|
|
: 'wd-badge--idle';
|
|
return `<span class="wd-badge ${mod}">${esc(role)}</span>`;
|
|
};
|
|
tbody.innerHTML = allUsers.map(u => `
|
|
<div class="wd-list-row users-row">
|
|
<div class="wd-list-cell wd-list-cell--name"><code style="font-size:11px;font-family:var(--font-mono);">${esc(u.username)}</code></div>
|
|
<div class="wd-list-cell">${esc(u.display_name || '—')}</div>
|
|
<div class="wd-list-cell">${roleBadge(u.role)}</div>
|
|
<div class="wd-list-cell"><span class="text-tertiary text-xs">${u.group_count || 0} group${u.group_count !== 1 ? 's' : ''}</span></div>
|
|
<div class="wd-list-cell text-xs text-tertiary">${u.created_at ? new Date(u.created_at).toLocaleDateString() : '—'}</div>
|
|
<div class="wd-list-cell wd-list-cell--actions" style="text-align:right">
|
|
<div style="display:flex;gap:4px;justify-content:flex-end;">
|
|
<button class="wd-btn wd-btn--ghost wd-btn--sm" onclick="editUser('${u.id}')">Edit</button>
|
|
<button class="wd-btn wd-btn--danger wd-btn--sm" onclick="confirmDeleteUser('${u.id}',${esc(JSON.stringify(u.username))})">Delete</button>
|
|
</div>
|
|
</div>
|
|
</div>`).join('');
|
|
}
|
|
|
|
// ── User panel ────────────────────────────────────────────
|
|
function openUserPanel(userId) {
|
|
document.getElementById('editUserId').value = '';
|
|
document.getElementById('uUsername').value = '';
|
|
document.getElementById('uDisplayName').value = '';
|
|
document.getElementById('uRole').value = 'editor';
|
|
document.getElementById('uPassword').value = '';
|
|
document.getElementById('uUsername').disabled = false;
|
|
document.getElementById('userPanelTitle').textContent = 'New user';
|
|
document.getElementById('saveUserBtn').textContent = 'Create user';
|
|
document.getElementById('uPasswordLabel').textContent = 'Password';
|
|
document.getElementById('uPasswordHint').textContent = '';
|
|
document.getElementById('userPanel').classList.add('open');
|
|
document.getElementById('userOverlay').classList.add('open');
|
|
}
|
|
|
|
function editUser(id) {
|
|
const u = allUsers.find(x => x.id === id);
|
|
if (!u) return;
|
|
document.getElementById('editUserId').value = u.id;
|
|
document.getElementById('uUsername').value = u.username;
|
|
document.getElementById('uUsername').disabled = true;
|
|
document.getElementById('uDisplayName').value = u.display_name || '';
|
|
document.getElementById('uRole').value = u.role;
|
|
document.getElementById('uPassword').value = '';
|
|
document.getElementById('userPanelTitle').textContent = 'Edit user';
|
|
document.getElementById('saveUserBtn').textContent = 'Save changes';
|
|
document.getElementById('uPasswordLabel').textContent = 'New password';
|
|
document.getElementById('uPasswordHint').textContent = 'Leave blank to keep existing password';
|
|
document.getElementById('userPanel').classList.add('open');
|
|
document.getElementById('userOverlay').classList.add('open');
|
|
}
|
|
|
|
function closeUserPanel() {
|
|
document.getElementById('userPanel').classList.remove('open');
|
|
document.getElementById('userOverlay').classList.remove('open');
|
|
}
|
|
|
|
async function saveUser() {
|
|
const id = document.getElementById('editUserId').value;
|
|
const username = document.getElementById('uUsername').value.trim();
|
|
const display_name = document.getElementById('uDisplayName').value.trim();
|
|
const role = document.getElementById('uRole').value;
|
|
const password = document.getElementById('uPassword').value;
|
|
|
|
if (!id && !username) { toast('Username required', '', 'warning'); return; }
|
|
if (!id && !password) { toast('Password required for new user', '', 'warning'); return; }
|
|
|
|
const btn = document.getElementById('saveUserBtn');
|
|
btn.disabled = true;
|
|
|
|
let r;
|
|
if (id) {
|
|
const payload = { display_name, role };
|
|
if (password) payload.password = password;
|
|
r = await updateUser(id, payload);
|
|
} else {
|
|
r = await createUser({ username, display_name, role, password });
|
|
}
|
|
|
|
btn.disabled = false;
|
|
if (r.success) {
|
|
toast(id ? 'User updated' : 'User created', '', 'success');
|
|
closeUserPanel();
|
|
loadUsers();
|
|
} else {
|
|
toast('Failed to save user', r.error, 'error');
|
|
}
|
|
}
|
|
|
|
async function confirmDeleteUser(id, name) {
|
|
if (!confirm(`Delete user "${name}"? This cannot be undone.`)) return;
|
|
const r = await deleteUser(id);
|
|
if (r.success) { toast('User deleted', '', 'success'); loadUsers(); }
|
|
else toast('Failed to delete user', r.error, 'error');
|
|
}
|
|
|
|
// ── Load groups ───────────────────────────────────────────
|
|
async function loadGroups() {
|
|
const r = await getGroups();
|
|
if (!r.success) { toast('Failed to load groups', r.error, 'error'); return; }
|
|
allGroups = r.data;
|
|
renderGroups();
|
|
}
|
|
|
|
function renderGroups() {
|
|
const tbody = document.getElementById('groupsTbody');
|
|
document.getElementById('groupCount').textContent = `${allGroups.length} group${allGroups.length !== 1 ? 's' : ''}`;
|
|
if (!allGroups.length) {
|
|
tbody.innerHTML = `<div class="empty-row">No groups yet</div>`;
|
|
return;
|
|
}
|
|
tbody.innerHTML = allGroups.map(g => `
|
|
<div class="wd-list-row groups-row">
|
|
<div class="wd-list-cell wd-list-cell--name" style="font-weight:500">${esc(g.name)}</div>
|
|
<div class="wd-list-cell text-secondary text-sm">${esc(g.description || '—')}</div>
|
|
<div class="wd-list-cell text-xs text-tertiary">${g.member_count || 0} member${g.member_count !== 1 ? 's' : ''}</div>
|
|
<div class="wd-list-cell text-xs text-tertiary">${g.created_at ? new Date(g.created_at).toLocaleDateString() : '—'}</div>
|
|
<div class="wd-list-cell wd-list-cell--actions" style="text-align:right">
|
|
<div style="display:flex;gap:4px;justify-content:flex-end;">
|
|
<button class="wd-btn wd-btn--ghost wd-btn--sm" onclick="editGroup('${g.id}')">Edit</button>
|
|
<button class="wd-btn wd-btn--danger wd-btn--sm" onclick="confirmDeleteGroup('${g.id}',${esc(JSON.stringify(g.name))})">Delete</button>
|
|
</div>
|
|
</div>
|
|
</div>`).join('');
|
|
}
|
|
|
|
// ── Group panel ───────────────────────────────────────────
|
|
function openGroupPanel() {
|
|
document.getElementById('editGroupId').value = '';
|
|
document.getElementById('gName').value = '';
|
|
document.getElementById('gDescription').value = '';
|
|
document.getElementById('gMembersSection').style.display = 'none';
|
|
document.getElementById('groupPanelTitle').textContent = 'New group';
|
|
document.getElementById('saveGroupBtn').textContent = 'Create group';
|
|
document.getElementById('groupPanel').classList.add('open');
|
|
document.getElementById('groupOverlay').classList.add('open');
|
|
}
|
|
|
|
async function editGroup(id) {
|
|
const g = allGroups.find(x => x.id === id);
|
|
if (!g) return;
|
|
document.getElementById('editGroupId').value = g.id;
|
|
document.getElementById('gName').value = g.name;
|
|
document.getElementById('gDescription').value = g.description || '';
|
|
document.getElementById('groupPanelTitle').textContent = 'Edit group';
|
|
document.getElementById('saveGroupBtn').textContent = 'Save changes';
|
|
document.getElementById('groupPanel').classList.add('open');
|
|
document.getElementById('groupOverlay').classList.add('open');
|
|
|
|
// Load members
|
|
document.getElementById('gMembersSection').style.display = 'block';
|
|
const mr = await getGroupMembers(id);
|
|
currentGroupMembers = mr.success ? mr.data : [];
|
|
renderMemberChips(id);
|
|
|
|
// Populate add-member dropdown
|
|
const sel = document.getElementById('addMemberSelect');
|
|
const memberIds = new Set(currentGroupMembers.map(m => m.id));
|
|
sel.innerHTML = '<option value="">Add member…</option>' +
|
|
allUsers.filter(u => !memberIds.has(u.id))
|
|
.map(u => `<option value="${u.id}">${esc(u.display_name || u.username)}</option>`)
|
|
.join('');
|
|
sel.onchange = async () => {
|
|
if (!sel.value) return;
|
|
await addGroupMember(id, sel.value);
|
|
const mr2 = await getGroupMembers(id);
|
|
currentGroupMembers = mr2.success ? mr2.data : [];
|
|
renderMemberChips(id);
|
|
// Update dropdown
|
|
const memberIds2 = new Set(currentGroupMembers.map(m => m.id));
|
|
sel.innerHTML = '<option value="">Add member…</option>' +
|
|
allUsers.filter(u => !memberIds2.has(u.id))
|
|
.map(u => `<option value="${u.id}">${esc(u.display_name || u.username)}</option>`)
|
|
.join('');
|
|
loadGroups();
|
|
};
|
|
}
|
|
|
|
function renderMemberChips(groupId) {
|
|
const container = document.getElementById('memberChips');
|
|
if (!currentGroupMembers.length) {
|
|
container.innerHTML = `<span class="text-xs text-tertiary">No members yet</span>`;
|
|
return;
|
|
}
|
|
container.innerHTML = currentGroupMembers.map(m => `
|
|
<span class="member-chip">
|
|
${esc(m.display_name || m.username)}
|
|
<button onclick="removeMember('${groupId}','${m.id}')" title="Remove">
|
|
<svg viewBox="0 0 10 10" fill="none" stroke="currentColor" stroke-width="1.5" width="10" height="10"><path d="M2 2l6 6M8 2L2 8"/></svg>
|
|
</button>
|
|
</span>`).join('');
|
|
}
|
|
|
|
async function removeMember(groupId, userId) {
|
|
await removeGroupMember(groupId, userId);
|
|
const mr = await getGroupMembers(groupId);
|
|
currentGroupMembers = mr.success ? mr.data : [];
|
|
renderMemberChips(groupId);
|
|
const memberIds = new Set(currentGroupMembers.map(m => m.id));
|
|
const sel = document.getElementById('addMemberSelect');
|
|
sel.innerHTML = '<option value="">Add member…</option>' +
|
|
allUsers.filter(u => !memberIds.has(u.id))
|
|
.map(u => `<option value="${u.id}">${esc(u.display_name || u.username)}</option>`)
|
|
.join('');
|
|
loadGroups();
|
|
}
|
|
|
|
function closeGroupPanel() {
|
|
document.getElementById('groupPanel').classList.remove('open');
|
|
document.getElementById('groupOverlay').classList.remove('open');
|
|
currentGroupMembers = [];
|
|
}
|
|
|
|
async function saveGroup() {
|
|
const id = document.getElementById('editGroupId').value;
|
|
const name = document.getElementById('gName').value.trim();
|
|
const description = document.getElementById('gDescription').value.trim();
|
|
if (!name) { toast('Group name required', '', 'warning'); return; }
|
|
|
|
const btn = document.getElementById('saveGroupBtn');
|
|
btn.disabled = true;
|
|
|
|
const r = id
|
|
? await updateGroup(id, { name, description })
|
|
: await createGroup({ name, description });
|
|
|
|
btn.disabled = false;
|
|
if (r.success) {
|
|
toast(id ? 'Group updated' : 'Group created', name, 'success');
|
|
closeGroupPanel();
|
|
loadGroups();
|
|
} else {
|
|
toast('Failed to save group', r.error, 'error');
|
|
}
|
|
}
|
|
|
|
async function confirmDeleteGroup(id, name) {
|
|
if (!confirm(`Delete group "${name}"?`)) return;
|
|
const r = await deleteGroup(id);
|
|
if (r.success) { toast('Group deleted', '', 'success'); loadGroups(); }
|
|
else toast('Failed to delete group', r.error, 'error');
|
|
}
|
|
|
|
// ── Toast ─────────────────────────────────────────────────
|
|
function toast(title, msg, type = 'info') {
|
|
const icons = {
|
|
success: `<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="8" cy="8" r="6.5"/><path d="M5 8l2 2 4-4"/></svg>`,
|
|
error: `<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="8" cy="8" r="6.5"/><path d="M8 5v4M8 11v.5"/></svg>`,
|
|
warning: `<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M8 2L1 14h14L8 2z"/><path d="M8 7v3M8 12v.5"/></svg>`,
|
|
info: `<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><circle cx="8" cy="8" r="6.5"/><path d="M8 7v5M8 5v.5"/></svg>`,
|
|
};
|
|
const el = document.createElement('div');
|
|
el.className = `wd-toast wd-toast--${type}`;
|
|
el.innerHTML = `<div class="wd-toast-icon">${icons[type]||icons.info}</div><div class="wd-toast-body"><div class="wd-toast-title">${esc(title)}</div>${msg?`<div class="wd-toast-msg">${esc(msg)}</div>`:''}</div>`;
|
|
document.getElementById('toastContainer').appendChild(el);
|
|
setTimeout(() => el.remove(), 4000);
|
|
}
|
|
|
|
function esc(s) {
|
|
if (!s) return '';
|
|
return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
|
|
}
|
|
</script>
|
|
<script src="js/auth-guard.js"></script>
|
|
</body>
|
|
</html>
|