diff --git a/app/(tabs)/perfil/index.tsx b/app/(tabs)/perfil/index.tsx index 937e6ee..3864a96 100644 --- a/app/(tabs)/perfil/index.tsx +++ b/app/(tabs)/perfil/index.tsx @@ -8,7 +8,7 @@ import { colors } from '@/assets/styles/colors'; import { UserInfoResponse } from '@/assets/types'; import styles from '@/styles/screens/tabs/perfil.styles'; import { type Href, router } from 'expo-router'; -import { useEffect, useState } from 'react'; +import { useEffect, useMemo, useState } from 'react'; import { LoadingSpinner } from '@/assets/components/LoadingSpinner'; import { Alert, @@ -33,10 +33,16 @@ import { useSafeAreaInsets } from 'react-native-safe-area-context'; const ANIM_DURATION = 280; +const formatDisplayName = (nome?: string, apelido?: string) => { + const parts = [nome?.trim(), apelido?.trim()].filter((p): p is string => !!p); + return parts.length > 0 ? parts.join(' ') : null; +}; + export default function Perfil() { const insets = useSafeAreaInsets(); - const { token, logout, updateProfile, isLoading } = useAuth(); + const { token, user: authUser, logout, updateProfile, isLoading } = useAuth(); const [userInfo, setUserInfo] = useState(null); + const [isLoadingProfile, setIsLoadingProfile] = useState(true); const [refreshing, setRefreshing] = useState(false); const [isEditingOpen, setIsEditingOpen] = useState(false); const [newPassword, setNewPassword] = useState(''); @@ -72,7 +78,12 @@ export default function Perfil() { })); const getUserInfo = async () => { - if (!token) return; + if (!token) { + setUserInfo(null); + setIsLoadingProfile(false); + return; + } + setIsLoadingProfile(true); try { const formData = new FormData(); formData.append('token', token); @@ -81,13 +92,22 @@ export default function Perfil() { body: formData, }); const data: UserInfoResponse = await response.json(); - console.log('data', data); setUserInfo(data); } catch { Alert.alert('Erro', 'Nao foi possivel carregar os dados do perfil.'); + } finally { + setIsLoadingProfile(false); } }; + const displayName = useMemo(() => { + const fromApi = formatDisplayName(userInfo?.user?.nome, userInfo?.user?.apelido); + if (fromApi) return fromApi; + return formatDisplayName(authUser?.nome, authUser?.apelido); + }, [userInfo, authUser]); + + const displayEmail = userInfo?.user?.email?.trim() || authUser?.email?.trim() || null; + useEffect(() => { getUserInfo(); }, [token]); @@ -210,8 +230,12 @@ export default function Perfil() { O meu Perfil - {userInfo?.user?.nome + ' ' + userInfo?.user?.apelido || 'Utilizador'} - {userInfo?.user?.email || 'Utilizador'} + {isLoadingProfile && !displayName ? ( + + ) : ( + {displayName || 'Utilizador'} + )} + {displayEmail || '—'}