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.
 
 
 
 
 

452 lines
14 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 { AddressCard, AddNewAddressCard } from '../../components/checkout/AddressCard';
import { EmptyStateBox } from '../../components/common/ShimmerSkeletons';
import { AddressFormSheet } from '../../components/modals/AddressFormSheet';
import { useShopApp } from '../../context/ShopAppContext';
import { BUY01_ADDRESSES } from '../../data/checkoutData';
import { mapAddressToOption } from '../../lib/mappers';
import { isBareMode, ROUTES, USE_MOCK_DATA } from '../../lib/routes';
import type { ApiAddress } from '../../lib/shopApi';
/**
* Node 4159:367 — `shop-buy01` (Shipping Details, 375×814)
*/
export const ShopBuy01Page: React.FC = () => {
const navigate = useNavigate();
const location = useLocation();
const isBare = isBareMode(location.search);
const useMockFallback = isBare || USE_MOCK_DATA;
const {
isUserAuthenticated,
requireAuth,
addresses,
selectedAddressId,
selectAddress,
saveAddress,
shippingMethods,
selectedShippingMethod,
selectShippingMethod,
cart,
showToast,
} = useShopApp();
const [bareSelectedId, setBareSelectedId] = useState('addr-1');
const [addressModalOpen, setAddressModalOpen] = useState(false);
const [editingAddress, setEditingAddress] = useState<ApiAddress | null>(null);
const bareAddresses = useMemo(
() =>
BUY01_ADDRESSES.map((addr) => ({
...addr,
selected: addr.id === bareSelectedId,
})),
[bareSelectedId]
);
const liveAddressOptions = useMemo(
() =>
addresses.map((addr, idx) =>
mapAddressToOption(addr, selectedAddressId, idx)
),
[addresses, selectedAddressId]
);
const computedTotalText = useMemo(() => {
if (isBare || !cart?.pricing) return useMockFallback ? '$24.00' : '$0.00';
const subtotalNum = Number(cart.pricing.subtotal) || 0;
const discountNum = Number(cart.pricing.discount_amount) || 0;
const shipNum = Number(selectedShippingMethod?.price || 0);
const total = Math.max(0, subtotalNum - discountNum + shipNum);
return `$${total.toFixed(2)}`;
}, [cart?.pricing, isBare, selectedShippingMethod?.price, useMockFallback]);
const handleContinue = () => {
if (!isBare && !isUserAuthenticated) {
void requireAuth({
featureName: 'checkout',
onAuthenticated: () => navigate(ROUTES.checkoutReview),
});
return;
}
if (!isBare && addresses.length === 0) {
showToast('Please add a delivery address to continue');
setEditingAddress(null);
setAddressModalOpen(true);
return;
}
navigate(ROUTES.checkoutReview);
};
return (
<MobileShell width={375} height={814} backgroundColor="#f3f1ef">
<StandardPageHeader
title="Shipping Details"
titleTop={30}
backTo={ROUTES.cart}
/>
{/* Shipping Details Body */}
<div
style={{
flex: 1,
display: 'flex',
flexDirection: 'column',
padding: '24px var(--page-px) 24px',
gap: 32,
}}
>
{/* Delivery Address 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',
}}
>
Delivery Address
</span>
</div>
<div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
{isBare ? (
bareAddresses.map((address) => (
<AddressCard
key={address.id}
item={address}
variant="checkout"
onSelect={() => setBareSelectedId(address.id)}
/>
))
) : liveAddressOptions.length > 0 ? (
liveAddressOptions.map((option, idx) => (
<AddressCard
key={option.id}
item={option}
variant="checkout"
onSelect={() => selectAddress(addresses[idx].id)}
onChangeAddressClick={() => {
setEditingAddress(addresses[idx]);
setAddressModalOpen(true);
}}
/>
))
) : !useMockFallback ? (
<EmptyStateBox message="No saved delivery addresses yet." compact />
) : null}
<AddNewAddressCard
onClick={() => {
if (isBare) {
navigate(ROUTES.addresses);
} else {
setEditingAddress(null);
setAddressModalOpen(true);
}
}}
/>
</div>
</section>
{/* Delivery Method Section */}
<section style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
<div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
<img
src="/assets/images/icon-box-fill-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 method
</span>
</div>
{!isBare && shippingMethods.length > 0 ? (
<div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
{shippingMethods.map((method) => {
const isSelected = selectedShippingMethod?.code === method.code;
return (
<div
key={method.id}
onClick={() => selectShippingMethod(method.code)}
style={{
width: '100%',
minHeight: 56,
padding: 12,
borderRadius: 15,
background: isSelected
? 'linear-gradient(180deg, #fff7e9 0%, #fdf9f4 100%)'
: '#ffffff',
border: isSelected ? '1px solid #d78e06' : '1px solid transparent',
boxSizing: 'border-box',
display: 'flex',
alignItems: 'center',
gap: 14,
cursor: 'pointer',
}}
>
<div
style={{
width: 20,
height: 20,
borderRadius: 10,
backgroundColor: isSelected ? '#d78e06' : 'transparent',
border: isSelected ? undefined : '1px solid #dbdbdb',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexShrink: 0,
}}
>
{isSelected && (
<div
style={{
width: 8,
height: 8,
borderRadius: '50%',
backgroundColor: '#ffffff',
}}
/>
)}
</div>
<img
src="/assets/images/icon-delivery-truck-grey-24.svg"
alt=""
style={{
width: 24,
height: 24,
flexShrink: 0,
}}
/>
<div
style={{
flex: 1,
minWidth: 0,
display: 'flex',
flexDirection: 'column',
gap: 2,
}}
>
<div
style={{
fontFamily: 'var(--font-sans)',
fontWeight: 700,
fontSize: 12,
lineHeight: '17px',
color: '#36363c',
}}
>
{method.title}
</div>
<div
style={{
fontFamily: 'var(--font-sans)',
fontWeight: 400,
fontSize: 10,
lineHeight: '12.9px',
color: '#747474',
}}
>
{method.subtitle ||
`${method.estimated_days_min}-${method.estimated_days_max} business days`}
</div>
</div>
<div
style={{
fontFamily: 'var(--font-sans)',
fontWeight: 700,
fontSize: 14,
lineHeight: '19px',
color: '#292a2e',
flexShrink: 0,
}}
>
{method.formatted_price}
</div>
</div>
);
})}
</div>
) : useMockFallback ? (
<div
style={{
width: '100%',
minHeight: 56,
padding: 12,
borderRadius: 15,
background: 'linear-gradient(180deg, #fff7e9 0%, #fdf9f4 100%)',
border: '1px solid #d78e06',
boxSizing: 'border-box',
display: 'flex',
alignItems: 'center',
gap: 14,
}}
>
<div
style={{
width: 20,
height: 20,
borderRadius: 10,
backgroundColor: '#d78e06',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexShrink: 0,
}}
>
<div
style={{
width: 8,
height: 8,
borderRadius: '50%',
backgroundColor: '#ffffff',
}}
/>
</div>
<img
src="/assets/images/icon-delivery-truck-grey-24.svg"
alt=""
style={{
width: 24,
height: 24,
flexShrink: 0,
}}
/>
<div
style={{
flex: 1,
minWidth: 0,
display: 'flex',
flexDirection: 'column',
gap: 2,
}}
>
<div
style={{
fontFamily: 'var(--font-sans)',
fontWeight: 700,
fontSize: 12,
lineHeight: '17px',
color: '#36363c',
}}
>
Standard shiping
</div>
<div
style={{
fontFamily: 'var(--font-sans)',
fontWeight: 400,
fontSize: 10,
lineHeight: '12.9px',
color: '#747474',
}}
>
2-3 business day
</div>
</div>
<div
style={{
fontFamily: 'var(--font-sans)',
fontWeight: 700,
fontSize: 14,
lineHeight: '19px',
color: '#292a2e',
flexShrink: 0,
}}
>
$5,00
</div>
</div>
) : (
<EmptyStateBox message="No delivery methods available." compact />
)}
</section>
{/* Green Encryption Banner */}
<div
style={{
marginTop: 'auto',
width: '100%',
minHeight: 37,
padding: '8px 12px',
borderRadius: 12,
backgroundColor: 'rgba(14, 177, 60, 0.1)',
border: '1px solid #bce6c8',
boxSizing: 'border-box',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<span
style={{
fontFamily: 'var(--font-sans)',
fontWeight: 600,
fontSize: 12,
lineHeight: '17px',
color: '#0eb13c',
textAlign: 'center',
}}
>
Your information is encrypted during transmission.
</span>
</div>
</div>
{/* Bottom Sticky Continue Bar */}
<StickyFooterBar
height={90}
price={computedTotalText}
topLabel="Total Cost"
buttonText="Continue"
buttonFontWeight={700}
iconSrc="/assets/images/icon-arrow-right-white.svg"
iconPosition="right"
targetRoute={ROUTES.checkoutReview}
onButtonClick={handleContinue}
/>
<AddressFormSheet
isOpen={addressModalOpen}
onClose={() => setAddressModalOpen(false)}
initialAddress={editingAddress}
onSave={saveAddress}
/>
</MobileShell>
);
};