fix to name on profile page
This commit is contained in:
@@ -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<UserInfoResponse | null>(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() {
|
||||
<Text style={styles.title}>O meu Perfil</Text>
|
||||
|
||||
<View style={styles.userCard}>
|
||||
<Text style={styles.userName}>{userInfo?.user?.nome + ' ' + userInfo?.user?.apelido || 'Utilizador'}</Text>
|
||||
<Text style={styles.userEmail}>{userInfo?.user?.email || 'Utilizador'}</Text>
|
||||
{isLoadingProfile && !displayName ? (
|
||||
<LoadingSpinner size="small" />
|
||||
) : (
|
||||
<Text style={styles.userName}>{displayName || 'Utilizador'}</Text>
|
||||
)}
|
||||
<Text style={styles.userEmail}>{displayEmail || '—'}</Text>
|
||||
</View>
|
||||
|
||||
<View style={styles.menuCard}>
|
||||
|
||||
Reference in New Issue
Block a user