diff --git a/components/MobileDrawer.tsx b/components/MobileDrawer.tsx index ae50c9f..d0ff020 100644 --- a/components/MobileDrawer.tsx +++ b/components/MobileDrawer.tsx @@ -1,7 +1,7 @@ "use client"; -import React, { useEffect, useState } from 'react'; -import { X, Folder, LayoutGrid } from 'lucide-react'; +import React, { useEffect, useState, useMemo } from 'react'; +import { X, Folder, FolderOpen, ChevronDown, ArrowRight, LayoutGrid } from 'lucide-react'; import { motion, AnimatePresence } from 'framer-motion'; import Link from 'next/link'; import { catalogService, CategoryResponse } from '@/services/api/catalogService'; @@ -13,6 +13,7 @@ interface MobileDrawerProps { export const MobileDrawer: React.FC = ({ isOpen, onClose }) => { const [categories, setCategories] = useState([]); + const [expandedCats, setExpandedCats] = useState>({}); useEffect(() => { if (isOpen) { @@ -22,6 +23,26 @@ export const MobileDrawer: React.FC = ({ isOpen, onClose }) = } }, [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(() => { if (!isOpen) return; const handleKeyDown = (e: KeyboardEvent) => { @@ -68,7 +89,7 @@ export const MobileDrawer: React.FC = ({ isOpen, onClose }) = animate={{ x: 0 }} exit={{ x: '-100%' }} 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" >
{/* Header */} @@ -104,22 +125,100 @@ export const MobileDrawer: React.FC = ({ isOpen, onClose }) =
{/* Categories */} - {categories.length > 0 && ( + {parentCategories.length > 0 && (

Categories

-
    - {categories.map((cat) => ( -
  • - - - {cat.name} - -
  • - ))} +
      + {parentCategories.map((cat) => { + const hasSubs = cat.subCategories && cat.subCategories.length > 0; + const isExpanded = !!expandedCats[cat.category_id]; + + return ( +
    • + {hasSubs ? ( +
      + + + {/* Subcategories Accordion */} + + {isExpanded && ( + + {/* View All Parent Category */} +
    • + + All {cat.name} + + +
    • + + {/* Subcategories */} + {cat.subCategories.map((sub) => ( +
    • + + + {sub.name} + +
    • + ))} + + )} + +
+ ) : ( + + + {cat.name} + + )} + + ); + })} )} diff --git a/services/api/catalogService.ts b/services/api/catalogService.ts index ee424bf..a0db05f 100644 --- a/services/api/catalogService.ts +++ b/services/api/catalogService.ts @@ -21,6 +21,7 @@ export interface CategoryResponse { is_parent_feature?: boolean; is_active: boolean; product_count?: number; + sub_categories?: CategoryResponse[]; created_at: string; }