import React, { useState } from 'react'; import { View, Text, StyleSheet, KeyboardAvoidingView, Platform, ScrollView, TouchableOpacity, TextInput, Image, Alert, } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { router, useLocalSearchParams } from 'expo-router'; import { Ionicons } from '@expo/vector-icons'; import * as ImagePicker from 'expo-image-picker'; import { Button } from '@/components/ui/Button'; import { ErrorMessage } from '@/components/ui/ErrorMessage'; import { AppColors, FontSizes, Spacing, BorderRadius, FontWeights } from '@/constants/theme'; import { api } from '@/services/api'; export default function AddLovedOneScreen() { const params = useLocalSearchParams<{ email: string; partnerCode: string }>(); const partnerCode = params.partnerCode || ''; const [name, setName] = useState(''); const [address, setAddress] = useState(''); const [avatarUri, setAvatarUri] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const handlePickAvatar = async () => { const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync(); if (status !== 'granted') { Alert.alert('Permission needed', 'Please allow access to your photo library.'); return; } const result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ['images'], allowsEditing: true, aspect: [1, 1], quality: 0.5, }); if (!result.canceled && result.assets[0]) { setAvatarUri(result.assets[0].uri); } }; const handleTakePhoto = async () => { const { status } = await ImagePicker.requestCameraPermissionsAsync(); if (status !== 'granted') { Alert.alert('Permission needed', 'Please allow access to your camera.'); return; } const result = await ImagePicker.launchCameraAsync({ allowsEditing: true, aspect: [1, 1], quality: 0.5, }); if (!result.canceled && result.assets[0]) { setAvatarUri(result.assets[0].uri); } }; const handleAvatarPress = () => { Alert.alert( 'Add Photo', 'Choose how to add a photo', [ { text: 'Take Photo', onPress: handleTakePhoto }, { text: 'Choose from Library', onPress: handlePickAvatar }, { text: 'Cancel', style: 'cancel' }, ] ); }; const handleContinue = async () => { setError(null); const trimmedName = name.trim(); if (!trimmedName) { setError('Please enter the name of your loved one'); return; } setIsLoading(true); try { // Navigate to the purchase/subscription screen with all data router.replace({ pathname: '/(auth)/purchase', params: { lovedOneName: trimmedName, lovedOneAddress: address.trim(), lovedOneAvatar: avatarUri || '', partnerCode, }, }); } catch (err) { setError(err instanceof Error ? err.message : 'Something went wrong'); } finally { setIsLoading(false); } }; const handleSkip = async () => { // Mark onboarding as completed so we don't redirect back here await api.setOnboardingCompleted(true); // Skip and go to main app without adding loved one router.replace('/(tabs)'); }; const nameInitial = name.trim() ? name.trim().charAt(0).toUpperCase() : '+'; return ( {/* Header */} Add a Loved One Tell us about the person you want to care for {/* Error Message */} {error && ( setError(null)} /> )} {/* Avatar Section */} {avatarUri ? ( ) : ( {nameInitial} )} Tap to add photo {/* Form */} Name * { setName(text); setError(null); }} placeholder="e.g., Grandma Julia" placeholderTextColor={AppColors.textMuted} autoCapitalize="words" autoCorrect={false} editable={!isLoading} /> Address (optional) {/* Continue Button */}