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.
203 lines
6.5 KiB
203 lines
6.5 KiB
import React, { useEffect, useMemo, useState } from 'react';
|
|
import { useLocation, useParams, useSearchParams } from 'react-router-dom';
|
|
import { MobileShell } from '../../components/layout/MobileShell';
|
|
import { StandardPageHeader } from '../../components/layout/TopHeader';
|
|
import { SearchBar } from '../../components/common/SearchBar';
|
|
import { GridProductCard } from '../../components/common/GridProductCard';
|
|
import {
|
|
EmptyStateBox,
|
|
ProductGridShimmer,
|
|
RetryBanner,
|
|
} from '../../components/common/ShimmerSkeletons';
|
|
import { TWO_COLUMN_GRID_PRODUCTS } from '../../data/catalogData';
|
|
import { useShopApp } from '../../context/ShopAppContext';
|
|
import { tr } from '../../lib/cookies';
|
|
import { mapProductToGridItem } from '../../lib/mappers';
|
|
import { isBareMode, ROUTES, USE_MOCK_DATA } from '../../lib/routes';
|
|
import {
|
|
useCategoryDetailQuery,
|
|
useProductsQuery,
|
|
} from '../../lib/shopQueries';
|
|
|
|
interface ShopCatItemsPageProps {
|
|
mode?: 'category' | 'search' | 'bookmarks';
|
|
}
|
|
|
|
function humanizeSlug(slug?: string): string {
|
|
if (!slug) return 'Category';
|
|
return slug
|
|
.replace(/-/g, ' ')
|
|
.replace(/\b\w/g, (ch) => ch.toUpperCase());
|
|
}
|
|
|
|
/**
|
|
* Node 4181:12817 — `shop-catitems` (Category Items, Search Results, Bookmarks, 375×814)
|
|
*/
|
|
export const ShopCatItemsPage: React.FC<ShopCatItemsPageProps> = ({
|
|
mode = 'category',
|
|
}) => {
|
|
const location = useLocation();
|
|
const isBare = isBareMode(location.search);
|
|
const useMockFallback = isBare || USE_MOCK_DATA;
|
|
const { categorySlug } = useParams<{ categorySlug?: string }>();
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
const initialQ = searchParams.get('q') || '';
|
|
|
|
const { hydrated, bookmarkedProducts, isBookmarked, toggleBookmark } =
|
|
useShopApp();
|
|
|
|
const [query, setQuery] = useState(initialQ);
|
|
const isSearchOrAll = mode === 'search' || !categorySlug;
|
|
const shouldFetchLive = !isBare && mode !== 'bookmarks';
|
|
|
|
const productsQuery = useProductsQuery(
|
|
{ q: initialQ || undefined },
|
|
shouldFetchLive && isSearchOrAll
|
|
);
|
|
const categoryQuery = useCategoryDetailQuery(
|
|
categorySlug || '',
|
|
shouldFetchLive && !isSearchOrAll
|
|
);
|
|
|
|
const products = !shouldFetchLive
|
|
? null
|
|
: isSearchOrAll
|
|
? productsQuery.data?.results ?? null
|
|
: categoryQuery.data?.products ?? null;
|
|
|
|
const activeQuery = isSearchOrAll ? productsQuery : categoryQuery;
|
|
const loading =
|
|
shouldFetchLive &&
|
|
(activeQuery.isPending || (!products && activeQuery.isFetching));
|
|
const loadError = shouldFetchLive && activeQuery.isError;
|
|
|
|
const headerTitle = useMemo(() => {
|
|
if (mode === 'bookmarks') return 'Saved Items';
|
|
if (isSearchOrAll) {
|
|
return initialQ ? `Results for "${initialQ}"` : 'Explore Products';
|
|
}
|
|
const cat = categoryQuery.data?.category;
|
|
return (
|
|
cat?.localized_title ||
|
|
cat?.title ||
|
|
(categorySlug ? humanizeSlug(categorySlug) : 'Home & Living')
|
|
);
|
|
}, [categoryQuery.data?.category, categorySlug, initialQ, isSearchOrAll, mode]);
|
|
|
|
useEffect(() => {
|
|
setQuery(initialQ);
|
|
}, [initialQ]);
|
|
|
|
const displayedProducts = useMemo(() => {
|
|
const source = mode === 'bookmarks' ? bookmarkedProducts : products;
|
|
if (!source) return null;
|
|
const trimmed = query.trim().toLowerCase();
|
|
if (!trimmed) return source;
|
|
return source.filter(
|
|
(p) =>
|
|
p.title.toLowerCase().includes(trimmed) ||
|
|
(p.store_name || '').toLowerCase().includes(trimmed) ||
|
|
(p.category_title || '').toLowerCase().includes(trimmed)
|
|
);
|
|
}, [bookmarkedProducts, mode, products, query]);
|
|
|
|
const backFallback =
|
|
mode === 'bookmarks'
|
|
? ROUTES.account
|
|
: mode === 'search'
|
|
? ROUTES.home
|
|
: ROUTES.categories;
|
|
|
|
const isBookmarksLoading = !isBare && mode === 'bookmarks' && !hydrated;
|
|
|
|
return (
|
|
<MobileShell width={375} height={814} backgroundColor="#f3f1ef">
|
|
<StandardPageHeader
|
|
title={isBare ? 'Home & Living' : headerTitle}
|
|
backTo={backFallback}
|
|
/>
|
|
|
|
<div
|
|
style={{
|
|
flex: 1,
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
padding: '11px var(--page-px) 28px',
|
|
gap: 23,
|
|
}}
|
|
>
|
|
<SearchBar
|
|
showFilterButton={false}
|
|
value={isBare ? undefined : query}
|
|
onChange={isBare ? undefined : setQuery}
|
|
onSubmit={
|
|
isBare
|
|
? undefined
|
|
: (val) => {
|
|
if (mode === 'search') {
|
|
setSearchParams(val ? { q: val } : {});
|
|
} else {
|
|
setQuery(val);
|
|
}
|
|
}
|
|
}
|
|
/>
|
|
|
|
{!isBare && (loading || isBookmarksLoading) ? (
|
|
<ProductGridShimmer count={6} />
|
|
) : !isBare && displayedProducts ? (
|
|
displayedProducts.length > 0 ? (
|
|
<div className="habib-product-grid">
|
|
{displayedProducts.map((product) => (
|
|
<GridProductCard
|
|
key={product.id}
|
|
item={mapProductToGridItem(product)}
|
|
slugOrId={product.slug}
|
|
isBookmarked={isBookmarked(product.id, product.slug)}
|
|
onToggleBookmark={() => {
|
|
void toggleBookmark(product.slug, product.id, product.slug);
|
|
}}
|
|
/>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<div
|
|
style={{
|
|
padding: '48px 16px',
|
|
textAlign: 'center',
|
|
fontFamily: 'var(--font-sans)',
|
|
color: '#747474',
|
|
fontSize: '13px',
|
|
fontWeight: 600,
|
|
}}
|
|
>
|
|
{tr(
|
|
mode === 'bookmarks'
|
|
? 'No saved items yet. Tap the bookmark icon on any product to save it here.'
|
|
: 'No matching products found.'
|
|
)}
|
|
</div>
|
|
)
|
|
) : (
|
|
<>
|
|
{!isBare && loadError && (
|
|
<RetryBanner
|
|
message="Could not load products. Tap Retry to refresh."
|
|
onRetry={() => void activeQuery.refetch()}
|
|
/>
|
|
)}
|
|
{useMockFallback ? (
|
|
<div className="habib-product-grid">
|
|
{TWO_COLUMN_GRID_PRODUCTS.map((product) => (
|
|
<GridProductCard key={product.id} item={product} />
|
|
))}
|
|
</div>
|
|
) : (
|
|
<EmptyStateBox message="No products available." />
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
</MobileShell>
|
|
);
|
|
};
|