issue 5 : fix - ensure dynamic currency price display and prevent stale cart calculations #23
30
src/App.tsx
30
src/App.tsx
|
|
@ -495,6 +495,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 () => {
|
const calculatePricing = async () => {
|
||||||
setLoadingPricing(true);
|
setLoadingPricing(true);
|
||||||
try {
|
try {
|
||||||
|
|
@ -512,11 +528,15 @@ export default function App() {
|
||||||
setPricingCalc(calcData);
|
setPricingCalc(calcData);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error('Pricing engine calculation error:', err);
|
console.error('Pricing engine calculation error:', err);
|
||||||
const errorMsg = err.response?.data?.detail || 'Invalid or expired coupon code.';
|
if (appliedCoupon) {
|
||||||
setPricingCalc((prev: any) => ({
|
const errorMsg = err.response?.data?.detail || 'Invalid or expired coupon code.';
|
||||||
...(prev || {}),
|
setPricingCalc((prev: any) => ({
|
||||||
coupon_error: errorMsg
|
...(prev || {}),
|
||||||
}));
|
coupon_error: errorMsg,
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
setPricingCalc(null);
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setLoadingPricing(false);
|
setLoadingPricing(false);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -102,11 +102,16 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-3">
|
<div className="flex items-center gap-3">
|
||||||
<span className="text-xs font-bold text-indigo-600">
|
<span className="text-xs font-bold text-indigo-600">
|
||||||
{currency === 'INR'
|
{(() => {
|
||||||
? `₹${item.priceInr}`
|
const calculatedItem = pricingCalc?.currency === currency
|
||||||
: currency === 'EUR'
|
? pricingCalc?.cart_items?.find((ci: any) => ci.code === item.code)
|
||||||
? `€${item.priceEur}`
|
: null;
|
||||||
: `$${item.priceUsd}`}
|
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>
|
</span>
|
||||||
<button
|
<button
|
||||||
onClick={() => toggleCartItem(item)}
|
onClick={() => toggleCartItem(item)}
|
||||||
|
|
@ -173,7 +178,13 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* Checkout Summary Calculations */}
|
{/* 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="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">
|
<div className="flex justify-between text-xs text-slate-600 font-bold">
|
||||||
<span>Base Subtotal</span>
|
<span>Base Subtotal</span>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue