From 7e31f55fb7f5d563c2c616eacad8bfd85531468f Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sat, 25 Oct 2025 17:57:18 +0330 Subject: [PATCH] menu category mobile --- src/components/CategoryModal.tsx | 297 ++++++++++++++----------------- 1 file changed, 136 insertions(+), 161 deletions(-) diff --git a/src/components/CategoryModal.tsx b/src/components/CategoryModal.tsx index 81feaea..6e3d662 100644 --- a/src/components/CategoryModal.tsx +++ b/src/components/CategoryModal.tsx @@ -2,6 +2,9 @@ import { FC, useState } from 'react' import { ArrowLeft2 } from 'iconsax-react' import { useRouter } from 'next/navigation' +import Image from 'next/image' +import { useGetCategories } from '@/share/hooks/useShareData' +import { Category } from '@/share/types/SharedTypes' type Props = { isOpen: boolean @@ -10,202 +13,174 @@ type Props = { const CategoryModal: FC = ({ isOpen, onClose }) => { const router = useRouter() - const [selectedMainCategory, setSelectedMainCategory] = useState(0) + const { data, isLoading } = useGetCategories() + const [selectedMainCategory, setSelectedMainCategory] = useState(null) const [expandedSubcategory, setExpandedSubcategory] = useState(null) - // Main categories for the left column - const mainCategories = [ - { icon: '🏪', title: 'سوپر مارکت ارگانیک' }, - { icon: '🍞', title: 'خانه نان و شیرینی سالم' }, - { icon: '🍎', title: 'بوفه رژیم‌های خاص' }, - { icon: '💪', title: 'بوفه سبک زندگی' }, - { icon: '🌿', title: 'عطاری سلامت' }, - { icon: '🎁', title: 'بوفه هدایا و سوغات' } - ] + const parentCategories = data?.results?.data?.filter(cat => !cat.parent) || [] - // Subcategories for each main category - const categorySubcategories = { - 'سوپر مارکت ارگانیک': [ - { title: 'ارگانیک گواهی‌دار', hasChildren: false }, - { title: 'روغن های سالم', hasChildren: true }, - { title: 'چاشنی سلامت', hasChildren: true }, - { title: 'صبحانه سالم', hasChildren: false }, - { title: 'نوشیدنی سالم', hasChildren: false }, - { title: 'آرایشی', hasChildren: false }, - { title: 'بهداشتی', hasChildren: false }, - { title: 'تازه خوری', hasChildren: false }, - { title: 'حبوبات و غلات', hasChildren: false }, - { title: 'تنقلات', hasChildren: false }, - { title: 'چای و دمنوش', hasChildren: false }, - { title: 'قهوه', hasChildren: false }, - { title: 'قند و شکر', hasChildren: false } - ], - 'خانه نان و شیرینی سالم': [ - { title: 'نان سنتی', hasChildren: false }, - { title: 'نان صنعتی', hasChildren: false }, - { title: 'شیرینی', hasChildren: false }, - { title: 'کیک و کلوچه', hasChildren: false } - ], - 'بوفه رژیم‌های خاص': [ - { title: 'رژیم کتو', hasChildren: false }, - { title: 'رژیم وگان', hasChildren: false }, - { title: 'بدون گلوتن', hasChildren: false } - ], - 'بوفه سبک زندگی': [ - { title: 'ورزشی', hasChildren: false }, - { title: 'لاغری', hasChildren: false }, - { title: 'سالمندان', hasChildren: false } - ], - 'عطاری سلامت': [ - { title: 'گیاهان دارویی', hasChildren: false }, - { title: 'عسل طبیعی', hasChildren: false }, - { title: 'دمنوش', hasChildren: false } - ], - 'بوفه هدایا و سوغات': [ - { title: 'هدایا', hasChildren: false }, - { title: 'سوغات', hasChildren: false }, - { title: 'بسته بندی ویژه', hasChildren: false } - ] - } - - // Third level subcategories (for items that expand) - const thirdLevelCategories = { - 'روغن های سالم': [ - { icon: '🫒', title: 'روغن زیتون پیوست و مو' }, - { icon: '🌻', title: 'روغن ماساژ' }, - { icon: '🥥', title: 'روغن های خوراکی' } - ], - 'چاشنی سلامت': [ - { icon: '🥒', title: 'ترشی' }, - { icon: '🥫', title: 'رب ها' }, - { icon: '🍯', title: 'سس ها' }, - { icon: '🥬', title: 'سبزیجات خشک' }, - { icon: '🌶️', title: 'ادویه' }, - { icon: '🍎', title: 'سرکه' }, - { icon: '🫒', title: 'زیتون' } - ] - } - - const handleMainCategoryClick = (index: number) => { - setSelectedMainCategory(index) + const handleMainCategoryClick = (categoryId: string) => { + setSelectedMainCategory(categoryId) setExpandedSubcategory(null) } - const handleSubcategoryClick = (subcategory: { title: string; hasChildren: boolean }) => { - if (subcategory.hasChildren && thirdLevelCategories[subcategory.title as keyof typeof thirdLevelCategories]) { - setExpandedSubcategory(expandedSubcategory === subcategory.title ? null : subcategory.title) + const handleSubcategoryClick = (category: Category) => { + if (category.children && category.children.length > 0) { + setExpandedSubcategory(expandedSubcategory === category._id ? null : category._id) } else { - router.push(`/products/${subcategory.title}`) + router.push(`/products/${category.title_en}`) onClose() } } + const handleCategoryNavigate = (category: Category) => { + router.push(`/products/${category.title_en}`) + onClose() + } + if (!isOpen) return null - const currentMainCategory = mainCategories[selectedMainCategory] - const currentSubcategories = categorySubcategories[currentMainCategory.title as keyof typeof categorySubcategories] || [] + const currentMainCategoryData = selectedMainCategory + ? parentCategories.find(cat => cat._id === selectedMainCategory) + : parentCategories[0] + + const currentSubcategories = currentMainCategoryData?.children || [] return (
{/* Left Column - Main Categories */}
-
- {mainCategories.map((category, index) => ( -
-
handleMainCategoryClick(index)} - > -
- {category.icon} + {isLoading ? ( +
+
در حال بارگذاری...
+
+ ) : ( +
+ {parentCategories.map((category, index) => ( +
+
handleMainCategoryClick(category._id)} + > +
+ {category.icon ? ( + {category.title_fa} + ) : ( + 📦 + )} +
+ + {category.title_fa} +
- - {category.title} - + {index < parentCategories.length - 1 && ( +
+ )}
- {index < mainCategories.length - 1 && ( -
- )} -
- ))} -
+ ))} +
+ )}
{/* Right Column - Subcategories */}
- {/* Special Categories */} -
-
{ - router.push('/products') - onClose() - }} - > - همه محصولات سوپر مارکت ارگانیک + {isLoading ? ( +
+
در حال بارگذاری...
-
{ - router.push('/products/organic') - onClose() - }} - > - ارگانیک گواهی‌دار -
-
- - {/* Subcategories List */} -
- {currentSubcategories.map((subcategory, index) => ( -
-
handleSubcategoryClick(subcategory)} - > - {subcategory.title} - {subcategory.hasChildren && ( - - )} + ) : ( + <> + {/* Main Category Link */} + {currentMainCategoryData && ( +
+
handleCategoryNavigate(currentMainCategoryData)} + > + + همه محصولات {currentMainCategoryData.title_fa} + +
+ )} - {/* Expanded third level categories */} - {expandedSubcategory === subcategory.title && thirdLevelCategories[subcategory.title as keyof typeof thirdLevelCategories] && ( -
-
- همه محصولات {subcategory.title} -
-
- {thirdLevelCategories[subcategory.title as keyof typeof thirdLevelCategories].map((item, itemIndex) => ( -
{ - router.push(`/products/${item.title}`) - onClose() - }} - > -
- {item.icon} + {/* Subcategories List */} +
+ {currentSubcategories.length > 0 ? ( + currentSubcategories.map((subcategory, index) => ( +
+
handleSubcategoryClick(subcategory)} + > + {subcategory.title_fa} + {subcategory.children && subcategory.children.length > 0 && ( + + )} +
+ + {/* Expanded third level categories */} + {expandedSubcategory === subcategory._id && subcategory.children && subcategory.children.length > 0 && ( +
+
handleCategoryNavigate(subcategory)} + > + همه محصولات {subcategory.title_fa} +
+
+ {subcategory.children.map((item) => ( +
handleCategoryNavigate(item)} + > +
+ {item.icon ? ( + {item.title_fa} + ) : ( + 📦 + )} +
+ + {item.title_fa} + +
+ ))}
- - {item.title} -
- ))} + )} + + {index < currentSubcategories.length - 1 && ( +
+ )}
+ )) + ) : ( +
+ زیردسته‌ای یافت نشد
)} - - {index < currentSubcategories.length - 1 && ( -
- )}
- ))} -
+ + )}