396 lines
17 KiB
TypeScript
396 lines
17 KiB
TypeScript
import React from 'react';
|
||
import { ShoppingCart, ShoppingBag, Trash2, RefreshCw, CreditCard, XCircle } from 'lucide-react';
|
||
import type { ModuleItem, Currency, UserProfile, VerificationResult } from '../../types';
|
||
|
||
interface CartDrawerProps {
|
||
cart: ModuleItem[];
|
||
currency: Currency;
|
||
setCurrency: (c: Currency) => void;
|
||
toggleCartItem: (module: ModuleItem) => void;
|
||
couponCode: string;
|
||
setCouponCode: (code: string) => void;
|
||
appliedCoupon: string | null;
|
||
applyPromoCode: () => void;
|
||
removePromoCode: () => void;
|
||
pricingCalc: any;
|
||
loadingPricing: boolean;
|
||
loggedInUser: UserProfile | null;
|
||
onUpdateBillingPhone: () => void;
|
||
verificationResult: VerificationResult | null;
|
||
checkoutLoading: boolean;
|
||
handleCheckout: () => void;
|
||
}
|
||
|
||
export const CartDrawer: React.FC<CartDrawerProps> = ({
|
||
cart,
|
||
currency,
|
||
setCurrency,
|
||
toggleCartItem,
|
||
couponCode,
|
||
setCouponCode,
|
||
appliedCoupon,
|
||
applyPromoCode,
|
||
removePromoCode,
|
||
pricingCalc,
|
||
loadingPricing,
|
||
loggedInUser,
|
||
onUpdateBillingPhone,
|
||
verificationResult,
|
||
checkoutLoading,
|
||
handleCheckout,
|
||
}) => {
|
||
const fallbackCalc = cart.length > 0 ? (() => {
|
||
const subtotal = cart.reduce((acc, item) => {
|
||
const price = currency === 'INR' ? item.priceInr : currency === 'EUR' ? item.priceEur : item.priceUsd;
|
||
return acc + (Number(price) || 0);
|
||
}, 0);
|
||
return {
|
||
currency,
|
||
base_subtotal: subtotal,
|
||
bundle_discount_amount: 0,
|
||
coupon_discount_amount: 0,
|
||
total_discounts: 0,
|
||
convenience_fee_rate: 0,
|
||
convenience_fee: 0,
|
||
taxable_amount: 0,
|
||
gst_amount: 0,
|
||
grand_total: subtotal,
|
||
discount_policy: 'None',
|
||
coupon_applied: 'None',
|
||
coupon_error: null,
|
||
};
|
||
})() : null;
|
||
|
||
const activePricing = (pricingCalc && pricingCalc.currency === currency) ? pricingCalc : fallbackCalc;
|
||
|
||
return (
|
||
<div className="xl:col-span-1">
|
||
<div className="sticky top-12 bg-white border border-slate-200 rounded-3xl p-6 shadow-xl space-y-6">
|
||
<div className="flex justify-between items-center border-b border-slate-100 pb-4">
|
||
<h3 className="text-lg font-bold text-slate-800 flex items-center gap-2 min-w-0">
|
||
<ShoppingCart className="h-5 w-5 text-indigo-500 flex-shrink-0" />
|
||
<span className="truncate">Cart Summary</span>
|
||
</h3>
|
||
<span className="text-xs font-bold bg-indigo-50 text-indigo-600 px-2.5 py-0.5 rounded-full border border-indigo-100 flex-shrink-0">
|
||
{cart.length} {cart.length === 1 ? 'Item' : 'Items'}
|
||
</span>
|
||
</div>
|
||
|
||
{/* Currency selector toggle */}
|
||
<div className="flex flex-col sm:flex-row gap-2 sm:items-center justify-between">
|
||
<span className="text-xs font-bold text-slate-500">Billing Currency:</span>
|
||
<div className="flex bg-slate-100 rounded-lg p-0.5 border border-slate-200 self-end sm:self-auto flex-shrink-0">
|
||
<button
|
||
onClick={() => setCurrency('INR')}
|
||
className={`px-3 py-1 rounded-md text-xs font-bold transition-all duration-300 ${currency === 'INR'
|
||
? 'bg-white text-slate-900 shadow-sm border border-slate-200/50'
|
||
: 'text-slate-500 hover:text-slate-800'
|
||
}`}
|
||
>
|
||
INR (₹)
|
||
</button>
|
||
<button
|
||
onClick={() => setCurrency('USD')}
|
||
className={`px-3 py-1 rounded-md text-xs font-bold transition-all duration-300 ${currency === 'USD'
|
||
? 'bg-white text-slate-900 shadow-sm border border-slate-200/50'
|
||
: 'text-slate-500 hover:text-slate-800'
|
||
}`}
|
||
>
|
||
USD ($)
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
{/* Selected modules list */}
|
||
{cart.length === 0 ? (
|
||
<div className="text-center py-10 space-y-3">
|
||
<div className="h-12 w-12 rounded-2xl bg-slate-50 border border-slate-200/60 flex items-center justify-center mx-auto text-slate-400">
|
||
<ShoppingBag className="h-5 w-5" />
|
||
</div>
|
||
<p className="text-xs text-slate-500 max-w-[200px] mx-auto leading-relaxed font-semibold">
|
||
Your checkout cart is empty. Select from curriculum.
|
||
</p>
|
||
</div>
|
||
) : (
|
||
<div className="space-y-3 max-h-[220px] overflow-y-auto pr-1">
|
||
{cart.map((item) => (
|
||
<div
|
||
key={item.code}
|
||
className="flex justify-between items-center gap-4 bg-slate-50 border border-slate-200/60 p-3 rounded-2xl"
|
||
>
|
||
<div className="flex-1 min-w-0">
|
||
<p className="text-xs font-bold text-slate-800 truncate">{item.name}</p>
|
||
<p className="text-[9px] text-slate-400 tracking-wider uppercase font-semibold">{item.category}</p>
|
||
</div>
|
||
<div className="flex items-center gap-3">
|
||
<span className="text-xs font-bold text-indigo-600">
|
||
{(() => {
|
||
const calculatedItem = activePricing?.cart_items?.find((ci: any) => ci.code === item.code);
|
||
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)}
|
||
className="h-7 w-7 rounded-lg bg-rose-50 text-rose-500 hover:bg-rose-100 flex items-center justify-center transition-colors"
|
||
title="Remove item"
|
||
>
|
||
<Trash2 className="h-3.5 w-3.5" />
|
||
</button>
|
||
</div>
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
|
||
{/* Coupon Code Input */}
|
||
{cart.length > 0 && (
|
||
<div className="space-y-2">
|
||
<label className="text-xs font-bold text-slate-500">Promo / Coupon Code</label>
|
||
<div className="flex gap-2">
|
||
<input
|
||
type="text"
|
||
value={couponCode}
|
||
onChange={(e) => setCouponCode(e.target.value)}
|
||
placeholder="e.g. AGENT40, EVENT_NEW"
|
||
className="flex-1 bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 placeholder:text-slate-400 uppercase"
|
||
/>
|
||
<button
|
||
onClick={applyPromoCode}
|
||
className="bg-indigo-600 hover:bg-indigo-500 transition-colors text-white font-bold text-xs px-4 py-2 rounded-xl shadow shadow-indigo-600/10"
|
||
>
|
||
Apply
|
||
</button>
|
||
</div>
|
||
{appliedCoupon &&
|
||
(pricingCalc?.coupon_error || !pricingCalc?.coupon_applied || pricingCalc?.coupon_applied === 'None' || (pricingCalc?.coupon_discount_amount !== undefined && pricingCalc?.coupon_discount_amount <= 0) ? (
|
||
<div className="flex justify-between items-center bg-rose-50 border border-rose-200 text-rose-700 text-xs px-3 py-2 rounded-xl font-bold">
|
||
<span className="flex items-center gap-1">
|
||
⚠️ {pricingCalc?.coupon_error || 'Invalid or expired coupon code.'}
|
||
</span>
|
||
<button
|
||
onClick={removePromoCode}
|
||
className="text-rose-700 hover:text-rose-900 font-bold ml-2 cursor-pointer"
|
||
>
|
||
Remove
|
||
</button>
|
||
</div>
|
||
) : (
|
||
<div className="flex justify-between items-center bg-emerald-50 border border-emerald-200 text-emerald-700 text-xs px-3 py-2 rounded-xl font-bold">
|
||
<span>
|
||
Coupon Active: {appliedCoupon} ✓{' '}
|
||
{pricingCalc?.coupon_applied && pricingCalc.coupon_applied !== 'None'
|
||
? `(${pricingCalc.coupon_applied.replace(/Code: \w+ - /, '')})`
|
||
: ''}
|
||
</span>
|
||
<button
|
||
onClick={removePromoCode}
|
||
className="text-emerald-700 hover:text-rose-600 font-bold ml-2 cursor-pointer"
|
||
>
|
||
Remove
|
||
</button>
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
|
||
{/* Checkout Summary Calculations */}
|
||
{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>
|
||
)}
|
||
{activePricing && 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>
|
||
<span>
|
||
{currency === 'INR'
|
||
? `₹${activePricing.base_subtotal}`
|
||
: currency === 'EUR'
|
||
? `€${activePricing.base_subtotal}`
|
||
: `$${activePricing.base_subtotal}`}
|
||
</span>
|
||
</div>
|
||
{activePricing.renewal_discount_amount > 0 && (
|
||
<div className="flex justify-between text-xs text-emerald-700 font-bold">
|
||
<span>Early Renewal Discount (20%)</span>
|
||
<span>
|
||
-{currency === 'INR'
|
||
? `₹${activePricing.renewal_discount_amount}`
|
||
: currency === 'EUR'
|
||
? `€${activePricing.renewal_discount_amount}`
|
||
: `$${activePricing.renewal_discount_amount}`}
|
||
</span>
|
||
</div>
|
||
)}
|
||
{activePricing.bundle_discount_amount > 0 && (
|
||
<div className="flex justify-between text-xs text-emerald-700 font-bold">
|
||
<span>
|
||
{activePricing.discount_policy && activePricing.discount_policy !== 'None'
|
||
? (activePricing.discount_policy.startsWith('Staggered Buy:')
|
||
? activePricing.discount_policy.replace('Staggered Buy:', 'Loyalty Discount:')
|
||
: activePricing.discount_policy)
|
||
: 'Loyalty Discount'}
|
||
</span>
|
||
<span>
|
||
-{currency === 'INR'
|
||
? `₹${activePricing.bundle_discount_amount}`
|
||
: currency === 'EUR'
|
||
? `€${activePricing.bundle_discount_amount}`
|
||
: `$${activePricing.bundle_discount_amount}`}
|
||
</span>
|
||
</div>
|
||
)}
|
||
{activePricing.coupon_discount_amount > 0 && (
|
||
<div className="flex justify-between text-xs text-emerald-700 font-bold">
|
||
<span>Coupon Discount ({appliedCoupon})</span>
|
||
<span>
|
||
-{currency === 'INR'
|
||
? `₹${activePricing.coupon_discount_amount}`
|
||
: currency === 'EUR'
|
||
? `€${activePricing.coupon_discount_amount}`
|
||
: `$${activePricing.coupon_discount_amount}`}
|
||
</span>
|
||
</div>
|
||
)}
|
||
{activePricing.total_discounts > 0 &&
|
||
!activePricing.renewal_discount_amount &&
|
||
!activePricing.bundle_discount_amount &&
|
||
!activePricing.coupon_discount_amount && (
|
||
<div className="flex justify-between text-xs text-emerald-700 font-bold">
|
||
<span>Total Discounts</span>
|
||
<span>
|
||
-{currency === 'INR'
|
||
? `₹${activePricing.total_discounts}`
|
||
: currency === 'EUR'
|
||
? `€${activePricing.total_discounts}`
|
||
: `$${activePricing.total_discounts}`}
|
||
</span>
|
||
</div>
|
||
)}
|
||
{activePricing.convenience_fee > 0 && (
|
||
<div className="flex justify-between text-xs text-slate-600 font-bold">
|
||
<span>Gateway fee ({activePricing.convenience_fee_rate}%)</span>
|
||
<span>
|
||
{currency === 'INR'
|
||
? `₹${activePricing.convenience_fee}`
|
||
: currency === 'EUR'
|
||
? `€${activePricing.convenience_fee}`
|
||
: `$${activePricing.convenience_fee}`}
|
||
</span>
|
||
</div>
|
||
)}
|
||
{activePricing.gst_amount > 0 && (
|
||
<div className="space-y-0.5">
|
||
<div className="flex justify-between text-xs text-slate-600 font-bold">
|
||
<span>GST (18%)</span>
|
||
<span>
|
||
{currency === 'INR'
|
||
? `₹${activePricing.gst_amount}`
|
||
: currency === 'EUR'
|
||
? `€${activePricing.gst_amount}`
|
||
: `$${activePricing.gst_amount}`}
|
||
</span>
|
||
</div>
|
||
<div className="flex justify-between text-[10px] text-slate-400 font-semibold italic">
|
||
<span>
|
||
(Taxable Amount:{' '}
|
||
{currency === 'INR'
|
||
? `₹${activePricing.taxable_amount}`
|
||
: currency === 'EUR'
|
||
? `€${activePricing.taxable_amount}`
|
||
: `$${activePricing.taxable_amount}`}
|
||
)
|
||
</span>
|
||
<span></span>
|
||
</div>
|
||
</div>
|
||
)}
|
||
<div className="h-px bg-slate-200 my-2" />
|
||
<div className="flex justify-between items-center text-sm font-bold text-slate-800">
|
||
<span>Total Chargeable</span>
|
||
<span className="text-indigo-600 text-lg font-extrabold">
|
||
{currency === 'INR'
|
||
? `₹${activePricing.grand_total}`
|
||
: currency === 'EUR'
|
||
? `€${activePricing.grand_total}`
|
||
: `$${activePricing.grand_total}`}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{/* Billing Details Prefill Summary */}
|
||
{loggedInUser && cart.length > 0 && (
|
||
<div className="bg-slate-50 border border-slate-200/60 rounded-2xl p-4 space-y-3 my-2 text-left">
|
||
<div className="flex justify-between items-center">
|
||
<span className="text-[10px] uppercase font-black tracking-wider text-slate-400">
|
||
Billing Profile
|
||
</span>
|
||
<button
|
||
onClick={onUpdateBillingPhone}
|
||
className="text-[9px] text-indigo-600 hover:text-indigo-500 font-black uppercase tracking-wider underline focus:outline-none"
|
||
>
|
||
{loggedInUser.phone_number ? 'Edit Number' : 'Add Number'}
|
||
</button>
|
||
</div>
|
||
<div className="space-y-1">
|
||
<div className="flex justify-between text-xs font-semibold text-slate-600">
|
||
<span>Email:</span>
|
||
<span className="text-slate-800 font-bold truncate max-w-[180px]">{loggedInUser.email}</span>
|
||
</div>
|
||
<div className="flex justify-between text-xs font-semibold text-slate-600">
|
||
<span>Phone:</span>
|
||
<span className="text-slate-800 font-bold">
|
||
{loggedInUser.phone_number || (
|
||
<span className="text-rose-500 font-black italic">Required for checkout</span>
|
||
)}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{/* Verification failure result badge */}
|
||
{verificationResult && verificationResult.status === 'error' && (
|
||
<div className="p-4 rounded-2xl border flex gap-3 bg-rose-50 border-rose-200 text-rose-700">
|
||
<XCircle className="h-5 w-5 flex-shrink-0" />
|
||
<div className="space-y-1">
|
||
<p className="text-xs font-black uppercase">Verification Failed</p>
|
||
<p className="text-[11px] leading-relaxed font-bold">{verificationResult.message}</p>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{/* Checkout Trigger */}
|
||
{cart.length > 0 && (
|
||
<div className="space-y-4 pt-2">
|
||
<button
|
||
onClick={handleCheckout}
|
||
disabled={checkoutLoading || loadingPricing}
|
||
className="w-full h-12 bg-indigo-600 hover:bg-indigo-500 disabled:bg-slate-200 disabled:text-slate-400 transition-all duration-300 text-white font-bold text-sm rounded-2xl shadow-xl shadow-indigo-600/10 flex items-center justify-center gap-2"
|
||
>
|
||
{checkoutLoading ? (
|
||
<RefreshCw className="h-4 w-4 animate-spin" />
|
||
) : (
|
||
<>
|
||
<CreditCard className="h-4 w-4" /> Secure Payment with Razorpay
|
||
</>
|
||
)}
|
||
</button>
|
||
|
||
<p className="text-[10px] text-slate-400 text-center leading-relaxed font-semibold">
|
||
* Integrated with Razorpay Gateway API (Test Mode).
|
||
</p>
|
||
</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
};
|