diff --git a/frontend/application/components/ProductPage.tsx b/frontend/application/components/ProductPage.tsx index 7c4e6e23..19bcd79b 100644 --- a/frontend/application/components/ProductPage.tsx +++ b/frontend/application/components/ProductPage.tsx @@ -109,38 +109,42 @@ export default function ProductPage({ productSlug }: { productSlug: string }) { const apiProduct = allProducts.find(p => p.id === product.id) || product; if (!apiProduct) return null; const staticProduct = PRODUCTS.find(p => p.id === apiProduct.id || p.artNo === apiProduct.artNo); - if (staticProduct) { + if (staticProduct && typeof staticProduct.calculateDosage === 'function') { return { ...apiProduct, - calculateDosage: staticProduct.calculateDosage + calculateDosage: staticProduct.calculateDosage, + packageSize: apiProduct.packageSize || staticProduct.packageSize || 100 }; } - if (typeof apiProduct.calculateDosage !== 'function') { - return { - ...apiProduct, - calculateDosage: (w: number, y: boolean) => { - return { - quantity: 1, - unit: apiProduct.unit || "قرص", - description: "مصرف روزانه بر اساس دستور پزشک" - }; - } - }; - } - return apiProduct; + + // Dynamic weight-based calculation fallback for any product without custom logic + return { + ...apiProduct, + packageSize: apiProduct.packageSize || 100, + calculateDosage: (w: number, isYoung: boolean) => { + const factor = isYoung ? 0.2 : 0.1; + const baseQty = Math.max(1, Math.round(w * factor)); + return { + quantity: baseQty, + unit: apiProduct.unit?.includes('ml') ? "میلی‌لیتر" : apiProduct.unit?.includes('g') ? "گرم" : "قرص", + description: isYoung ? "دوز رشد روزانه" : "دوز نگهداری روزانه" + }; + } + }; }, [product, allProducts]); useEffect(() => { - if (activePet) { + if (activePet && activePet.weight > 0) { setWeight(activePet.weight); setPetType(activePet.age <= 1 ? "young" : "adult"); } - }, [activePet, setWeight, setPetType]); + }, [activePet]); const calculation = useMemo(() => { if (!fullProduct || typeof fullProduct.calculateDosage !== 'function') return null; const result = fullProduct.calculateDosage(weight, petType === "young"); - const duration = Math.floor(fullProduct.packageSize / result.quantity); + const packageSize = fullProduct.packageSize || 100; + const duration = Math.max(1, Math.floor(packageSize / (result.quantity || 1))); return { dailyDose: result.quantity,