import React from 'react'; import { View, Alert, TouchableOpacity, Text } from 'react-native'; import { Image } from 'expo-image'; import { tw, theme, MyText, MyTouchableOpacity, Quantifier, MiniQuantifier } from 'common-ui'; import CartIcon from '@/components/icons/CartIcon'; import MaterialIcons from '@expo/vector-icons/MaterialIcons'; import Ionicons from '@expo/vector-icons/Ionicons'; import dayjs from 'dayjs'; import { useGetCart, useUpdateCartItem, useRemoveFromCart, useAddToCart, } from '@/hooks/cart-query-hooks'; import { useProductSlotIdentifier } from '@/hooks/useProductSlotIdentifier'; interface ProductCardProps { item: any; itemWidth: number; onPress?: () => void; showDeliveryInfo?: boolean; miniView?: boolean; nullIfNotAvailable?: boolean; containerComp?: React.ComponentType | React.JSXElementConstructor; } const formatQuantity = (quantity: number, unit: string): { value: string; display: string } => { if (unit?.toLowerCase() === 'kg' && quantity < 1) { return { value: `${Math.round(quantity * 1000)} g`, display: `${Math.round(quantity * 1000)}g` }; } return { value: `${quantity} ${unit}(s)`, display: `${quantity}${unit}` }; }; const ProductCard: React.FC = ({ item, itemWidth, onPress, showDeliveryInfo = true, miniView = false, nullIfNotAvailable = false, containerComp: ContainerComp = React.Fragment, }) => { const { data: cartData } = useGetCart(); const { getQuickestSlot } = useProductSlotIdentifier(); const updateCartItem = useUpdateCartItem({ showSuccessAlert: false, showErrorAlert: false, refetchCart: true, }); const removeFromCart = useRemoveFromCart({ showSuccessAlert: false, showErrorAlert: false, refetchCart: true, }); const { addToCart = () => {} } = useAddToCart({ showSuccessAlert: false, showErrorAlert: false, refetchCart: true, }) || {}; // Find current quantity from cart data const cartItem = cartData?.items?.find((cartItem: any) => cartItem.productId === item.id); const quantity = cartItem?.quantity || 0; // Precompute the next slot and determine display out of stock status const slotId = getQuickestSlot(item.id); const displayIsOutOfStock = item.isOutOfStock || !slotId; // Return null if nullIfNotAvailable is true and the product is out of stock if (nullIfNotAvailable && displayIsOutOfStock) { return null; } const handleQuantityChange = (newQuantity: number) => { if (newQuantity === 0 && cartItem) { removeFromCart.mutate({ itemId: cartItem.id }); } else if (newQuantity === 1 && !cartItem) { const slotId = getQuickestSlot(item.id); if (!slotId) { Alert.alert("Error", "No available delivery slot for this product"); return; } addToCart(item.id, 1, slotId, () => {}); } else if (cartItem) { updateCartItem.mutate({ itemId: cartItem.id, quantity: newQuantity }); } }; return ( {/* TODO: Navigate to product detail */})} activeOpacity={0.9} > {displayIsOutOfStock && ( Out of Stock )} {miniView && ( {quantity > 0 ? ( ) : ( handleQuantityChange(1)} activeOpacity={0.8} > )} )} {item.name} ₹{item.price} {item.marketPrice && Number(item.marketPrice) > Number(item.price) && ( ₹{item.marketPrice} )} Quantity: {formatQuantity(item.productQuantity || 1, item.unitNotation).display} {showDeliveryInfo && item.nextDeliveryDate && ( {dayjs(item.nextDeliveryDate).format("ddd, DD MMM • h:mm A")} )} {!miniView && ( <> {displayIsOutOfStock ? ( Unavailable ) : quantity > 0 ? ( ) : ( handleQuantityChange(1)} > Add to Cart )} )} ); }; export default ProductCard;