From bad9a52b96117b979f926d353f7dcb457e2c16ba Mon Sep 17 00:00:00 2001 From: iFixKart Dev Date: Fri, 6 Nov 2026 15:14:54 +0530 Subject: [PATCH] feat(categories): parent-only homepage carousel with sub-category drilldown modal and grouped categories layout --- app/categories/page.tsx | 112 +++++++++++++++++++---------- sections/CategoriesSection.tsx | 127 +++++++++++++++++++++++++++------ 2 files changed, 182 insertions(+), 57 deletions(-) diff --git a/app/categories/page.tsx b/app/categories/page.tsx index 14e2e79..59c8ee4 100644 --- a/app/categories/page.tsx +++ b/app/categories/page.tsx @@ -69,44 +69,84 @@ export default function AllCategoriesPage() { ) : ( -
- {categories.map((cat: any) => { - const catImage = getImageUrl(cat.image_url || cat.image || cat.icon_url); - return ( - - - {/* Image Box */} -
- {catImage ? ( - - ) : ( - - )} +
+ {(() => { + 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} → +
- {/* Title & Count */} -

- {cat.name} -

- - {cat.product_count !== undefined ? `${cat.product_count} Products` : 'Catalog Item'} - - - - ); - })} + {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.

+
+ )} +
+ ); + }); + })()}
)}
diff --git a/sections/CategoriesSection.tsx b/sections/CategoriesSection.tsx index d3b9b6d..e4acaea 100644 --- a/sections/CategoriesSection.tsx +++ b/sections/CategoriesSection.tsx @@ -80,11 +80,15 @@ export const CategoriesSection: React.FC = ({ const [loading, setLoading] = useState(true); const [error, setError] = useState(false); const [canSlide, setCanSlide] = useState(false); - const [productThumbs, setProductThumbs] = useState>( - {} - ); + const [productThumbs, setProductThumbs] = useState>({}); + const [activeParentCat, setActiveParentCat] = useState(null); const swiperRef = useRef(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 = ({ 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,33 +300,114 @@ export const CategoriesSection: React.FC = ({ cat.product_count !== undefined && cat.product_count !== null ? Number(cat.product_count) : null; + const hasSubs = cat.sub_categories && cat.sub_categories.length > 0; return ( - { + if (hasSubs) { + e.preventDefault(); + setActiveParentCat(cat); + } + }} + className="cursor-pointer" > -
- -
-

- {cat.name} -

- - {count !== null ? `${count} Products` : 'Shop now'} - - + { + if (hasSubs) { + e.preventDefault(); + setActiveParentCat(cat); + } + }} + className="flex flex-col items-center text-center group" + > +
+ + {hasSubs && ( + + {cat.sub_categories.length} sub + + )} +
+

+ {cat.name} +

+ + {count !== null ? `${count} Products` : 'Shop now'} + + +
); })} )} + + {/* Sub-Category Drilldown Modal */} + {activeParentCat && ( +
+
+
+
+

{activeParentCat.name}

+

+ Select a sub-category to view products ({activeParentCat.product_count || 0} total items) +

+
+ +
+ +
+ {(activeParentCat.sub_categories || []).map((sub: any) => ( + 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" + > +
+
+ +
+
+

+ {sub.name} +

+

+ {sub.product_count !== undefined ? `${sub.product_count} Products` : 'Browse'} +

+
+
+ + + ))} +
+ +
+ 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}) + +
+
+
+ )} );