Co-authored-by: Stephan Charles <[email protected]> Co-authored-by: Amelia Hernandez <[email protected]> Co-authored-by: Markus Franz <[email protected]> Co-authored-by: Sabrina Hota <[email protected]> Co-authored-by: Nilüfer Doğan <[email protected]>
162 lines
5.9 KiB
TypeScript
162 lines
5.9 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { useAuth } from '../context/AuthContext';
|
|
import { getMyPointsLog, updateMyProfile } from '../lib/api';
|
|
import { capWords } from '../lib/time';
|
|
import type { PointsLogRow } from '../lib/types';
|
|
import { Avatar, QrImage, Spinner, Toggle } from '../components/ui';
|
|
import { useToast } from '../context/ToastContext';
|
|
|
|
const CATEGORY_LABEL: Record<PointsLogRow['category'], string> = {
|
|
checkin: 'Session check-ins',
|
|
connection: 'Connections',
|
|
feedback: 'Feedback',
|
|
talk_networking: 'Talk networking',
|
|
admin: 'Other',
|
|
};
|
|
|
|
const MANFRED_POSES = ['confident', 'coffee', 'celebrating', 'waving'];
|
|
|
|
export function Profile() {
|
|
const { contact, registration, signOut, refresh } = useAuth();
|
|
const { toast } = useToast();
|
|
const [manfredPose] = useState(() => MANFRED_POSES[Math.floor(Math.random() * MANFRED_POSES.length)]);
|
|
const [log, setLog] = useState<PointsLogRow[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
// Profile self-service (LinkedIn + visibility).
|
|
const [linkedin, setLinkedin] = useState(contact?.linkedin_url ?? '');
|
|
const [showEmail, setShowEmail] = useState(contact?.show_email ?? false);
|
|
const [showLinkedin, setShowLinkedin] = useState(contact?.show_linkedin ?? false);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
const email = contact?.email ?? '';
|
|
|
|
useEffect(() => {
|
|
let active = true;
|
|
(async () => {
|
|
try {
|
|
const data = await getMyPointsLog(email);
|
|
if (active) setLog(data);
|
|
} catch {
|
|
/* non-fatal */
|
|
} finally {
|
|
if (active) setLoading(false);
|
|
}
|
|
})();
|
|
return () => {
|
|
active = false;
|
|
};
|
|
}, [email]);
|
|
|
|
if (!contact) return null;
|
|
|
|
const points = registration?.points ?? 0;
|
|
const totals: Record<string, number> = {};
|
|
for (const l of log) totals[l.category] = (totals[l.category] ?? 0) + l.delta;
|
|
|
|
async function saveProfile() {
|
|
setSaving(true);
|
|
try {
|
|
await updateMyProfile({
|
|
linkedin_url: linkedin.trim() || null,
|
|
show_email: showEmail,
|
|
show_linkedin: showLinkedin,
|
|
});
|
|
await refresh();
|
|
toast('Profile updated', 'ok');
|
|
} catch (e) {
|
|
toast(e instanceof Error ? e.message : 'Could not save profile', 'err');
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<section className="screen">
|
|
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 16 }}>
|
|
<div className="h1" style={{ marginBottom: 0 }}>Profile</div>
|
|
<img
|
|
src={`/manfred/${manfredPose}.webp`}
|
|
alt="Manfred"
|
|
title="Manfred"
|
|
data-testid="profile-manfred"
|
|
width={76}
|
|
height={76}
|
|
style={{ objectFit: 'contain', flexShrink: 0 }}
|
|
/>
|
|
</div>
|
|
<div className="card" style={{ textAlign: 'center' }}>
|
|
<Avatar name={contact.full_name} id={contact.email} size={72} fs={26} />
|
|
<div className="h2" style={{ marginTop: 10 }}>{contact.full_name}</div>
|
|
<div className="muted small">{contact.email}</div>
|
|
<div className="row" style={{ justifyContent: 'center', marginTop: 10, gap: 8 }}>
|
|
{registration && <span className={`pill reg-${registration.type}`}>{capWords(registration.type)}</span>}
|
|
{contact.is_admin && registration?.type !== 'staff' && <span className="pill admin">staff</span>}
|
|
<span className="pill amber">⚡ {points} pts</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="card qr-wrap">
|
|
<div className="h2">My QR code</div>
|
|
{registration?.qr_token ? (
|
|
<QrImage value={registration.qr_token} size={180} />
|
|
) : (
|
|
<div className="muted small">No QR token assigned.</div>
|
|
)}
|
|
<div className="muted small" style={{ textAlign: 'center' }}>
|
|
Show this to <b>staff</b> for session check-in<br />and to <b>peers</b> to make a connection.
|
|
</div>
|
|
</div>
|
|
|
|
<div className="card">
|
|
<div className="h2" style={{ marginBottom: 6 }}>Profile settings</div>
|
|
<div className="muted small" style={{ marginBottom: 12 }}>
|
|
Choose what other attendees can see on your profile.
|
|
</div>
|
|
<div style={{ marginBottom: 12 }}>
|
|
<div className="muted small" style={{ marginBottom: 4 }}>LinkedIn</div>
|
|
<input
|
|
className="input"
|
|
type="url"
|
|
data-testid="profile-linkedin-url"
|
|
placeholder="https://linkedin.com/in/you"
|
|
value={linkedin}
|
|
onChange={(e) => setLinkedin(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div style={{ display: 'flex', flexDirection: 'column', gap: 10, marginBottom: 14 }}>
|
|
<Toggle checked={showEmail} onChange={setShowEmail} label="Show my email" testId="profile-show-email" />
|
|
<Toggle checked={showLinkedin} onChange={setShowLinkedin} label="Show my LinkedIn" testId="profile-show-linkedin" />
|
|
</div>
|
|
<button className="btn primary" data-testid="profile-save" disabled={saving} onClick={() => void saveProfile()}>
|
|
{saving ? 'Saving…' : 'Save profile'}
|
|
</button>
|
|
</div>
|
|
|
|
<div className="card">
|
|
<div className="h2" style={{ marginBottom: 6 }}>Points breakdown</div>
|
|
{loading ? (
|
|
<div className="center" style={{ padding: 12 }}>
|
|
<Spinner />
|
|
</div>
|
|
) : (
|
|
<>
|
|
{Object.entries(totals).map(([cat, val]) => (
|
|
<div className="break-row" key={cat}>
|
|
<span>{CATEGORY_LABEL[cat as PointsLogRow['category']] ?? cat}</span>
|
|
<span className="v">+{val}</span>
|
|
</div>
|
|
))}
|
|
<div className="break-row">
|
|
<span style={{ fontWeight: 700 }}>Total</span>
|
|
<span className="v">{points}</span>
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
|
|
<button className="btn danger" data-testid="profile-logout" onClick={() => void signOut()}>Log out</button>
|
|
</section>
|
|
);
|
|
}
|