158 lines
7.8 KiB
TypeScript
158 lines
7.8 KiB
TypeScript
'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<CategoryResponse[]>([]);
|
|
const [loading, setLoading] = useState<boolean>(true);
|
|
const [error, setError] = useState<boolean>(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 (
|
|
<div className="min-h-screen bg-[#F8F9FB] text-[#1E293B] flex flex-col font-sans">
|
|
<AnnouncementBar />
|
|
<Header />
|
|
<StickyHeaderSpacer />
|
|
<Navbar />
|
|
|
|
<main className="flex-grow py-8 pb-24 text-gray-800 antialiased">
|
|
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
|
{loading ? (
|
|
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-6">
|
|
{[1, 2, 3, 4, 5, 6, 7, 8].map((i) => (
|
|
<div key={i} className="flex flex-col items-center animate-pulse">
|
|
<div className="w-full aspect-square bg-gray-200 rounded-theme mb-3" />
|
|
<div className="h-4 bg-gray-200 rounded w-2/3 mb-1" />
|
|
<div className="h-3 bg-gray-200 rounded w-1/3" />
|
|
</div>
|
|
))}
|
|
</div>
|
|
) : error || categories.length === 0 ? (
|
|
<div className="bg-white border border-gray-250 rounded-2xl p-12 text-center max-w-lg mx-auto shadow-xs">
|
|
<Layers className="w-16 h-16 text-gray-300 mx-auto mb-4 stroke-1" />
|
|
<h2 className="text-lg font-bold text-gray-800">No Categories Available</h2>
|
|
<p className="text-xs text-gray-400 mt-1 mb-6">We could not retrieve catalog categories at this moment.</p>
|
|
<Link href="/" className="bg-[#1877f2] text-white text-xs font-bold px-6 py-3 rounded-lg hover:bg-[#1565c0] transition-colors inline-block">
|
|
Go to Homepage
|
|
</Link>
|
|
</div>
|
|
) : (
|
|
<div className="space-y-10">
|
|
{(() => {
|
|
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 (
|
|
<div key={parent.category_id || parent.slug} className="bg-white rounded-2xl p-6 sm:p-8 border border-gray-200/80 shadow-xs">
|
|
<div className="flex flex-wrap items-center justify-between pb-5 mb-6 border-b border-gray-100 gap-4">
|
|
<div className="flex items-center gap-3">
|
|
<div className="w-10 h-10 rounded-xl bg-primary/10 text-primary flex items-center justify-center font-bold">
|
|
<Layers className="w-5 h-5" />
|
|
</div>
|
|
<div>
|
|
<h2 className="text-lg font-bold text-gray-900">{parent.name}</h2>
|
|
<p className="text-xs text-gray-400 mt-0.5">
|
|
{subs.length > 0 ? `${subs.length} Sub-categories • ` : ''}
|
|
{parent.product_count !== undefined ? `${parent.product_count} Total Products` : 'Catalog Category'}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<Link
|
|
href={`/shop?category=${parent.slug}`}
|
|
className="text-xs font-bold text-primary hover:underline flex items-center gap-1 bg-primary/5 hover:bg-primary/10 px-3.5 py-1.5 rounded-lg transition-colors"
|
|
>
|
|
View All in {parent.name} →
|
|
</Link>
|
|
</div>
|
|
|
|
{subs.length > 0 ? (
|
|
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-4">
|
|
{subs.map((sub: any) => {
|
|
const subImage = getImageUrl(sub.image_url || sub.image || sub.icon_url);
|
|
return (
|
|
<motion.div
|
|
key={sub.category_id || sub.slug}
|
|
whileHover={{ y: -4 }}
|
|
transition={{ type: 'spring', stiffness: 300, damping: 25 }}
|
|
>
|
|
<Link
|
|
href={`/products?category=${sub.slug}`}
|
|
className="flex flex-col items-center group text-center p-4 rounded-xl border border-gray-100 hover:border-primary/40 hover:bg-primary/5 transition-all"
|
|
>
|
|
<div className="w-full aspect-square bg-[#F8F9FA] rounded-xl flex items-center justify-center p-4 mb-3 border border-gray-100 group-hover:bg-white transition-colors relative overflow-hidden">
|
|
{subImage ? (
|
|
<motion.img
|
|
whileHover={{ scale: 1.05 }}
|
|
src={subImage}
|
|
alt={sub.name}
|
|
className="max-h-full max-w-full object-contain"
|
|
/>
|
|
) : (
|
|
<Layers className="w-8 h-8 text-gray-300 group-hover:text-primary transition-colors stroke-[1.5]" />
|
|
)}
|
|
</div>
|
|
<h3 className="text-[13px] font-bold text-gray-800 group-hover:text-primary transition-colors line-clamp-1">
|
|
{sub.name}
|
|
</h3>
|
|
<span className="text-[11px] font-medium text-gray-400 mt-0.5">
|
|
{sub.product_count !== undefined ? `${sub.product_count} Products` : 'Browse'}
|
|
</span>
|
|
</Link>
|
|
</motion.div>
|
|
);
|
|
})}
|
|
</div>
|
|
) : (
|
|
<div className="py-4 text-center text-xs text-gray-400">
|
|
<p>No sub-categories defined. Direct catalog products are under this category.</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
});
|
|
})()}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</main>
|
|
|
|
<Footer />
|
|
</div>
|
|
);
|
|
}
|