11import React , { useState } from 'react' ;
22import { SafeAreaView , View , Text , StyleSheet , ScrollView , TouchableOpacity , Image , Switch } from 'react-native' ;
3- import { useUser } from '@/contexts/UserContext' ;
43import { useAuth } from '@/contexts/AuthContext' ;
54import { router } from 'expo-router' ;
65import { DevTools } from '@/components/DevTools' ;
76import { Dialog } from '@/components/ui/Dialog' ;
87import { Button } from '@/components/ui/Button' ;
98
109const ProfileScreen = ( ) => {
11- const { name, height, weight, age } = useUser ( ) ;
1210 const { user, logout, isLoading } = useAuth ( ) ;
1311 const [ notificationsEnabled , setNotificationsEnabled ] = useState ( false ) ;
1412 const [ showDevTools , setShowDevTools ] = useState ( false ) ;
1513 const [ showLogoutDialog , setShowLogoutDialog ] = useState ( false ) ;
1614
1715 const toggleNotifications = ( ) => setNotificationsEnabled ( prev => ! prev ) ;
1816
17+ // Calculate age from date of birth
18+ const calculateAge = ( dateOfBirth : string | undefined ) : number => {
19+ if ( ! dateOfBirth ) return 0 ;
20+ const today = new Date ( ) ;
21+ const birthDate = new Date ( dateOfBirth ) ;
22+ let age = today . getFullYear ( ) - birthDate . getFullYear ( ) ;
23+ const monthDiff = today . getMonth ( ) - birthDate . getMonth ( ) ;
24+ if ( monthDiff < 0 || ( monthDiff === 0 && today . getDate ( ) < birthDate . getDate ( ) ) ) {
25+ age -- ;
26+ }
27+ return age ;
28+ } ;
29+
30+ const age = calculateAge ( user ?. date_of_birth ) ;
31+
1932 const handleLogout = ( ) => {
2033 setShowLogoutDialog ( true ) ;
2134 } ;
@@ -49,7 +62,7 @@ const ProfileScreen = () => {
4962 < ScrollView contentContainerStyle = { styles . content } >
5063 { /* Profile Box */ }
5164 < View style = { styles . profileBox } >
52- < Text style = { styles . profileName } > { user ?. full_name || name || 'User' } </ Text >
65+ < Text style = { styles . profileName } > { user ?. full_name || user ?. email || 'User' } </ Text >
5366 < Text style = { styles . profileEmail } > { user ?. email || 'No email' } </ Text >
5467 < TouchableOpacity style = { styles . editBtn } >
5568 < Text style = { styles . editText } > Edit</ Text >
@@ -58,11 +71,11 @@ const ProfileScreen = () => {
5871 < View style = { styles . statsRow } >
5972 < View style = { styles . statBox } >
6073 < Text style = { styles . statLabel } > Height</ Text >
61- < Text style = { styles . statValue } > { height ? `${ height } cm` : '-' } </ Text >
74+ < Text style = { styles . statValue } > { user ?. height ? `${ user . height } cm` : '-' } </ Text >
6275 </ View >
6376 < View style = { styles . statBox } >
6477 < Text style = { styles . statLabel } > Weight</ Text >
65- < Text style = { styles . statValue } > { weight ? `${ weight } kg` : '-' } </ Text >
78+ < Text style = { styles . statValue } > { user ?. weight ? `${ user . weight } kg` : '-' } </ Text >
6679 </ View >
6780 < View style = { styles . statBox } >
6881 < Text style = { styles . statLabel } > Age</ Text >
0 commit comments