You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
426 lines
13 KiB
426 lines
13 KiB
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<PaymentMethodItem[]>(() => {
|
|
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 (
|
|
<MobileShell width={375} height={814} backgroundColor="#f3f1ef">
|
|
<StandardPageHeader
|
|
title="Review & Confirm"
|
|
titleTop={29}
|
|
showBack={!isBare}
|
|
backTo={ROUTES.checkoutShipping}
|
|
/>
|
|
|
|
{/* Review Sections Body */}
|
|
<div
|
|
style={{
|
|
flex: 1,
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
padding: '24px var(--page-px) 24px',
|
|
gap: 28,
|
|
}}
|
|
>
|
|
{/* Review Items Section */}
|
|
<section style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
|
|
<img
|
|
src="/assets/images/icon-checkbox-checked-gold.svg"
|
|
alt=""
|
|
style={{
|
|
width: 24,
|
|
height: 24,
|
|
flexShrink: 0,
|
|
}}
|
|
/>
|
|
<span
|
|
style={{
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 700,
|
|
fontSize: 16,
|
|
lineHeight: '20px',
|
|
color: '#111111',
|
|
}}
|
|
>
|
|
Review items
|
|
</span>
|
|
</div>
|
|
|
|
<div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
|
|
{!isBare && cart?.items?.length ? (
|
|
cart.items.map((item, idx) => (
|
|
<ReviewItemCard
|
|
key={item.id}
|
|
item={mapCartItemToReview(item, idx)}
|
|
/>
|
|
))
|
|
) : useMockFallback ? (
|
|
PAYMENT_REVIEW_ITEMS.map((item) => (
|
|
<ReviewItemCard key={item.id} item={item} />
|
|
))
|
|
) : (
|
|
<EmptyStateBox message="Your cart is empty." compact />
|
|
)}
|
|
</div>
|
|
</section>
|
|
|
|
{/* Delivery Address Section */}
|
|
<section style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
|
|
<div
|
|
style={{
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'space-between',
|
|
gap: 6,
|
|
}}
|
|
>
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
|
|
<img
|
|
src="/assets/images/icon-location-pin-gold.svg"
|
|
alt=""
|
|
style={{
|
|
width: 24,
|
|
height: 24,
|
|
flexShrink: 0,
|
|
}}
|
|
/>
|
|
<span
|
|
style={{
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 700,
|
|
fontSize: 16,
|
|
lineHeight: '20px',
|
|
color: '#111111',
|
|
}}
|
|
>
|
|
Delivery Address
|
|
</span>
|
|
</div>
|
|
|
|
{!isBare && (
|
|
<span
|
|
onClick={() => navigate(ROUTES.checkoutShipping)}
|
|
style={{
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 600,
|
|
fontSize: 11,
|
|
color: '#d78e06',
|
|
cursor: 'pointer',
|
|
}}
|
|
>
|
|
Edit
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
{/* Read-only Address Card */}
|
|
{!isBare && !selectedAddress && !useMockFallback ? (
|
|
<EmptyStateBox message="No delivery address selected." compact />
|
|
) : (
|
|
<div
|
|
style={{
|
|
width: '100%',
|
|
minHeight: 74,
|
|
padding: 12,
|
|
borderRadius: 15,
|
|
backgroundColor: '#ffffff',
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
gap: 8,
|
|
}}
|
|
>
|
|
<div
|
|
style={{
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 700,
|
|
fontSize: 12,
|
|
lineHeight: '17px',
|
|
color: '#292a2e',
|
|
}}
|
|
>
|
|
{!isBare && selectedAddress
|
|
? selectedAddress.recipient_name
|
|
: 'Ali Ghaderi'}
|
|
</div>
|
|
{!isBare && selectedAddress?.phone && (
|
|
<div
|
|
dir="ltr"
|
|
style={{
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 600,
|
|
fontSize: 11,
|
|
lineHeight: '15px',
|
|
color: '#73889d',
|
|
}}
|
|
>
|
|
{formatPhoneForDisplay(
|
|
selectedAddress.phone,
|
|
selectedAddress.country
|
|
)}
|
|
</div>
|
|
)}
|
|
<div
|
|
style={{
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 400,
|
|
fontSize: 12,
|
|
lineHeight: '18px',
|
|
color: '#3d3d3d',
|
|
wordBreak: 'break-word',
|
|
}}
|
|
>
|
|
{!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'}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</section>
|
|
|
|
{/* Order Summary Section */}
|
|
<section style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
|
|
<img
|
|
src="/assets/images/icon-location-pin-gold.svg"
|
|
alt=""
|
|
style={{
|
|
width: 24,
|
|
height: 24,
|
|
flexShrink: 0,
|
|
}}
|
|
/>
|
|
<span
|
|
style={{
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 700,
|
|
fontSize: 16,
|
|
lineHeight: '20px',
|
|
color: '#111111',
|
|
}}
|
|
>
|
|
Orded Summery
|
|
</span>
|
|
</div>
|
|
|
|
{/* Summary Card */}
|
|
<div
|
|
style={{
|
|
width: '100%',
|
|
padding: 12,
|
|
borderRadius: 15,
|
|
backgroundColor: '#ffffff',
|
|
}}
|
|
>
|
|
<OrderSummaryRows
|
|
rows={rows}
|
|
totalText={isBare ? '$70.50' : totalText}
|
|
/>
|
|
</div>
|
|
</section>
|
|
</div>
|
|
|
|
{/* Bottom Sticky Payment Bar */}
|
|
<StickyFooterBar
|
|
height={90}
|
|
price={isBare ? '$24.00' : totalText}
|
|
topLabel="Total Cost"
|
|
buttonText="Payment"
|
|
buttonFontWeight={700}
|
|
iconSrc="/assets/images/icon-credit-card-white.svg"
|
|
targetRoute={ROUTES.checkoutReview}
|
|
disabled={!isBare && (!cart?.items || cart.items.length === 0)}
|
|
onButtonClick={() => {
|
|
if (isBare) {
|
|
setPaymentSheetOpen(true);
|
|
return;
|
|
}
|
|
void requireAuth({
|
|
featureName: 'payment and checkout',
|
|
onAuthenticated: () => setPaymentSheetOpen(true),
|
|
});
|
|
}}
|
|
/>
|
|
|
|
<PaymentMethodsSheet
|
|
isOpen={paymentSheetOpen}
|
|
onClose={() => setPaymentSheetOpen(false)}
|
|
methods={paymentMethods}
|
|
selectedMethodId={selectedPaymentMethod}
|
|
onSelectMethod={setSelectedPaymentMethod}
|
|
totalPriceText={totalText}
|
|
onConfirmPurchase={() => {
|
|
void handleConfirmPurchase();
|
|
}}
|
|
isSubmitting={submitting}
|
|
/>
|
|
</MobileShell>
|
|
);
|
|
};
|