feat(mobile): display parent categories with expandable subcategory dropdowns in mobile drawer

This commit is contained in:
iFixKart Dev 2026-11-16 23:45:37 +05:30
parent c35fc01b31
commit fd93e7023a
2 changed files with 117 additions and 17 deletions

View file

@ -1,7 +1,7 @@
"use client"; "use client";
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState, useMemo } from 'react';
import { X, Folder, LayoutGrid } from 'lucide-react'; import { X, Folder, FolderOpen, ChevronDown, ArrowRight, LayoutGrid } from 'lucide-react';
import { motion, AnimatePresence } from 'framer-motion'; import { motion, AnimatePresence } from 'framer-motion';
import Link from 'next/link'; import Link from 'next/link';
import { catalogService, CategoryResponse } from '@/services/api/catalogService'; import { catalogService, CategoryResponse } from '@/services/api/catalogService';
@ -13,6 +13,7 @@ interface MobileDrawerProps {
export const MobileDrawer: React.FC<MobileDrawerProps> = ({ isOpen, onClose }) => { export const MobileDrawer: React.FC<MobileDrawerProps> = ({ isOpen, onClose }) => {
const [categories, setCategories] = useState<CategoryResponse[]>([]); const [categories, setCategories] = useState<CategoryResponse[]>([]);
const [expandedCats, setExpandedCats] = useState<Record<string, boolean>>({});
useEffect(() => { useEffect(() => {
if (isOpen) { if (isOpen) {
@ -22,6 +23,26 @@ export const MobileDrawer: React.FC<MobileDrawerProps> = ({ isOpen, onClose }) =
} }
}, [isOpen]); }, [isOpen]);
const toggleCategory = (catId: string) => {
setExpandedCats((prev) => ({
...prev,
[catId]: !prev[catId],
}));
};
const parentCategories = useMemo(() => {
const parents = categories.filter((c) => !c.parent_category_id);
return parents.map((parent) => {
const nestedSubs = parent.sub_categories || [];
const flatSubs = categories.filter((c) => c.parent_category_id === parent.category_id);
const subCategories = nestedSubs.length > 0 ? nestedSubs : flatSubs;
return {
...parent,
subCategories,
};
});
}, [categories]);
React.useEffect(() => { React.useEffect(() => {
if (!isOpen) return; if (!isOpen) return;
const handleKeyDown = (e: KeyboardEvent) => { const handleKeyDown = (e: KeyboardEvent) => {
@ -68,7 +89,7 @@ export const MobileDrawer: React.FC<MobileDrawerProps> = ({ isOpen, onClose }) =
animate={{ x: 0 }} animate={{ x: 0 }}
exit={{ x: '-100%' }} exit={{ x: '-100%' }}
transition={{ type: 'tween', duration: 0.3 }} transition={{ type: 'tween', duration: 0.3 }}
className="fixed top-0 left-0 bottom-0 w-[280px] max-w-[85vw] bg-white z-[120] xl:hidden flex flex-col justify-between shadow-xl overflow-y-auto overscroll-contain" className="fixed top-0 left-0 bottom-0 w-[290px] max-w-[85vw] bg-white z-[120] xl:hidden flex flex-col justify-between shadow-xl overflow-y-auto overscroll-contain"
> >
<div> <div>
{/* Header */} {/* Header */}
@ -104,22 +125,100 @@ export const MobileDrawer: React.FC<MobileDrawerProps> = ({ isOpen, onClose }) =
</div> </div>
{/* Categories */} {/* Categories */}
{categories.length > 0 && ( {parentCategories.length > 0 && (
<div className="p-4 text-left"> <div className="p-4 text-left">
<h4 className="text-[11px] font-bold text-gray-400 uppercase tracking-widest mb-3">Categories</h4> <h4 className="text-[11px] font-bold text-gray-400 uppercase tracking-widest mb-3">Categories</h4>
<ul className="space-y-3"> <ul className="space-y-1">
{categories.map((cat) => ( {parentCategories.map((cat) => {
<li key={cat.category_id}> const hasSubs = cat.subCategories && cat.subCategories.length > 0;
const isExpanded = !!expandedCats[cat.category_id];
return (
<li key={cat.category_id} className="rounded-lg overflow-hidden">
{hasSubs ? (
<div>
<button
type="button"
onClick={() => toggleCategory(cat.category_id)}
className={`w-full flex items-center justify-between py-2.5 px-2 rounded-lg text-sm font-semibold transition-colors cursor-pointer ${
isExpanded
? 'bg-primary/5 text-primary'
: 'text-gray-700 hover:text-primary hover:bg-gray-50'
}`}
>
<div className="flex items-center gap-2.5 min-w-0">
{isExpanded ? (
<FolderOpen size={16} className="text-primary shrink-0" />
) : (
<Folder size={16} className="text-primary/70 shrink-0" />
)}
<span className="truncate">{cat.name}</span>
</div>
<div className="flex items-center gap-1.5 shrink-0 ml-2">
<span className="text-[10px] font-bold px-1.5 py-0.5 rounded-full bg-gray-100 text-gray-500">
{cat.subCategories.length}
</span>
<ChevronDown
size={16}
className={`text-gray-400 transition-transform duration-200 ${
isExpanded ? 'rotate-180 text-primary' : ''
}`}
/>
</div>
</button>
{/* Subcategories Accordion */}
<AnimatePresence initial={false}>
{isExpanded && (
<motion.ul
initial={{ opacity: 0, height: 0 }}
animate={{ opacity: 1, height: 'auto' }}
exit={{ opacity: 0, height: 0 }}
transition={{ duration: 0.2, ease: 'easeInOut' }}
className="overflow-hidden pl-5 pr-2 py-1 space-y-1 bg-gray-50/70 rounded-b-lg border-l-2 border-primary/30 ml-4 my-1"
>
{/* View All Parent Category */}
<li>
<Link <Link
href={`/shop?category=${encodeURIComponent(cat.slug)}`} href={`/shop?category=${encodeURIComponent(cat.slug)}`}
onClick={onClose} onClick={onClose}
className="flex items-center gap-3 text-sm font-semibold text-gray-650 hover:text-primary transition-colors" className="flex items-center gap-2 py-1.5 text-xs font-bold text-primary hover:underline transition-colors"
> >
<Folder size={15} className="text-primary/70" /> <span>All {cat.name}</span>
<span>{cat.name}</span> <ArrowRight size={12} />
</Link>
</li>
{/* Subcategories */}
{cat.subCategories.map((sub) => (
<li key={sub.category_id}>
<Link
href={`/shop?category=${encodeURIComponent(sub.slug)}`}
onClick={onClose}
className="flex items-center gap-2 py-1.5 text-xs font-medium text-gray-600 hover:text-primary transition-colors"
>
<span className="w-1.5 h-1.5 rounded-full bg-gray-300 shrink-0" />
<span className="truncate">{sub.name}</span>
</Link> </Link>
</li> </li>
))} ))}
</motion.ul>
)}
</AnimatePresence>
</div>
) : (
<Link
href={`/shop?category=${encodeURIComponent(cat.slug)}`}
onClick={onClose}
className="flex items-center gap-2.5 py-2.5 px-2 rounded-lg text-sm font-semibold text-gray-700 hover:text-primary hover:bg-gray-50 transition-colors"
>
<Folder size={16} className="text-primary/70 shrink-0" />
<span className="truncate">{cat.name}</span>
</Link>
)}
</li>
);
})}
</ul> </ul>
</div> </div>
)} )}

View file

@ -21,6 +21,7 @@ export interface CategoryResponse {
is_parent_feature?: boolean; is_parent_feature?: boolean;
is_active: boolean; is_active: boolean;
product_count?: number; product_count?: number;
sub_categories?: CategoryResponse[];
created_at: string; created_at: string;
} }