import React, { useEffect, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { Link, useNavigate } from 'react-router-dom'; import { Trash2, Plus, Minus, ArrowRight, ShieldCheck, ShoppingBag, Truck } from 'lucide-react'; import { fetchCart, updateCartItemAsync, removeCartItemAsync, applyCouponAsync } from '../../store/slices/cartSlice'; import { showToast } from '../../store/slices/uiSlice'; import { formatINR } from '../../utils/formatCurrency'; const CartPage = () => { const dispatch = useDispatch(); const navigate = useNavigate(); const { items, coupon } = useSelector((state) => state.cart); const [couponCode, setCouponCode] = useState(''); useEffect(() => { dispatch(fetchCart()); }, [dispatch]); const subtotal = items.reduce((acc, item) => { const itemPrice = item.product?.salePrice || item.product?.price || item.price; return acc + itemPrice * item.quantity; }, 0); const discountAmount = coupon ? (coupon.discountType === 'percentage' ? (subtotal * coupon.discountValue) / 100 : coupon.discountValue) : 0; const freeShippingThreshold = 5000; // ₹5,000 threshold for India const shippingFee = subtotal >= freeShippingThreshold || subtotal === 0 ? 0 : 499; const totalAmount = Math.max(0, subtotal - discountAmount + shippingFee); const freeShippingProgress = Math.min(100, (subtotal / freeShippingThreshold) * 100); const handleApplyCoupon = (e) => { e.preventDefault(); if (couponCode.trim()) { dispatch(applyCouponAsync(couponCode.trim())) .unwrap() .then(() => dispatch(showToast({ message: 'Coupon applied successfully!', type: 'success' }))) .catch((err) => dispatch(showToast({ message: err || 'Invalid coupon', type: 'error' }))); } }; if (items.length === 0) { return (
Discover hand-crafted custom shop electric, acoustic, and bass guitars.
Explore Guitar Catalog