From 897867c633ea11aef595f3fb025afa3b65f1cb92 Mon Sep 17 00:00:00 2001 From: parsa aghaei Date: Sat, 25 Jul 2026 11:42:00 +0330 Subject: [PATCH] fix: Resolve calculateDosage runtime TypeError in CheckoutPage --- frontend/application/components/CheckoutPage.tsx | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/frontend/application/components/CheckoutPage.tsx b/frontend/application/components/CheckoutPage.tsx index a8991afb..0d24b600 100644 --- a/frontend/application/components/CheckoutPage.tsx +++ b/frontend/application/components/CheckoutPage.tsx @@ -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() {
{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 (