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
This commit is contained in:
1 parent
57cab6cbc2
commit
24ce3a44bf
1 file changed
+22
-18
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user