ifixkart-storefront/components/MobileDrawer.tsx

236 lines
10 KiB
TypeScript

"use client";
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';
interface MobileDrawerProps {
isOpen: boolean;
onClose: () => void;
}
export const MobileDrawer: React.FC<MobileDrawerProps> = ({ isOpen, onClose }) => {
const [categories, setCategories] = useState<CategoryResponse[]>([]);
const [expandedCats, setExpandedCats] = useState<Record<string, boolean>>({});
useEffect(() => {
if (isOpen) {
catalogService.getCategories().then((res) => {
if (Array.isArray(res)) setCategories(res);
});
}
}, [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) => {
if (e.key === 'Escape') onClose();
};
document.addEventListener('keydown', handleKeyDown);
return () => document.removeEventListener('keydown', handleKeyDown);
}, [isOpen, onClose]);
// Lock background scroll while the drawer is open (same pattern as CartDrawer),
// and restore the previous value on close.
React.useEffect(() => {
if (!isOpen) return;
const prev = document.body.style.overflow;
document.body.style.overflow = 'hidden';
return () => {
document.body.style.overflow = prev;
};
}, [isOpen]);
const links = [
{ name: 'Home', href: '/' },
{ name: 'Shop', href: '/shop' },
{ name: 'Top Deals', href: '/shop?tag=deals' },
{ name: 'Services', href: '/services' },
];
return (
<AnimatePresence>
{isOpen && (
<>
{/* Backdrop */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 0.5 }}
exit={{ opacity: 0 }}
onClick={onClose}
className="fixed inset-0 bg-black z-[115] xl:hidden"
/>
{/* Drawer Panel */}
<motion.div
initial={{ x: '-100%' }}
animate={{ x: 0 }}
exit={{ x: '-100%' }}
transition={{ type: 'tween', duration: 0.3 }}
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>
{/* Header */}
<div className="flex items-center justify-between p-4 border-b border-gray-100">
<span className="text-xl font-extrabold tracking-tight text-gray-900">
iFix<span className="text-primary">Kart</span>
</span>
<button
onClick={onClose}
className="p-2.5 -mr-1 text-gray-400 hover:text-gray-650 hover:bg-gray-50 rounded-lg transition-colors cursor-pointer"
aria-label="Close Menu"
>
<X size={22} />
</button>
</div>
{/* Navigation Links */}
<div className="p-4 border-b border-gray-100 text-left">
<h4 className="text-[11px] font-bold text-gray-400 uppercase tracking-widest mb-3">Navigation</h4>
<ul className="space-y-3.5">
{links.map((link) => (
<li key={link.name}>
<Link
href={link.href}
onClick={onClose}
className="flex items-center justify-between text-sm font-bold text-gray-700 hover:text-primary transition-colors"
>
<span>{link.name}</span>
</Link>
</li>
))}
</ul>
</div>
{/* Categories */}
{parentCategories.length > 0 && (
<div className="p-4 text-left">
<h4 className="text-[11px] font-bold text-gray-400 uppercase tracking-widest mb-3">Categories</h4>
<ul className="space-y-1">
{parentCategories.map((cat) => {
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
href={`/shop?category=${encodeURIComponent(cat.slug)}`}
onClick={onClose}
className="flex items-center gap-2 py-1.5 text-xs font-bold text-primary hover:underline transition-colors"
>
<span>All {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>
</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>
</div>
)}
</div>
{/* Footer — honors iOS home indicator / Android gesture bar */}
<div className="p-4 pt-3 border-t border-gray-100 text-xs text-gray-400 text-center font-medium bg-gray-50 pb-[max(1rem,env(safe-area-inset-bottom))]">
© {new Date().getFullYear()} iFixKart. All Rights Reserved.
</div>
</motion.div>
</>
)}
</AnimatePresence>
);
};