// src/components/store/ProductCard.jsx import { useNavigate } from 'react-router-dom'; import ReactMarkdown from 'react-markdown'; import { ShoppingCart, CheckCircle2 } from 'lucide-react'; import { parsePeriod, getCategoryBadge } from './StoreHelpers'; export default function ProductCard({ product, selectedPeriod, categoryName }) { const navigate = useNavigate(); const getPricingData = () => { if (product.pricing?.type !== 'recurrent' || !product.pricing?.recurrent) { const oncePrice = product.pricing?.once?.price ? parseFloat(product.pricing.once.price).toFixed(2) : '0.00'; return { isAvailable: true, displayPrice: oncePrice, suffix: '(Une fois)', originalPrice: null, savingsPercent: 0, isOnce: true }; } const recurrentPrices = product.pricing.recurrent; const availablePeriods = Object.keys(recurrentPrices).filter( period => recurrentPrices[period].enabled == 1 || recurrentPrices[period].enabled === true ); if (!recurrentPrices[selectedPeriod] || !availablePeriods.includes(selectedPeriod)) return { isAvailable: false }; const currentPrice = parseFloat(recurrentPrices[selectedPeriod].price); const currentPeriodInfo = parsePeriod(selectedPeriod); const currentYearlyCost = currentPrice * currentPeriodInfo.factorToYear; const currentMonthlyEquivalent = currentYearlyCost / 12; let savingsPercent = 0; let originalPrice = null; const sortedAvailablePeriods = [...availablePeriods].sort((a, b) => parsePeriod(a).weight - parsePeriod(b).weight); const basePeriodCode = sortedAvailablePeriods[0]; if (basePeriodCode !== selectedPeriod) { const basePrice = parseFloat(recurrentPrices[basePeriodCode].price); const basePeriodInfo = parsePeriod(basePeriodCode); const baseYearlyCost = basePrice * basePeriodInfo.factorToYear; const baseMonthlyEquivalent = baseYearlyCost / 12; if (baseYearlyCost > currentYearlyCost) { savingsPercent = Math.round((1 - (currentYearlyCost / baseYearlyCost)) * 100); originalPrice = baseMonthlyEquivalent.toFixed(2); } } return { isAvailable: true, displayPrice: currentMonthlyEquivalent.toFixed(2), suffix: '/ mois', originalPrice, savingsPercent, billingPrice: currentPrice.toFixed(2), billingPhrase: currentPeriodInfo.billingPhrase, isOnce: false }; }; const priceData = getPricingData(); return (