'use client' import { useEffect, useState, useMemo } from 'react' import PageLayout from '../components/PageLayout' import Waves from '../components/waves' type Affiliate = { id: string name: string description: string url: string logoUrl?: string category: string commissionRate?: string } // Fallback placeholder image const PLACEHOLDER_IMAGE = 'https://images.unsplash.com/photo-1557804506-669a67965ba0?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3270&q=80' export default function AffiliateLinksPage() { const [affiliates, setAffiliates] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState('') // NEW: selected category const [selectedCategory, setSelectedCategory] = useState('all') useEffect(() => { async function fetchAffiliates() { try { const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:3001' const res = await fetch(`${BASE_URL}/api/affiliates/active`) if (!res.ok) { throw new Error('Failed to fetch affiliates') } const data = await res.json() const activeAffiliates = data.data || [] setAffiliates(activeAffiliates.map((item: any) => ({ id: String(item.id), name: String(item.name || 'Partner'), description: String(item.description || ''), url: String(item.url || '#'), logoUrl: item.logoUrl || PLACEHOLDER_IMAGE, category: String(item.category || 'Other'), commissionRate: item.commission_rate ? String(item.commission_rate) : undefined }))) } catch (err) { console.error('Error loading affiliates:', err) setError('Failed to load affiliate partners') } finally { setLoading(false) } } fetchAffiliates() }, []) const posts = affiliates.map(affiliate => ({ id: affiliate.id, title: affiliate.name, href: affiliate.url, description: affiliate.description, imageUrl: affiliate.logoUrl || PLACEHOLDER_IMAGE, category: { title: affiliate.category, href: '#' }, commissionRate: affiliate.commissionRate })) // NEW: fixed categories from the provided image, merged with backend ones const categories = useMemo(() => { const fromImage = [ 'Technology', 'Energy', 'Finance', 'Healthcare', 'Education', 'Travel', 'Retail', 'Construction', 'Food', 'Automotive', 'Fashion', 'Pets', ] const set = new Set(fromImage) affiliates.forEach(a => { if (a.category) set.add(a.category) }) return ['all', ...Array.from(set)] }, [affiliates]) return (
{/* Header (aligned with management pages) */}

Affiliate Partners

Discover our trusted partners and earn commissions through affiliate links.

{/* NEW: Category filter */}
{/* States */} {loading && (

Loading affiliate partners...

)} {error && !loading && (
{error}
)} {!loading && !error && posts.length === 0 && (
No affiliate partners available at the moment.
)} {/* Cards (aligned to white panels, border, shadow) */} {!loading && !error && posts.length > 0 && (
{posts.map((post) => { // NEW: highlight when matches selected category (keep all visible) const isHighlighted = selectedCategory !== 'all' && post.category.title === selectedCategory return ( ) })}
)}
) }