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 () => {
|
||||
setLoadingPricing(true);
|
||||
try {
|
||||
|
|
@ -512,11 +528,15 @@ export default function App() {
|
|||
setPricingCalc(calcData);
|
||||
} catch (err: any) {
|
||||
console.error('Pricing engine calculation error:', err);
|
||||
const errorMsg = err.response?.data?.detail || 'Invalid or expired coupon code.';
|
||||
setPricingCalc((prev: any) => ({
|
||||
...(prev || {}),
|
||||
coupon_error: errorMsg
|
||||
}));
|
||||
if (appliedCoupon) {
|
||||
const errorMsg = err.response?.data?.detail || 'Invalid or expired coupon code.';
|
||||
setPricingCalc((prev: any) => ({
|
||||
...(prev || {}),
|
||||
coupon_error: errorMsg,
|
||||
}));
|
||||
} else {
|
||||
setPricingCalc(null);
|
||||
}
|
||||
} finally {
|
||||
setLoadingPricing(false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)}
|
||||
|
|
@ -173,7 +178,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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue