fix(cart): ensure dynamic currency price display and prevent stale cart calculations

This commit is contained in:
ShivaniKhaire 2026-10-05 15:46:35 +05:30
parent 776e09a918
commit 2f4b694e27
2 changed files with 34 additions and 6 deletions

View File

@ -456,6 +456,22 @@ export default function App() {
}
};
// Keep cart items dynamic prices updated when publicPlans are loaded
useEffect(() => {
if (publicPlans.length > 0 && cart.length > 0) {
setCart(prev =>
prev.map(item => {
const live = getModulePrice(item);
return {
...item,
priceInr: live.priceInr,
priceUsd: live.priceUsd,
};
})
);
}
}, [publicPlans]);
const calculatePricing = async () => {
setLoadingPricing(true);
try {
@ -469,6 +485,7 @@ export default function App() {
setPricingCalc(res.data);
} catch (err) {
console.error('Pricing engine calculation error:', err);
setPricingCalc(null);
} finally {
setLoadingPricing(false);
}

View File

@ -102,11 +102,16 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
</div>
<div className="flex items-center gap-3">
<span className="text-xs font-bold text-indigo-600">
{currency === 'INR'
? `₹${item.priceInr}`
: currency === 'EUR'
? `€${item.priceEur}`
: `$${item.priceUsd}`}
{(() => {
const calculatedItem = pricingCalc?.currency === currency
? pricingCalc?.cart_items?.find((ci: any) => ci.code === item.code)
: null;
const price = calculatedItem
? calculatedItem.price
: (currency === 'INR' ? item.priceInr : currency === 'EUR' ? item.priceEur : item.priceUsd);
const symbol = currency === 'INR' ? '₹' : currency === 'EUR' ? '€' : '$';
return `${symbol}${price}`;
})()}
</span>
<button
onClick={() => toggleCartItem(item)}
@ -171,7 +176,13 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
)}
{/* Checkout Summary Calculations */}
{pricingCalc && cart.length > 0 && (
{loadingPricing && (!pricingCalc || pricingCalc.currency !== currency) && (
<div className="flex items-center justify-center p-6 bg-slate-50 border border-slate-200/60 rounded-2xl gap-2 text-xs font-semibold text-slate-500">
<RefreshCw className="h-4 w-4 animate-spin text-indigo-600" />
<span>Calculating {currency} pricing...</span>
</div>
)}
{pricingCalc && pricingCalc.currency === currency && cart.length > 0 && (
<div className="space-y-3 bg-slate-50 border border-slate-200/60 p-4 rounded-2xl">
<div className="flex justify-between text-xs text-slate-600 font-bold">
<span>Base Subtotal</span>