import { Badge, Card, Grid } from "antd"; import ArabicPrice from "components/ArabicPrice"; import ImageWithFallback from "components/ImageWithFallback"; import { ItemDescriptionIcons } from "components/ItemDescriptionIcons/ItemDescriptionIcons"; import ProText from "components/ProText"; import ProTitle from "components/ProTitle"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import { useAppSelector } from "redux/hooks"; import { colors } from "ThemeConstants"; import { Product } from "utils/types/appTypes"; import { AddToCart } from "../AddToCart"; import styles from "./MenuList.module.css"; interface MenuListProps { data: | { products: Product[]; categories: { id: number; name: string; image?: string }[]; } | undefined; id: string; categoryRefs: React.RefObject<{ [key: number]: HTMLDivElement | null }>; } const { useBreakpoint } = Grid; export function MenuList({ data, categoryRefs }: MenuListProps) { const { isRTL } = useAppSelector((state) => state.locale); const products = data?.products; const { xs, md } = useBreakpoint(); const { items } = useAppSelector((state) => state.order); const restaurantName = localStorage.getItem("restaurantName"); const navigate = useNavigate(); const { t } = useTranslation(); const { themeName } = useAppSelector((state) => state.theme); // Show error state if data exists but has no products if (data && (!data.products || data.products.length === 0)) { return (
{t("menu.noMenuItemsAvailable")}
); } // Group products by category const productsByCategory = products?.reduce((acc, product) => { if (product.categoryId && !acc[product?.categoryId]) { acc[product?.categoryId] = []; } acc[product?.categoryId || 0].push(product); return acc; }, {} as Record); return ( <>
{data?.categories?.map((category) => { const categoryProducts = productsByCategory?.[category.id] || []; if (categoryProducts.length === 0) return null; return (
{ if (categoryRefs.current) { categoryRefs.current[category.id] = el; } }} style={{ marginBottom: "1rem" }} > {isRTL ? category.name : category.name}
{categoryProducts.map((item: Product) => (
{ localStorage.setItem("product", JSON.stringify(item)); navigate(`/${restaurantName}/product/${item.id}`); }} >
{isRTL ? item.name : item.nameOther} {item.description && ( {item.description} )}
{item.original_price !== item.price && ( )}
{items.find((i) => i.id === item.id) && ( i.id === item.id)?.quantity } className={ styles.cartBadge + " " + (isRTL ? styles.cartBadgeRTL : styles.cartBadgeLTR) } style={{ backgroundColor: colors.primary, }} title={`${ items.find((i) => i.id === item.id)?.quantity } in cart`} /> )}
))}
); })}
); }