From b6e9f01938dfda1aa1697e3556559b58ebea6f14 Mon Sep 17 00:00:00 2001 From: Claw AI Date: Sat, 28 Mar 2026 17:03:40 +0000 Subject: [PATCH] Profile page fetches full data from API via GetByUserId - 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 --- app/profile/page.js | 78 ++++++++++++++++++++++++++++++++------------- app/utils/api.js | 12 +++++++ 2 files changed, 68 insertions(+), 22 deletions(-) diff --git a/app/profile/page.js b/app/profile/page.js index e0f4038..62c5bab 100644 --- a/app/profile/page.js +++ b/app/profile/page.js @@ -28,6 +28,7 @@ import { } from 'lucide-react'; import toast, { Toaster } from 'react-hot-toast'; import AuthService from '../services/AuthService'; +import { getCustomerByUserId, getOwnerByUserId } from '../utils/api'; export default function ProfilePage() { const router = useRouter(); @@ -66,39 +67,72 @@ export default function ProfilePage() { const authUser = AuthService.getUser(); if (authUser) { const userData = { + id: authUser.id, name: authUser.name || '', email: authUser.email || '', phone: authUser.phone || '', role: AuthService.isOwner() ? 'owner' : 'customer', }; setUser(userData); - - 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' }) - }; + console.log('[Profile] User from JWT:', userData); + + // Fetch full profile from API using user ID (SID from JWT) + async function fetchProfile() { + try { + const fetchFn = userData.role === 'owner' ? getOwnerByUserId : getCustomerByUserId; + console.log('[Profile] Fetching profile via', userData.role === 'owner' ? 'Owner' : 'Customer', 'GetByUserId:', userData.id); + const profile = await fetchFn(userData.id); + console.log('[Profile] API profile:', profile); + + if (profile) { + const profileData = { + name: profile.fullName || profile.name || `${profile.firstName || ''} ${profile.lastName || ''}`.trim() || userData.name || '', + email: profile.email || userData.email || '', + 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'); if (savedAvatar) { setAvatarPreview(savedAvatar); } - - setIsLoading(false); + + fetchProfile(); } else { router.push('/login'); } diff --git a/app/utils/api.js b/app/utils/api.js index e6c3fbb..aeb018b 100644 --- a/app/utils/api.js +++ b/app/utils/api.js @@ -157,6 +157,18 @@ export async function 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 ─── /**