'use client'; import React, { useState, useEffect } from 'react'; import Link from 'next/link'; import { Layers } from 'lucide-react'; import { motion } from 'framer-motion'; import { AnnouncementBar } from '@/layout/AnnouncementBar'; import { Header } from '@/layout/Header'; import { Navbar } from '@/layout/Navbar'; import { Footer } from '@/layout/Footer'; import { StickyHeaderSpacer } from '@/components/StickyHeaderSpacer'; import { catalogService, CategoryResponse } from '@/services/api/catalogService'; import { getImageUrl } from '@/lib/utils'; export default function AllCategoriesPage() { const [categories, setCategories] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(false); useEffect(() => { setLoading(true); catalogService.getCategories() .then((res) => { setLoading(false); if (res && res.length > 0) { const sorted = [...res].sort((a, b) => { const orderA = parseInt(a.sort_order || '0', 10); const orderB = parseInt(b.sort_order || '0', 10); return orderA - orderB; }); setCategories(sorted); } else { setCategories([]); } }) .catch((err) => { console.error("Failed to load categories:", err); setLoading(false); setError(true); }); }, []); return (
{loading ? (
{[1, 2, 3, 4, 5, 6, 7, 8].map((i) => (
))}
) : error || categories.length === 0 ? (

No Categories Available

We could not retrieve catalog categories at this moment.

Go to Homepage
) : (
{(() => { const parents = categories.filter((c) => !c.parent_category_id); const displayParents = parents.length > 0 ? parents : categories; return displayParents.map((parent) => { const subs = categories.filter((c) => c.parent_category_id === parent.category_id); const parentImg = getImageUrl(parent.image_url || (parent as any).image || (parent as any).icon_url); return (

{parent.name}

{subs.length > 0 ? `${subs.length} Sub-categories • ` : ''} {parent.product_count !== undefined ? `${parent.product_count} Total Products` : 'Catalog Category'}

View All in {parent.name} →
{subs.length > 0 ? (
{subs.map((sub: any) => { const subImage = getImageUrl(sub.image_url || sub.image || sub.icon_url); return (
{subImage ? ( ) : ( )}

{sub.name}

{sub.product_count !== undefined ? `${sub.product_count} Products` : 'Browse'}
); })}
) : (

No sub-categories defined. Direct catalog products are under this category.

)}
); }); })()}
)}
); }