From 1e5a9e72e6454716197fd7d94bcf04a18e9a68b3 Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Mon, 30 Mar 2026 14:49:00 +0530 Subject: [PATCH] feat: fetch and integrate detailed agent field values for CMS profile selection --- src/app/dashboard/cms/page.tsx | 63 +++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/src/app/dashboard/cms/page.tsx b/src/app/dashboard/cms/page.tsx index cc1afc4..5ecdbf5 100644 --- a/src/app/dashboard/cms/page.tsx +++ b/src/app/dashboard/cms/page.tsx @@ -3,6 +3,7 @@ import { useEffect, useState, useCallback, useRef } from 'react'; import { useRouter } from 'next/navigation'; import { + api, cmsService, usersService, uploadService, @@ -261,18 +262,42 @@ export default function CmsPage() { }, 300); } - function selectAgent(user: User, onChange: (items: ProfessionalItem[]) => void, currentItems: ProfessionalItem[]) { + async function selectAgent(user: User, onChange: (items: ProfessionalItem[]) => void, currentItems: ProfessionalItem[]) { if (currentItems.length >= 3) return; const agent = user.agentProfile; const profile = user.profile; const name = `${profile?.firstName || ''} ${profile?.lastName || ''}`.trim() || user.email; const subtitle = agent?.agentType?.name || agent?.headline || ''; - const location = [profile?.city, profile?.state].filter(Boolean).join(', '); - const experience = agent?.yearsOfExperience ? `${agent.yearsOfExperience}+ years in real estate.` : ''; const imageUrl = profile?.avatar || ''; - const isVerified = agent?.isVerified || false; - const newItem: ProfessionalItem = { name, subtitle, location, experience, expertise: [], imageUrl }; + // Fetch full agent profile with field values for location/experience/expertise + let location = [profile?.city, profile?.state].filter(Boolean).join(', '); + let experience = agent?.yearsOfExperience ? `${agent.yearsOfExperience}+ years in real estate.` : ''; + let expertise: string[] = []; + + if (agent?.id) { + try { + const res = await api.get(`/agents/${agent.id}/field-values`); + const fieldValues = res.data?.data?.fieldValues || res.data?.data || []; + for (const fv of fieldValues) { + const slug = fv.field?.slug || fv.fieldSlug || ''; + if (!location && (slug === 'city' || slug === 'state')) { + const vals = Array.isArray(fv.jsonValue) ? fv.jsonValue.join(', ') : (fv.textValue || ''); + if (vals) location = location ? `${location}, ${vals}` : vals; + } + if (!experience && (slug === 'years_in_business' || slug === 'experience')) { + experience = fv.textValue ? `${fv.textValue} years in real estate.` : ''; + } + if (slug === 'about_me_expertise' || slug === 'expertise_areas') { + if (Array.isArray(fv.jsonValue)) expertise = [...expertise, ...fv.jsonValue]; + } + } + } catch { + // Use basic profile data as fallback + } + } + + const newItem: ProfessionalItem = { name, subtitle, location, experience, expertise, imageUrl }; onChange([...currentItems, newItem]); setAgentSearchQuery(''); setAgentSearchResults([]); @@ -280,18 +305,38 @@ export default function CmsPage() { } // Select agent for Featured Agent Cards (different format) - function selectFeaturedAgent(user: User) { + async function selectFeaturedAgent(user: User) { const data = editContent as FeaturesContent; if ((data.featuredAgents || []).length >= 3) return; const agent = user.agentProfile; const profile = user.profile; const name = `${profile?.firstName || ''} ${profile?.lastName || ''}`.trim() || user.email; const role = agent?.agentType?.name || agent?.headline || ''; - const location = [profile?.city, profile?.state].filter(Boolean).join(', '); - const experience = agent?.yearsOfExperience ? `${agent.yearsOfExperience}+ years in real estate.` : ''; - const rating = ''; const imageUrl = profile?.avatar || ''; + let location = [profile?.city, profile?.state].filter(Boolean).join(', '); + let experience = agent?.yearsOfExperience ? `${agent.yearsOfExperience}+ years in real estate.` : ''; + const rating = ''; + + if (agent?.id) { + try { + const res = await api.get(`/agents/${agent.id}/field-values`); + const fieldValues = res.data?.data?.fieldValues || res.data?.data || []; + for (const fv of fieldValues) { + const slug = fv.field?.slug || fv.fieldSlug || ''; + if (!location && (slug === 'city' || slug === 'state')) { + const vals = Array.isArray(fv.jsonValue) ? fv.jsonValue.join(', ') : (fv.textValue || ''); + if (vals) location = location ? `${location}, ${vals}` : vals; + } + if (!experience && (slug === 'years_in_business' || slug === 'experience')) { + experience = fv.textValue ? `${fv.textValue} years in real estate.` : ''; + } + } + } catch { + // Use basic profile data as fallback + } + } + const newAgent: FeaturedAgentItem = { name, role, rating, location, experience, imageUrl }; setEditContent({ ...data, featuredAgents: [...(data.featuredAgents || []), newAgent] }); setAgentSearchQuery('');