forked from geetxnshgoyal/kickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
456 lines (407 loc) · 16.4 KB
/
admin.js
File metadata and controls
456 lines (407 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
(function () {
// ==== DOM ELEMENTS ====
const authForm = document.getElementById('admin-auth-form');
const adminKeyInput = document.getElementById('admin-key');
const clearKeyButton = document.getElementById('clear-key');
const rememberKeyCheckbox = document.getElementById('remember-key');
const adminKeyInfo = document.getElementById('admin-key-info');
const dashboard = document.querySelector('.admin-dashboard');
const adminAlert = document.getElementById('admin-alert');
const tabs = document.querySelectorAll('.admin-tab');
const refreshButton = document.getElementById('refresh-view');
const exportButton = document.getElementById('export-view');
const viewStatus = document.getElementById('view-status');
const teamUpName = document.getElementById('team-up-name');
const teamUpCreateButton = document.getElementById('team-up-create');
const individualList = document.getElementById('individual-list');
const teamList = document.getElementById('team-list');
const emptyIndividual = document.querySelector('#view-individuals .data-empty');
const emptyTeams = document.querySelector('#view-teams .data-empty');
// ==== CONSTANTS ====
const MAX_TEAM_SELECTION = 4;
// ==== APP STATE ====
const state = {
adminKey: '',
view: 'individuals',
individuals: [],
teams: [],
selectedParticipants: new Set(),
leaderParticipantId: null
};
// ==== UTILITIES ====
const API_BASE = (typeof window !== 'undefined' && window.location && window.location.origin)
? window.location.origin
: '';
function api(path) {
if (!path.startsWith('/')) path = `/${path}`;
return `${API_BASE}${path}`;
}
function escapeAttribute(value) {
return String(value)
.replace(/&/g, '&')
.replace(/"/g, '"');
}
// ==== ADMIN STATUS BAR ====
const adminStatusEl = document.createElement('div');
adminStatusEl.id = 'admin-status';
adminStatusEl.style.margin = '12px 0';
adminStatusEl.style.padding = '8px 12px';
adminStatusEl.style.borderRadius = '4px';
adminStatusEl.style.fontWeight = '600';
adminStatusEl.style.display = 'none';
document.body.insertBefore(adminStatusEl, document.body.firstChild);
async function refreshAdminStatus() {
try {
const res = await fetch(api('/api/status'));
const json = await res.json();
adminStatusEl.style.display = 'block';
if (json.status === 'connected') {
adminStatusEl.textContent = `DB connected (${json.mode})`;
adminStatusEl.style.background = '#0b8a3e22';
adminStatusEl.style.color = '#0bd67a';
} else if (json.status === 'mock') {
adminStatusEl.textContent = `Using mock DB (development)`;
adminStatusEl.style.background = '#0b64ff22';
adminStatusEl.style.color = '#3aa0ff';
} else {
adminStatusEl.textContent = `DB offline: ${json.message || 'no credentials'}`;
adminStatusEl.style.background = '#ff3b3022';
adminStatusEl.style.color = '#ff3b30';
}
} catch {
adminStatusEl.style.display = 'block';
adminStatusEl.textContent = 'Status unavailable';
adminStatusEl.style.background = '#ffcc0022';
adminStatusEl.style.color = '#ffcc00';
}
}
// ==== CSS RULE FOR SEAT HIGHLIGHT ====
const style = document.createElement('style');
style.textContent = `.seat-present { background: #ffc0e4; border-radius: 4px; padding: 4px; }`;
document.head.appendChild(style);
// ==== ALERT SYSTEM ====
function showAlert(message, variant = 'info') {
adminAlert.hidden = false;
adminAlert.textContent = message;
adminAlert.dataset.variant = variant;
}
function clearAlert() {
adminAlert.hidden = true;
adminAlert.textContent = '';
delete adminAlert.dataset.variant;
}
// ==== ADMIN KEY STORAGE ====
function saveAdminKey(key) {
state.adminKey = key;
if (rememberKeyCheckbox && rememberKeyCheckbox.checked) {
localStorage.setItem('kickstart-admin-key', key);
adminKeyInfo.textContent = 'Key saved to device.';
} else {
localStorage.removeItem('kickstart-admin-key');
adminKeyInfo.textContent = 'Key in memory for this session only.';
}
clearKeyButton.hidden = false;
}
function loadAdminKey() {
const saved = localStorage.getItem('kickstart-admin-key');
if (saved) {
adminKeyInput.value = saved;
state.adminKey = saved;
clearKeyButton.hidden = false;
rememberKeyCheckbox.checked = true;
adminKeyInfo.textContent = 'Key saved on device.';
return true;
}
return false;
}
// ==== DASHBOARD CONTROL ====
function setDashboardEnabled(enabled) {
dashboard.classList.toggle('is-disabled', !enabled);
dashboard.setAttribute('aria-disabled', String(!enabled));
}
async function unlockDashboard(event) {
event.preventDefault();
clearAlert();
const key = adminKeyInput.value.trim();
if (!key) {
showAlert('Enter the admin access key to continue.', 'error');
return;
}
saveAdminKey(key);
setDashboardEnabled(true);
showAlert('Dashboard unlocked.', 'success');
await loadView(state.view);
}
// ==== SELECTION / TEAM-UP ====
function resetSelection() {
state.selectedParticipants.clear();
state.leaderParticipantId = null;
teamUpName.value = '';
teamUpCreateButton.disabled = true;
updateSelectionUI();
}
function updateSelectionUI() {
const selectionSize = state.selectedParticipants.size;
const withinLimit = selectionSize >= 2 && selectionSize <= MAX_TEAM_SELECTION;
teamUpCreateButton.disabled = !withinLimit || !teamUpName.value.trim();
const leaderRadios = individualList.querySelectorAll('.select-leader');
leaderRadios.forEach((radio) => {
const participantId = radio.dataset.participantId;
const isSelected = state.selectedParticipants.has(participantId);
radio.disabled = !isSelected;
radio.checked = state.leaderParticipantId === participantId;
});
}
// ==== VIEW HANDLING ====
function setView(view) {
state.view = view;
tabs.forEach((tab) => {
const isActive = tab.dataset.view === view;
tab.classList.toggle('is-active', isActive);
tab.setAttribute('aria-selected', String(isActive));
});
document.querySelectorAll('.admin-view').forEach((panel) => {
panel.classList.toggle('is-hidden', panel.dataset.view !== view);
});
viewStatus.textContent = 'Loading…';
loadView(view);
}
async function loadView(view) {
try {
const response = await fetch(api(`/api/admin/registrations?view=${view}`), {
headers: { 'x-admin-key': state.adminKey }
});
if (response.status === 401) {
showAlert('Access denied: admin key rejected by server. Check or clear the saved key.', 'error');
return;
}
const contentType = response.headers.get('content-type') || '';
if (!contentType.includes('application/json')) {
throw new Error('Server returned non-JSON response.');
}
const data = await response.json();
if (!response.ok) throw new Error(data?.error || 'Unable to fetch data');
const teams = Array.isArray(data.teams) ? data.teams : [];
if (view === 'individuals') {
state.individuals = teams;
renderIndividuals();
} else {
state.teams = teams;
renderTeams();
}
viewStatus.textContent = `Loaded ${teams.length} record${teams.length === 1 ? '' : 's'}.`;
} catch (error) {
console.error('Load view failed:', error);
viewStatus.textContent = 'Error loading data.';
showAlert(error.message || 'Unable to load data.', 'error');
}
try { refreshAdminStatus(); } catch {}
}
// ==== RENDER FUNCTIONS ====
function renderIndividuals() {
state.selectedParticipants.clear();
state.leaderParticipantId = null;
if (!state.individuals.length) {
individualList.innerHTML = '';
emptyIndividual.hidden = false;
updateSelectionUI();
return;
}
emptyIndividual.hidden = true;
individualList.innerHTML = state.individuals.map((item) => {
let p = item?.participants?.[0] || item?.participant || item;
if (!p?.id) return '';
const pid = String(p.id);
const phone = p.phone ? `<span>${p.phone}</span>` : '';
const profile = p.profile ? `<a href="${p.profile}" target="_blank" rel="noopener">Profile ↗</a>` : '';
return `
<li class="card card--individual" data-participant-id="${escapeAttribute(pid)}">
<div class="card__select">
<input type="checkbox" class="select-participant" id="participant-${pid}" data-participant-id="${pid}">
<label for="participant-${pid}">Select</label>
</div>
<div class="card__body">
<h3>${escapeAttribute(p.name || 'Unnamed')}</h3>
<p>${escapeAttribute(p.email || '')}</p>
<div class="card__meta">${phone}${profile}</div>
<span class="tag">Registered solo</span>
</div>
<div class="card__leader">
<label><input type="radio" class="select-leader" name="team-leader" data-participant-id="${pid}" disabled> Lead</label>
</div>
</li>`;
}).join('');
individualList.querySelectorAll('.select-participant').forEach(cb => {
cb.addEventListener('change', onParticipantToggle);
});
individualList.querySelectorAll('.select-leader').forEach(radio => {
radio.addEventListener('change', () => {
state.leaderParticipantId = String(radio.dataset.participantId);
updateSelectionUI();
});
});
updateSelectionUI();
}
function onParticipantToggle(e) {
const id = e.currentTarget.dataset.participantId;
if (e.currentTarget.checked) {
if (state.selectedParticipants.size >= MAX_TEAM_SELECTION) {
e.currentTarget.checked = false;
showAlert(`You can only group up to ${MAX_TEAM_SELECTION} participants.`, 'error');
return;
}
state.selectedParticipants.add(id);
if (!state.leaderParticipantId) state.leaderParticipantId = id;
} else {
state.selectedParticipants.delete(id);
if (state.leaderParticipantId === id) {
const next = state.selectedParticipants.values().next().value;
state.leaderParticipantId = next ?? null;
}
}
updateSelectionUI();
}
function renderTeams() {
if (!state.teams.length) {
teamList.innerHTML = '';
emptyTeams.hidden = false;
return;
}
emptyTeams.hidden = true;
teamList.innerHTML = state.teams.map(team => {
const participants = Array.isArray(team.participants) ? team.participants : [];
const leader = participants.find(p => p.role === 'leader') || participants[0];
const others = participants.filter(p => p.id !== leader?.id);
const seat = team.seat_number || '';
const markLabel = team.attendance_marked ? 'Reassign seat' : 'Mark present';
const clearBtn = team.attendance_marked
? `<button class="button button--ghost" data-action="clear" data-team-id="${team.id}">Clear attendance</button>`
: '';
return `
<li class="card card--team" data-team-id="${team.id}">
<header class="card__header">
<h3>${team.name}</h3>
<span class="tag">${team.source || 'Unknown'}</span>
</header>
<div class="card__body">
<strong>Lead:</strong> ${leader?.name || 'Unknown'}
</div>
<footer class="card__footer">
<label>Seat <input type="text" class="seat-input ${team.attendance_marked ? 'seat-present' : ''}" data-team-id="${team.id}" value="${escapeAttribute(seat)}"></label>
<button class="button" data-action="mark" data-team-id="${team.id}">${markLabel}</button>
${clearBtn}
</footer>
</li>`;
}).join('');
teamList.querySelectorAll('button[data-action="mark"]').forEach(btn => {
btn.addEventListener('click', async e => {
const id = e.currentTarget.dataset.teamId;
const seatInput = teamList.querySelector(`.seat-input[data-team-id="${id}"]`);
await handleAttendance(id, true, seatInput?.value.trim() || '');
});
});
teamList.querySelectorAll('button[data-action="clear"]').forEach(btn => {
btn.addEventListener('click', async e => handleAttendance(e.currentTarget.dataset.teamId, false));
});
}
// ==== ATTENDANCE ====
async function handleAttendance(teamId, present, seatNumber = '') {
clearAlert();
try {
const res = await fetch(api('/api/admin/attendance'), {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'x-admin-key': state.adminKey },
body: JSON.stringify({ teamId, present, seatNumber })
});
const data = await res.json();
if (!res.ok) throw new Error(data?.error || 'Unable to update attendance');
showAlert(present ? `Team marked present (Seat ${data.seatNumber || 'pending'})` : 'Attendance cleared', 'success');
await loadView('teams');
} catch (err) {
showAlert(err.message || 'Unable to update attendance.', 'error');
}
}
// ==== EXPORT ====
async function exportCurrentView() {
if (!state.adminKey) return showAlert('Unlock the dashboard first.', 'error');
clearAlert();
try {
const format = (document.getElementById('export-format') || {}).value || 'csv';
const res = await fetch(api(`/api/admin/export?view=${state.view}&format=${encodeURIComponent(format)}`), {
headers: { 'x-admin-key': state.adminKey }
});
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw new Error(err.error || 'Export failed');
}
const blob = await res.blob();
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `kickstart-${state.view}-${new Date().toISOString().slice(0, 10)}.csv`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
showAlert('CSV downloaded successfully.', 'success');
} catch (err) {
showAlert(err.message || 'Unable to download CSV.', 'error');
}
}
// ==== TEAM CREATION ====
async function submitTeamUp() {
clearAlert();
const name = teamUpName.value.trim();
if (state.selectedParticipants.size < 2) return showAlert('Select at least two participants.', 'error');
if (!name) return showAlert('Enter a team name.', 'error');
const ids = [...state.selectedParticipants];
if (ids.length > MAX_TEAM_SELECTION) return showAlert(`Max ${MAX_TEAM_SELECTION} participants.`, 'error');
const leader = state.leaderParticipantId || ids[0];
try {
teamUpCreateButton.disabled = true;
teamUpCreateButton.textContent = 'Creating…';
const res = await fetch(api('/api/admin/team-up'), {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'x-admin-key': state.adminKey },
body: JSON.stringify({ participantIds: ids, teamName: name, leaderParticipantId: leader })
});
const data = await res.json();
if (!res.ok) throw new Error(data?.error || 'Unable to create team');
showAlert('Team created successfully.', 'success');
resetSelection();
await loadView('individuals');
await loadView('teams');
} catch (err) {
showAlert(err.message || 'Unable to create team.', 'error');
} finally {
teamUpCreateButton.disabled = false;
teamUpCreateButton.textContent = 'Create team from selection';
}
}
// ==== EVENT LISTENERS ====
authForm.addEventListener('submit', unlockDashboard);
clearKeyButton.addEventListener('click', () => {
localStorage.removeItem('kickstart-admin-key');
adminKeyInput.value = '';
state.adminKey = '';
setDashboardEnabled(false);
clearKeyButton.hidden = true;
rememberKeyCheckbox.checked = false;
adminKeyInfo.textContent = '';
showAlert('Stored key cleared.', 'info');
});
tabs.forEach(tab => tab.addEventListener('click', () => {
const view = tab.dataset.view;
if (view !== state.view) setView(view);
}));
refreshButton.addEventListener('click', () => loadView(state.view));
exportButton.addEventListener('click', exportCurrentView);
teamUpName.addEventListener('input', updateSelectionUI);
teamUpCreateButton.addEventListener('click', submitTeamUp);
// ==== INIT ====
loadAdminKey();
if (state.adminKey) {
setDashboardEnabled(true);
loadView(state.view);
}
})();