-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattendee.html
More file actions
517 lines (453 loc) · 25.7 KB
/
Copy pathattendee.html
File metadata and controls
517 lines (453 loc) · 25.7 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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<!doctype html>
<html lang="en">
<head>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-FMCYFTFPEL"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-FMCYFTFPEL');
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9715826188316382"
crossorigin="anonymous"></script>
<link rel="manifest" href="/manifest.json" />
<meta charset="utf-8">
<title>Queue | GFTV HelloQueue</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="GFTV Virtual Queue Management System">
<meta property="og:type" content="website">
<meta property="og:title" content="Queue | GFTV HelloQueue" />
<meta property="og:site_name" content="GFTV TIDSC" />
<meta property="og:description" content="GFTV Virtual Queue Management System" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Jua">
<link rel="stylesheet" href="/style.css">
<meta name="theme-color" content="#ccffcc" />
<link rel="apple-touch-icon" href="/GHQ-main.png">
<link rel="icon" href="/favicon.ico">
<link rel="shortcut icon" href="/favicon.ico">
<meta name="twitter:card" content="summary">
<meta name="twitter:image:src" content="/GHQ-main.png">
<meta property="og:image" content="/GHQ-main.png" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
<script defer="" src="https://use.fontawesome.com/releases/v6.5.2/js/all.js"></script>
</head>
<body>
<div id="topbar-root"></div>
<div class="attendee-container">
<div id="attendee-content">
<div style="display:flex;justify-content:center;padding:60px 0;">
<div class="loader"></div>
</div>
</div>
</div>
<!-- Telegram connect modal -->
<div class="modal-overlay" id="tg-connect-modal">
<div class="modal">
<div class="modal-header">
<span class="modal-title">Connect with Telegram</span>
</div>
<div style="padding:0 4px 8px;">
<p style="font-size:0.9rem;color:var(--text-muted);margin-bottom:16px;">
To join the queue, connect your Telegram account. You'll receive notifications when it's your turn.
</p>
<div id="tg-connect-step-generate">
<button class="btn btn-primary w-full" id="tg-generate-btn">Generate code</button>
</div>
<div id="tg-connect-step-code" style="display:none;">
<div id="tg-command-box" style="background:var(--surface-2,#f5f5f5);border-radius:var(--radius-sm);padding:16px;text-align:center;margin-bottom:16px;cursor:pointer;user-select:none;" title="Tap to copy">
<div style="font-size:0.8rem;color:var(--text-muted);margin-bottom:6px;">Tap to copy command:</div>
<div id="tg-command-display" style="font-family:monospace;font-size:1.1rem;font-weight:bold;letter-spacing:0.05em;"></div>
<div id="tg-copy-hint" style="font-size:0.75rem;color:var(--text-muted);margin-top:6px;">Copy</div>
</div>
<a id="tg-deeplink-btn" href="#" target="_blank" class="btn btn-primary w-full" style="margin-bottom:8px;">
Open Telegram Bot ↗
</a>
<div style="font-size:0.8rem;color:var(--text-muted);text-align:center;margin-bottom:12px;">
Tap the button above, then press <strong>Start</strong> in Telegram.
</div>
<div style="text-align:center;font-size:0.85rem;color:var(--text-muted);" id="tg-waiting-msg">
<span class="loader" style="width:16px;height:16px;display:inline-block;vertical-align:middle;margin-right:6px;"></span>
Waiting for confirmation…
</div>
<button class="btn btn-ghost w-full" id="tg-cancel-btn" style="margin-top:12px;">Cancel</button>
</div>
</div>
</div>
</div>
<div id="toast-container" class="toast-container"></div>
<script type="module">
import { initTheme, buildTopbar, getToken, api, toast, Icons } from '/script.js';
initTheme();
buildTopbar(document.getElementById('topbar-root'));
const parts = window.location.pathname.split('/').filter(Boolean);
const eventCode = parts[1];
const queueCode = parts[2];
if (!eventCode || !queueCode) {
document.getElementById('attendee-content').innerHTML = `
<div class="glass queue-card" style="text-align:center;padding:32px;">
<div style="margin:8px 0;">${Icons.meh.replace('width="18" height="18"', 'width="48" height="48"')}</div>
<div style="margin-top:10px;">Invalid queue link.</div>
</div>`;
} else {
initAttendee(eventCode, queueCode);
}
// Token helpers - stored separately so the queue page works without a dashboard login.
function getAttendeeToken() {
return localStorage.getItem('hq_attendee_token') || getToken();
}
function setAttendeeToken(token) {
localStorage.setItem('hq_attendee_token', token);
}
function clearAttendeeToken() {
localStorage.removeItem('hq_attendee_token');
}
async function queueApi(path, opts = {}) {
const token = getAttendeeToken();
const res = await fetch('/api' + path, {
method: opts.method || 'GET',
headers: {
'Content-Type': 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {}),
},
body: opts.body ? JSON.stringify(opts.body) : undefined,
});
let data;
try {
data = await res.json();
} catch {
throw new Error(`Server error (${res.status})`);
}
if (!res.ok) {
// Session invalidated from another browser - clear and re-prompt
if (res.status === 401) clearAttendeeToken();
throw new Error(data.error || 'Request failed');
}
return data;
}
// Init
let pollTimer = null;
let queueId = null;
let hasRendered = false;
async function initAttendee(eventCode, queueCode) {
let displayData = null;
try {
const res = await fetch(`/api/display/${eventCode}/${queueCode}`);
if (!res.ok) throw new Error();
displayData = await res.json();
queueId = displayData.queue?.id || null;
} catch {
document.getElementById('attendee-content').innerHTML = `
<div class="glass queue-card" style="text-align:center;padding:32px;">
<div style="margin:8px 0;">${Icons.x.replace('width="15" height="15"', 'width="48" height="48"')}</div>
<div style="margin-top:10px;">Queue not found.</div>
</div>`;
return;
}
await refreshStatus(eventCode, queueCode, displayData);
pollTimer = setInterval(() => refreshStatus(eventCode, queueCode, displayData, true), 5000);
}
async function refreshStatus(eventCode, queueCode, displayData, silent = false) {
const content = document.getElementById('attendee-content');
if (!content) return;
try {
const res = await fetch(`/api/display/${eventCode}/${queueCode}`);
if (res.ok) {
displayData = await res.json();
if (displayData.queue?.id) queueId = displayData.queue.id;
}
} catch { /* keep old displayData */ }
if (!getAttendeeToken()) {
renderConnectUI(displayData);
return;
}
if (!queueId) {
renderConnectUI(displayData);
return;
}
try {
const data = await queueApi(`/queues/${queueId}/status`);
hasRendered = true;
renderStatusUI(data, displayData, queueId);
} catch (err) {
if (err.message === 'Telegram account not linked') {
renderConnectUI(displayData);
return;
}
if (hasRendered) {
// Don't wipe the UI on a transient server error — just notify
toast(err.message, 'error');
} else if (!silent) {
content.innerHTML = `<div class="glass queue-card" style="text-align:center;padding:32px;">${err.message}</div>`;
}
}
}
// Status view
function renderStatusUI(data, display, qId) {
const content = document.getElementById('attendee-content');
if (!content) return;
const queue = data.queue || {};
const entry = data.entry;
const isServing = entry?.status === 'serving';
const isWaiting = entry?.status === 'waiting';
const isMissed = entry?.status === 'missed';
const hasEntered = isServing && !!entry?.entered_at;
let qrBlock = '';
if (isServing && data.qr_data_url) {
const qrLabel = hasEntered
? `${Icons.check} Entrance confirmed — show this QR code at the exit`
: 'Show this QR code at the entrance';
const qrHint = hasEntered
? 'Keep this page open until you are ready to leave'
: 'Single-use · Valid until scanned';
qrBlock = `
<div style="text-align:center;margin:20px 0;">
<div class="text-sm${hasEntered ? '' : ' text-muted'}" style="margin-bottom:12px;${hasEntered ? 'color:var(--status-serving);font-weight:600;' : ''}">${qrLabel}</div>
<div class="qr-wrapper">
<img src="${data.qr_data_url}" alt="Entry QR Code" />
</div>
${data.qr_token ? `
<div id="qr-token-copy" title="Tap to copy"
style="display:flex;align-items:center;gap:8px;margin-top:14px;padding:8px 14px;background:var(--surface-2);border:1px solid var(--border);border-radius:var(--radius-sm);cursor:pointer;user-select:none;font-family:monospace;font-size:0.82rem;letter-spacing:0.04em;color:var(--text);max-width:100%;box-sizing:border-box;">
<span id="qr-token-text" style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;min-width:0;flex:1;">${data.qr_token}</span>
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>
</div>` : ''}
<div class="text-sm text-muted" style="margin-top:8px;">${qrHint}</div>
</div>`;
}
let statusBanner = '';
if (isServing) {
statusBanner = hasEntered
? `<div style="background:var(--status-serving);color:#fff;padding:12px;border-radius:var(--radius-sm);text-align:center;margin-bottom:14px;font-size:1.05rem;">${Icons.check} You have entered! Show your QR code at the exit when you leave.</div>`
: `<div style="background:var(--status-serving);color:#fff;padding:12px;border-radius:var(--radius-sm);text-align:center;margin-bottom:14px;font-size:1.05rem;">${Icons.star} It's your turn! Proceed to the entrance.</div>`;
} else if (isMissed) {
statusBanner = `<div style="background:var(--status-missed);color:#fff;padding:12px;border-radius:var(--radius-sm);text-align:center;margin-bottom:14px;">${Icons.frown} You missed your turn. You can rejoin below.</div>`;
}
content.innerHTML = `
<div class="attendee-header">
<div style="font-size:1.4rem;">${queue.event_name || display?.event?.name || 'Event'}</div>
<div class="text-muted" style="font-size:0.9rem;">${queue.name || display?.queue?.name || 'Queue'}</div>
</div>
${statusBanner}
${entry ? `
<div class="glass queue-card">
<div class="your-number">
<div class="your-number-label">Your Queue Number</div>
<div class="your-number-badge queue-number-lg ${entry.status}">${entry.queue_number}</div>
</div>
${qrBlock}
<div class="info-row">
<span class="info-row-label">Status</span>
<span class="info-row-value">${entry.status}</span>
</div>
${isWaiting ? `
<div class="info-row">
<span class="info-row-label">Position</span>
<span class="info-row-value highlight">#${entry.position} in queue</span>
</div>` : ''}
<div class="info-row">
<span class="info-row-label">Waiting ahead</span>
<span class="info-row-value">${data.waiting_count || 0} people</span>
</div>
<div class="info-row">
<span class="info-row-label">Served so far</span>
<span class="info-row-value">${data.served_count || 0}</span>
</div>
</div>
<div class="glass queue-card" style="margin-top:12px;">
<div class="section-label">Notifications (via Telegram)</div>
<div class="notify-toggles">
<div class="notify-toggle-row">
<span>Alert when it's my turn</span>
<label class="toggle">
<input type="checkbox" id="toggle-serving" ${entry.notify_serving ? 'checked' : ''} />
<span class="toggle-slider"></span>
</label>
</div>
<div class="notify-toggle-row">
<span>Alert when I'm next in line</span>
<label class="toggle">
<input type="checkbox" id="toggle-next" ${entry.notify_next ? 'checked' : ''} />
<span class="toggle-slider"></span>
</label>
</div>
</div>
</div>
<div class="attendee-actions">
${isMissed ? `<button class="btn btn-primary w-full" id="rejoin-btn">Rejoin Queue</button>` : ''}
${!isServing ? `<button class="btn btn-danger w-full" id="leave-btn">Leave Queue</button>` : ''}
</div>
` : `
<div class="glass queue-card">
<div style="text-align:center;padding:10px 0 16px;">
<div style="margin-bottom:4px;">${Icons.ticket.replace('width="20" height="20"', 'width="48" height="48"')}</div>
<div style="margin:10px 0;font-size:1.05rem;">Join the queue</div>
<div class="text-sm text-muted">
${display?.total_in_queue || 0} people waiting · ~${display?.est_wait_minutes || 0} min
</div>
${queue.status !== 'open' ? `<div style="margin-top:10px;color:var(--status-missed);">This queue is currently closed.</div>` : ''}
</div>
<div class="info-row"><span class="info-row-label">Queue status</span><span class="info-row-value">${queue.status}</span></div>
<div class="info-row"><span class="info-row-label">People served</span><span class="info-row-value">${data.served_count || 0}</span></div>
<button class="btn btn-primary w-full" id="join-btn" style="margin-top:16px;" ${queue.status !== 'open' ? 'disabled' : ''}>
Join Queue
</button>
</div>
`}
`;
document.getElementById('qr-token-copy')?.addEventListener('click', async () => {
const token = document.getElementById('qr-token-text')?.textContent;
if (!token) return;
try {
await navigator.clipboard.writeText(token);
toast('Token copied!', 'success');
} catch {
toast('Could not copy — try long-pressing the text', 'info');
}
});
document.getElementById('toggle-serving')?.addEventListener('change', async (e) => {
await queueApi(`/queues/${qId}/notify-prefs`, { method: 'PUT', body: { notify_serving: e.target.checked } }).catch(() => {});
});
document.getElementById('toggle-next')?.addEventListener('change', async (e) => {
await queueApi(`/queues/${qId}/notify-prefs`, { method: 'PUT', body: { notify_next: e.target.checked } }).catch(() => {});
});
document.getElementById('leave-btn')?.addEventListener('click', async () => {
if (!confirm('Leave this queue?')) return;
try {
await queueApi(`/queues/${qId}/leave`, { method: 'POST' });
toast('Left queue', 'info');
await refreshStatus(eventCode, queueCode, display);
} catch (err) { toast(err.message, 'error'); }
});
document.getElementById('rejoin-btn')?.addEventListener('click', async () => {
try {
await queueApi(`/queues/${qId}/leave`, { method: 'POST' });
await queueApi(`/queues/${qId}/join`, { method: 'POST' });
toast('Rejoined queue!', 'success');
await refreshStatus(eventCode, queueCode, display);
} catch (err) { toast(err.message, 'error'); }
});
document.getElementById('join-btn')?.addEventListener('click', async () => {
const btn = document.getElementById('join-btn');
btn.disabled = true;
btn.textContent = 'Joining…';
try {
await queueApi(`/queues/${qId}/join`, { method: 'POST' });
toast('Joined queue!', 'success');
await refreshStatus(eventCode, queueCode, display);
} catch (err) {
toast(err.message, 'error');
btn.disabled = false;
btn.textContent = 'Join Queue';
}
});
}
// Connect UI
function renderConnectUI(display) {
const content = document.getElementById('attendee-content');
if (!content) return;
const queueStatus = display?.queue?.status || 'unknown';
content.innerHTML = `
<div class="attendee-header">
<div style="font-size:1.4rem;">${display?.event?.name || 'Event'}</div>
<div class="text-muted" style="font-size:0.9rem;">${display?.queue?.name || 'Queue'}</div>
</div>
<div class="glass queue-card">
<div class="info-row"><span class="info-row-label">Status</span><span class="info-row-value">${queueStatus}</span></div>
<div class="info-row"><span class="info-row-label">Now serving</span><span class="info-row-value highlight">${display?.serving?.[0] ?? '-'}</span></div>
<div class="info-row"><span class="info-row-label">Waiting</span><span class="info-row-value">${display?.total_in_queue || 0} people</span></div>
<div class="info-row"><span class="info-row-label">Est. wait</span><span class="info-row-value">~${display?.est_wait_minutes || 0} min</span></div>
</div>
<div class="glass queue-card" style="margin-top:12px;text-align:center;padding:24px;">
<div style="margin-bottom:10px;">${Icons.plane.replace('width="18" height="18"', 'width="40" height="40"')}</div>
<div style="margin-bottom:6px;font-size:1rem;font-weight:600;">Join via Telegram</div>
<div style="margin-bottom:16px;font-size:0.88rem;color:var(--text-muted);">
Connect your Telegram to join and get turn notifications.
</div>
<button class="btn btn-primary w-full" id="open-tg-connect-btn">Connect Telegram</button>
</div>
`;
document.getElementById('open-tg-connect-btn').addEventListener('click', openConnectModal);
}
// Connect modal
let pollCodeTimer = null;
function openConnectModal() {
const modal = document.getElementById('tg-connect-modal');
modal.classList.add('open');
showStep('generate');
}
function closeConnectModal() {
document.getElementById('tg-connect-modal').classList.remove('open');
if (pollCodeTimer) { clearInterval(pollCodeTimer); pollCodeTimer = null; }
}
function showStep(step) {
document.getElementById('tg-connect-step-generate').style.display = step === 'generate' ? '' : 'none';
document.getElementById('tg-connect-step-code').style.display = step === 'code' ? '' : 'none';
}
document.getElementById('tg-cancel-btn').addEventListener('click', closeConnectModal);
document.getElementById('tg-generate-btn').addEventListener('click', async () => {
const btn = document.getElementById('tg-generate-btn');
btn.disabled = true;
btn.textContent = 'Generating…';
try {
const res = await fetch('/api/auth/attendee-login', { method: 'POST' });
const data = await res.json();
if (!res.ok) throw new Error(data.error || 'Failed to generate code');
const { code, bot_username } = data;
const deepLink = `https://t.me/${bot_username}?start=attend_${code}`;
const command = `/attend ${code}`;
document.getElementById('tg-command-display').textContent = command;
document.getElementById('tg-deeplink-btn').href = deepLink;
showStep('code');
document.getElementById('tg-command-box').onclick = async () => {
try {
await navigator.clipboard.writeText(command);
document.getElementById('tg-copy-hint').innerHTML = `${Icons.check} Copied!`;
setTimeout(() => { document.getElementById('tg-copy-hint').textContent = 'Copy'; }, 2000);
} catch {
document.getElementById('tg-copy-hint').textContent = 'Copy manually';
}
};
// poll until confirmed
pollCodeTimer = setInterval(async () => {
try {
const pollRes = await fetch(`/api/auth/attendee-login?code=${code}`);
const pollData = await pollRes.json();
if (pollRes.status === 200 && pollData.token) {
clearInterval(pollCodeTimer);
pollCodeTimer = null;
setAttendeeToken(pollData.token);
closeConnectModal();
toast('Connected! Loading your queue…', 'success');
await refreshStatus(eventCode, queueCode, null);
} else if (pollRes.status === 410) {
clearInterval(pollCodeTimer);
pollCodeTimer = null;
showStep('generate');
toast('Code expired. Please try again.', 'error');
}
} catch { /* keep polling */ }
}, 2000);
} catch (err) {
toast(err.message, 'error');
} finally {
btn.disabled = false;
btn.textContent = 'Generate code';
}
});
document.getElementById('tg-connect-modal').addEventListener('click', (e) => {
if (e.target === e.currentTarget) closeConnectModal();
});
window.addEventListener('beforeunload', () => {
if (pollTimer) clearInterval(pollTimer);
if (pollCodeTimer) clearInterval(pollCodeTimer);
});
</script>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js');
}
</script>
</body>
</html>