From 24ce3a44bfe6cfd5edd78d4a4a70557e5dc0afc6 Mon Sep 17 00:00:00 2001 From: parsa aghaei Date: Tue, 21 Jul 2026 12:52:05 +0330 Subject: [PATCH] fix(calculator): enhance dosage calculator reactivity for logged-in and guest users - Automatically prefill pet weight and age if user has a registered pet profile - Provide dynamic weight-based dosage calculation fallback for all products when browsing as guest - Update daily dose, duration, and package longevity reactively upon slider and age button interactions --- .../application/components/ProductPage.tsx | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) 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,