import React, { useState } from 'react' import { FaPlus, FaMinus } from 'react-icons/fa' import { BillingCycle, Product, ProductDto } from '@/proxy/order/models' import { CartState } from '@/utils/cartUtils' import { useLocalization } from '@/utils/hooks/useLocalization' interface ProductCardProps { product: ProductDto globalBillingCycle: BillingCycle globalPeriod: number addItem: (product: ProductDto, quantity: number, billingCycle: BillingCycle) => void cartState: CartState } export const ProductCard: React.FC = ({ product, globalBillingCycle, globalPeriod, addItem, cartState, }) => { const [quantity, setQuantity] = useState(1) const { translate } = useLocalization() // Direct check with current cart state const isInCart = cartState.items.some( (item) => item.product.id === product.id && item.billingCycle === globalBillingCycle, ) const formatPrice = (price: number) => { return new Intl.NumberFormat('tr-TR', { style: 'currency', currency: 'TRY', minimumFractionDigits: 0, }).format(price) } const getCurrentPrice = () => { return globalBillingCycle === 'monthly' ? product.monthlyPrice! : product.yearlyPrice! } const getTotalPrice = () => { return getCurrentPrice() * quantity * globalPeriod } const handleAddToCart = () => { // Non-quantity products should not be added if already in cart if (!product.isQuantityBased && isInCart) { return } // For non-quantity products, always use quantity 1 const quantityToAdd = !product.isQuantityBased ? 1 : quantity addItem(product, quantityToAdd, globalBillingCycle) setQuantity(1) } const getUnitText = () => { switch (product.unit) { case 'monthly': return globalBillingCycle === 'monthly' ? translate('::Public.Products.MonthPeriod') : translate('::Public.Products.YearPeriod') case 'yearly': return translate('::Public.Products.YearPeriod') default: return '' } } const getPeriodText = () => { const periodText = globalBillingCycle === 'monthly' ? translate('::Public.products.billingcycle.month') : translate('::Public.products.billingcycle.year') return globalPeriod > 1 ? ` (${globalPeriod} ${periodText})` : '' } // Check if product has valid price for current billing cycle const hasValidPrice = () => { if (globalBillingCycle === 'monthly' && !product.monthlyPrice) return false if (globalBillingCycle === 'yearly' && !product.yearlyPrice) return false return true } // If product doesn't have valid price, don't render it at all if (!hasValidPrice()) { return null } return (
{product.imageUrl && (
{product.name}
)}
{translate('::' + product.category)}

{translate('::' + product.name)}

{translate('::' + product.description)}

{/* Quantity and Yearly Savings above price */}
{product.isQuantityBased && (
{translate('::Public.products.quantity')}
{quantity}
)} {globalBillingCycle === 'yearly' && product.monthlyPrice && product.yearlyPrice && (
{translate('::Public.products.savings.yearly', { percent: Math.round((1 - product.yearlyPrice / (product.monthlyPrice * 12)) * 100), })}
)}
{/* Bottom section with consistent height */}
{formatPrice(getCurrentPrice())} {getUnitText()}
{globalPeriod > 1 && (
{translate('::Public.payment.summary.total')} {formatPrice(getTotalPrice())} {getPeriodText()}
)}
{(() => { const isNonQuantityProduct = !product.isQuantityBased const isDisabled = isNonQuantityProduct && isInCart return ( ) })()}
) }