Files
Convivo/apps/convivo/src/screens/Scan.tsx
T
Lago 692df23149 Add Playwright E2E suite (46 tests) + app testids + test_cases.md
- e2e/: full Playwright suite covering every spec case (staff auth/UI,
  welcome + session check-in, attendee access/gating, connections,
  profile visibility, speaker material, sponsor, feedback, E2E journeys,
  role security matrix). 46/46 passing.
- apps/convivo: add data-testid attributes (gate, tabs, scanner, toggles)
  to support the E2E suite.
- test_cases.md: spec-case-to-test coverage map (all cases fully covered).
- .gitignore: ignore e2e node_modules/reports/test-results.
2026-09-09 10:13:26 +02:00

229 lines
9.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useMemo, useState } from 'react';
import { useAuth } from '../context/AuthContext';
import { useEvent } from '../lib/useEvent';
import { checkinAttendee, createConnectionRequest, lanyardLookup, welcomeCheckin } from '../lib/api';
import { capWords } from '../lib/time';
import type { CheckinResult, PublicContact, WelcomeCheckinResult } from '../lib/types';
import { Avatar, Scanner, Sheet, Spinner } from '../components/ui';
import { useToast } from '../context/ToastContext';
export function Scan() {
const { contact } = useAuth();
const { slots, loading } = useEvent();
const isStaff = !!contact?.is_admin;
const [mode, setMode] = useState<'attendee' | 'staff'>(isStaff ? 'staff' : 'attendee');
const [sessionId, setSessionId] = useState<string>('');
const [welcomeMode, setWelcomeMode] = useState(false);
const [connectTarget, setConnectTarget] = useState<{ contact: PublicContact; qr: string } | null>(null);
const [checkin, setCheckin] = useState<CheckinResult | null>(null);
const [welcome, setWelcome] = useState<WelcomeCheckinResult | null>(null);
const [busy, setBusy] = useState(false);
const { toast } = useToast();
// The first checkable (non-service) session, computed synchronously so a fast
// scan never races the async default-selection effect below.
const defaultSessionId = useMemo(
() => slots.flatMap((s) => s.sessions).find((s) => s.slot_type !== 'service_session')?.id ?? '',
[slots],
);
useEffect(() => {
if (defaultSessionId && !sessionId) setSessionId(defaultSessionId);
}, [defaultSessionId, sessionId]);
if (loading || !contact) {
return (
<section className="screen">
<div className="center" style={{ flex: 1 }}>
<Spinner />
</div>
</section>
);
}
const meEmail = contact.email;
const activeStaff = mode === 'staff' && isStaff;
async function handleScan(token: string) {
setBusy(true);
try {
if (activeStaff) {
if (welcomeMode) {
const res = await welcomeCheckin(token);
setWelcome(res);
} else {
// Fall back to the first checkable session so a fast scan (before the
// async default-selection effect runs) never fails with "pick a session".
const targetSession = sessionId || defaultSessionId;
if (!targetSession) {
toast('Pick a session first', 'err');
return;
}
const res = await checkinAttendee(targetSession, token);
setCheckin(res);
}
} else {
const found = await lanyardLookup(token);
if (!found) {
toast('No attendee found for that code', 'err');
return;
}
if (found.email === meEmail) {
toast('That’s your own lanyard 🙂', 'err');
return;
}
setConnectTarget({ contact: found, qr: token });
}
} catch (e) {
toast(e instanceof Error ? e.message : 'Something went wrong', 'err');
} finally {
setBusy(false);
}
}
async function confirmConnect() {
if (!connectTarget) return;
setBusy(true);
try {
const conn = await createConnectionRequest(connectTarget.qr);
if (conn.status === 'accepted') toast('You’re connected! 🤝', 'ok');
else if (conn.status === 'pending') toast('Connection request sent', 'ok');
else toast('Request already resolved');
window.dispatchEvent(new Event('connections-changed'));
setConnectTarget(null);
} catch (e) {
toast(e instanceof Error ? e.message : 'Could not send request', 'err');
} finally {
setBusy(false);
}
}
const selSession = slots.flatMap((s) => s.sessions).find((s) => s.id === sessionId);
return (
<section className="screen">
<div className="row between">
<div>
<div className="h1">{activeStaff ? 'Check-in' : 'Scan'}</div>
<div className="muted small">{activeStaff ? 'Scan attendees to check them in' : 'Scan a peer’s lanyard to connect'}</div>
</div>
{isStaff && (
<div className="seg" style={{ width: 170 }}>
<button className={mode === 'attendee' ? 'active' : ''} data-testid="scan-mode-attendee" onClick={() => setMode('attendee')}>Attendee</button>
<button className={mode === 'staff' ? 'active' : ''} data-testid="scan-mode-staff" onClick={() => setMode('staff')}>Staff</button>
</div>
)}
</div>
{activeStaff && (
<div>
<div className="row between" style={{ marginBottom: 10 }}>
<span className="muted small" style={{ fontWeight: 700 }}>Check-in type</span>
<label className="uiverse-toggle" data-testid="welcome-checkin-toggle" title={welcomeMode ? 'Welcome check-in' : 'Session check-in'}>
<input
type="checkbox"
checked={welcomeMode}
onChange={(e) => setWelcomeMode(e.target.checked)}
/>
</label>
</div>
{welcomeMode ? (
<div className="muted small" style={{ padding: '10px 12px', background: 'var(--surface)', border: '1px solid var(--line)', borderRadius: 12 }}>
🏷 <b>Welcome check-in</b> — marks the attendee present at the event.
</div>
) : (
<>
<div className="muted small" style={{ marginBottom: 7 }}>Checking in to session</div>
<select className="input" data-testid="session-selector" value={sessionId} onChange={(e) => setSessionId(e.target.value)}>
{slots
.filter(({ slot }) => slot.type !== 'service_session')
.map(({ slot, sessions }) => (
<optgroup key={slot.id} label={`${slot.name} · ${slot.start_time.slice(0, 5)}`}>
{sessions.map((s) => (
<option key={s.id} value={s.id}>{s.title}</option>
))}
</optgroup>
))}
</select>
</>
)}
</div>
)}
<Scanner onResult={handleScan} label={activeStaff ? 'Point at an attendee’s lanyard' : 'Point at a peer’s lanyard'} />
{busy && <div className="center"><Spinner /></div>}
{activeStaff && !welcomeMode && selSession && (
<div className="muted small" style={{ textAlign: 'center', opacity: 0.6 }}>
{selSession.room} · {selSession.speakers}
</div>
)}
{/* Connect sheet */}
<Sheet open={!!connectTarget} onClose={() => setConnectTarget(null)}>
{connectTarget && (
<div style={{ textAlign: 'center' }}>
<Avatar name={connectTarget.contact.full_name} id={connectTarget.contact.email} size={72} fs={26} />
<h3 style={{ marginTop: 12 }}>{connectTarget.contact.full_name}</h3>
{connectTarget.contact.registration_type && (
<span className={`pill reg-${connectTarget.contact.registration_type}`} style={{ marginTop: 8 }}>
{capWords(connectTarget.contact.registration_type)}
</span>
)}
{connectTarget.contact.linkedin_url && (
<div className="sub" style={{ marginTop: 6 }}>
<a href={connectTarget.contact.linkedin_url} target="_blank" rel="noreferrer">LinkedIn</a>
</div>
)}
<div style={{ marginTop: 16 }}>
<button className="btn primary" data-testid="connection-send" disabled={busy} onClick={confirmConnect}>
{busy ? 'Sending…' : 'Connect 🤝'}
</button>
<button className="btn ghost" style={{ marginTop: 8 }} onClick={() => setConnectTarget(null)}>
Not now
</button>
</div>
</div>
)}
</Sheet>
{/* Check-in result sheet */}
<Sheet open={!!checkin} onClose={() => setCheckin(null)}>
{checkin && (
<div style={{ textAlign: 'center' }} data-testid="session-checkin-result">
<div style={{ fontSize: 52 }}>{checkin.already ? '⚠️' : '✅'}</div>
<h3 style={{ marginTop: 10 }}>{checkin.attendee_name}</h3>
<div className="sub">{checkin.already ? 'Already checked in' : 'Checked in'}</div>
<div style={{ marginTop: 14 }}>
{checkin.already ? (
<span className="pill amber">No extra points</span>
) : (
<span className="pill green">+10 pts · now {checkin.points}</span>
)}
</div>
<button className="btn primary" style={{ marginTop: 18 }} onClick={() => setCheckin(null)}>
Scan next
</button>
</div>
)}
</Sheet>
{/* Welcome check-in result sheet */}
<Sheet open={!!welcome} onClose={() => setWelcome(null)}>
{welcome && (
<div style={{ textAlign: 'center' }} data-testid="welcome-checkin-result">
<div style={{ fontSize: 52 }}>{welcome.already ? '⚠️' : '👋'}</div>
<h3 style={{ marginTop: 10 }}>{welcome.attendee_name}</h3>
<div className="sub">{welcome.already ? 'Already checked in to the event' : 'Welcome to the event!'}</div>
<div style={{ marginTop: 14 }}>
<span className="pill green">✓ Checked in</span>
</div>
<button className="btn primary" style={{ marginTop: 18 }} onClick={() => setWelcome(null)}>
Scan next
</button>
</div>
)}
</Sheet>
</section>
);
}