feat(categories): parent-only homepage carousel with sub-category drilldown modal and grouped categories layout
This commit is contained in:
parent
3f3c5e3647
commit
bad9a52b96
2 changed files with 182 additions and 57 deletions
|
|
@ -69,45 +69,85 @@ export default function AllCategoriesPage() {
|
|||
</Link>
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-6">
|
||||
{categories.map((cat: any) => {
|
||||
const catImage = getImageUrl(cat.image_url || cat.image || cat.icon_url);
|
||||
<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={cat.category_id || cat.slug}
|
||||
whileHover={{ y: -6 }}
|
||||
key={sub.category_id || sub.slug}
|
||||
whileHover={{ y: -4 }}
|
||||
transition={{ type: 'spring', stiffness: 300, damping: 25 }}
|
||||
>
|
||||
<Link
|
||||
href={`/products?category=${cat.slug}`}
|
||||
className="flex flex-col items-center group text-center"
|
||||
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"
|
||||
>
|
||||
{/* Image Box */}
|
||||
<div className="w-full aspect-square bg-white rounded-theme flex items-center justify-center p-6 border border-gray-200 group-hover:border-blue-200 group-hover:bg-blue-50/10 transition-all duration-300 relative overflow-hidden shadow-xs">
|
||||
{catImage ? (
|
||||
<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.06 }}
|
||||
src={catImage}
|
||||
alt={cat.name}
|
||||
className="max-h-full max-w-full object-contain rounded-lg drop-shadow-md"
|
||||
whileHover={{ scale: 1.05 }}
|
||||
src={subImage}
|
||||
alt={sub.name}
|
||||
className="max-h-full max-w-full object-contain"
|
||||
/>
|
||||
) : (
|
||||
<Layers className="w-12 h-12 text-gray-400 group-hover:text-primary transition-colors stroke-[1.5]" />
|
||||
<Layers className="w-8 h-8 text-gray-300 group-hover:text-primary transition-colors stroke-[1.5]" />
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Title & Count */}
|
||||
<h3 className="text-[13px] font-bold text-gray-800 mt-3.5 group-hover:text-primary transition-colors line-clamp-1">
|
||||
{cat.name}
|
||||
<h3 className="text-[13px] font-bold text-gray-800 group-hover:text-primary transition-colors line-clamp-1">
|
||||
{sub.name}
|
||||
</h3>
|
||||
<span className="text-[10px] font-bold text-gray-400 mt-0.5 tracking-wide">
|
||||
{cat.product_count !== undefined ? `${cat.product_count} Products` : 'Catalog Item'}
|
||||
<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>
|
||||
|
|
|
|||
|
|
@ -80,11 +80,15 @@ export const CategoriesSection: React.FC<CategoriesSectionProps> = ({
|
|||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState(false);
|
||||
const [canSlide, setCanSlide] = useState(false);
|
||||
const [productThumbs, setProductThumbs] = useState<Record<string, string>>(
|
||||
{}
|
||||
);
|
||||
const [productThumbs, setProductThumbs] = useState<Record<string, string>>({});
|
||||
const [activeParentCat, setActiveParentCat] = useState<any | null>(null);
|
||||
const swiperRef = useRef<SwiperType | null>(null);
|
||||
|
||||
const rootCategories = React.useMemo(() => {
|
||||
const roots = categories.filter((c) => !c.parent_category_id);
|
||||
return roots.length > 0 ? roots : categories;
|
||||
}, [categories]);
|
||||
|
||||
useEffect(() => {
|
||||
const sortCats = (cats: any[]) =>
|
||||
[...cats].sort((a, b) => {
|
||||
|
|
@ -285,7 +289,7 @@ export const CategoriesSection: React.FC<CategoriesSectionProps> = ({
|
|||
1280: { slidesPerView: 6, spaceBetween: 24 },
|
||||
}}
|
||||
>
|
||||
{categories.map((cat) => {
|
||||
{rootCategories.map((cat) => {
|
||||
const rawCatImg = cat.image_url || cat.image || cat.icon_url;
|
||||
const primarySrc = rawCatImg ? getImageUrl(rawCatImg) : '';
|
||||
const fallbackSrc =
|
||||
|
|
@ -296,19 +300,40 @@ export const CategoriesSection: React.FC<CategoriesSectionProps> = ({
|
|||
cat.product_count !== undefined && cat.product_count !== null
|
||||
? Number(cat.product_count)
|
||||
: null;
|
||||
const hasSubs = cat.sub_categories && cat.sub_categories.length > 0;
|
||||
|
||||
return (
|
||||
<SwiperSlide key={cat.category_id || cat.id || cat.slug}>
|
||||
<div
|
||||
onClick={(e) => {
|
||||
if (hasSubs) {
|
||||
e.preventDefault();
|
||||
setActiveParentCat(cat);
|
||||
}
|
||||
}}
|
||||
className="cursor-pointer"
|
||||
>
|
||||
<Link
|
||||
href={cat.link || `/shop?category=${cat.slug}`}
|
||||
href={hasSubs ? '#' : (cat.link || `/shop?category=${cat.slug}`)}
|
||||
onClick={(e) => {
|
||||
if (hasSubs) {
|
||||
e.preventDefault();
|
||||
setActiveParentCat(cat);
|
||||
}
|
||||
}}
|
||||
className="flex flex-col items-center text-center group"
|
||||
>
|
||||
<div className="w-full aspect-square bg-[#F5F5F5] rounded-xl flex items-center justify-center p-5 md:p-6 overflow-hidden transition-colors duration-200 group-hover:bg-[#ececec]">
|
||||
<div className="w-full aspect-square bg-[#F5F5F5] rounded-xl flex items-center justify-center p-5 md:p-6 overflow-hidden transition-colors duration-200 group-hover:bg-[#ececec] relative">
|
||||
<CategoryThumb
|
||||
name={cat.name}
|
||||
primarySrc={primarySrc}
|
||||
fallbackSrc={fallbackSrc}
|
||||
/>
|
||||
{hasSubs && (
|
||||
<span className="absolute bottom-2 right-2 bg-white/90 text-primary text-[10px] font-bold px-1.5 py-0.5 rounded shadow-xs">
|
||||
{cat.sub_categories.length} sub
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<h3 className="text-[14px] font-bold text-[#333] mt-3 leading-snug group-hover:text-primary transition-colors">
|
||||
{cat.name}
|
||||
|
|
@ -317,12 +342,72 @@ export const CategoriesSection: React.FC<CategoriesSectionProps> = ({
|
|||
{count !== null ? `${count} Products` : 'Shop now'}
|
||||
</span>
|
||||
</Link>
|
||||
</div>
|
||||
</SwiperSlide>
|
||||
);
|
||||
})}
|
||||
</Swiper>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Sub-Category Drilldown Modal */}
|
||||
{activeParentCat && (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/60 backdrop-blur-xs animate-in fade-in duration-200">
|
||||
<div className="bg-white rounded-2xl max-w-md w-full p-6 shadow-2xl relative border border-gray-100 max-h-[85vh] flex flex-col">
|
||||
<div className="flex items-center justify-between pb-4 border-b border-gray-100">
|
||||
<div>
|
||||
<h3 className="text-base font-bold text-gray-900">{activeParentCat.name}</h3>
|
||||
<p className="text-xs text-gray-500 mt-0.5">
|
||||
Select a sub-category to view products ({activeParentCat.product_count || 0} total items)
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setActiveParentCat(null)}
|
||||
className="w-8 h-8 rounded-full bg-gray-100 hover:bg-gray-200 flex items-center justify-center text-gray-500 hover:text-gray-800 transition-colors cursor-pointer text-sm font-bold"
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="py-4 space-y-2 overflow-y-auto flex-1">
|
||||
{(activeParentCat.sub_categories || []).map((sub: any) => (
|
||||
<Link
|
||||
key={sub.category_id || sub.slug}
|
||||
href={`/products?category=${sub.slug}`}
|
||||
onClick={() => setActiveParentCat(null)}
|
||||
className="flex items-center justify-between p-3 rounded-xl border border-gray-100 hover:border-primary/40 hover:bg-primary/5 transition-all group"
|
||||
>
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-8 h-8 rounded-lg bg-gray-100 flex items-center justify-center text-primary group-hover:bg-primary group-hover:text-white transition-colors">
|
||||
<Layers className="w-4 h-4" />
|
||||
</div>
|
||||
<div>
|
||||
<p className="text-[13px] font-semibold text-gray-800 group-hover:text-primary transition-colors">
|
||||
{sub.name}
|
||||
</p>
|
||||
<p className="text-[11px] text-gray-400">
|
||||
{sub.product_count !== undefined ? `${sub.product_count} Products` : 'Browse'}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<ArrowRight className="w-4 h-4 text-gray-400 group-hover:text-primary group-hover:translate-x-0.5 transition-all" />
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="pt-3 border-t border-gray-100 flex items-center justify-between gap-3">
|
||||
<Link
|
||||
href={`/shop?category=${activeParentCat.slug}`}
|
||||
onClick={() => setActiveParentCat(null)}
|
||||
className="w-full text-center py-2.5 px-4 bg-primary text-white text-xs font-bold rounded-xl hover:brightness-95 transition-all shadow-sm"
|
||||
>
|
||||
View All {activeParentCat.name} ({activeParentCat.product_count || 0})
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in a new issue