import React, { useMemo, useState } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { MobileShell } from '../../components/layout/MobileShell'; import { StandardPageHeader } from '../../components/layout/TopHeader'; import { StickyFooterBar } from '../../components/layout/StickyFooterBar'; import { ReviewItemCard } from '../../components/checkout/ReviewItemCard'; import { OrderSummaryRows, type SummaryLineRow, } from '../../components/checkout/OrderSummaryRows'; import { EmptyStateBox } from '../../components/common/ShimmerSkeletons'; import { PaymentMethodsSheet } from '../../components/modals/PaymentMethodsSheet'; import { useShopApp } from '../../context/ShopAppContext'; import { PAYMENT_METHODS_LIST, PAYMENT_REVIEW_ITEMS, type PaymentMethodItem, } from '../../data/checkoutData'; import { resolveMediaUrl } from '../../lib/http'; import { mapCartItemToReview } from '../../lib/mappers'; import { formatPhoneForDisplay, isValidPhoneString } from '../../lib/phone'; import { isBareMode, ROUTES, USE_MOCK_DATA } from '../../lib/routes'; import { usePaymentMethodsQuery, usePlaceOrderMutation, } from '../../lib/shopQueries'; /** * Node 4160:11203 — `shop-payment` (Review & Confirm, 375×814) */ export const ShopPaymentPage: React.FC = () => { const navigate = useNavigate(); const location = useLocation(); const isBare = isBareMode(location.search); const useMockFallback = isBare || USE_MOCK_DATA; const { isUserAuthenticated, requireAuth, cart, selectedAddress, selectedShippingMethod, selectedPaymentMethod, setSelectedPaymentMethod, showToast, } = useShopApp(); const [paymentSheetOpen, setPaymentSheetOpen] = useState(false); const paymentMethodsQuery = usePaymentMethodsQuery(!isBare); const placeOrderMutation = usePlaceOrderMutation(); const paymentMethodsData = paymentMethodsQuery.data; const submitting = placeOrderMutation.isPending; const paymentMethods = useMemo(() => { if (!isBare && paymentMethodsData?.results?.length) { return paymentMethodsData.results.map((pm, idx) => ({ id: pm.code || pm.id, relY: idx * 69, label: pm.label, iconSrc: resolveMediaUrl(pm.icon_url) || '/assets/images/icon-payment-google.svg', selected: (pm.code || pm.id) === selectedPaymentMethod, })); } return useMockFallback ? PAYMENT_METHODS_LIST : []; }, [ isBare, paymentMethodsData, selectedPaymentMethod, useMockFallback, ]); const livePricing = cart?.pricing; const { rows, totalText } = useMemo(() => { if (isBare || !livePricing) { return useMockFallback ? { rows: undefined, totalText: '$24.00', } : { rows: [], totalText: '$0.00', }; } const subtotalNum = Number(livePricing.subtotal) || 0; const shipNum = Number(selectedShippingMethod?.price || 0); const totalNum = Math.max(0, subtotalNum + shipNum); const computedRows: SummaryLineRow[] = [ { id: 'row-items', label: `Items (${livePricing.total_quantity ?? livePricing.total_units ?? 0})`, value: livePricing.formatted_subtotal, }, { id: 'row-shipping', label: selectedShippingMethod?.title || (useMockFallback ? 'Standard shipping' : 'Shipping'), value: selectedShippingMethod?.formatted_price || (useMockFallback ? '$5.00' : '$0.00'), }, ]; return { rows: computedRows, totalText: `$${totalNum.toFixed(2)}`, }; }, [isBare, livePricing, selectedShippingMethod, useMockFallback]); const handleConfirmPurchase = async () => { if (isBare) { setPaymentSheetOpen(false); navigate(ROUTES.checkoutResult('ORD-28465')); return; } if (!isUserAuthenticated) { setPaymentSheetOpen(false); const authed = await requireAuth({ featureName: 'order payment', }); if (!authed) return; } if (!selectedAddress && !useMockFallback) { showToast('Please select a delivery address'); return; } if ( selectedAddress && !isValidPhoneString(selectedAddress.phone, selectedAddress.country) ) { showToast('Please enter a valid phone number for your delivery address'); navigate(ROUTES.checkoutShipping); return; } if (submitting) return; try { const order = await placeOrderMutation.mutateAsync({ address_id: selectedAddress?.id ?? null, shipping_address: selectedAddress ? undefined : { recipient_name: 'Ali Ghaderi', phone: '+12125550199', street_line: '250 Park Avenue South', city: 'New York', postal_code: '10003', country: 'United States', }, shipping_method: selectedShippingMethod?.code || 'standard', payment_method: selectedPaymentMethod || 'google_pay', }); setPaymentSheetOpen(false); navigate(ROUTES.checkoutResult(order.order_number), { replace: true }); } catch (err) { showToast(err instanceof Error ? err.message : 'Could not complete order'); } }; return ( {/* Review Sections Body */}
{/* Review Items Section */}
Review items
{!isBare && cart?.items?.length ? ( cart.items.map((item, idx) => ( )) ) : useMockFallback ? ( PAYMENT_REVIEW_ITEMS.map((item) => ( )) ) : ( )}
{/* Delivery Address Section */}
Delivery Address
{!isBare && ( navigate(ROUTES.checkoutShipping)} style={{ fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 11, color: '#d78e06', cursor: 'pointer', }} > Edit )}
{/* Read-only Address Card */} {!isBare && !selectedAddress && !useMockFallback ? ( ) : (
{!isBare && selectedAddress ? selectedAddress.recipient_name : 'Ali Ghaderi'}
{!isBare && selectedAddress?.phone && (
{formatPhoneForDisplay( selectedAddress.phone, selectedAddress.country )}
)}
{!isBare && selectedAddress ? selectedAddress.full_address || [ selectedAddress.street_line, selectedAddress.city, selectedAddress.postal_code, selectedAddress.country, ] .filter(Boolean) .join(', ') : '250 Park Avenue South, New York, NY 10003 United States'}
)}
{/* Order Summary Section */}
Orded Summery
{/* Summary Card */}
{/* Bottom Sticky Payment Bar */} { if (isBare) { setPaymentSheetOpen(true); return; } void requireAuth({ featureName: 'payment and checkout', onAuthenticated: () => setPaymentSheetOpen(true), }); }} /> setPaymentSheetOpen(false)} methods={paymentMethods} selectedMethodId={selectedPaymentMethod} onSelectMethod={setSelectedPaymentMethod} totalPriceText={totalText} onConfirmPurchase={() => { void handleConfirmPurchase(); }} isSubmitting={submitting} />
); };