From 86aebd7a9fc27026ea4d474769c6bbc11b2d7215 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sun, 4 Jan 2026 16:38:04 +0330 Subject: [PATCH] back in fixed category --- src/app/[name]/(Main)/[id]/page.tsx | 14 ++++++++++++-- .../[name]/(Main)/components/CategoryScroll.tsx | 11 +++++------ src/app/[name]/(Main)/page.tsx | 17 +++++++++-------- src/components/listview/MenuItem.tsx | 2 +- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/app/[name]/(Main)/[id]/page.tsx b/src/app/[name]/(Main)/[id]/page.tsx index b7755fd..4623160 100644 --- a/src/app/[name]/(Main)/[id]/page.tsx +++ b/src/app/[name]/(Main)/[id]/page.tsx @@ -19,7 +19,7 @@ type Props = object function FoodPage({ }: Props) { - const { id } = useParams(); + const { id, name } = useParams(); const router = useRouter(); const { isSuccess } = useGetProfile(); const { data: food, isLoading } = useGetFood(id as string); @@ -116,6 +116,16 @@ function FoodPage({ }: Props) { ? foodData.content.join('، ') : foodData.content || foodData.desc || ''; + const handleBackToMenu = () => { + const urlParams = new URLSearchParams(window.location.search); + const categoryParam = urlParams.get('category') || foodData.category?.id; + if (categoryParam) { + router.push(`/${name}?category=${categoryParam}`); + } else { + router.push(`/${name}`); + } + }; + return (
@@ -132,7 +142,7 @@ function FoodPage({ }: Props) {
{categoryName || '-'}
-
diff --git a/src/app/[name]/(Main)/components/CategoryScroll.tsx b/src/app/[name]/(Main)/components/CategoryScroll.tsx index f25fd66..548fb73 100644 --- a/src/app/[name]/(Main)/components/CategoryScroll.tsx +++ b/src/app/[name]/(Main)/components/CategoryScroll.tsx @@ -29,7 +29,7 @@ const variantConfig: Record< type Props = { categories: Category[]; selectedCategory: string; - onSelect: (index: number) => void; + onSelect: (categoryId: string) => void; variant?: Variant; className?: string; }; @@ -43,7 +43,7 @@ const CategoryScroll = ({ }: Props) => { const { renderer: Renderer, imageSize } = variantConfig[variant]; - const handleSelect = (index: number) => () => onSelect(index); + const handleSelect = (categoryId: string) => () => onSelect(categoryId); return ( همه */} - {categories.map((item, index) => { - const categoryIndex = index; - const isSelected = String(categoryIndex) === selectedCategory; + {categories.map((item) => { + const isSelected = item.id === selectedCategory; return ( { useEffect(() => { if (isInitialMount) { setSearch(null); - setSelectedCategory(null); + // category را نگه می‌داریم تا در بک زدن حفظ شود + // setSelectedCategory(null); setSelectedIngredients(null); setSelectedDeliveryId(null); setSorting(null); setIsInitialMount(false); } - }, [isInitialMount, setSearch, setSelectedCategory, setSelectedIngredients, setSelectedDeliveryId, setSorting]); + }, [isInitialMount, setSearch, setSelectedIngredients, setSelectedDeliveryId, setSorting]); const onScroll = useCallback(() => { if (!wrapperRef?.current?.parentElement?.parentElement?.scrollTop || !smallCategoriesRef.current?.offsetTop) return; @@ -80,8 +81,8 @@ const MenuIndex = () => { setSearch(e.target.value); }, [setSearch]); - const updateCategory = useCallback((index: number) => { - setSelectedCategory(String(index)); + const updateCategory = useCallback((categoryId: string) => { + setSelectedCategory(categoryId); }, [setSelectedCategory]); const sortingIndex = Number(sorting); @@ -106,10 +107,10 @@ const MenuIndex = () => { if (!foods.length) return []; const lowerSearch = search.toLowerCase(); const lowerIngredients = selectedIngredients ? selectedIngredients.toLowerCase() : ""; - const selectedCatId = categories[+selectedCategory]?.id; + const selectedCatId = selectedCategory === '0' ? null : selectedCategory; const filtered = foods.filter((item) => { - const matchesCategory = item.category?.id === selectedCatId; + const matchesCategory = !selectedCatId || item.category?.id === selectedCatId; const itemName = item.name || item.title || item.foodName || ""; const matchesSearch = !search || itemName.toLowerCase().includes(lowerSearch); @@ -150,7 +151,7 @@ const MenuIndex = () => { return 0; } }); - }, [selectedCategory, search, selectedIngredients, selectedDeliveryId, foods, categories, sorting]); + }, [selectedCategory, search, selectedIngredients, selectedDeliveryId, foods, sorting]); if (isLoading) { return ; @@ -170,7 +171,7 @@ const MenuIndex = () => {
- {categories[+selectedCategory]?.title || ''} + {selectedCategory === '0' ? '' : categories.find(c => c.id === selectedCategory)?.title || ''}