show subcategory

This commit is contained in:
hamid zarghami
2026-02-15 09:56:43 +03:30
parent 2a94597682
commit da30f67717
4 changed files with 76 additions and 44 deletions
@@ -42,33 +42,26 @@ const CategoryScroll = ({
className, className,
}: Props) => { }: Props) => {
const { renderer: Renderer, imageSize } = variantConfig[variant]; const { renderer: Renderer, imageSize } = variantConfig[variant];
const selectedParent =
categories.find((c) => c.id === selectedCategory) ??
categories.find((c) => c.children?.some((ch) => ch.id === selectedCategory));
const children = selectedParent?.children ?? [];
const handleSelect = (categoryId: string) => () => onSelect(categoryId); const handleSelect = (categoryId: string) => () => onSelect(categoryId);
return ( return (
<div className="flex flex-col">
<HorizontalScrollView <HorizontalScrollView
className={clsx( className={clsx(
"w-full noscrollbar py-4!", "w-full noscrollbar pt-4! pb-1!",
variant === "large" && "mt-4!", variant === "large" && "mt-4!",
className className
)} )}
> >
{/* <Renderer
key="all"
className={clsx(selectedCategory === "0" && "bg-container!")}
onClick={handleSelect(0)}
>
<Image
priority
src="/assets/images/food-image.png"
width={imageSize}
height={imageSize}
alt="category image"
/>
<span className="text-xs text-foreground">همه</span>
</Renderer> */}
{categories.map((item) => { {categories.map((item) => {
const isSelected = item.id === selectedCategory; const isSelected =
item.id === selectedCategory ||
item.children?.some((ch) => ch.id === selectedCategory);
return ( return (
<Renderer <Renderer
@@ -88,6 +81,30 @@ const CategoryScroll = ({
); );
})} })}
</HorizontalScrollView> </HorizontalScrollView>
{children.length > 0 && (
<HorizontalScrollView
className={clsx(
"w-full noscrollbar py-2!",
variant === "small" && "py-1!"
)}
>
{children.map((child) => {
const isChildSelected = child.id === selectedCategory;
return (
<CategorySmallItemRenderer
key={child.id}
className={clsx(isChildSelected && "bg-container!")}
onClick={handleSelect(child.id)}
>
<span className="text-[10px] text-foreground whitespace-nowrap">{child.title}</span>
</CategorySmallItemRenderer>
);
})}
</HorizontalScrollView>
)}
</div>
); );
}; };
+14 -2
View File
@@ -109,8 +109,20 @@ const MenuIndex = () => {
const lowerFilterQuery = filterSearch ? filterSearch.toLowerCase() : ""; const lowerFilterQuery = filterSearch ? filterSearch.toLowerCase() : "";
const selectedCatId = selectedCategory === '0' ? null : selectedCategory; const selectedCatId = selectedCategory === '0' ? null : selectedCategory;
// اگر دستهٔ اصلی انتخاب شده، خودش + همهٔ زیردسته‌ها را در نظر بگیر تا غذاهای زیردسته هم بیایند
const effectiveCategoryIds = (() => {
if (!selectedCatId) return null;
const parent = categories.find((c) => c.id === selectedCatId);
if (parent?.children?.length) {
return new Set([parent.id, ...parent.children.map((ch) => ch.id)]);
}
return new Set([selectedCatId]);
})();
const filtered = products.filter((item) => { const filtered = products.filter((item) => {
const matchesCategory = !selectedCatId || item.category?.id === selectedCatId; const matchesCategory =
!selectedCatId ||
(item.category?.id != null && effectiveCategoryIds?.has(item.category.id));
const itemName = item.name ?? item.title; const itemName = item.name ?? item.title;
const matchesSearch = const matchesSearch =
!search || itemName.toLowerCase().includes(lowerSearch); !search || itemName.toLowerCase().includes(lowerSearch);
@@ -149,7 +161,7 @@ const MenuIndex = () => {
return 0; return 0;
} }
}); });
}, [selectedCategory, search, filterSearch, products, sorting]); }, [selectedCategory, search, filterSearch, products, sorting, categories]);
if (isLoading) { if (isLoading) {
return <MenuSkeleton viewMode={viewMode} />; return <MenuSkeleton viewMode={viewMode} />;
+4 -1
View File
@@ -10,10 +10,13 @@ export interface Category {
createdAt: string; createdAt: string;
updatedAt: string; updatedAt: string;
deletedAt: string | null; deletedAt: string | null;
parent: string | null;
title: string; title: string;
isActive: boolean; isActive: boolean;
restaurant: string; shop: string;
avatarUrl: string | null; avatarUrl: string | null;
order: number | null;
children: Category[];
} }
export type CategoriesResponse = BaseResponse<Category[]>; export type CategoriesResponse = BaseResponse<Category[]>;
@@ -11,7 +11,7 @@ function CategorySmallItemRenderer({ children, ...rest }: Props) {
<div className={`${rest.className} cursor-pointer transition-all duration-200 ease-out gradient-border overflow-hidden rounded-xl backdrop-blur-md bg-container/40 dark:bg-background`}> <div className={`${rest.className} cursor-pointer transition-all duration-200 ease-out gradient-border overflow-hidden rounded-xl backdrop-blur-md bg-container/40 dark:bg-background`}>
<div <div
{...rest} {...rest}
className="rounded-normal h-[44px] flex flex-row justify-center items-center p-2.5 gap-2" className="rounded-normal flex flex-row justify-center items-center h-8 px-2 gap-2"
> >
{children} {children}
</div> </div>