Profile page fetches full data from API via GetByUserId
All checks were successful
Build frontend / build (push) Successful in 39s
All checks were successful
Build frontend / build (push) Successful in 39s
- Added getCustomerByUserId and getOwnerByUserId API functions - Profile page extracts user ID (SID) from JWT, calls appropriate endpoint - Falls back to JWT/localStorage if API call fails - Maps API fields (firstName, lastName, whatsAppNumber, phone, etc.) to form
This commit is contained in:
@ -28,6 +28,7 @@ import {
|
|||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import toast, { Toaster } from 'react-hot-toast';
|
import toast, { Toaster } from 'react-hot-toast';
|
||||||
import AuthService from '../services/AuthService';
|
import AuthService from '../services/AuthService';
|
||||||
|
import { getCustomerByUserId, getOwnerByUserId } from '../utils/api';
|
||||||
|
|
||||||
export default function ProfilePage() {
|
export default function ProfilePage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@ -66,39 +67,72 @@ export default function ProfilePage() {
|
|||||||
const authUser = AuthService.getUser();
|
const authUser = AuthService.getUser();
|
||||||
if (authUser) {
|
if (authUser) {
|
||||||
const userData = {
|
const userData = {
|
||||||
|
id: authUser.id,
|
||||||
name: authUser.name || '',
|
name: authUser.name || '',
|
||||||
email: authUser.email || '',
|
email: authUser.email || '',
|
||||||
phone: authUser.phone || '',
|
phone: authUser.phone || '',
|
||||||
role: AuthService.isOwner() ? 'owner' : 'customer',
|
role: AuthService.isOwner() ? 'owner' : 'customer',
|
||||||
};
|
};
|
||||||
setUser(userData);
|
setUser(userData);
|
||||||
|
console.log('[Profile] User from JWT:', userData);
|
||||||
const savedProfile = localStorage.getItem('userProfile');
|
|
||||||
let profileData;
|
// Fetch full profile from API using user ID (SID from JWT)
|
||||||
|
async function fetchProfile() {
|
||||||
if (savedProfile) {
|
try {
|
||||||
profileData = JSON.parse(savedProfile);
|
const fetchFn = userData.role === 'owner' ? getOwnerByUserId : getCustomerByUserId;
|
||||||
} else {
|
console.log('[Profile] Fetching profile via', userData.role === 'owner' ? 'Owner' : 'Customer', 'GetByUserId:', userData.id);
|
||||||
profileData = {
|
const profile = await fetchFn(userData.id);
|
||||||
name: userData.name || '',
|
console.log('[Profile] API profile:', profile);
|
||||||
email: userData.email || '',
|
|
||||||
phone: '',
|
if (profile) {
|
||||||
whatsapp: '',
|
const profileData = {
|
||||||
bio: '',
|
name: profile.fullName || profile.name || `${profile.firstName || ''} ${profile.lastName || ''}`.trim() || userData.name || '',
|
||||||
location: '',
|
email: profile.email || userData.email || '',
|
||||||
joinedDate: new Date().toLocaleDateString('ar-SA', { month: 'long', year: 'numeric' })
|
phone: profile.phone || profile.phoneNumber || userData.phone || '',
|
||||||
};
|
whatsapp: profile.whatsAppNumber || profile.whatsapp || '',
|
||||||
|
bio: profile.bio || '',
|
||||||
|
location: profile.address || profile.location || '',
|
||||||
|
joinedDate: profile.createdAt
|
||||||
|
? new Date(profile.createdAt).toLocaleDateString('ar-SA', { month: 'long', year: 'numeric' })
|
||||||
|
: new Date().toLocaleDateString('ar-SA', { month: 'long', year: 'numeric' }),
|
||||||
|
};
|
||||||
|
setFormData(profileData);
|
||||||
|
setTempValues(profileData);
|
||||||
|
localStorage.setItem('userProfile', JSON.stringify(profileData));
|
||||||
|
setIsLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('[Profile] API fetch failed, falling back to JWT/localStorage:', err);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback to JWT + localStorage
|
||||||
|
const savedProfile = localStorage.getItem('userProfile');
|
||||||
|
let profileData;
|
||||||
|
if (savedProfile) {
|
||||||
|
profileData = JSON.parse(savedProfile);
|
||||||
|
} else {
|
||||||
|
profileData = {
|
||||||
|
name: userData.name || '',
|
||||||
|
email: userData.email || '',
|
||||||
|
phone: '',
|
||||||
|
whatsapp: '',
|
||||||
|
bio: '',
|
||||||
|
location: '',
|
||||||
|
joinedDate: new Date().toLocaleDateString('ar-SA', { month: 'long', year: 'numeric' })
|
||||||
|
};
|
||||||
|
}
|
||||||
|
setFormData(profileData);
|
||||||
|
setTempValues(profileData);
|
||||||
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
setFormData(profileData);
|
|
||||||
setTempValues(profileData);
|
|
||||||
|
|
||||||
const savedAvatar = localStorage.getItem('userAvatar');
|
const savedAvatar = localStorage.getItem('userAvatar');
|
||||||
if (savedAvatar) {
|
if (savedAvatar) {
|
||||||
setAvatarPreview(savedAvatar);
|
setAvatarPreview(savedAvatar);
|
||||||
}
|
}
|
||||||
|
|
||||||
setIsLoading(false);
|
fetchProfile();
|
||||||
} else {
|
} else {
|
||||||
router.push('/login');
|
router.push('/login');
|
||||||
}
|
}
|
||||||
|
|||||||
@ -157,6 +157,18 @@ export async function getTerms() {
|
|||||||
return apiFetch('/Terms/GetTerms');
|
return apiFetch('/Terms/GetTerms');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── Profile ───
|
||||||
|
|
||||||
|
export async function getCustomerByUserId(userId) {
|
||||||
|
console.log('[API] Fetching customer by user ID:', userId);
|
||||||
|
return apiFetch(`/Customer/GetByUserId/${userId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getOwnerByUserId(userId) {
|
||||||
|
console.log('[API] Fetching owner by user ID:', userId);
|
||||||
|
return apiFetch(`/Owner/GetByUserId/${userId}`);
|
||||||
|
}
|
||||||
|
|
||||||
// ─── Auth: Registration ───
|
// ─── Auth: Registration ───
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user