import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useAuth } from '../context/AuthContext'; const API = '/api'; const calcBodyFat = (gender, waist, neck, hip, height) => { if (!waist || !neck || !height) return null; if (gender === 'female' && !hip) return null; if (gender === 'male') return Math.max(0, 495 / (1.0324 - 0.19077 * Math.log10(waist - neck) + 0.15456 * Math.log10(height)) - 450).toFixed(1); return Math.max(0, 495 / (1.29579 - 0.35004 * Math.log10(waist + hip - neck) + 0.22100 * Math.log10(height)) - 450).toFixed(1); }; export default function OnboardingWizard() { const [step, setStep] = useState(1); const [data, setData] = useState({ gender: '', age: '', height_cm: '', weight: '', neck_cm: '', waist_cm: '', hip_cm: '', experience_level: '', bench_1rm: '', squat_1rm: '', deadlift_1rm: '', goal: '', workouts_per_week: '' }); const [saving, setSaving] = useState(false); const { token, updateProfile, refreshProfile } = useAuth(); const navigate = useNavigate(); const update = (field, value) => setData(d => ({ ...d, [field]: value })); const bodyFat = calcBodyFat(data.gender, parseFloat(data.waist_cm), parseFloat(data.neck_cm), parseFloat(data.hip_cm), parseFloat(data.height_cm)); const finish = async () => { setSaving(true); try { // 1. Save profile await updateProfile({ gender: data.gender, age: data.age, height_cm: data.height_cm, experience_level: data.experience_level, goal: data.goal, workouts_per_week: data.workouts_per_week, onboarding_complete: true }); // 2. Save measurements if (data.weight || data.neck_cm || data.waist_cm) { await fetch(`${API}/user/measurements`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify({ weight: data.weight, neck_cm: data.neck_cm, waist_cm: data.waist_cm, hip_cm: data.hip_cm, body_fat_pct: bodyFat }) }); } // 3. Save strength if provided if (data.bench_1rm || data.squat_1rm || data.deadlift_1rm) { await fetch(`${API}/user/strength`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify({ bench_1rm: data.bench_1rm, squat_1rm: data.squat_1rm, deadlift_1rm: data.deadlift_1rm }) }); } if (refreshProfile) await refreshProfile(); navigate('/'); } catch (err) { console.error('Onboarding error:', err); setSaving(false); } }; return (
För att beräkna kroppsfett (US Navy-metoden)
1RM (valfritt)