import React, { useMemo, useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { MobileShell } from '../../components/layout/MobileShell'; import { BottomNavBar } from '../../components/layout/BottomNavBar'; import { StickyFooterBar } from '../../components/layout/StickyFooterBar'; import { MainCartItemCard } from '../../components/checkout/CartItemCard'; import type { SummaryLineRow } from '../../components/checkout/OrderSummaryRows'; import { CartItemsShimmer, InlineSpinner, } from '../../components/common/ShimmerSkeletons'; import { OrderSummarySheet } from '../../components/modals/OrderSummarySheet'; import { useShopApp } from '../../context/ShopAppContext'; import { CART_ITEMS_MAIN, type CartItemData } from '../../data/checkoutData'; import { mapCartItemToCard } from '../../lib/mappers'; import { isBareMode, ROUTES, USE_MOCK_DATA } from '../../lib/routes'; interface InteractiveCartItem extends CartItemData { qty: number; unitPrice: number; oldUnitPrice: number; } const createInitialBareItems = (): InteractiveCartItem[] => CART_ITEMS_MAIN.map((item) => { const unitPrice = parseFloat(item.price.replace(/[^0-9.]/g, '')) || 24; return { ...item, qty: parseInt(item.quantity, 10) || 1, unitPrice, oldUnitPrice: 28.5, }; }); /** * Node 4129:17527 — `shop-cart` (Shopping Cart, 375×814) */ export const ShopCartPage: React.FC = () => { const navigate = useNavigate(); const location = useLocation(); const isBare = isBareMode(location.search); const useMockFallback = isBare || USE_MOCK_DATA; const { hydrated, requireAuth, cart, isCartBusy, updateCartItemQuantity, removeCartItem, clearCart, applyPromoCode, addToCart, } = useShopApp(); const [bareItems, setBareItems] = useState( createInitialBareItems ); const [hasInteractedBare, setHasInteractedBare] = useState(false); const [isSummaryOpen, setIsSummaryOpen] = useState(false); const liveItems = cart?.items || []; const livePricing = cart?.pricing; const resolvedLiveQty = livePricing?.total_quantity ?? livePricing?.total_units ?? liveItems.reduce((sum, item) => sum + item.quantity, 0); const resolvedShopsCount = livePricing?.shops_count || new Set(liveItems.map((item) => item.store_id)).size; const bareTotalQty = bareItems.reduce((sum, item) => sum + item.qty, 0); const bareTotalPrice = bareItems.reduce( (sum, item) => sum + item.unitPrice * item.qty, 0 ); const bareTotalOldPrice = bareItems.reduce( (sum, item) => sum + item.oldUnitPrice * item.qty, 0 ); const useFigmaSnapshot = isBare && !hasInteractedBare; const hasAnyItems = isBare ? bareItems.length > 0 : liveItems.length > 0; const topInfoText = useFigmaSnapshot ? '3 item from 2 Shops' : isBare ? `${bareTotalQty} item from ${bareItems.length} ${ bareItems.length === 1 ? 'Shop' : 'Shops' }` : `${resolvedLiveQty} item from ${resolvedShopsCount} ${ resolvedShopsCount === 1 ? 'Shop' : 'Shops' }`; const footerPrice = useFigmaSnapshot ? '$24.00' : isBare ? `$${bareTotalPrice.toFixed(2)}` : livePricing?.formatted_total || '$0.00'; const footerOldPrice = useFigmaSnapshot ? '$28.50' : isBare ? bareTotalQty > 0 ? `$${bareTotalOldPrice.toFixed(2)}` : undefined : livePricing && Number(livePricing.discount_amount) > 0 ? livePricing.formatted_subtotal : undefined; const footerTotalItemsLabel = useFigmaSnapshot ? 'Total 4 item' : isBare ? `Total ${bareTotalQty} item` : `Total ${resolvedLiveQty} item`; const summaryRows = useMemo(() => { if (isBare) { return [ { id: 'row-items', label: `Items (${bareTotalQty})`, value: `$${bareTotalPrice.toFixed(2)}`, }, ...bareItems.map((it) => ({ id: `row-${it.id}`, label: `${it.title} (×${it.qty})`, value: `$${(it.unitPrice * it.qty).toFixed(2)}`, })), ]; } const rows: SummaryLineRow[] = [ { id: 'row-items', label: `Items (${resolvedLiveQty})`, value: livePricing?.formatted_subtotal || '$0.00', }, ...liveItems.map((it) => ({ id: `row-${it.id}`, label: `${it.title} (×${it.quantity})`, value: it.formatted_line_total, })), ]; if (livePricing && Number(livePricing.discount_amount) > 0) { rows.push({ id: 'row-discount', label: `Promo (${livePricing.promo_code})`, value: `-${livePricing.formatted_discount}`, }); } return rows; }, [bareItems, bareTotalPrice, bareTotalQty, isBare, liveItems, livePricing]); const summaryTotalText = isBare ? `$${bareTotalPrice.toFixed(2)}` : livePricing?.formatted_total || '$0.00'; const handleClearOrRestore = async () => { if (isBare) { setHasInteractedBare(true); setBareItems((prev) => (prev.length > 0 ? [] : createInitialBareItems())); return; } if (liveItems.length > 0) { await clearCart(); } else { await addToCart('islam-in-the-bible-book', 1); } }; return ( {/* Main Cart Content */}
{/* Top Info Bar */}
{topInfoText} {!isBare && isCartBusy && }
{(hasAnyItems || useMockFallback) && (
{ if (!isCartBusy) { void handleClearOrRestore(); } }} role="button" aria-label={hasAnyItems ? 'Clear all' : 'Add sample item'} style={{ fontFamily: 'var(--font-sans)', fontWeight: 700, fontSize: 10, lineHeight: '13.9px', color: hasAnyItems ? '#d44747' : '#d78e06', textAlign: 'right', cursor: 'pointer', whiteSpace: 'nowrap', userSelect: 'none', }} > {hasAnyItems ? 'Clear all' : isBare ? 'Restore' : '+ Add Sample'}
)}
{/* Cart Item Cards */} {!isBare && !hydrated ? ( ) : hasAnyItems ? (
{isBare ? bareItems.map((item, idx) => { const displayCardItem: CartItemData = { ...item, y: 69 + idx * 133, quantity: String(item.qty), price: useFigmaSnapshot ? item.price : `$${(item.unitPrice * item.qty).toFixed(2)}`, }; return ( { setHasInteractedBare(true); setBareItems((prev) => prev.map((it) => it.id === id ? { ...it, qty: it.qty + 1 } : it ) ); }} onDecrement={(id) => { setHasInteractedBare(true); setBareItems((prev) => prev .map((it) => it.id === id ? { ...it, qty: it.qty - 1 } : it ) .filter((it) => it.qty > 0) ); }} onRemove={(id) => { setHasInteractedBare(true); setBareItems((prev) => prev.filter((it) => it.id !== id)); }} onClickItem={() => navigate(ROUTES.product())} onClickOrigin={() => navigate(ROUTES.store())} /> ); }) : liveItems.map((item, idx) => ( { void updateCartItemQuantity(item.id, item.quantity + 1); }} onDecrement={() => { if (item.quantity <= 1) { void removeCartItem(item.id); } else { void updateCartItemQuantity(item.id, item.quantity - 1); } }} onRemove={() => { void removeCartItem(item.id); }} onClickItem={() => navigate(ROUTES.product(item.product_slug || item.product_id)) } onClickOrigin={() => navigate(ROUTES.store(item.store_slug || item.store_id)) } /> ))}
) : (
Your cart is empty
Browse verified Muslim booths and add products to start your order.
)}
{/* Sticky Bottom Stack: Checkout Bar + Bottom Navigation Bar */}
setIsSummaryOpen(true)} buttonText="checkout" buttonFontWeight={700} iconSrc="/assets/images/icon-credit-card-white.svg" targetRoute={ROUTES.checkoutShipping} onButtonClick={() => { if (isBare) { navigate(ROUTES.checkoutShipping); return; } void requireAuth({ featureName: 'checkout', onAuthenticated: () => navigate(ROUTES.checkoutShipping), }); }} disabled={!hasAnyItems || isCartBusy} />
setIsSummaryOpen(false)} rows={summaryRows} totalText={summaryTotalText} promoCode={cart?.promo_code || ''} onApplyPromo={isBare ? undefined : applyPromoCode} />
); };