'use client'; import { useState } from 'react'; import { Box, Stepper, Step, StepLabel, Button, Typography, Paper, TextField, Avatar, IconButton, Alert, CircularProgress, MenuItem, Card, CardActionArea, CardContent, Radio, RadioGroup, FormControlLabel, FormControl, Grid, } from '@mui/material'; import { ArrowBack, ArrowForward, Check, Language, Straighten } from '@mui/icons-material'; import { motion, AnimatePresence } from 'framer-motion'; import { useRouter } from 'next/navigation'; import { useAuth } from '@/lib/auth/AuthContext'; import { childrenApi } from '@/lib/api/children'; import { useLocale, MeasurementSystem } from '@/hooks/useLocale'; import { useTranslation } from '@/hooks/useTranslation'; import { supportedLanguages } from '@/lib/i18n/config'; import { usersApi } from '@/lib/api/users'; const steps = ['Welcome', 'Language', 'Measurements', 'Add Child', 'Complete']; export default function OnboardingPage() { const [activeStep, setActiveStep] = useState(0); const [selectedLanguage, setSelectedLanguage] = useState('en'); const [selectedMeasurement, setSelectedMeasurement] = useState('metric'); const [childName, setChildName] = useState(''); const [childBirthDate, setChildBirthDate] = useState(''); const [childGender, setChildGender] = useState<'male' | 'female' | 'other'>('other'); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const router = useRouter(); const { user, refreshUser } = useAuth(); const { setLanguage, setMeasurementSystem } = useLocale(); const { t } = useTranslation('onboarding'); const handleNext = async () => { setError(''); // Step 1: Save language preference if (activeStep === 1) { try { setLoading(true); await setLanguage(selectedLanguage); // Save to backend if (user?.id) { await usersApi.updatePreferences({ language: selectedLanguage, }); } setActiveStep((prevActiveStep) => prevActiveStep + 1); } catch (err: any) { console.error('Failed to save language:', err); // Language was saved locally even if backend failed, so continue setActiveStep((prevActiveStep) => prevActiveStep + 1); } finally { setLoading(false); } return; } // Step 2: Save measurement preference if (activeStep === 2) { try { setLoading(true); setMeasurementSystem(selectedMeasurement); // Save to backend if (user?.id) { await usersApi.updatePreferences({ measurementUnit: selectedMeasurement, }); await refreshUser(); } setActiveStep((prevActiveStep) => prevActiveStep + 1); } catch (err: any) { console.error('Failed to save measurement:', err); // Measurement was saved locally even if backend failed, so continue setActiveStep((prevActiveStep) => prevActiveStep + 1); } finally { setLoading(false); } return; } // Step 3: Validate and save child data (Add Child) if (activeStep === 3) { if (!childName.trim() || !childBirthDate) { setError(t('child.name') + ' and ' + t('child.dateOfBirth') + ' are required'); return; } const familyId = user?.families?.[0]?.familyId; if (!familyId) { console.error('No family found. User object:', JSON.stringify(user, null, 2)); setError('Unable to create child profile. Your account setup is incomplete. Please contact support or try logging out and back in.'); return; } try { setLoading(true); setError(''); await childrenApi.createChild(familyId, { name: childName.trim(), birthDate: childBirthDate, gender: childGender, }); setActiveStep((prevActiveStep) => prevActiveStep + 1); } catch (err: any) { console.error('Failed to create child:', err); setError(err.response?.data?.message || 'Failed to save child. Please try again.'); } finally { setLoading(false); } return; } if (activeStep === steps.length - 1) { // Complete onboarding router.push('/'); } else { setActiveStep((prevActiveStep) => prevActiveStep + 1); } }; const handleBack = () => { setActiveStep((prevActiveStep) => prevActiveStep - 1); }; const handleSkip = () => { router.push('/'); }; return ( {steps.map((label, index) => { let stepLabel = label; if (index === 0) stepLabel = t('welcome.title').split('!')[0]; else if (index === 1) stepLabel = t('language.title'); else if (index === 2) stepLabel = t('measurements.title'); else if (index === 3) stepLabel = t('child.title'); else if (index === 4) stepLabel = t('complete.title').split('!')[0]; return ( {stepLabel} ); })} {/* Step 0: Welcome */} {activeStep === 0 && ( {t('welcome.title')} 🎉 {t('welcome.description')} 📊 {t('welcome.getStarted')} 🤖 AI Insights 👨‍👩‍👧 Family Sharing )} {/* Step 1: Language Selection */} {activeStep === 1 && ( {t('language.title')} {t('language.subtitle')} {error && ( {error} )} {supportedLanguages.map((lang) => ( setSelectedLanguage(lang.code)} sx={{ p: 2 }} > {lang.nativeName} {lang.name} ))} {t('language.description')} )} {/* Step 2: Measurement System */} {activeStep === 2 && ( {t('measurements.title')} {t('measurements.subtitle')} {error && ( {error} )} setSelectedMeasurement('metric')} sx={{ p: 3, height: '100%' }} > {t('measurements.metric.title')} {t('measurements.metric.description')} setSelectedMeasurement('imperial')} sx={{ p: 3, height: '100%' }} > {t('measurements.imperial.title')} {t('measurements.imperial.description')} {t('measurements.description')} )} {/* Step 3: Add Child */} {activeStep === 3 && ( {t('child.title')} {t('child.subtitle')} {error && ( {error} )} setChildName(e.target.value)} margin="normal" required disabled={loading} InputProps={{ sx: { borderRadius: 3 }, }} /> setChildBirthDate(e.target.value)} margin="normal" required disabled={loading} InputLabelProps={{ shrink: true, }} InputProps={{ sx: { borderRadius: 3 }, }} /> setChildGender(e.target.value as 'male' | 'female' | 'other')} margin="normal" disabled={loading} InputProps={{ sx: { borderRadius: 3 }, }} > {t('child.genders.male')} {t('child.genders.female')} {t('child.genders.preferNotToSay')} {t('child.skipForNow')} )} {/* Step 4: Complete */} {activeStep === 4 && ( {t('complete.title')} 🎉 {t('complete.description')} {t('complete.subtitle')} • Track your first feeding, sleep, or diaper change
• Chat with our AI assistant for parenting tips
• Explore insights and predictions based on your data
)}
{activeStep === 3 && ( )}
); }