Files
frontend/src/app/(agent)/agent/dashboard/page.tsx

313 lines
12 KiB
TypeScript
Raw Normal View History

2026-01-11 21:30:57 +05:30
'use client';
2026-01-24 22:19:30 +05:30
import { useState, useEffect } from 'react';
2026-01-11 21:30:57 +05:30
import { useSession } from 'next-auth/react';
2026-01-11 22:09:41 +05:30
import Image from 'next/image';
2026-01-12 12:34:58 +05:30
// Import shared components
2026-01-12 12:34:58 +05:30
import {
InfoCard,
ExperienceSection,
ProfileCard,
SpecializationSection,
TestimonialsSection,
StatusButtons,
ContactInfo,
2026-01-24 22:19:30 +05:30
PhotoUploadModal,
} from '@/components/profile';
2026-01-24 22:19:30 +05:30
import { agentsService, AgentProfile } from '@/services/agents.service';
import { uploadService } from '@/services/upload.service';
2026-01-11 22:09:41 +05:30
2026-01-24 22:19:30 +05:30
// Mock data for sections not yet available from API
const mockData = {
2026-01-11 22:09:41 +05:30
experience: {
years: '10+ years',
contracts: '10+ Contracts',
licensingAreas: ['Colorado Springs', 'Monument', 'Falcon', 'Castle Rock', 'Fountain', 'Momentum'],
expertiseYears: [
{ area: 'Colorado', years: '10 yrs' },
{ area: 'Falcon', years: '10 yrs' },
{ area: 'Colorado', years: '10 yrs' },
{ area: 'Colorado', years: '10 yrs' },
],
certifications: [
{ name: 'Certified Residential Specialist (CRS)', org: 'RESIDENTIAL REAL ESTATE COUNCIL' },
{ name: 'Certified Residential Specialist (CRS)', org: 'RESIDENTIAL REAL ESTATE COUNCIL' },
],
},
availability: {
type: 'Full-time',
days: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
},
preferredWorkEnvironment: 'Preferred work environment includes on-site property visits, in-depth market research, client consultations, property tours, and active involvement in negotiations to ensure the best outcomes',
testimonialHighlight: "The most amazing experience I've had as a real estate professional is helping a family secure their dream home and seeing their happiness when they received the keys.",
specialization: {
types: ['Buyers', 'Sellers', 'First time Buyers', 'First time Sellers', 'Divorce'],
loanTypes: ['Conventional', 'FHA', 'VA', 'USDA'],
propertyTypes: ['SFR', 'Town home', 'Rural', 'Luxury', 'Condo'],
hobbies: ['Boating', 'Horses', 'RV', 'Automotive', 'Aviation'],
pricePoints: ['Under $100K', '$100K $300K', '$300K $500K', '$500K $1M', '$1M and above'],
},
testimonials: [
{
id: 1,
2026-01-12 14:13:37 +05:30
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo ligula eget dolor. Sed dignissim, nisl eget tincidunt vulputate, lacus justo bibendum ipsum, vitae tempus risus lorem at nunc. Integer sed arcu vitae risus feugiat vehicula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
author: 'Kenedy Kenney',
2026-01-11 22:09:41 +05:30
role: 'Chief Operations Officer',
rating: 5,
},
{
id: 2,
2026-01-12 14:13:37 +05:30
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo ligula eget dolor. Sed dignissim, nisl eget tincidunt vulputate, lacus justo bibendum ipsum, vitae tempus risus lorem at nunc. Integer sed arcu vitae risus feugiat vehicula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
author: 'Kenedy Kenney',
role: 'Chief Operations Officer',
rating: 5,
},
{
id: 3,
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo ligula eget dolor. Sed dignissim, nisl eget tincidunt vulputate, lacus justo bibendum ipsum, vitae tempus risus lorem at nunc. Integer sed arcu vitae risus feugiat vehicula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
author: 'Kenedy Kenney',
2026-01-11 22:09:41 +05:30
role: 'Chief Operations Officer',
rating: 5,
},
],
};
2026-01-11 21:30:57 +05:30
export default function AgentDashboard() {
const { data: session } = useSession();
2026-01-24 22:19:30 +05:30
const [agentProfile, setAgentProfile] = useState<AgentProfile | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [isPhotoModalOpen, setIsPhotoModalOpen] = useState(false);
const [imageError, setImageError] = useState(false);
const defaultImage = '/assets/demo_images/8f017153a7b4a239a4f0691234ef97dd55092282.jpg';
// Fetch agent profile on mount
useEffect(() => {
const fetchProfile = async () => {
try {
setLoading(true);
setError(null);
const profile = await agentsService.getMyProfile();
setAgentProfile(profile);
} catch (err) {
console.error('Failed to fetch profile:', err);
setError('Failed to load profile data');
} finally {
setLoading(false);
}
};
fetchProfile();
}, []);
const handlePhotoUpload = async (file: File) => {
// Upload avatar to S3 (uses agent ID as filename for auto-replacement)
const uploadedFile = await uploadService.uploadAvatar(file);
// Update agent profile with the S3 URL
const updatedProfile = await agentsService.updateProfile({ avatar: uploadedFile.url });
setImageError(false); // Reset error state for new image
setAgentProfile(updatedProfile);
};
const getProfileImageUrl = () => {
// If image failed to load, return default
if (imageError) {
return defaultImage;
}
// Check for null, undefined, or empty string
if (!agentProfile?.avatar || agentProfile.avatar.trim() === '') {
return defaultImage;
}
// S3 URLs are full URLs starting with http
if (agentProfile.avatar.startsWith('http')) {
return agentProfile.avatar;
}
// For relative paths (local assets), return as-is
return agentProfile.avatar;
};
if (loading) {
return (
<div className="flex items-center justify-center h-[calc(100vh-180px)]">
<div className="flex flex-col items-center gap-4">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-[#E58625]"></div>
<p className="text-[14px] font-serif text-[#00293D]/70">Loading profile...</p>
</div>
</div>
);
}
if (error || !agentProfile) {
return (
<div className="flex items-center justify-center h-[calc(100vh-180px)]">
<div className="bg-white rounded-[20px] p-8 shadow-[0px_10px_20px_rgba(217,217,217,0.5)] max-w-md text-center">
<div className="w-16 h-16 bg-red-100 rounded-full flex items-center justify-center mx-auto mb-4">
<svg className="w-8 h-8 text-red-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
</div>
<h2 className="text-[18px] font-bold font-serif text-[#00293D] mb-2">Unable to Load Profile</h2>
<p className="text-[14px] font-serif text-[#00293D]/70 mb-4">{error || 'Profile not found'}</p>
<button
onClick={() => window.location.reload()}
className="px-6 py-2 bg-[#E58625] rounded-full text-[14px] font-semibold font-serif text-white hover:bg-[#E58625]/90 transition-colors"
>
Try Again
</button>
</div>
</div>
);
}
// Format member since date
const formatMemberSince = (dateString?: string) => {
if (!dateString) return 'Member';
try {
const date = new Date(dateString);
return date.toLocaleDateString('en-US', { month: 'long', year: 'numeric' });
} catch {
return 'Member';
}
};
2026-01-11 21:30:57 +05:30
return (
<div className="space-y-6">
{/* Main Layout - Responsive: Column on mobile, Row on desktop */}
<div className="flex flex-col lg:flex-row gap-6 lg:items-stretch">
2026-01-11 22:09:41 +05:30
{/* Left Sidebar - Status & Contact */}
<div className="w-full lg:w-[280px] flex-shrink-0 space-y-4 flex flex-col items-center lg:items-start">
2026-01-12 14:13:37 +05:30
{/* Profile Image */}
<div className="relative w-[200px] lg:w-[260px]">
<div className="w-[200px] h-[200px] lg:w-[260px] lg:h-[260px] rounded-[15px] overflow-hidden">
2026-01-24 22:19:30 +05:30
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={getProfileImageUrl()}
2026-01-12 14:13:37 +05:30
alt="Profile"
className="w-full h-full object-cover"
2026-01-24 22:19:30 +05:30
onError={(e) => {
const target = e.target as HTMLImageElement;
target.src = defaultImage;
}}
2026-01-12 14:13:37 +05:30
/>
{/* Gradient Overlay */}
<div className="absolute inset-0 bg-gradient-to-t from-black/50 via-black/20 to-transparent pointer-events-none" />
2026-01-12 14:13:37 +05:30
</div>
{/* Edit Icon - Top Right Outside */}
2026-01-24 22:19:30 +05:30
<button
onClick={() => setIsPhotoModalOpen(true)}
className="absolute -top-3 -right-3 w-9 h-9 bg-white rounded-[10px] shadow-md flex items-center justify-center hover:bg-gray-50 transition-colors"
>
2026-01-12 14:13:37 +05:30
<Image
src="/assets/icons/edit-icon.svg"
alt="Edit profile image"
2026-01-12 14:13:37 +05:30
width={20}
height={20}
/>
</button>
2026-01-12 14:13:37 +05:30
</div>
{/* Status Buttons */}
<StatusButtons />
2026-01-12 14:13:37 +05:30
{/* Contact Info */}
2026-01-24 22:19:30 +05:30
<ContactInfo
email={agentProfile.email || agentProfile.user?.email || ''}
phone={agentProfile.phone || ''}
/>
2026-01-12 14:13:37 +05:30
</div>
2026-01-11 21:30:57 +05:30
{/* Right Content - Profile Info + Experience + All Sections */}
<div className="flex-1 space-y-4">
{/* Profile Card */}
<ProfileCard
2026-01-24 22:19:30 +05:30
firstName={agentProfile.firstName}
lastName={agentProfile.lastName}
isVerified={agentProfile.isVerified}
title={agentProfile.agentType?.name || 'Real Estate Agent'}
location={agentProfile.serviceAreas?.[0] || 'United States'}
memberSince={formatMemberSince((agentProfile as unknown as { createdAt?: string }).createdAt)}
bio={agentProfile.bio || ''}
expertise={agentProfile.specializations || []}
showEditButton={true}
editHref="/agent/edit"
messageHref="/agent/message"
requestsHref="/agent/network"
/>
2026-01-11 21:30:57 +05:30
{/* Experience Section */}
2026-01-24 22:19:30 +05:30
<ExperienceSection experience={mockData.experience} />
{/* Info Cards Section */}
<div className="flex flex-col lg:grid lg:grid-cols-3 gap-6">
<InfoCard
title="Availability"
icon={
<Image
src="/assets/icons/availability-clock-icon.svg"
alt="Availability"
width={28}
height={31}
/>
}
content={
<div>
2026-01-24 22:19:30 +05:30
<p className="font-semibold font-serif text-[14px] leading-[19px] text-[#00293D] mb-2">{mockData.availability.type}</p>
<div className="space-y-1">
2026-01-24 22:19:30 +05:30
{mockData.availability.days.map((day) => (
<p key={day} className="font-normal font-serif text-[14px] leading-[19px] text-[#00293D]">{day}</p>
))}
</div>
</div>
}
/>
<InfoCard
title=""
icon={
<Image
src="/assets/icons/work-environment-icon.svg"
alt="Work environment"
width={28}
height={28}
/>
}
2026-01-24 22:19:30 +05:30
content={<p className="font-serif font-semibold text-[14px] leading-[19px] text-[#00293D]">{mockData.preferredWorkEnvironment}</p>}
/>
<InfoCard
title=""
icon={
<Image
src="/assets/icons/testimonial-star-icon.svg"
alt="Testimonial"
width={28}
height={28}
/>
}
2026-01-24 22:19:30 +05:30
content={<p className="text-[14px] font-semibold font-serif leading-[19px] text-center text-[#00293D]">&ldquo;{mockData.testimonialHighlight}&rdquo;</p>}
/>
2026-01-12 14:13:37 +05:30
</div>
{/* Specialization Section */}
2026-01-24 22:19:30 +05:30
<SpecializationSection specialization={mockData.specialization} />
{/* Testimonials Section */}
2026-01-24 22:19:30 +05:30
<TestimonialsSection testimonials={mockData.testimonials} />
2026-01-12 14:13:37 +05:30
</div>
</div>
2026-01-24 22:19:30 +05:30
{/* Photo Upload Modal */}
<PhotoUploadModal
isOpen={isPhotoModalOpen}
onClose={() => setIsPhotoModalOpen(false)}
onUpload={handlePhotoUpload}
currentPhoto={agentProfile.avatar}
/>
2026-01-11 21:30:57 +05:30
</div>
);
}