import React, { useEffect, useState, FormEvent } from 'react'; import { api, Volunteer } from '../api'; import { useAuth } from '../auth'; export default function Profile() { const { volunteerID } = useAuth(); const [volunteer, setVolunteer] = useState(null); const [name, setName] = useState(''); const [phone, setPhone] = useState(''); const [error, setError] = useState(''); const [success, setSuccess] = useState(''); const [saving, setSaving] = useState(false); useEffect(() => { if (!volunteerID) return; api.getVolunteer(volunteerID).then(v => { const vol = v as Volunteer; setVolunteer(vol); setName(vol.name); setPhone(vol.phone ?? ''); }).catch(() => setError('Could not load profile.')); }, [volunteerID]); async function handleSubmit(e: FormEvent) { e.preventDefault(); setError(''); setSuccess(''); if (!volunteerID) return; setSaving(true); try { const updated = await api.updateVolunteer(volunteerID, { name, phone: phone || undefined }); setVolunteer(updated); setSuccess('Profile updated.'); } catch (err: any) { setError(err.message); } finally { setSaving(false); } } if (!volunteer) return

Loading…

; return (

My Profile

{error &&

{error}

} {success &&

{success}

}

Completed shifts: {volunteer.completed_shifts} {volunteer.is_trainee && ' · Trainee'}

); }