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 { UserInfoResponse } from '@/assets/types';
|
||||||
import styles from '@/styles/screens/tabs/perfil.styles';
|
import styles from '@/styles/screens/tabs/perfil.styles';
|
||||||
import { type Href, router } from 'expo-router';
|
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 { LoadingSpinner } from '@/assets/components/LoadingSpinner';
|
||||||
import {
|
import {
|
||||||
Alert,
|
Alert,
|
||||||
@@ -33,10 +33,16 @@ import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
|||||||
|
|
||||||
const ANIM_DURATION = 280;
|
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() {
|
export default function Perfil() {
|
||||||
const insets = useSafeAreaInsets();
|
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 [userInfo, setUserInfo] = useState<UserInfoResponse | null>(null);
|
||||||
|
const [isLoadingProfile, setIsLoadingProfile] = useState(true);
|
||||||
const [refreshing, setRefreshing] = useState(false);
|
const [refreshing, setRefreshing] = useState(false);
|
||||||
const [isEditingOpen, setIsEditingOpen] = useState(false);
|
const [isEditingOpen, setIsEditingOpen] = useState(false);
|
||||||
const [newPassword, setNewPassword] = useState('');
|
const [newPassword, setNewPassword] = useState('');
|
||||||
@@ -72,7 +78,12 @@ export default function Perfil() {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
const getUserInfo = async () => {
|
const getUserInfo = async () => {
|
||||||
if (!token) return;
|
if (!token) {
|
||||||
|
setUserInfo(null);
|
||||||
|
setIsLoadingProfile(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setIsLoadingProfile(true);
|
||||||
try {
|
try {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('token', token);
|
formData.append('token', token);
|
||||||
@@ -81,13 +92,22 @@ export default function Perfil() {
|
|||||||
body: formData,
|
body: formData,
|
||||||
});
|
});
|
||||||
const data: UserInfoResponse = await response.json();
|
const data: UserInfoResponse = await response.json();
|
||||||
console.log('data', data);
|
|
||||||
setUserInfo(data);
|
setUserInfo(data);
|
||||||
} catch {
|
} catch {
|
||||||
Alert.alert('Erro', 'Nao foi possivel carregar os dados do perfil.');
|
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(() => {
|
useEffect(() => {
|
||||||
getUserInfo();
|
getUserInfo();
|
||||||
}, [token]);
|
}, [token]);
|
||||||
@@ -210,8 +230,12 @@ export default function Perfil() {
|
|||||||
<Text style={styles.title}>O meu Perfil</Text>
|
<Text style={styles.title}>O meu Perfil</Text>
|
||||||
|
|
||||||
<View style={styles.userCard}>
|
<View style={styles.userCard}>
|
||||||
<Text style={styles.userName}>{userInfo?.user?.nome + ' ' + userInfo?.user?.apelido || 'Utilizador'}</Text>
|
{isLoadingProfile && !displayName ? (
|
||||||
<Text style={styles.userEmail}>{userInfo?.user?.email || 'Utilizador'}</Text>
|
<LoadingSpinner size="small" />
|
||||||
|
) : (
|
||||||
|
<Text style={styles.userName}>{displayName || 'Utilizador'}</Text>
|
||||||
|
)}
|
||||||
|
<Text style={styles.userEmail}>{displayEmail || '—'}</Text>
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
<View style={styles.menuCard}>
|
<View style={styles.menuCard}>
|
||||||
|
|||||||
Reference in New Issue
Block a user