feat(checkout): display available promotional coupons with one-click apply
This commit is contained in:
parent
bad9a52b96
commit
7db6748ffb
2 changed files with 137 additions and 27 deletions
|
|
@ -20,8 +20,10 @@ import {
|
||||||
initiatePayment,
|
initiatePayment,
|
||||||
cancelPayment,
|
cancelPayment,
|
||||||
verifyPayment,
|
verifyPayment,
|
||||||
|
fetchAvailableCoupons,
|
||||||
type CustomerAddress,
|
type CustomerAddress,
|
||||||
type CheckoutCreateResponse,
|
type CheckoutCreateResponse,
|
||||||
|
type AvailableCoupon,
|
||||||
} from '@/services/api/checkoutService';
|
} from '@/services/api/checkoutService';
|
||||||
import { deliveryService, DeliverySettings } from '@/services/api/deliveryService';
|
import { deliveryService, DeliverySettings } from '@/services/api/deliveryService';
|
||||||
import GoogleAuthModal from '@/components/auth/GoogleAuthModal';
|
import GoogleAuthModal from '@/components/auth/GoogleAuthModal';
|
||||||
|
|
@ -91,6 +93,8 @@ export default function CheckoutPage() {
|
||||||
const [appliedCoupon, setAppliedCoupon] = useState<any | null>(null);
|
const [appliedCoupon, setAppliedCoupon] = useState<any | null>(null);
|
||||||
const [appliedCouponDiscount, setAppliedCouponDiscount] = useState<number>(0);
|
const [appliedCouponDiscount, setAppliedCouponDiscount] = useState<number>(0);
|
||||||
const [couponApplying, setCouponApplying] = useState(false);
|
const [couponApplying, setCouponApplying] = useState(false);
|
||||||
|
const [availableCoupons, setAvailableCoupons] = useState<AvailableCoupon[]>([]);
|
||||||
|
const [loadingCoupons, setLoadingCoupons] = useState(false);
|
||||||
|
|
||||||
const [addressForm, setAddressForm] = useState({
|
const [addressForm, setAddressForm] = useState({
|
||||||
address_type: 'SHIPPING',
|
address_type: 'SHIPPING',
|
||||||
|
|
@ -116,7 +120,9 @@ export default function CheckoutPage() {
|
||||||
// Coupon discount deduction
|
// Coupon discount deduction
|
||||||
let couponDiscount = 0;
|
let couponDiscount = 0;
|
||||||
if (appliedCoupon) {
|
if (appliedCoupon) {
|
||||||
if (appliedCoupon.discount_type === 'PERCENT') {
|
if (appliedCoupon.discount_amount !== undefined && appliedCoupon.discount_amount !== null) {
|
||||||
|
couponDiscount = Number(appliedCoupon.discount_amount);
|
||||||
|
} else if (appliedCoupon.discount_type === 'PERCENT' || appliedCoupon.discount_type === 'PERCENTAGE') {
|
||||||
couponDiscount = (subtotal * Number(appliedCoupon.discount_value || 0)) / 100;
|
couponDiscount = (subtotal * Number(appliedCoupon.discount_value || 0)) / 100;
|
||||||
if (appliedCoupon.max_discount_cap) {
|
if (appliedCoupon.max_discount_cap) {
|
||||||
couponDiscount = Math.min(couponDiscount, Number(appliedCoupon.max_discount_cap));
|
couponDiscount = Math.min(couponDiscount, Number(appliedCoupon.max_discount_cap));
|
||||||
|
|
@ -125,6 +131,9 @@ export default function CheckoutPage() {
|
||||||
couponDiscount = Number(appliedCoupon.discount_value || 0);
|
couponDiscount = Number(appliedCoupon.discount_value || 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (appliedCouponDiscount > 0 && couponDiscount === 0) {
|
||||||
|
couponDiscount = appliedCouponDiscount;
|
||||||
|
}
|
||||||
couponDiscount = Math.min(subtotal, Math.max(0, couponDiscount));
|
couponDiscount = Math.min(subtotal, Math.max(0, couponDiscount));
|
||||||
|
|
||||||
const postDiscountSubtotal = Math.max(0, subtotal - couponDiscount);
|
const postDiscountSubtotal = Math.max(0, subtotal - couponDiscount);
|
||||||
|
|
@ -170,6 +179,16 @@ export default function CheckoutPage() {
|
||||||
creditService.getMyWallet().then(setWallet).catch(() => null);
|
creditService.getMyWallet().then(setWallet).catch(() => null);
|
||||||
}, [isAuthenticated, authReady]);
|
}, [isAuthenticated, authReady]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setLoadingCoupons(true);
|
||||||
|
fetchAvailableCoupons()
|
||||||
|
.then((coupons) => {
|
||||||
|
setAvailableCoupons(coupons || []);
|
||||||
|
})
|
||||||
|
.catch(() => {})
|
||||||
|
.finally(() => setLoadingCoupons(false));
|
||||||
|
}, [isAuthenticated, authReady]);
|
||||||
|
|
||||||
const syncAndValidateCart = async () => {
|
const syncAndValidateCart = async () => {
|
||||||
if (!isAuthenticated) return;
|
if (!isAuthenticated) return;
|
||||||
setErrorMsg(null);
|
setErrorMsg(null);
|
||||||
|
|
@ -369,12 +388,13 @@ export default function CheckoutPage() {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleApplyCoupon = async () => {
|
const handleApplyCoupon = async (customCode?: string) => {
|
||||||
const code = couponCodeInput.trim().toUpperCase();
|
const code = (customCode || couponCodeInput).trim().toUpperCase();
|
||||||
if (!code) {
|
if (!code) {
|
||||||
toast.error('Please enter a coupon code');
|
toast.error('Please enter a coupon code');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
setCouponCodeInput(code);
|
||||||
setCouponApplying(true);
|
setCouponApplying(true);
|
||||||
try {
|
try {
|
||||||
await syncCartItems(
|
await syncCartItems(
|
||||||
|
|
@ -385,12 +405,12 @@ export default function CheckoutPage() {
|
||||||
);
|
);
|
||||||
const res = await apiFetch<any>('/api/v1/cart/coupon/apply', {
|
const res = await apiFetch<any>('/api/v1/cart/coupon/apply', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ coupon_code: code }),
|
body: JSON.stringify({ code, coupon_code: code }),
|
||||||
});
|
});
|
||||||
if (res && res.coupon) {
|
if (res && res.coupon) {
|
||||||
setAppliedCoupon(res.coupon);
|
setAppliedCoupon(res.coupon);
|
||||||
setAppliedCouponDiscount(Number(res.discount || 0));
|
setAppliedCouponDiscount(Number(res.discount || res.discount_amount || 0));
|
||||||
toast.success(`Coupon ${code} applied successfully! Saved ₹${Number(res.discount || 0).toFixed(0)}`);
|
toast.success(`Coupon ${code} applied successfully! Saved ₹${Number(res.discount || res.discount_amount || 0).toFixed(0)}`);
|
||||||
} else {
|
} else {
|
||||||
toast.error('Could not apply coupon');
|
toast.error('Could not apply coupon');
|
||||||
}
|
}
|
||||||
|
|
@ -1206,8 +1226,9 @@ export default function CheckoutPage() {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Promo Code Input or Applied Badge */}
|
{/* Promo Code Input or Applied Badge */}
|
||||||
<div className="pt-2 border-t border-gray-100 space-y-2">
|
<div className="pt-2 border-t border-gray-100 space-y-2.5">
|
||||||
{!appliedCoupon ? (
|
{!appliedCoupon ? (
|
||||||
|
<>
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<div className="relative flex-1">
|
<div className="relative flex-1">
|
||||||
<Tag className="w-3.5 h-3.5 text-gray-400 absolute left-3 top-1/2 -translate-y-1/2" />
|
<Tag className="w-3.5 h-3.5 text-gray-400 absolute left-3 top-1/2 -translate-y-1/2" />
|
||||||
|
|
@ -1221,19 +1242,89 @@ export default function CheckoutPage() {
|
||||||
</div>
|
</div>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={handleApplyCoupon}
|
onClick={() => handleApplyCoupon()}
|
||||||
disabled={couponApplying || !couponCodeInput.trim()}
|
disabled={couponApplying || !couponCodeInput.trim()}
|
||||||
className="bg-primary hover:brightness-95 text-white font-bold px-3 py-2 rounded-lg text-[12px] transition cursor-pointer disabled:opacity-50"
|
className="bg-primary hover:brightness-95 text-white font-bold px-3 py-2 rounded-lg text-[12px] transition cursor-pointer disabled:opacity-50"
|
||||||
>
|
>
|
||||||
{couponApplying ? 'Applying...' : 'Apply'}
|
{couponApplying ? 'Applying...' : 'Apply'}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Available Promotional Coupons List */}
|
||||||
|
{availableCoupons.length > 0 && (
|
||||||
|
<div className="pt-1.5 space-y-2">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span className="text-[11px] font-bold text-gray-700 uppercase tracking-wide flex items-center gap-1.5">
|
||||||
|
<Sparkles className="w-3.5 h-3.5 text-primary" /> Available Offers & Coupons
|
||||||
|
</span>
|
||||||
|
<span className="text-[10px] font-bold text-primary bg-primary/10 px-2 py-0.5 rounded-full">
|
||||||
|
{availableCoupons.length} {availableCoupons.length === 1 ? 'offer' : 'offers'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-2 max-h-56 overflow-y-auto pr-0.5">
|
||||||
|
{availableCoupons.map((c) => {
|
||||||
|
const isEligible = c.is_eligible !== false;
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={c.coupon_id}
|
||||||
|
className={`p-2.5 rounded-lg border transition-all ${
|
||||||
|
isEligible
|
||||||
|
? 'border-dashed border-primary/40 bg-primary/5 hover:border-primary hover:bg-primary/10'
|
||||||
|
: 'border-gray-200 bg-gray-50/70 opacity-60'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<div className="flex items-start justify-between gap-2">
|
||||||
|
<div className="min-w-0 flex-1">
|
||||||
|
<div className="flex flex-wrap items-center gap-1.5">
|
||||||
|
<span className="font-mono font-bold text-[12px] tracking-wider text-primary px-1.5 py-0.5 bg-white border border-primary/30 rounded">
|
||||||
|
{c.code}
|
||||||
|
</span>
|
||||||
|
<span className="text-[11px] font-bold text-gray-800">
|
||||||
|
{c.discount_type === 'PERCENTAGE' || (c.discount_type as string) === 'PERCENT'
|
||||||
|
? `${c.discount_value}% OFF`
|
||||||
|
: `₹${c.discount_value} OFF`}
|
||||||
|
{c.max_discount_cap ? ` (Up to ₹${c.max_discount_cap})` : ''}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<p className="text-[11px] text-gray-600 mt-1 line-clamp-2">
|
||||||
|
{c.description || c.title}
|
||||||
|
</p>
|
||||||
|
<div className="flex flex-wrap items-center gap-2 mt-1 text-[10px] text-gray-500">
|
||||||
|
{c.min_order_amount > 0 && (
|
||||||
|
<span>Min spend: ₹{c.min_order_amount}</span>
|
||||||
|
)}
|
||||||
|
{c.first_order_only && (
|
||||||
|
<span className="text-amber-700 font-semibold">★ First Order Only</span>
|
||||||
|
)}
|
||||||
|
{!isEligible && c.ineligibility_reason && (
|
||||||
|
<span className="text-red-500 font-medium">({c.ineligibility_reason})</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
disabled={couponApplying || !isEligible}
|
||||||
|
onClick={() => handleApplyCoupon(c.code)}
|
||||||
|
className="shrink-0 px-2.5 py-1 text-[11px] font-bold text-white bg-primary hover:brightness-95 rounded-md cursor-pointer transition disabled:opacity-40 disabled:cursor-not-allowed shadow-xs"
|
||||||
|
>
|
||||||
|
{couponApplying && couponCodeInput === c.code ? 'Applying...' : 'Apply'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
) : (
|
) : (
|
||||||
<div className="p-2.5 bg-emerald-50 border border-emerald-200 rounded-lg flex items-center justify-between text-[12px]">
|
<div className="p-2.5 bg-emerald-50 border border-emerald-200 rounded-lg flex items-center justify-between text-[12px]">
|
||||||
<div className="flex items-center gap-1.5">
|
<div className="flex items-center gap-1.5">
|
||||||
<Tag className="w-3.5 h-3.5 text-emerald-600" />
|
<Tag className="w-3.5 h-3.5 text-emerald-600" />
|
||||||
<span className="font-bold font-mono text-emerald-900">{appliedCoupon.code}</span>
|
<span className="font-bold font-mono text-emerald-900">{appliedCoupon.code}</span>
|
||||||
<span className="text-[11px] text-emerald-700">
|
<span className="text-[11px] text-emerald-700 font-semibold">
|
||||||
(-{formatCurrency(couponDiscount)})
|
(-{formatCurrency(couponDiscount)})
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,21 @@ export interface RazorpayInitiateResponse {
|
||||||
invoice_no?: string | null;
|
invoice_no?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AvailableCoupon {
|
||||||
|
coupon_id: string;
|
||||||
|
code: string;
|
||||||
|
title: string;
|
||||||
|
description?: string | null;
|
||||||
|
discount_type: 'PERCENTAGE' | 'FIXED' | 'FREE_SHIPPING';
|
||||||
|
discount_value: number;
|
||||||
|
max_discount_cap?: number | null;
|
||||||
|
min_order_amount: number;
|
||||||
|
first_order_only: boolean;
|
||||||
|
valid_to?: string | null;
|
||||||
|
is_eligible: boolean;
|
||||||
|
ineligibility_reason?: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
export async function syncCartItems(items: SyncCartItem[]) {
|
export async function syncCartItems(items: SyncCartItem[]) {
|
||||||
return apiFetch<{ message: string; cart_id: string }>('/api/v1/cart/items', {
|
return apiFetch<{ message: string; cart_id: string }>('/api/v1/cart/items', {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
|
|
@ -75,6 +90,10 @@ export async function fetchCustomerAddresses() {
|
||||||
return apiFetch<CustomerAddress[]>('/api/v1/customer/profile/addresses');
|
return apiFetch<CustomerAddress[]>('/api/v1/customer/profile/addresses');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function fetchAvailableCoupons() {
|
||||||
|
return apiFetch<AvailableCoupon[]>('/api/v1/storefront/coupons');
|
||||||
|
}
|
||||||
|
|
||||||
export async function createCustomerAddress(address: Omit<CustomerAddress, 'address_id'> & { address_id?: string }) {
|
export async function createCustomerAddress(address: Omit<CustomerAddress, 'address_id'> & { address_id?: string }) {
|
||||||
return apiFetch<CustomerAddress>('/api/v1/customer/profile/addresses', {
|
return apiFetch<CustomerAddress>('/api/v1/customer/profile/addresses', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue