enh
This commit is contained in:
parent
11f878b979
commit
0ca0a95aa8
12 changed files with 855 additions and 430 deletions
|
|
@ -1600,6 +1600,9 @@ Covered by HOME-81..98, plus:
|
||||||
| STORE-20 | edge | same value written twice | map identity preserved (no subscriber wake) |
|
| STORE-20 | edge | same value written twice | map identity preserved (no subscriber wake) |
|
||||||
| STORE-21 | edge | tag selected twice | store identity preserved (no subscriber wake) |
|
| STORE-21 | edge | tag selected twice | store identity preserved (no subscriber wake) |
|
||||||
| STORE-22 | edge | resetHomeTags | selection null and expansion empty |
|
| STORE-22 | edge | resetHomeTags | selection null and expansion empty |
|
||||||
|
| STORE-23 | edge | flash tag/store selected twice | store identity preserved (no subscriber wake) |
|
||||||
|
| STORE-24 | edge | resetFlashTabs | tag/store null and search empty |
|
||||||
|
| STORE-25 | edge | unchanged flash search query | store identity preserved (no subscriber wake) |
|
||||||
|
|
||||||
### App helpers
|
### App helpers
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,9 +67,9 @@ export default function FlashDeliveryView() {
|
||||||
}, [shouldNavigateToCart, router])
|
}, [shouldNavigateToCart, router])
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleProductPress = (productId: number) => {
|
const handleProductPress = React.useCallback((productId: number) => {
|
||||||
router.push(`/(drawer)/(tabs)/flash-delivery/product-detail/${productId}`);
|
router.push(`/(drawer)/(tabs)/flash-delivery/product-detail/${productId}`);
|
||||||
};
|
}, [router]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
|
||||||
|
|
@ -297,4 +297,7 @@ const ProductCard: React.FC<ProductCardProps> = ({
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ProductCard;
|
// Memoised: list pages re-render their section on selection changes, and an
|
||||||
|
// unmemoised card re-runs its cart/slot hooks and rebuilds its image for every
|
||||||
|
// sibling. Parents pass a stable `item` and a stable `onPress`.
|
||||||
|
export default React.memo(ProductCard);
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ import { useHideTabNav } from '@/src/hooks/useHideTabNav';
|
||||||
import CartIcon from '@/components/icons/CartIcon';
|
import CartIcon from '@/components/icons/CartIcon';
|
||||||
import ProductCard from '@/components/ProductCard';
|
import ProductCard from '@/components/ProductCard';
|
||||||
import { useSlotStore } from '@/components/stores/slotStore';
|
import { useSlotStore } from '@/components/stores/slotStore';
|
||||||
|
import { useFlashTabStore } from '@/src/store/flashTabStore';
|
||||||
|
|
||||||
import { LinearGradient } from 'expo-linear-gradient';
|
import { LinearGradient } from 'expo-linear-gradient';
|
||||||
import FloatingCartBar from '@/components/floating-cart-bar';
|
import FloatingCartBar from '@/components/floating-cart-bar';
|
||||||
|
|
@ -552,13 +553,253 @@ const flashItemWidth = (screenWidth - 48) / 3;
|
||||||
// Three rows per page, like the home catalog.
|
// Three rows per page, like the home catalog.
|
||||||
const FLASH_PAGE_SIZE = 9;
|
const FLASH_PAGE_SIZE = 9;
|
||||||
|
|
||||||
export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDeliveryProductsProps) {
|
interface FlashTabItem {
|
||||||
|
id: number;
|
||||||
|
label: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Active tab, resolved the way the page always did: the picked id while it
|
||||||
|
* still exists in this region's catalogue, otherwise the first tab. */
|
||||||
|
function resolveActiveId(items: FlashTabItem[], picked: number | null): number | null {
|
||||||
|
if (picked != null && items.some((item) => item.id === picked)) return picked;
|
||||||
|
return items[0]?.id ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tag strip. Reads the selection straight from the store, so a tap does not
|
||||||
|
* re-render the page, and receives a memoised `items` array so its memo holds.
|
||||||
|
*/
|
||||||
|
const FlashTagStrip = React.memo(({ items }: { items: FlashTabItem[] }) => {
|
||||||
|
const selectedTagId = useFlashTabStore((state) => state.selectedTagId);
|
||||||
|
const selectTag = useFlashTabStore((state) => state.selectTag);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<FlashTabStrip
|
||||||
|
testID="flash-tag-strip"
|
||||||
|
tabTestIDPrefix="flash-tag-tab"
|
||||||
|
items={items}
|
||||||
|
activeId={resolveActiveId(items, selectedTagId)}
|
||||||
|
onSelect={selectTag}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
FlashTagStrip.displayName = 'FlashTagStrip';
|
||||||
|
|
||||||
|
const FlashStoreStrip = React.memo(({ items }: { items: FlashTabItem[] }) => {
|
||||||
|
const selectedStoreId = useFlashTabStore((state) => state.selectedStoreId);
|
||||||
|
const selectStore = useFlashTabStore((state) => state.selectStore);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<FlashTabStrip
|
||||||
|
testID="flash-store-strip"
|
||||||
|
tabTestIDPrefix="flash-store-tab"
|
||||||
|
items={items}
|
||||||
|
activeId={resolveActiveId(items, selectedStoreId)}
|
||||||
|
onSelect={selectStore}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
FlashStoreStrip.displayName = 'FlashStoreStrip';
|
||||||
|
|
||||||
|
/** Search box. Owns the store subscription so typing never re-renders the page. */
|
||||||
|
const FlashSearchBar = React.memo(() => {
|
||||||
|
const searchQuery = useFlashTabStore((state) => state.searchQuery);
|
||||||
|
const setSearchQuery = useFlashTabStore((state) => state.setSearchQuery);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SearchBar
|
||||||
|
testID="flash-product-search"
|
||||||
|
value={searchQuery}
|
||||||
|
onChangeText={setSearchQuery}
|
||||||
|
placeholder="Search products..."
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
FlashSearchBar.displayName = 'FlashSearchBar';
|
||||||
|
|
||||||
|
interface FlashProductItemProps {
|
||||||
|
item: any;
|
||||||
|
onPress: (productId: number) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One card. `onPress` is derived from the id so the parent's handler identity
|
||||||
|
* is the only prop that matters. */
|
||||||
|
const FlashProductItem = React.memo(({ item, onPress }: FlashProductItemProps) => {
|
||||||
|
const handlePress = React.useCallback(() => onPress(item.id), [item.id, onPress]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<View style={tw`mb-3`}>
|
||||||
|
<ProductCard
|
||||||
|
item={item}
|
||||||
|
itemWidth={flashItemWidth}
|
||||||
|
cartType="flash"
|
||||||
|
variant="hero"
|
||||||
|
miniView
|
||||||
|
showDeliveryInfo={false}
|
||||||
|
useAddToCartDialog={false}
|
||||||
|
onPress={handlePress}
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
FlashProductItem.displayName = 'FlashProductItem';
|
||||||
|
|
||||||
|
interface FlashProductGridProps {
|
||||||
|
products: any[];
|
||||||
|
onProductPress: (productId: number) => void;
|
||||||
|
gridTestID: string;
|
||||||
|
showMoreTestID: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One paged grid. Owns its visible count, and is keyed by the active tab so a
|
||||||
|
* tab switch resets the page size on the first render instead of in an effect.
|
||||||
|
* The search value lives in the store, so only the grids wake on typing.
|
||||||
|
*/
|
||||||
|
const FlashProductGrid = React.memo(({
|
||||||
|
products,
|
||||||
|
onProductPress,
|
||||||
|
gridTestID,
|
||||||
|
showMoreTestID,
|
||||||
|
}: FlashProductGridProps) => {
|
||||||
|
const searchQuery = useFlashTabStore((state) => state.searchQuery);
|
||||||
|
const trimmedQuery = searchQuery.trim().toLowerCase();
|
||||||
|
|
||||||
|
const filtered = React.useMemo(() => {
|
||||||
|
if (!trimmedQuery) return products;
|
||||||
|
return products.filter((product: any) =>
|
||||||
|
product.name?.toLowerCase().includes(trimmedQuery)
|
||||||
|
);
|
||||||
|
}, [products, trimmedQuery]);
|
||||||
|
|
||||||
|
const [visibleCount, setVisibleCount] = React.useState(FLASH_PAGE_SIZE);
|
||||||
|
// Adjust during render, not in an effect: an effect would paint the new
|
||||||
|
// results with the previous count and then reset, costing a second render.
|
||||||
|
const [lastQuery, setLastQuery] = React.useState(trimmedQuery);
|
||||||
|
if (lastQuery !== trimmedQuery) {
|
||||||
|
setLastQuery(trimmedQuery);
|
||||||
|
setVisibleCount(FLASH_PAGE_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
const visible = filtered.length > visibleCount ? filtered.slice(0, visibleCount) : filtered;
|
||||||
|
|
||||||
|
if (filtered.length === 0) {
|
||||||
|
return (
|
||||||
|
<View style={tw`items-center justify-center py-10`}>
|
||||||
|
<MyText style={tw`text-gray-400 font-medium`}>No 1 Hr Delivery products available.</MyText>
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<View testID={gridTestID} style={tw`flex-row flex-wrap justify-between`}>
|
||||||
|
{visible.map((product: any) => (
|
||||||
|
<FlashProductItem key={product.id} item={product} onPress={onProductPress} />
|
||||||
|
))}
|
||||||
|
</View>
|
||||||
|
{filtered.length > visibleCount && (
|
||||||
|
<View style={tw`items-center py-4`}>
|
||||||
|
<MyTouchableOpacity
|
||||||
|
testID={showMoreTestID}
|
||||||
|
style={tw`px-6 py-3 rounded-full bg-brand500 shadow-sm`}
|
||||||
|
activeOpacity={0.85}
|
||||||
|
onPress={() => setVisibleCount((count) => count + FLASH_PAGE_SIZE)}
|
||||||
|
>
|
||||||
|
<MyText style={tw`text-white text-sm font-semibold`}>Show More</MyText>
|
||||||
|
</MyTouchableOpacity>
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
FlashProductGrid.displayName = 'FlashProductGrid';
|
||||||
|
|
||||||
|
interface FlashTagSectionProps {
|
||||||
|
tagItems: FlashTabItem[];
|
||||||
|
tagIds: number[];
|
||||||
|
productsByTagId: Record<number, any[]>;
|
||||||
|
/** With no tags configured the section lists every flash product. */
|
||||||
|
fallbackProducts: any[];
|
||||||
|
onProductPress: (productId: number) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Tag strip + its grid. Only this section re-renders when a tag is picked. */
|
||||||
|
const FlashTagSection = React.memo(({
|
||||||
|
tagItems,
|
||||||
|
tagIds,
|
||||||
|
productsByTagId,
|
||||||
|
fallbackProducts,
|
||||||
|
onProductPress,
|
||||||
|
}: FlashTagSectionProps) => {
|
||||||
|
const selectedTagId = useFlashTabStore((state) => state.selectedTagId);
|
||||||
|
const tagId =
|
||||||
|
selectedTagId != null && tagIds.includes(selectedTagId)
|
||||||
|
? selectedTagId
|
||||||
|
: tagIds[0] ?? null;
|
||||||
|
const products = tagId == null ? fallbackProducts : productsByTagId[tagId] ?? [];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<FlashTagStrip items={tagItems} />
|
||||||
|
<View testID="flash-tag-section" style={tw`px-4`}>
|
||||||
|
<FlashProductGrid
|
||||||
|
key={tagId ?? 'all'}
|
||||||
|
products={products}
|
||||||
|
onProductPress={onProductPress}
|
||||||
|
gridTestID="flash-tag-grid"
|
||||||
|
showMoreTestID="flash-products-show-more"
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
FlashTagSection.displayName = 'FlashTagSection';
|
||||||
|
|
||||||
|
interface FlashStoreSectionProps {
|
||||||
|
storeItems: FlashTabItem[];
|
||||||
|
storeIds: number[];
|
||||||
|
productsByStoreId: Record<number, any[]>;
|
||||||
|
onProductPress: (productId: number) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** "Shop by Store" heading, its strip and grid. Picking a store wakes only this. */
|
||||||
|
const FlashStoreSection = React.memo(({
|
||||||
|
storeItems,
|
||||||
|
storeIds,
|
||||||
|
productsByStoreId,
|
||||||
|
onProductPress,
|
||||||
|
}: FlashStoreSectionProps) => {
|
||||||
|
const selectedStoreId = useFlashTabStore((state) => state.selectedStoreId);
|
||||||
|
const storeId =
|
||||||
|
selectedStoreId != null && storeIds.includes(selectedStoreId)
|
||||||
|
? selectedStoreId
|
||||||
|
: storeIds[0] ?? null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<View testID="flash-store-section" style={tw`mt-8`}>
|
||||||
|
<View style={tw`px-4 mb-2`}>
|
||||||
|
<MyText style={tw`text-2xl font-extrabold text-gray-900 tracking-tight`}>
|
||||||
|
Shop by Store
|
||||||
|
</MyText>
|
||||||
|
</View>
|
||||||
|
<FlashStoreStrip items={storeItems} />
|
||||||
|
<View style={tw`px-4`}>
|
||||||
|
<FlashProductGrid
|
||||||
|
key={storeId ?? 'all'}
|
||||||
|
products={productsByStoreId[storeId ?? -1] ?? []}
|
||||||
|
onProductPress={onProductPress}
|
||||||
|
gridTestID="flash-store-grid"
|
||||||
|
showMoreTestID="flash-store-show-more"
|
||||||
|
/>
|
||||||
|
</View>
|
||||||
|
</View>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
FlashStoreSection.displayName = 'FlashStoreSection';
|
||||||
|
|
||||||
|
export function FlashDeliveryProducts({ onProductPress }: FlashDeliveryProductsProps) {
|
||||||
useHideTabNav('quick_delivery');
|
useHideTabNav('quick_delivery');
|
||||||
const [searchQuery, setSearchQuery] = useState('');
|
|
||||||
const [selectedTagId, setSelectedTagId] = useState<number | null>(null);
|
|
||||||
const [selectedStoreId, setSelectedStoreId] = useState<number | null>(null);
|
|
||||||
const [tagVisibleCount, setTagVisibleCount] = useState(FLASH_PAGE_SIZE);
|
|
||||||
const [storeVisibleCount, setStoreVisibleCount] = useState(FLASH_PAGE_SIZE);
|
|
||||||
// Owned by the user's pull, not by React Query: `isFetching` is true for every
|
// Owned by the user's pull, not by React Query: `isFetching` is true for every
|
||||||
// background fetch (remount-after-stale, invalidation, version bump), which
|
// background fetch (remount-after-stale, invalidation, version bump), which
|
||||||
// made the pull-to-refresh spinner pop up on its own.
|
// made the pull-to-refresh spinner pop up on its own.
|
||||||
|
|
@ -569,92 +810,75 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery
|
||||||
const { data: storesData, refetch: refetchStores } = useStores();
|
const { data: storesData, refetch: refetchStores } = useStores();
|
||||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap);
|
||||||
|
|
||||||
const tags = productsQuery?.data?.tags || [];
|
// Single memo for the flash catalogue. Tabs, per-tab rows and per-store rows
|
||||||
const stores = storesData?.stores || [];
|
// are derived together so the screen never holds two views of the same
|
||||||
|
// payload that can drift apart. Picking a tab no longer touches any of it.
|
||||||
const trimmedQuery = searchQuery.trim().toLowerCase();
|
const {
|
||||||
|
tagItems,
|
||||||
const flashProducts = React.useMemo(() => {
|
tagIds,
|
||||||
|
productsByTagId,
|
||||||
|
storeItems,
|
||||||
|
storeIds,
|
||||||
|
productsByStoreId,
|
||||||
|
flashProducts,
|
||||||
|
} = React.useMemo(() => {
|
||||||
const products = productsQuery?.data?.products || [];
|
const products = productsQuery?.data?.products || [];
|
||||||
return products.filter((p: any) => {
|
const tags = productsQuery?.data?.tags || [];
|
||||||
const info = productSlotsMap[p.id];
|
const stores = storesData?.stores || [];
|
||||||
|
|
||||||
|
const flashProducts = products.filter((product: any) => {
|
||||||
|
const info = productSlotsMap[product.id];
|
||||||
return info?.isFlashAvailable && !info?.isOutOfStock;
|
return info?.isFlashAvailable && !info?.isOutOfStock;
|
||||||
});
|
});
|
||||||
}, [productsQuery?.data?.products, productSlotsMap]);
|
|
||||||
|
|
||||||
const flashProductIds = React.useMemo(
|
const flashProductIds = new Set<number>(flashProducts.map((product: any) => product.id));
|
||||||
() => new Set<number>(flashProducts.map((p: any) => p.id)),
|
const flashStoreIds = new Set<number>(flashProducts.map((product: any) => product.storeId));
|
||||||
[flashProducts]
|
|
||||||
|
// A tab only exists if it has at least one in-stock flash product; empty
|
||||||
|
// tags and stores are not shown at all.
|
||||||
|
const tagTabs = tags.filter((tag: any) =>
|
||||||
|
((tag.productIds || []) as number[]).some((id) => flashProductIds.has(id))
|
||||||
|
);
|
||||||
|
const storeTabs = stores.filter((store: any) => flashStoreIds.has(store.id));
|
||||||
|
|
||||||
|
const productsByTagId: Record<number, any[]> = {};
|
||||||
|
for (const tag of tagTabs) {
|
||||||
|
const ids = new Set<number>((tag.productIds || []) as number[]);
|
||||||
|
productsByTagId[tag.id] = flashProducts.filter((product: any) => ids.has(product.id));
|
||||||
|
}
|
||||||
|
|
||||||
|
const productsByStoreId: Record<number, any[]> = {};
|
||||||
|
for (const store of storeTabs) {
|
||||||
|
productsByStoreId[store.id] = flashProducts.filter(
|
||||||
|
(product: any) => product.storeId === store.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
tagItems: tagTabs.map((tag: any) => ({ id: tag.id, label: tag.tagName })),
|
||||||
|
tagIds: tagTabs.map((tag: any) => tag.id),
|
||||||
|
productsByTagId,
|
||||||
|
storeItems: storeTabs.map((store: any) => ({
|
||||||
|
id: store.id,
|
||||||
|
label: store.name.replace(/^The\s+/i, ''),
|
||||||
|
})),
|
||||||
|
storeIds: storeTabs.map((store: any) => store.id),
|
||||||
|
productsByStoreId,
|
||||||
|
flashProducts,
|
||||||
|
};
|
||||||
|
}, [productsQuery?.data, storesData, productSlotsMap]);
|
||||||
|
|
||||||
|
// Stable so FlashProductItem/FlashProductGrid memos survive a re-render.
|
||||||
|
const handleProductPress = React.useCallback(
|
||||||
|
(productId: number) => {
|
||||||
|
if (onProductPress) {
|
||||||
|
onProductPress(productId);
|
||||||
|
} else {
|
||||||
|
router.push(`/(drawer)/(tabs)/flash-delivery/product-detail/${productId}`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[onProductPress, router]
|
||||||
);
|
);
|
||||||
const flashStoreIds = React.useMemo(
|
|
||||||
() => new Set<number>(flashProducts.map((p: any) => p.storeId)),
|
|
||||||
[flashProducts]
|
|
||||||
);
|
|
||||||
|
|
||||||
// A tab only exists if it has at least one in-stock flash product; empty
|
|
||||||
// tags and stores are not shown at all.
|
|
||||||
const tagTabs = React.useMemo(
|
|
||||||
() =>
|
|
||||||
tags.filter((tag: any) =>
|
|
||||||
((tag.productIds || []) as number[]).some((id) => flashProductIds.has(id))
|
|
||||||
),
|
|
||||||
[tags, flashProductIds]
|
|
||||||
);
|
|
||||||
const storeTabs = React.useMemo(
|
|
||||||
() => stores.filter((store: any) => flashStoreIds.has(store.id)),
|
|
||||||
[stores, flashStoreIds]
|
|
||||||
);
|
|
||||||
|
|
||||||
// First visible tab is the default, matching the home page.
|
|
||||||
const activeTagId = tagTabs.some((tag: any) => tag.id === selectedTagId)
|
|
||||||
? selectedTagId
|
|
||||||
: tagTabs[0]?.id ?? null;
|
|
||||||
const activeStoreId = storeTabs.some((store: any) => store.id === selectedStoreId)
|
|
||||||
? selectedStoreId
|
|
||||||
: storeTabs[0]?.id ?? null;
|
|
||||||
|
|
||||||
const tagProductIds = React.useMemo(() => {
|
|
||||||
const tag = tagTabs.find((t: any) => t.id === activeTagId);
|
|
||||||
return new Set<number>((tag?.productIds || []) as number[]);
|
|
||||||
}, [tagTabs, activeTagId]);
|
|
||||||
|
|
||||||
const matchesSearch = React.useCallback(
|
|
||||||
(product: any) =>
|
|
||||||
!trimmedQuery || product.name?.toLowerCase().includes(trimmedQuery),
|
|
||||||
[trimmedQuery]
|
|
||||||
);
|
|
||||||
|
|
||||||
const tagProducts = React.useMemo(
|
|
||||||
() =>
|
|
||||||
flashProducts.filter(
|
|
||||||
(product: any) =>
|
|
||||||
(activeTagId == null || tagProductIds.has(product.id)) &&
|
|
||||||
matchesSearch(product)
|
|
||||||
),
|
|
||||||
[flashProducts, activeTagId, tagProductIds, matchesSearch]
|
|
||||||
);
|
|
||||||
|
|
||||||
const storeProducts = React.useMemo(
|
|
||||||
() =>
|
|
||||||
flashProducts.filter(
|
|
||||||
(product: any) =>
|
|
||||||
(activeStoreId == null || product.storeId === activeStoreId) &&
|
|
||||||
matchesSearch(product)
|
|
||||||
),
|
|
||||||
[flashProducts, activeStoreId, matchesSearch]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Each section pages independently; a tab or search change starts over.
|
|
||||||
React.useEffect(() => {
|
|
||||||
setTagVisibleCount(FLASH_PAGE_SIZE);
|
|
||||||
}, [activeTagId, trimmedQuery]);
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
setStoreVisibleCount(FLASH_PAGE_SIZE);
|
|
||||||
}, [activeStoreId, trimmedQuery]);
|
|
||||||
|
|
||||||
const visibleTagProducts = tagProducts.slice(0, tagVisibleCount);
|
|
||||||
const visibleStoreProducts = storeProducts.slice(0, storeVisibleCount);
|
|
||||||
|
|
||||||
const refetchProducts = productsQuery?.refetch;
|
const refetchProducts = productsQuery?.refetch;
|
||||||
const handleRefresh = React.useCallback(async () => {
|
const handleRefresh = React.useCallback(async () => {
|
||||||
|
|
@ -679,52 +903,6 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery
|
||||||
[isRefreshing, handleRefresh]
|
[isRefreshing, handleRefresh]
|
||||||
);
|
);
|
||||||
|
|
||||||
const renderProduct = (item: any) => (
|
|
||||||
<View key={item.id} style={tw`mb-3`}>
|
|
||||||
<ProductCard
|
|
||||||
item={item}
|
|
||||||
itemWidth={flashItemWidth}
|
|
||||||
cartType="flash"
|
|
||||||
variant="hero"
|
|
||||||
miniView
|
|
||||||
showDeliveryInfo={false}
|
|
||||||
useAddToCartDialog={false}
|
|
||||||
onPress={() => {
|
|
||||||
if (onProductPress) {
|
|
||||||
onProductPress(item.id);
|
|
||||||
} else {
|
|
||||||
router.push(`/(drawer)/(tabs)/flash-delivery/product-detail/${item.id}`);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</View>
|
|
||||||
);
|
|
||||||
|
|
||||||
const renderGrid = (products: any[], testID: string) => (
|
|
||||||
<View testID={testID} style={tw`flex-row flex-wrap justify-between`}>
|
|
||||||
{products.map(renderProduct)}
|
|
||||||
</View>
|
|
||||||
);
|
|
||||||
|
|
||||||
const renderShowMore = (testID: string, onPress: () => void) => (
|
|
||||||
<View style={tw`items-center py-4`}>
|
|
||||||
<MyTouchableOpacity
|
|
||||||
testID={testID}
|
|
||||||
style={tw`px-6 py-3 rounded-full bg-brand500 shadow-sm`}
|
|
||||||
activeOpacity={0.85}
|
|
||||||
onPress={onPress}
|
|
||||||
>
|
|
||||||
<MyText style={tw`text-white text-sm font-semibold`}>Show More</MyText>
|
|
||||||
</MyTouchableOpacity>
|
|
||||||
</View>
|
|
||||||
);
|
|
||||||
|
|
||||||
const renderEmpty = () => (
|
|
||||||
<View style={tw`items-center justify-center py-10`}>
|
|
||||||
<MyText style={tw`text-gray-400 font-medium`}>No 1 Hr Delivery products available.</MyText>
|
|
||||||
</View>
|
|
||||||
);
|
|
||||||
|
|
||||||
if (productsQuery?.isLoading) {
|
if (productsQuery?.isLoading) {
|
||||||
return (
|
return (
|
||||||
<AppContainer>
|
<AppContainer>
|
||||||
|
|
@ -754,60 +932,26 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery
|
||||||
refreshControl={refreshControl}
|
refreshControl={refreshControl}
|
||||||
>
|
>
|
||||||
<View style={tw`px-4 pt-2`}>
|
<View style={tw`px-4 pt-2`}>
|
||||||
<SearchBar
|
<FlashSearchBar />
|
||||||
testID="flash-product-search"
|
|
||||||
value={searchQuery}
|
|
||||||
onChangeText={setSearchQuery}
|
|
||||||
placeholder="Search products..."
|
|
||||||
/>
|
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
{/* Tag section — tabs scroll with the page, not sticky. */}
|
{/* Tag section — tabs scroll with the page, not sticky. */}
|
||||||
<FlashTabStrip
|
<FlashTagSection
|
||||||
testID="flash-tag-strip"
|
tagItems={tagItems}
|
||||||
tabTestIDPrefix="flash-tag-tab"
|
tagIds={tagIds}
|
||||||
items={tagTabs.map((tag: any) => ({ id: tag.id, label: tag.tagName }))}
|
productsByTagId={productsByTagId}
|
||||||
activeId={activeTagId}
|
fallbackProducts={flashProducts}
|
||||||
onSelect={setSelectedTagId}
|
onProductPress={handleProductPress}
|
||||||
/>
|
/>
|
||||||
<View testID="flash-tag-section" style={tw`px-4`}>
|
|
||||||
{tagProducts.length > 0
|
|
||||||
? renderGrid(visibleTagProducts, 'flash-tag-grid')
|
|
||||||
: renderEmpty()}
|
|
||||||
{tagProducts.length > tagVisibleCount &&
|
|
||||||
renderShowMore('flash-products-show-more', () =>
|
|
||||||
setTagVisibleCount((count) => count + FLASH_PAGE_SIZE)
|
|
||||||
)}
|
|
||||||
</View>
|
|
||||||
|
|
||||||
{/* Shop by Store — one tab per store. */}
|
{/* Shop by Store — one tab per store. */}
|
||||||
{storeTabs.length > 0 && (
|
{storeIds.length > 0 && (
|
||||||
<View testID="flash-store-section" style={tw`mt-8`}>
|
<FlashStoreSection
|
||||||
<View style={tw`px-4 mb-2`}>
|
storeItems={storeItems}
|
||||||
<MyText style={tw`text-2xl font-extrabold text-gray-900 tracking-tight`}>
|
storeIds={storeIds}
|
||||||
Shop by Store
|
productsByStoreId={productsByStoreId}
|
||||||
</MyText>
|
onProductPress={handleProductPress}
|
||||||
</View>
|
|
||||||
<FlashTabStrip
|
|
||||||
testID="flash-store-strip"
|
|
||||||
tabTestIDPrefix="flash-store-tab"
|
|
||||||
items={storeTabs.map((store: any) => ({
|
|
||||||
id: store.id,
|
|
||||||
label: store.name.replace(/^The\s+/i, ''),
|
|
||||||
}))}
|
|
||||||
activeId={activeStoreId}
|
|
||||||
onSelect={setSelectedStoreId}
|
|
||||||
/>
|
/>
|
||||||
<View style={tw`px-4`}>
|
|
||||||
{storeProducts.length > 0
|
|
||||||
? renderGrid(visibleStoreProducts, 'flash-store-grid')
|
|
||||||
: renderEmpty()}
|
|
||||||
{storeProducts.length > storeVisibleCount &&
|
|
||||||
renderShowMore('flash-store-show-more', () =>
|
|
||||||
setStoreVisibleCount((count) => count + FLASH_PAGE_SIZE)
|
|
||||||
)}
|
|
||||||
</View>
|
|
||||||
</View>
|
|
||||||
)}
|
)}
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import { useCentralSlotStore } from '@/src/store/centralSlotStore'
|
||||||
import { useSlotStore } from '@/components/stores/slotStore'
|
import { useSlotStore } from '@/components/stores/slotStore'
|
||||||
import { useFlashNavigationStore } from '@/components/stores/flashNavigationStore'
|
import { useFlashNavigationStore } from '@/components/stores/flashNavigationStore'
|
||||||
import { useHomeTagStore } from '@/src/store/homeTagStore'
|
import { useHomeTagStore } from '@/src/store/homeTagStore'
|
||||||
|
import { useFlashTabStore } from '@/src/store/flashTabStore'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The one and only way to change the browsing region.
|
* The one and only way to change the browsing region.
|
||||||
|
|
@ -57,6 +58,8 @@ export async function confirmRegionSwitch(
|
||||||
// Tag ids belong to the outgoing region; a stale selection would point at a
|
// Tag ids belong to the outgoing region; a stale selection would point at a
|
||||||
// tag the new catalog does not have.
|
// tag the new catalog does not have.
|
||||||
useHomeTagStore.getState().resetHomeTags()
|
useHomeTagStore.getState().resetHomeTags()
|
||||||
|
// Same for the 1 Hr Delivery tag/store tabs and its search box.
|
||||||
|
useFlashTabStore.getState().resetFlashTabs()
|
||||||
|
|
||||||
// 3. Drop cached regional catalog/slot snapshots so they cannot leak across.
|
// 3. Drop cached regional catalog/slot snapshots so they cannot leak across.
|
||||||
const productStore = useCentralProductStore.getState()
|
const productStore = useCentralProductStore.getState()
|
||||||
|
|
|
||||||
45
apps/user-ui/src/store/flashTabStore.ts
Normal file
45
apps/user-ui/src/store/flashTabStore.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
import { create } from 'zustand'
|
||||||
|
|
||||||
|
interface FlashTabState {
|
||||||
|
/** User-picked flash tag; null means "first tag with flash products". */
|
||||||
|
selectedTagId: number | null
|
||||||
|
/** User-picked store in "Shop by Store"; null means the first store tab. */
|
||||||
|
selectedStoreId: number | null
|
||||||
|
/** Search box value; kept here so typing wakes only the two grids. */
|
||||||
|
searchQuery: string
|
||||||
|
selectTag: (tagId: number) => void
|
||||||
|
selectStore: (storeId: number) => void
|
||||||
|
setSearchQuery: (query: string) => void
|
||||||
|
resetFlashTabs: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flash page selection lives outside FlashDeliveryProducts on purpose: while it
|
||||||
|
* was local state, every tap re-rendered the page, both tab strips, both grids
|
||||||
|
* and every ProductCard. Only FlashTagStrip/FlashStoreStrip, FlashSearchBar and
|
||||||
|
* the two scene components subscribe here.
|
||||||
|
*/
|
||||||
|
export const useFlashTabStore = create<FlashTabState>((set) => ({
|
||||||
|
selectedTagId: null,
|
||||||
|
selectedStoreId: null,
|
||||||
|
searchQuery: '',
|
||||||
|
|
||||||
|
// No-op when unchanged: re-selecting the active tab must not wake
|
||||||
|
// subscribers (same guard as homeTagStore.selectTag).
|
||||||
|
selectTag: (tagId) =>
|
||||||
|
set((state) =>
|
||||||
|
state.selectedTagId === tagId ? state : { selectedTagId: tagId }
|
||||||
|
),
|
||||||
|
|
||||||
|
selectStore: (storeId) =>
|
||||||
|
set((state) =>
|
||||||
|
state.selectedStoreId === storeId ? state : { selectedStoreId: storeId }
|
||||||
|
),
|
||||||
|
|
||||||
|
setSearchQuery: (query) =>
|
||||||
|
set((state) => (state.searchQuery === query ? state : { searchQuery: query })),
|
||||||
|
|
||||||
|
// Tag and store ids are region-scoped, so a region switch has to drop them.
|
||||||
|
resetFlashTabs: () =>
|
||||||
|
set({ selectedTagId: null, selectedStoreId: null, searchQuery: '' }),
|
||||||
|
}))
|
||||||
|
|
@ -26,6 +26,7 @@ import { useLocationStore } from '@/src/store/locationStore'
|
||||||
import { useSlotStore } from '@/components/stores/slotStore'
|
import { useSlotStore } from '@/components/stores/slotStore'
|
||||||
import { useFlashNavigationStore } from '@/components/stores/flashNavigationStore'
|
import { useFlashNavigationStore } from '@/components/stores/flashNavigationStore'
|
||||||
import { useHomeTagStore } from '@/src/store/homeTagStore'
|
import { useHomeTagStore } from '@/src/store/homeTagStore'
|
||||||
|
import { useFlashTabStore } from '@/src/store/flashTabStore'
|
||||||
|
|
||||||
export function resetStores() {
|
export function resetStores() {
|
||||||
useAppStore.setState({ hideTabsNav: {} })
|
useAppStore.setState({ hideTabsNav: {} })
|
||||||
|
|
@ -56,6 +57,7 @@ export function resetStores() {
|
||||||
useSlotStore.setState({ slotId: undefined, storeId: undefined })
|
useSlotStore.setState({ slotId: undefined, storeId: undefined })
|
||||||
useFlashNavigationStore.setState({ shouldNavigateToCart: false })
|
useFlashNavigationStore.setState({ shouldNavigateToCart: false })
|
||||||
useHomeTagStore.setState({ selectedTagId: null, expandedByTagId: {} })
|
useHomeTagStore.setState({ selectedTagId: null, expandedByTagId: {} })
|
||||||
|
useFlashTabStore.setState({ selectedTagId: null, selectedStoreId: null, searchQuery: '' })
|
||||||
useLocationStore.getState().stop()
|
useLocationStore.getState().stop()
|
||||||
useLocationStore.setState({ coords: null, status: 'idle', error: null })
|
useLocationStore.setState({ coords: null, status: 'idle', error: null })
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import { useRegionStore } from '@/src/store/regionStore'
|
||||||
import { useSlotStore } from '@/components/stores/slotStore'
|
import { useSlotStore } from '@/components/stores/slotStore'
|
||||||
import { useFlashNavigationStore } from '@/components/stores/flashNavigationStore'
|
import { useFlashNavigationStore } from '@/components/stores/flashNavigationStore'
|
||||||
import { useHomeTagStore } from '@/src/store/homeTagStore'
|
import { useHomeTagStore } from '@/src/store/homeTagStore'
|
||||||
|
import { useFlashTabStore } from '@/src/store/flashTabStore'
|
||||||
import {
|
import {
|
||||||
AVAILABILITY,
|
AVAILABILITY,
|
||||||
MERGED_PRODUCTS,
|
MERGED_PRODUCTS,
|
||||||
|
|
@ -82,6 +83,39 @@ describe('stores', () => {
|
||||||
expect(useHomeTagStore.getState().expandedByTagId).toEqual({})
|
expect(useHomeTagStore.getState().expandedByTagId).toEqual({})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('STORE-23: re-selecting the active flash tab keeps the store identity', () => {
|
||||||
|
useFlashTabStore.getState().selectTag(5)
|
||||||
|
useFlashTabStore.getState().selectStore(3)
|
||||||
|
const before = useFlashTabStore.getState()
|
||||||
|
|
||||||
|
useFlashTabStore.getState().selectTag(5)
|
||||||
|
useFlashTabStore.getState().selectStore(3)
|
||||||
|
|
||||||
|
// Same state object, so the strips and sections are not woken.
|
||||||
|
expect(useFlashTabStore.getState()).toBe(before)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('STORE-24: resetFlashTabs drops both selections and the search box', () => {
|
||||||
|
useFlashTabStore.getState().selectTag(5)
|
||||||
|
useFlashTabStore.getState().selectStore(3)
|
||||||
|
useFlashTabStore.getState().setSearchQuery('chick')
|
||||||
|
|
||||||
|
useFlashTabStore.getState().resetFlashTabs()
|
||||||
|
|
||||||
|
expect(useFlashTabStore.getState().selectedTagId).toBeNull()
|
||||||
|
expect(useFlashTabStore.getState().selectedStoreId).toBeNull()
|
||||||
|
expect(useFlashTabStore.getState().searchQuery).toBe('')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('STORE-25: an unchanged flash search query keeps the store identity', () => {
|
||||||
|
useFlashTabStore.getState().setSearchQuery('chick')
|
||||||
|
const before = useFlashTabStore.getState()
|
||||||
|
|
||||||
|
useFlashTabStore.getState().setSearchQuery('chick')
|
||||||
|
|
||||||
|
expect(useFlashTabStore.getState()).toBe(before)
|
||||||
|
})
|
||||||
|
|
||||||
it('STORE-02: addressStore selects and clears an address id', () => {
|
it('STORE-02: addressStore selects and clears an address id', () => {
|
||||||
useAddressStore.getState().setSelectedAddressId(11)
|
useAddressStore.getState().setSelectedAddressId(11)
|
||||||
expect(useAddressStore.getState().selectedAddressId).toBe(11)
|
expect(useAddressStore.getState().selectedAddressId).toBe(11)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import { useCentralSlotStore } from './stores/central-slot-store'
|
||||||
import { useSlotStore } from './stores/slot-store'
|
import { useSlotStore } from './stores/slot-store'
|
||||||
import { useFlashNavigationStore } from './stores/flash-navigation-store'
|
import { useFlashNavigationStore } from './stores/flash-navigation-store'
|
||||||
import { useHomeTagStore } from './stores/home-tag-store'
|
import { useHomeTagStore } from './stores/home-tag-store'
|
||||||
|
import { useFlashTabStore } from './stores/flash-tab-store'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The one and only way to change the browsing region.
|
* The one and only way to change the browsing region.
|
||||||
|
|
@ -54,6 +55,8 @@ export async function confirmRegionSwitch(
|
||||||
// Tag ids belong to the outgoing region; a stale selection would point at a
|
// Tag ids belong to the outgoing region; a stale selection would point at a
|
||||||
// tag the new catalog does not have.
|
// tag the new catalog does not have.
|
||||||
useHomeTagStore.getState().resetHomeTags()
|
useHomeTagStore.getState().resetHomeTags()
|
||||||
|
// Same for the 1 Hr Delivery tag/store tabs and its search box.
|
||||||
|
useFlashTabStore.getState().resetFlashTabs()
|
||||||
|
|
||||||
// 3. Drop cached regional catalog/slot snapshots so they cannot leak across.
|
// 3. Drop cached regional catalog/slot snapshots so they cannot leak across.
|
||||||
const productStore = useCentralProductStore.getState()
|
const productStore = useCentralProductStore.getState()
|
||||||
|
|
|
||||||
41
apps/web-ui/src/lib/stores/flash-tab-store.ts
Normal file
41
apps/web-ui/src/lib/stores/flash-tab-store.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
import { create } from 'zustand'
|
||||||
|
|
||||||
|
interface FlashTabState {
|
||||||
|
/** User-picked flash tag; null means "first tag with flash products". */
|
||||||
|
selectedTagId: number | null
|
||||||
|
/** User-picked store in "Shop by Store"; null means the first store tab. */
|
||||||
|
selectedStoreId: number | null
|
||||||
|
/** Search box value; kept here so typing wakes only the two grids. */
|
||||||
|
searchQuery: string
|
||||||
|
selectTag: (tagId: number) => void
|
||||||
|
selectStore: (storeId: number) => void
|
||||||
|
setSearchQuery: (query: string) => void
|
||||||
|
resetFlashTabs: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mirrors apps/user-ui/src/store/flashTabStore.ts. The web flash page is
|
||||||
|
* split into the same memoized sections, so the state shape and reset
|
||||||
|
* behaviour stay identical to the native app.
|
||||||
|
*/
|
||||||
|
export const useFlashTabStore = create<FlashTabState>((set) => ({
|
||||||
|
selectedTagId: null,
|
||||||
|
selectedStoreId: null,
|
||||||
|
searchQuery: '',
|
||||||
|
|
||||||
|
selectTag: (tagId) =>
|
||||||
|
set((state) =>
|
||||||
|
state.selectedTagId === tagId ? state : { selectedTagId: tagId }
|
||||||
|
),
|
||||||
|
|
||||||
|
selectStore: (storeId) =>
|
||||||
|
set((state) =>
|
||||||
|
state.selectedStoreId === storeId ? state : { selectedStoreId: storeId }
|
||||||
|
),
|
||||||
|
|
||||||
|
setSearchQuery: (query) =>
|
||||||
|
set((state) => (state.searchQuery === query ? state : { searchQuery: query })),
|
||||||
|
|
||||||
|
resetFlashTabs: () =>
|
||||||
|
set({ selectedTagId: null, selectedStoreId: null, searchQuery: '' }),
|
||||||
|
}))
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
import { createFileRoute, useNavigate } from '@tanstack/react-router'
|
import { createFileRoute, useNavigate } from '@tanstack/react-router'
|
||||||
import { useState, useMemo, useRef, useEffect } from 'react'
|
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||||
import { MiniQuantifier } from 'web-components'
|
import { MiniQuantifier } from 'web-components'
|
||||||
import { useAllProducts, useStores } from '../hooks/prominent-api-hooks'
|
import { useAllProducts, useStores } from '../hooks/prominent-api-hooks'
|
||||||
import { useCentralProductStore } from '../lib/stores/central-product-store'
|
import { useCentralProductStore } from '../lib/stores/central-product-store'
|
||||||
import { useCentralSlotStore } from '../lib/stores/central-slot-store'
|
import { useCentralSlotStore } from '../lib/stores/central-slot-store'
|
||||||
|
import { useFlashTabStore } from '../lib/stores/flash-tab-store'
|
||||||
import { useAddToCart, useGetCart, useUpdateCartItem, useRemoveFromCart } from '../hooks/cart-query-hooks'
|
import { useAddToCart, useGetCart, useUpdateCartItem, useRemoveFromCart } from '../hooks/cart-query-hooks'
|
||||||
import { usePopulateCentralStores } from '../hooks/usePopulateCentralStores'
|
import { usePopulateCentralStores } from '../hooks/usePopulateCentralStores'
|
||||||
import { usePopulateCentralProductStore } from '../hooks/usePopulateCentralProductStore'
|
import { usePopulateCentralProductStore } from '../hooks/usePopulateCentralProductStore'
|
||||||
|
|
@ -18,9 +19,262 @@ export const Route = createFileRoute('/flash')({
|
||||||
// Three rows of three at a time, like the home catalog.
|
// Three rows of three at a time, like the home catalog.
|
||||||
const PAGE_SIZE = 9
|
const PAGE_SIZE = 9
|
||||||
|
|
||||||
function FlashDeliveryPage() {
|
const GRID_CLASS =
|
||||||
const navigate = useNavigate()
|
'grid grid-cols-3 gap-3 sm:gap-4 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5'
|
||||||
|
const STRIP_CLASS =
|
||||||
|
'scrollbar-hide -mx-4 mb-5 flex gap-1 overflow-x-auto px-4 md:mx-0 md:px-0'
|
||||||
|
|
||||||
|
interface TabItem {
|
||||||
|
id: number
|
||||||
|
label: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Active tab, resolved the way the page always did: the picked id while it
|
||||||
|
* still exists in this region's catalogue, otherwise the first tab. */
|
||||||
|
function resolveActiveId(items: TabItem[], picked: number | null): number | null {
|
||||||
|
if (picked != null && items.some((item) => item.id === picked)) return picked
|
||||||
|
return items[0]?.id ?? null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tag tabs. Reads the selection from the store, so a tap re-renders only this
|
||||||
|
* strip and the tag grid — mirroring the native FlashTagStrip/TagScene split.
|
||||||
|
*/
|
||||||
|
const FlashTagStrip = memo(({ items }: { items: TabItem[] }) => {
|
||||||
|
const selectedTagId = useFlashTabStore((state) => state.selectedTagId)
|
||||||
|
const selectTag = useFlashTabStore((state) => state.selectTag)
|
||||||
|
|
||||||
|
const scrollRef = useRef<HTMLDivElement>(null)
|
||||||
|
const positions = useRef<number[]>([])
|
||||||
|
const activeId = resolveActiveId(items, selectedTagId)
|
||||||
|
const index = Math.max(
|
||||||
|
0,
|
||||||
|
items.findIndex((item) => item.id === activeId)
|
||||||
|
)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const x = positions.current[index]
|
||||||
|
if (x != null) {
|
||||||
|
scrollRef.current?.scrollTo({ left: Math.max(0, x - 16), behavior: 'smooth' })
|
||||||
|
}
|
||||||
|
}, [index])
|
||||||
|
|
||||||
|
if (items.length === 0) return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div ref={scrollRef} className={STRIP_CLASS}>
|
||||||
|
{items.map((item, i) => {
|
||||||
|
const active = item.id === activeId
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={item.id}
|
||||||
|
ref={(el) => {
|
||||||
|
positions.current[i] = el?.offsetLeft ?? 0
|
||||||
|
}}
|
||||||
|
onClick={() => selectTag(item.id)}
|
||||||
|
className="shrink-0 cursor-pointer px-3 py-2"
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
className={`block text-sm font-bold ${
|
||||||
|
active ? 'text-flash-600' : 'text-gray-500'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{item.label}
|
||||||
|
</span>
|
||||||
|
<span
|
||||||
|
className={`mt-1 block h-1 rounded-full ${
|
||||||
|
active ? 'bg-flash-500' : 'bg-transparent'
|
||||||
|
}`}
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
FlashTagStrip.displayName = 'FlashTagStrip'
|
||||||
|
|
||||||
|
const FlashStoreStrip = memo(({ items }: { items: TabItem[] }) => {
|
||||||
|
const selectedStoreId = useFlashTabStore((state) => state.selectedStoreId)
|
||||||
|
const selectStore = useFlashTabStore((state) => state.selectStore)
|
||||||
|
|
||||||
|
const activeId = resolveActiveId(items, selectedStoreId)
|
||||||
|
|
||||||
|
if (items.length === 0) return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={STRIP_CLASS}>
|
||||||
|
{items.map((item) => {
|
||||||
|
const active = item.id === activeId
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={item.id}
|
||||||
|
onClick={() => selectStore(item.id)}
|
||||||
|
className="shrink-0 cursor-pointer px-3 py-2"
|
||||||
|
>
|
||||||
|
<span
|
||||||
|
className={`block text-sm font-bold ${
|
||||||
|
active ? 'text-flash-600' : 'text-gray-500'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{item.label}
|
||||||
|
</span>
|
||||||
|
<span
|
||||||
|
className={`mt-1 block h-1 rounded-full ${
|
||||||
|
active ? 'bg-flash-500' : 'bg-transparent'
|
||||||
|
}`}
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
FlashStoreStrip.displayName = 'FlashStoreStrip'
|
||||||
|
|
||||||
|
/** One card. Navigation is derived from the id so the parent handler identity
|
||||||
|
* is the only prop that can invalidate the memo. */
|
||||||
|
const FlashProductItem = memo(
|
||||||
|
({ item, onAddToCart }: { item: any; onAddToCart: (productId: number) => void }) => {
|
||||||
|
const navigate = useNavigate()
|
||||||
|
const handlePress = useCallback(() => {
|
||||||
|
navigate({ to: '/flash/product/$id', params: { id: String(item.id) } })
|
||||||
|
}, [navigate, item.id])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<CompactProductCard item={item} handleAddToCart={onAddToCart} onPress={handlePress} />
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
FlashProductItem.displayName = 'FlashProductItem'
|
||||||
|
|
||||||
|
interface FlashProductGridProps {
|
||||||
|
products: any[]
|
||||||
|
onAddToCart: (productId: number) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One paged grid. Owns its visible count and is keyed by the active tab, so a
|
||||||
|
* tab switch resets the page size on the first render instead of in an effect.
|
||||||
|
*/
|
||||||
|
const FlashProductGrid = memo(({ products, onAddToCart }: FlashProductGridProps) => {
|
||||||
|
const [visibleCount, setVisibleCount] = useState(PAGE_SIZE)
|
||||||
|
const visible = products.length > visibleCount ? products.slice(0, visibleCount) : products
|
||||||
|
|
||||||
|
if (products.length === 0) {
|
||||||
|
return (
|
||||||
|
<div className="py-10 text-center">
|
||||||
|
<p className="font-medium text-gray-400">No flash delivery products available.</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className={GRID_CLASS}>
|
||||||
|
{visible.map((product: any) => (
|
||||||
|
<FlashProductItem key={product.id} item={product} onAddToCart={onAddToCart} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{products.length > visibleCount && (
|
||||||
|
<div className="mt-6 flex justify-center">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setVisibleCount((count) => count + PAGE_SIZE)}
|
||||||
|
className="cursor-pointer rounded-full bg-flash-500 px-6 py-3 text-sm font-semibold text-white shadow-sm transition-colors hover:bg-flash-600"
|
||||||
|
>
|
||||||
|
Show More
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
FlashProductGrid.displayName = 'FlashProductGrid'
|
||||||
|
|
||||||
|
interface FlashTagSectionProps {
|
||||||
|
tagItems: TabItem[]
|
||||||
|
tagIds: number[]
|
||||||
|
productsByTagId: Record<number, any[]>
|
||||||
|
/** With no tags configured the section lists every flash product. */
|
||||||
|
fallbackProducts: any[]
|
||||||
|
onAddToCart: (productId: number) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
const FlashTagSection = memo(
|
||||||
|
({
|
||||||
|
tagItems,
|
||||||
|
tagIds,
|
||||||
|
productsByTagId,
|
||||||
|
fallbackProducts,
|
||||||
|
onAddToCart,
|
||||||
|
}: FlashTagSectionProps) => {
|
||||||
|
const selectedTagId = useFlashTabStore((state) => state.selectedTagId)
|
||||||
|
const tagId =
|
||||||
|
selectedTagId != null && tagIds.includes(selectedTagId)
|
||||||
|
? selectedTagId
|
||||||
|
: tagIds[0] ?? null
|
||||||
|
const products = tagId == null ? fallbackProducts : productsByTagId[tagId] ?? []
|
||||||
|
const label = tagItems.find((item) => item.id === tagId)?.label ?? 'All Products'
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<FlashTagStrip items={tagItems} />
|
||||||
|
|
||||||
|
<div className="mb-4 flex items-center justify-between">
|
||||||
|
<p className="text-lg font-extrabold text-gray-900">{label}</p>
|
||||||
|
<p className="text-sm text-gray-500">{products.length} items</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<FlashProductGrid
|
||||||
|
key={tagId ?? 'all'}
|
||||||
|
products={products}
|
||||||
|
onAddToCart={onAddToCart}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
FlashTagSection.displayName = 'FlashTagSection'
|
||||||
|
|
||||||
|
interface FlashStoreSectionProps {
|
||||||
|
storeItems: TabItem[]
|
||||||
|
storeIds: number[]
|
||||||
|
productsByStoreId: Record<number, any[]>
|
||||||
|
onAddToCart: (productId: number) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
const FlashStoreSection = memo(
|
||||||
|
({ storeItems, storeIds, productsByStoreId, onAddToCart }: FlashStoreSectionProps) => {
|
||||||
|
const selectedStoreId = useFlashTabStore((state) => state.selectedStoreId)
|
||||||
|
const storeId =
|
||||||
|
selectedStoreId != null && storeIds.includes(selectedStoreId)
|
||||||
|
? selectedStoreId
|
||||||
|
: storeIds[0] ?? null
|
||||||
|
const products = productsByStoreId[storeId ?? -1] ?? []
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mt-10">
|
||||||
|
<div className="mb-2 flex items-center justify-between">
|
||||||
|
<p className="text-lg font-extrabold text-gray-900">Shop by Store</p>
|
||||||
|
<p className="text-sm text-gray-500">{products.length} items</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<FlashStoreStrip items={storeItems} />
|
||||||
|
|
||||||
|
<FlashProductGrid
|
||||||
|
key={storeId ?? 'all'}
|
||||||
|
products={products}
|
||||||
|
onAddToCart={onAddToCart}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
FlashStoreSection.displayName = 'FlashStoreSection'
|
||||||
|
|
||||||
|
function FlashDeliveryPage() {
|
||||||
// Populate central stores so flash-eligible products resolve on direct load
|
// Populate central stores so flash-eligible products resolve on direct load
|
||||||
usePopulateCentralStores()
|
usePopulateCentralStores()
|
||||||
usePopulateCentralProductStore()
|
usePopulateCentralProductStore()
|
||||||
|
|
@ -30,111 +284,84 @@ function FlashDeliveryPage() {
|
||||||
const { productsById } = useCentralProductStore()
|
const { productsById } = useCentralProductStore()
|
||||||
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap)
|
const productSlotsMap = useCentralSlotStore((state) => state.productSlotsMap)
|
||||||
|
|
||||||
const tags = productsData?.tags || []
|
|
||||||
const stores = storesData?.stores || []
|
|
||||||
|
|
||||||
const addToCart = useAddToCart('flash')
|
const addToCart = useAddToCart('flash')
|
||||||
const { data: cartData } = useGetCart('flash')
|
const { data: cartData } = useGetCart('flash')
|
||||||
|
|
||||||
// Get flash products from central store
|
// Single memo for the flash catalogue: tabs and per-tab rows are derived
|
||||||
const allFlashProducts = useMemo(() => {
|
// together so the page never holds two views of the same payload. Picking a
|
||||||
return Object.values(productsById).filter(
|
// tab no longer touches any of it.
|
||||||
|
const {
|
||||||
|
tagItems,
|
||||||
|
tagIds,
|
||||||
|
productsByTagId,
|
||||||
|
storeItems,
|
||||||
|
storeIds,
|
||||||
|
productsByStoreId,
|
||||||
|
allFlashProducts,
|
||||||
|
} = useMemo(() => {
|
||||||
|
const tags = productsData?.tags || []
|
||||||
|
const stores = storesData?.stores || []
|
||||||
|
|
||||||
|
const allFlashProducts = Object.values(productsById).filter(
|
||||||
(product: any) =>
|
(product: any) =>
|
||||||
product &&
|
product &&
|
||||||
productSlotsMap[product.id]?.isFlashAvailable &&
|
productSlotsMap[product.id]?.isFlashAvailable &&
|
||||||
!productSlotsMap[product.id]?.isOutOfStock
|
!productSlotsMap[product.id]?.isOutOfStock
|
||||||
)
|
)
|
||||||
}, [productsById, productSlotsMap])
|
|
||||||
|
|
||||||
const flashProductIds = useMemo(
|
const flashProductIds = new Set<number>(allFlashProducts.map((p: any) => p.id))
|
||||||
() => new Set<number>(allFlashProducts.map((p: any) => p.id)),
|
const flashStoreIds = new Set<number>(allFlashProducts.map((p: any) => p.storeId))
|
||||||
[allFlashProducts]
|
|
||||||
)
|
|
||||||
const flashStoreIds = useMemo(
|
|
||||||
() => new Set<number>(allFlashProducts.map((p: any) => p.storeId)),
|
|
||||||
[allFlashProducts]
|
|
||||||
)
|
|
||||||
|
|
||||||
// A tab only exists when it has at least one in-stock flash product.
|
// A tab only exists when it has at least one in-stock flash product.
|
||||||
const tagTabs = useMemo(
|
const tagTabs = tags.filter((tag: any) =>
|
||||||
() =>
|
((tag.productIds || []) as number[]).some((id) => flashProductIds.has(id))
|
||||||
tags.filter((tag: any) =>
|
|
||||||
((tag.productIds || []) as number[]).some((id) => flashProductIds.has(id))
|
|
||||||
),
|
|
||||||
[tags, flashProductIds]
|
|
||||||
)
|
|
||||||
const storeTabs = useMemo(
|
|
||||||
() => stores.filter((store: any) => flashStoreIds.has(store.id)),
|
|
||||||
[stores, flashStoreIds]
|
|
||||||
)
|
|
||||||
|
|
||||||
// Tag tabs — same order and default as the home page.
|
|
||||||
const [selectedTagId, setSelectedTagId] = useState<number | null>(null)
|
|
||||||
const activeTagId = tagTabs.some((tag: any) => tag.id === selectedTagId)
|
|
||||||
? selectedTagId
|
|
||||||
: tagTabs[0]?.id ?? null
|
|
||||||
const activeTag = tagTabs.find((tag: any) => tag.id === activeTagId) || null
|
|
||||||
const tagProductIds = useMemo(
|
|
||||||
() => new Set<number>((activeTag?.productIds || []) as number[]),
|
|
||||||
[activeTag]
|
|
||||||
)
|
|
||||||
|
|
||||||
// Auto-scroll the active tag into view
|
|
||||||
const tabsScrollRef = useRef<HTMLDivElement>(null)
|
|
||||||
const tabPositions = useRef<number[]>([])
|
|
||||||
const activeTabIndex = Math.max(
|
|
||||||
0,
|
|
||||||
tagTabs.findIndex((tag: any) => tag.id === activeTagId)
|
|
||||||
)
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const x = tabPositions.current[activeTabIndex]
|
|
||||||
if (x != null) {
|
|
||||||
tabsScrollRef.current?.scrollTo({ left: Math.max(0, x - 16), behavior: 'smooth' })
|
|
||||||
}
|
|
||||||
}, [activeTabIndex])
|
|
||||||
|
|
||||||
// Products to show: the active tag's flash items, or everything when no
|
|
||||||
// tags are configured.
|
|
||||||
const filteredProducts = useMemo(() => {
|
|
||||||
if (activeTagId == null) return allFlashProducts
|
|
||||||
return allFlashProducts.filter((p: any) => tagProductIds.has(p.id))
|
|
||||||
}, [activeTagId, allFlashProducts, tagProductIds])
|
|
||||||
|
|
||||||
// Home's pattern: a page at a time, with a Show More button.
|
|
||||||
const [visibleCount, setVisibleCount] = useState(PAGE_SIZE)
|
|
||||||
useEffect(() => {
|
|
||||||
setVisibleCount(PAGE_SIZE)
|
|
||||||
}, [activeTagId])
|
|
||||||
const visibleProducts = filteredProducts.slice(0, visibleCount)
|
|
||||||
|
|
||||||
// Shop by Store — one tab per store that has flash products.
|
|
||||||
const [selectedStoreId, setSelectedStoreId] = useState<number | null>(null)
|
|
||||||
const activeStoreId = storeTabs.some((store: any) => store.id === selectedStoreId)
|
|
||||||
? selectedStoreId
|
|
||||||
: storeTabs[0]?.id ?? null
|
|
||||||
const storeProducts = useMemo(() => {
|
|
||||||
if (activeStoreId == null) return allFlashProducts
|
|
||||||
return allFlashProducts.filter((p: any) => p.storeId === activeStoreId)
|
|
||||||
}, [activeStoreId, allFlashProducts])
|
|
||||||
|
|
||||||
const [storeVisibleCount, setStoreVisibleCount] = useState(PAGE_SIZE)
|
|
||||||
useEffect(() => {
|
|
||||||
setStoreVisibleCount(PAGE_SIZE)
|
|
||||||
}, [activeStoreId])
|
|
||||||
const visibleStoreProducts = storeProducts.slice(0, storeVisibleCount)
|
|
||||||
|
|
||||||
const handleAddToCart = (productId: number) => {
|
|
||||||
const item = allFlashProducts.find((p: any) => p.id === productId)
|
|
||||||
addToCart.mutate(
|
|
||||||
{ skuId: productId, quantity: 1, storeId: item?.storeId ?? 0 },
|
|
||||||
{
|
|
||||||
onSuccess: () => {
|
|
||||||
alert(`Added ${item?.name || 'item'} for 1 Hr Delivery`)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
}
|
const storeTabs = stores.filter((store: any) => flashStoreIds.has(store.id))
|
||||||
|
|
||||||
|
const productsByTagId: Record<number, any[]> = {}
|
||||||
|
for (const tag of tagTabs) {
|
||||||
|
const ids = new Set<number>((tag.productIds || []) as number[])
|
||||||
|
productsByTagId[tag.id] = allFlashProducts.filter((product: any) =>
|
||||||
|
ids.has(product.id)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const productsByStoreId: Record<number, any[]> = {}
|
||||||
|
for (const store of storeTabs) {
|
||||||
|
productsByStoreId[store.id] = allFlashProducts.filter(
|
||||||
|
(product: any) => product.storeId === store.id
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
tagItems: tagTabs.map((tag: any) => ({ id: tag.id, label: tag.tagName })),
|
||||||
|
tagIds: tagTabs.map((tag: any) => tag.id),
|
||||||
|
productsByTagId,
|
||||||
|
storeItems: storeTabs.map((store: any) => ({
|
||||||
|
id: store.id,
|
||||||
|
label: store.name.replace(/^The\s+/i, ''),
|
||||||
|
})),
|
||||||
|
storeIds: storeTabs.map((store: any) => store.id),
|
||||||
|
productsByStoreId,
|
||||||
|
allFlashProducts,
|
||||||
|
}
|
||||||
|
}, [productsData, storesData, productsById, productSlotsMap])
|
||||||
|
|
||||||
|
// Stable so FlashProductItem memos survive a section re-render.
|
||||||
|
const handleAddToCart = useCallback(
|
||||||
|
(productId: number) => {
|
||||||
|
const item = allFlashProducts.find((p: any) => p.id === productId)
|
||||||
|
addToCart.mutate(
|
||||||
|
{ skuId: productId, quantity: 1, storeId: item?.storeId ?? 0 },
|
||||||
|
{
|
||||||
|
onSuccess: () => {
|
||||||
|
alert(`Added ${item?.name || 'item'} for 1 Hr Delivery`)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
},
|
||||||
|
[addToCart, allFlashProducts]
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppLayout isFlashDelivery={true}>
|
<AppLayout isFlashDelivery={true}>
|
||||||
|
|
@ -161,155 +388,21 @@ function FlashDeliveryPage() {
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Tag tabs — same model as the home page tabs */}
|
{/* Tag tabs — same model as the home page tabs */}
|
||||||
{tagTabs.length > 0 && (
|
<FlashTagSection
|
||||||
<div
|
tagItems={tagItems}
|
||||||
ref={tabsScrollRef}
|
tagIds={tagIds}
|
||||||
className="scrollbar-hide -mx-4 mb-5 flex gap-1 overflow-x-auto px-4 md:mx-0 md:px-0"
|
productsByTagId={productsByTagId}
|
||||||
>
|
fallbackProducts={allFlashProducts}
|
||||||
{tagTabs.map((tag: any, i: number) => {
|
onAddToCart={handleAddToCart}
|
||||||
const active = activeTagId === tag.id
|
/>
|
||||||
return (
|
|
||||||
<button
|
|
||||||
key={tag.id}
|
|
||||||
ref={(el) => {
|
|
||||||
tabPositions.current[i] = el?.offsetLeft ?? 0
|
|
||||||
}}
|
|
||||||
onClick={() => setSelectedTagId(tag.id)}
|
|
||||||
className="shrink-0 cursor-pointer px-3 py-2"
|
|
||||||
>
|
|
||||||
<span
|
|
||||||
className={`block text-sm font-bold ${
|
|
||||||
active ? 'text-flash-600' : 'text-gray-500'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{tag.tagName}
|
|
||||||
</span>
|
|
||||||
<span
|
|
||||||
className={`mt-1 block h-1 rounded-full ${
|
|
||||||
active ? 'bg-flash-500' : 'bg-transparent'
|
|
||||||
}`}
|
|
||||||
/>
|
|
||||||
</button>
|
|
||||||
)
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<div className="mb-4 flex items-center justify-between">
|
{storeIds.length > 0 && (
|
||||||
<p className="text-lg font-extrabold text-gray-900">
|
<FlashStoreSection
|
||||||
{activeTag?.tagName ?? 'All Products'}
|
storeItems={storeItems}
|
||||||
</p>
|
storeIds={storeIds}
|
||||||
<p className="text-sm text-gray-500">
|
productsByStoreId={productsByStoreId}
|
||||||
{filteredProducts.length} items
|
onAddToCart={handleAddToCart}
|
||||||
</p>
|
/>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="grid grid-cols-3 gap-3 sm:gap-4 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5">
|
|
||||||
{visibleProducts.map((product: any) => (
|
|
||||||
<CompactProductCard
|
|
||||||
key={product.id}
|
|
||||||
item={product}
|
|
||||||
handleAddToCart={handleAddToCart}
|
|
||||||
onPress={() =>
|
|
||||||
navigate({
|
|
||||||
to: '/flash/product/$id',
|
|
||||||
params: { id: String(product.id) },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{filteredProducts.length > visibleCount && (
|
|
||||||
<div className="mt-6 flex justify-center">
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={() => setVisibleCount((count) => count + PAGE_SIZE)}
|
|
||||||
className="cursor-pointer rounded-full bg-flash-500 px-6 py-3 text-sm font-semibold text-white shadow-sm transition-colors hover:bg-flash-600"
|
|
||||||
>
|
|
||||||
Show More
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{filteredProducts.length === 0 && (
|
|
||||||
<div className="py-10 text-center">
|
|
||||||
<p className="font-medium text-gray-400">
|
|
||||||
No flash delivery products available.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{storeTabs.length > 0 && (
|
|
||||||
<div className="mt-10">
|
|
||||||
<div className="mb-2 flex items-center justify-between">
|
|
||||||
<p className="text-lg font-extrabold text-gray-900">Shop by Store</p>
|
|
||||||
<p className="text-sm text-gray-500">{storeProducts.length} items</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{/* Store tabs — one per store, scrolled by the page itself */}
|
|
||||||
<div className="scrollbar-hide -mx-4 mb-5 flex gap-1 overflow-x-auto px-4 md:mx-0 md:px-0">
|
|
||||||
{storeTabs.map((store: any) => {
|
|
||||||
const active = activeStoreId === store.id
|
|
||||||
return (
|
|
||||||
<button
|
|
||||||
key={store.id}
|
|
||||||
onClick={() => setSelectedStoreId(store.id)}
|
|
||||||
className="shrink-0 cursor-pointer px-3 py-2"
|
|
||||||
>
|
|
||||||
<span
|
|
||||||
className={`block text-sm font-bold ${
|
|
||||||
active ? 'text-flash-600' : 'text-gray-500'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{store.name.replace(/^The\s+/i, '')}
|
|
||||||
</span>
|
|
||||||
<span
|
|
||||||
className={`mt-1 block h-1 rounded-full ${
|
|
||||||
active ? 'bg-flash-500' : 'bg-transparent'
|
|
||||||
}`}
|
|
||||||
/>
|
|
||||||
</button>
|
|
||||||
)
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="grid grid-cols-3 gap-3 sm:gap-4 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5">
|
|
||||||
{visibleStoreProducts.map((product: any) => (
|
|
||||||
<CompactProductCard
|
|
||||||
key={product.id}
|
|
||||||
item={product}
|
|
||||||
handleAddToCart={handleAddToCart}
|
|
||||||
onPress={() =>
|
|
||||||
navigate({
|
|
||||||
to: '/flash/product/$id',
|
|
||||||
params: { id: String(product.id) },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{storeProducts.length > storeVisibleCount && (
|
|
||||||
<div className="mt-6 flex justify-center">
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
onClick={() => setStoreVisibleCount((count) => count + PAGE_SIZE)}
|
|
||||||
className="cursor-pointer rounded-full bg-flash-500 px-6 py-3 text-sm font-semibold text-white shadow-sm transition-colors hover:bg-flash-600"
|
|
||||||
>
|
|
||||||
Show More
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{storeProducts.length === 0 && (
|
|
||||||
<div className="py-10 text-center">
|
|
||||||
<p className="font-medium text-gray-400">
|
|
||||||
No flash delivery products available.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -317,7 +410,9 @@ function FlashDeliveryPage() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function CompactProductCard({
|
// Memoised: identical to the native ProductCard change — an unmemoised card
|
||||||
|
// re-runs its cart hooks for every sibling whenever its section re-renders.
|
||||||
|
const CompactProductCard = memo(function CompactProductCard({
|
||||||
item,
|
item,
|
||||||
handleAddToCart,
|
handleAddToCart,
|
||||||
onPress,
|
onPress,
|
||||||
|
|
@ -401,4 +496,4 @@ function CompactProductCard({
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
})
|
||||||
|
|
|
||||||
|
|
@ -436,3 +436,55 @@ list keeps roughly a seventh of the off-screen rows it used to.
|
||||||
~ src/components/CouponForm.tsx
|
~ src/components/CouponForm.tsx
|
||||||
~ src/components/TagForm.tsx
|
~ src/components/TagForm.tsx
|
||||||
- admin-web uses the non-virtualized web MyFlatList; unchanged.
|
- admin-web uses the non-virtualized web MyFlatList; unchanged.
|
||||||
|
|
||||||
|
[2026-11-02 08:01:30 IST] flash-delivery: move tab/search selection out of the page component so a tab tap stops re-rendering the whole screen.
|
||||||
|
|
||||||
|
The page held selectedTagId, selectedStoreId, searchQuery and both visible
|
||||||
|
counts in FlashDeliveryProducts. A tap re-rendered the page, both strips, both
|
||||||
|
grids and every ProductCard (ProductCard is not memoized and renderProduct
|
||||||
|
built a new onPress per render). Tabs also rendered twice per tap: the
|
||||||
|
visible-count reset lived in a useEffect that ran after paint.
|
||||||
|
|
||||||
|
--- apps/user-ui/src/store/flashTabStore.ts (new)
|
||||||
|
+ selectedTagId / selectedStoreId / searchQuery plus selectTag, selectStore,
|
||||||
|
setSearchQuery, resetFlashTabs. select* are same-value no-ops (same guard as
|
||||||
|
homeTagStore) so re-tapping the active tab does not wake subscribers.
|
||||||
|
--- apps/web-ui/src/lib/stores/flash-tab-store.ts (new)
|
||||||
|
+ mirror of the native store (same shape, same reset).
|
||||||
|
--- apps/user-ui/components/SlotSpecificView.tsx
|
||||||
|
~ FlashDeliveryProducts now only derives the catalog: one memo builds
|
||||||
|
tagTabs, storeTabs, per-tag rows (productsByTagId) and per-store rows
|
||||||
|
(productsByStoreId), plus the stable tab item arrays.
|
||||||
|
~ New FlashSearchBar subscribes to the store, so typing no longer re-renders
|
||||||
|
the page, the strips or the other grid.
|
||||||
|
~ New FlashTagStrip / FlashStoreStrip wrappers subscribe to the store and
|
||||||
|
derive activeId themselves; FlashTabStrip keeps its memo and now receives
|
||||||
|
stable items/onSelect, so it no longer rebuilds on every keystroke.
|
||||||
|
~ New FlashTagSection / FlashStoreSection (memo) subscribe to the store, so a
|
||||||
|
tag tap re-renders only the tag grid and a store tap only the store section.
|
||||||
|
~ New FlashProductGrid owns visibleCount; the grid is keyed by active id so a
|
||||||
|
tab switch resets the page size in one render (no effect, no double paint).
|
||||||
|
A search change resets it during render with the same pattern.
|
||||||
|
~ New FlashProductItem (memo) derives its onPress from item.id; the page's
|
||||||
|
onProductPress handler is a useCallback.
|
||||||
|
--- apps/user-ui/components/ProductCard.tsx
|
||||||
|
~ default export wrapped in React.memo; parents pass stable item/onPress.
|
||||||
|
--- apps/user-ui/app/(drawer)/(tabs)/flash-delivery/(products)/index.tsx
|
||||||
|
~ handleProductPress wrapped in useCallback (it was recreated every render,
|
||||||
|
which defeated memoisation on every card).
|
||||||
|
--- apps/user-ui/src/lib/region-switch.ts, apps/web-ui/src/lib/region-switch.ts
|
||||||
|
~ resetFlashTabs() on region switch: tag/store ids are region-scoped.
|
||||||
|
--- apps/user-ui/tests/helpers/reset.ts
|
||||||
|
~ reset the flash tab store between cases.
|
||||||
|
--- apps/web-ui/src/routes/flash.tsx
|
||||||
|
~ same restructure: store-backed selection/search, memoized
|
||||||
|
FlashTagStrip / FlashStoreStrip / FlashProductGrid / FlashProductItem,
|
||||||
|
CompactProductCard wrapped in memo with a stable handleAddToCart.
|
||||||
|
- Flash grids are still plain maps inside the page ScrollView (no FlatList);
|
||||||
|
virtualization left for a follow-up since the page stacks two sections.
|
||||||
|
--- apps/user-ui/tests/lib/stores.test.ts
|
||||||
|
+ STORE-23: re-selecting the active flash tag/store keeps the store identity.
|
||||||
|
+ STORE-24: resetFlashTabs drops both selections and the search box.
|
||||||
|
+ STORE-25: an unchanged flash search query keeps the store identity.
|
||||||
|
--- apps/user-ui/JEST-TEST-CASES.md
|
||||||
|
~ catalogue rows STORE-23..25 added.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue