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.
453 lines
14 KiB
453 lines
14 KiB
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<InteractiveCartItem[]>(
|
|
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<SummaryLineRow[]>(() => {
|
|
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 (
|
|
<MobileShell width={375} height={814} backgroundColor="#f3f1ef">
|
|
{/* Main Cart Content */}
|
|
<div
|
|
style={{
|
|
flex: 1,
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
padding: 'calc(42px + var(--safe-top, 0px)) var(--page-px) 24px',
|
|
gap: 13,
|
|
}}
|
|
>
|
|
{/* Top Info Bar */}
|
|
<div
|
|
style={{
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'space-between',
|
|
gap: 12,
|
|
padding: '0 2px',
|
|
}}
|
|
>
|
|
<div
|
|
style={{
|
|
display: 'inline-flex',
|
|
alignItems: 'center',
|
|
gap: 6,
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 700,
|
|
fontSize: 10,
|
|
lineHeight: '13.9px',
|
|
color: '#292a2e',
|
|
}}
|
|
>
|
|
<span>{topInfoText}</span>
|
|
{!isBare && isCartBusy && <InlineSpinner size={12} gold />}
|
|
</div>
|
|
|
|
{(hasAnyItems || useMockFallback) && (
|
|
<div
|
|
onClick={() => {
|
|
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'}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* Cart Item Cards */}
|
|
{!isBare && !hydrated ? (
|
|
<CartItemsShimmer count={2} />
|
|
) : hasAnyItems ? (
|
|
<div
|
|
style={{
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
gap: 10,
|
|
}}
|
|
>
|
|
{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 (
|
|
<MainCartItemCard
|
|
key={item.id}
|
|
item={displayCardItem}
|
|
onIncrement={(id) => {
|
|
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) => (
|
|
<MainCartItemCard
|
|
key={item.id}
|
|
item={mapCartItemToCard(item, idx)}
|
|
onIncrement={() => {
|
|
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))
|
|
}
|
|
/>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<div
|
|
style={{
|
|
marginTop: 16,
|
|
padding: '32px 20px',
|
|
borderRadius: 20,
|
|
background: 'linear-gradient(180deg, #ffffff 0%, #f8f8f8 100%)',
|
|
border: '1px solid #ffffff',
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
alignItems: 'center',
|
|
textAlign: 'center',
|
|
gap: 12,
|
|
}}
|
|
>
|
|
<div
|
|
style={{
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 700,
|
|
fontSize: 16,
|
|
lineHeight: '22px',
|
|
color: '#111111',
|
|
}}
|
|
>
|
|
Your cart is empty
|
|
</div>
|
|
<div
|
|
style={{
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 400,
|
|
fontSize: 12,
|
|
lineHeight: '18px',
|
|
color: '#747474',
|
|
}}
|
|
>
|
|
Browse verified Muslim booths and add products to start your order.
|
|
</div>
|
|
<div
|
|
style={{
|
|
display: 'flex',
|
|
flexWrap: 'wrap',
|
|
justifyContent: 'center',
|
|
gap: 10,
|
|
marginTop: 8,
|
|
}}
|
|
>
|
|
<button
|
|
type="button"
|
|
onClick={() => navigate(ROUTES.home)}
|
|
style={{
|
|
padding: '10px 18px',
|
|
borderRadius: 11,
|
|
backgroundColor: '#d78e06',
|
|
color: '#ffffff',
|
|
border: 'none',
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 700,
|
|
fontSize: 12,
|
|
cursor: 'pointer',
|
|
}}
|
|
>
|
|
Browse Shop
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={() => navigate(ROUTES.categories)}
|
|
style={{
|
|
padding: '10px 18px',
|
|
borderRadius: 11,
|
|
backgroundColor: '#efece9',
|
|
color: '#292a2e',
|
|
border: '1px solid #e8e4df',
|
|
fontFamily: 'var(--font-sans)',
|
|
fontWeight: 600,
|
|
fontSize: 12,
|
|
cursor: 'pointer',
|
|
}}
|
|
>
|
|
Explore Categories
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* Sticky Bottom Stack: Checkout Bar + Bottom Navigation Bar */}
|
|
<div
|
|
style={{
|
|
position: 'sticky',
|
|
bottom: 0,
|
|
zIndex: 25,
|
|
width: '100%',
|
|
marginTop: 'auto',
|
|
}}
|
|
>
|
|
<StickyFooterBar
|
|
aboveBottomNav={true}
|
|
height={104}
|
|
price={footerPrice}
|
|
oldPrice={footerOldPrice}
|
|
totalItemsLabel={footerTotalItemsLabel}
|
|
onTotalItemsClick={() => 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}
|
|
/>
|
|
|
|
<BottomNavBar
|
|
activeTab="cart"
|
|
showCartBadge={hasAnyItems}
|
|
cartBadgeCount={isBare ? bareItems.length : liveItems.length}
|
|
/>
|
|
</div>
|
|
|
|
<OrderSummarySheet
|
|
isOpen={isSummaryOpen}
|
|
onClose={() => setIsSummaryOpen(false)}
|
|
rows={summaryRows}
|
|
totalText={summaryTotalText}
|
|
promoCode={cart?.promo_code || ''}
|
|
onApplyPromo={isBare ? undefined : applyPromoCode}
|
|
/>
|
|
</MobileShell>
|
|
);
|
|
};
|