back in fixed category
This commit is contained in:
@@ -19,7 +19,7 @@ type Props = object
|
|||||||
|
|
||||||
function FoodPage({ }: Props) {
|
function FoodPage({ }: Props) {
|
||||||
|
|
||||||
const { id } = useParams();
|
const { id, name } = useParams();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { isSuccess } = useGetProfile();
|
const { isSuccess } = useGetProfile();
|
||||||
const { data: food, isLoading } = useGetFood(id as string);
|
const { data: food, isLoading } = useGetFood(id as string);
|
||||||
@@ -116,6 +116,16 @@ function FoodPage({ }: Props) {
|
|||||||
? foodData.content.join('، ')
|
? foodData.content.join('، ')
|
||||||
: foodData.content || foodData.desc || '';
|
: 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 (
|
return (
|
||||||
<div className='lg:absolute lg:top-1/2 not-lg:max-w-lg not-lg:place-self-center lg:-translate-y-1/2 xl:right-[285px] lg:left-5 lg:right-4 lg:bottom-4 not-lg:w-full not-md:-translate-y-2 pt-6 flex flex-col lg:grid grid-cols-2 bottom-10'>
|
<div className='lg:absolute lg:top-1/2 not-lg:max-w-lg not-lg:place-self-center lg:-translate-y-1/2 xl:right-[285px] lg:left-5 lg:right-4 lg:bottom-4 not-lg:w-full not-md:-translate-y-2 pt-6 flex flex-col lg:grid grid-cols-2 bottom-10'>
|
||||||
<div className="relative w-full h-full not-lg:bg-container rounded-2xl overflow-hidden lg:rounded-r-none lg:order-1">
|
<div className="relative w-full h-full not-lg:bg-container rounded-2xl overflow-hidden lg:rounded-r-none lg:order-1">
|
||||||
@@ -132,7 +142,7 @@ function FoodPage({ }: Props) {
|
|||||||
<div className="bg-container whitespace-nowrap rounded-[34px] py-1.5 px-4 w-min text-xs text-disabled2 dark:text-disabled-text font-bold">
|
<div className="bg-container whitespace-nowrap rounded-[34px] py-1.5 px-4 w-min text-xs text-disabled2 dark:text-disabled-text font-bold">
|
||||||
{categoryName || '-'}
|
{categoryName || '-'}
|
||||||
</div>
|
</div>
|
||||||
<button onClick={() => router.back()} className='p-2 rounded-full bg-container/40'>
|
<button onClick={handleBackToMenu} className='p-2 rounded-full bg-container/40'>
|
||||||
<ArrowLeft size={18} className='stroke-primary dark:stroke-foreground' />
|
<ArrowLeft size={18} className='stroke-primary dark:stroke-foreground' />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ const variantConfig: Record<
|
|||||||
type Props = {
|
type Props = {
|
||||||
categories: Category[];
|
categories: Category[];
|
||||||
selectedCategory: string;
|
selectedCategory: string;
|
||||||
onSelect: (index: number) => void;
|
onSelect: (categoryId: string) => void;
|
||||||
variant?: Variant;
|
variant?: Variant;
|
||||||
className?: string;
|
className?: string;
|
||||||
};
|
};
|
||||||
@@ -43,7 +43,7 @@ const CategoryScroll = ({
|
|||||||
}: Props) => {
|
}: Props) => {
|
||||||
const { renderer: Renderer, imageSize } = variantConfig[variant];
|
const { renderer: Renderer, imageSize } = variantConfig[variant];
|
||||||
|
|
||||||
const handleSelect = (index: number) => () => onSelect(index);
|
const handleSelect = (categoryId: string) => () => onSelect(categoryId);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<HorizontalScrollView
|
<HorizontalScrollView
|
||||||
@@ -67,15 +67,14 @@ const CategoryScroll = ({
|
|||||||
/>
|
/>
|
||||||
<span className="text-xs text-foreground">همه</span>
|
<span className="text-xs text-foreground">همه</span>
|
||||||
</Renderer> */}
|
</Renderer> */}
|
||||||
{categories.map((item, index) => {
|
{categories.map((item) => {
|
||||||
const categoryIndex = index;
|
const isSelected = item.id === selectedCategory;
|
||||||
const isSelected = String(categoryIndex) === selectedCategory;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Renderer
|
<Renderer
|
||||||
key={item.id}
|
key={item.id}
|
||||||
className={clsx(isSelected && "bg-container!")}
|
className={clsx(isSelected && "bg-container!")}
|
||||||
onClick={handleSelect(categoryIndex)}
|
onClick={handleSelect(item.id)}
|
||||||
>
|
>
|
||||||
<Image
|
<Image
|
||||||
priority
|
priority
|
||||||
|
|||||||
@@ -44,13 +44,14 @@ const MenuIndex = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isInitialMount) {
|
if (isInitialMount) {
|
||||||
setSearch(null);
|
setSearch(null);
|
||||||
setSelectedCategory(null);
|
// category را نگه میداریم تا در بک زدن حفظ شود
|
||||||
|
// setSelectedCategory(null);
|
||||||
setSelectedIngredients(null);
|
setSelectedIngredients(null);
|
||||||
setSelectedDeliveryId(null);
|
setSelectedDeliveryId(null);
|
||||||
setSorting(null);
|
setSorting(null);
|
||||||
setIsInitialMount(false);
|
setIsInitialMount(false);
|
||||||
}
|
}
|
||||||
}, [isInitialMount, setSearch, setSelectedCategory, setSelectedIngredients, setSelectedDeliveryId, setSorting]);
|
}, [isInitialMount, setSearch, setSelectedIngredients, setSelectedDeliveryId, setSorting]);
|
||||||
|
|
||||||
const onScroll = useCallback(() => {
|
const onScroll = useCallback(() => {
|
||||||
if (!wrapperRef?.current?.parentElement?.parentElement?.scrollTop || !smallCategoriesRef.current?.offsetTop) return;
|
if (!wrapperRef?.current?.parentElement?.parentElement?.scrollTop || !smallCategoriesRef.current?.offsetTop) return;
|
||||||
@@ -80,8 +81,8 @@ const MenuIndex = () => {
|
|||||||
setSearch(e.target.value);
|
setSearch(e.target.value);
|
||||||
}, [setSearch]);
|
}, [setSearch]);
|
||||||
|
|
||||||
const updateCategory = useCallback((index: number) => {
|
const updateCategory = useCallback((categoryId: string) => {
|
||||||
setSelectedCategory(String(index));
|
setSelectedCategory(categoryId);
|
||||||
}, [setSelectedCategory]);
|
}, [setSelectedCategory]);
|
||||||
|
|
||||||
const sortingIndex = Number(sorting);
|
const sortingIndex = Number(sorting);
|
||||||
@@ -106,10 +107,10 @@ const MenuIndex = () => {
|
|||||||
if (!foods.length) return [];
|
if (!foods.length) return [];
|
||||||
const lowerSearch = search.toLowerCase();
|
const lowerSearch = search.toLowerCase();
|
||||||
const lowerIngredients = selectedIngredients ? selectedIngredients.toLowerCase() : "";
|
const lowerIngredients = selectedIngredients ? selectedIngredients.toLowerCase() : "";
|
||||||
const selectedCatId = categories[+selectedCategory]?.id;
|
const selectedCatId = selectedCategory === '0' ? null : selectedCategory;
|
||||||
|
|
||||||
const filtered = foods.filter((item) => {
|
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 itemName = item.name || item.title || item.foodName || "";
|
||||||
const matchesSearch =
|
const matchesSearch =
|
||||||
!search || itemName.toLowerCase().includes(lowerSearch);
|
!search || itemName.toLowerCase().includes(lowerSearch);
|
||||||
@@ -150,7 +151,7 @@ const MenuIndex = () => {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [selectedCategory, search, selectedIngredients, selectedDeliveryId, foods, categories, sorting]);
|
}, [selectedCategory, search, selectedIngredients, selectedDeliveryId, foods, sorting]);
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return <MenuSkeleton />;
|
return <MenuSkeleton />;
|
||||||
@@ -170,7 +171,7 @@ const MenuIndex = () => {
|
|||||||
<section className="w-full">
|
<section className="w-full">
|
||||||
<div className="flex gap-2 justify-between items-center relative" ref={smallCategoriesRef} >
|
<div className="flex gap-2 justify-between items-center relative" ref={smallCategoriesRef} >
|
||||||
<span className="sm:text-base text-sm font-medium">
|
<span className="sm:text-base text-sm font-medium">
|
||||||
{categories[+selectedCategory]?.title || ''}
|
{selectedCategory === '0' ? '' : categories.find(c => c.id === selectedCategory)?.title || ''}
|
||||||
</span>
|
</span>
|
||||||
<div className="inline-flex min-w-[247px] gap-2 justify-around items-center">
|
<div className="inline-flex min-w-[247px] gap-2 justify-around items-center">
|
||||||
<button onClick={toggleFilterModal} className="rounded-xl h-8 bg-container ps-4 pe-2 py-1.5 inline-flex items-center justify-between gap-[7px]">
|
<button onClick={toggleFilterModal} className="rounded-xl h-8 bg-container ps-4 pe-2 py-1.5 inline-flex items-center justify-between gap-[7px]">
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ const MenuItem = ({ food }: MenuItemProps) => {
|
|||||||
removeFromCart(food.id);
|
removeFromCart(food.id);
|
||||||
};
|
};
|
||||||
|
|
||||||
const foodDetailUrl = `/${name}/${food.id}`;
|
const foodDetailUrl = `/${name}/${food.id}?category=${food.category.id}`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex gap-4 w-full h-full items-center">
|
<div className="flex gap-4 w-full h-full items-center">
|
||||||
|
|||||||
Reference in New Issue
Block a user