fix: Resolve calculateDosage runtime TypeError in CheckoutPage

This commit is contained in:
parsa aghaei committed 2026-07-25 11:42:00 +03:30
1 parent 6638eaa10a
commit 897867c633
1 file changed
+10 -5
@@ -20,7 +20,7 @@ import { toPersian, cn } from "../lib/utils";
import { useCartStore } from "../lib/store/cartStore";
import { usePetStore } from "../lib/store/usePetStore";
import { useUserStore } from "../lib/store/userStore";
import { Product } from "../lib/data/products";
import { Product, PRODUCTS } from "../lib/data/products";
import { productService } from "../lib/services/productService";
import { toast } from "sonner";
import { useRouter } from 'next/navigation';
@@ -458,10 +458,15 @@ export default function CheckoutPage() {
<div className="space-y-2">
{items.map(item => {
const activePet = usePetStore.getState().getActivePet();
// Re-hydrate product to ensure methods like calculateDosage exist
const fullProduct = dbProducts.find(p => p.id === item.product.id) || item.product;
const dose = activePet ? fullProduct.calculateDosage(activePet.weight, activePet.age <= 1) : { quantity: 1 };
const days = Math.floor(fullProduct.packageSize / (dose.quantity || 1));
// Re-hydrate product to ensure methods like calculateDosage exist from static config
const staticProduct = PRODUCTS.find(p => p.id === item.product.id || p.artNo === item.product.artNo);
const calculateFn = staticProduct?.calculateDosage || item.product.calculateDosage;
const dose = activePet && typeof calculateFn === 'function'
? calculateFn(activePet.weight, activePet.age <= 1)
: { quantity: 1 };
const days = Math.floor((staticProduct?.packageSize || item.product.packageSize || 50) / (dose.quantity || 1));
return (
<div key={item.product.id} className="flex items-center justify-between text-[10px] font-medium font-vazir">