This commit is contained in:
shafi54 2026-11-01 14:15:17 +05:30
parent 6229ee9999
commit 2881c1ede8
8 changed files with 134 additions and 23 deletions

View file

@ -526,6 +526,8 @@ This file covers the route wrapper, `SlotLayout`, `SlotProducts` and the flash p
| FLASH-20 | happy | Shop by Store | heading + tab only for stores with flash products | | FLASH-20 | happy | Shop by Store | heading + tab only for stores with flash products |
| FLASH-21 | happy | store with flash products | its own tab and grid | | FLASH-21 | happy | store with flash products | its own tab and grid |
| FLASH-22 | edge | no stores | Shop by Store section hidden; tag section still renders | | FLASH-22 | edge | no stores | Shop by Store section hidden; tag section still renders |
| FLASH-24 | edge | background catalogue refetch | pull-to-refresh control stays idle |
| FLASH-25 | happy | pull down | spinner stays up until products + stores settle |
--- ---
@ -1555,6 +1557,7 @@ Covered by HOME-81..98, plus:
| HIDE-01 | edge | focus with key | hideTabsNav key true | | HIDE-01 | edge | focus with key | hideTabsNav key true |
| HIDE-02 | edge | default key | `default` | | HIDE-02 | edge | default key | `default` |
| HIDE-03 | edge | blur | false | | HIDE-03 | edge | blur | false |
| HIDE-04 | edge | re-render | store map identity unchanged (no per-render rewrite) |
| PSI-01 | edge | earliest future slot | id | | PSI-01 | edge | earliest future slot | id |
| PSI-02 | edge | past delivery only | null | | PSI-02 | edge | past delivery only | null |
| PSI-03 | edge | no map/slots | null | | PSI-03 | edge | no map/slots | null |
@ -1594,6 +1597,7 @@ Covered by HOME-81..98, plus:
| STORE-17 | edge | clear region | nulls + bump | | STORE-17 | edge | clear region | nulls + bump |
| STORE-18 | edge | clear already empty | no bump | | STORE-18 | edge | clear already empty | no bump |
| STORE-19 | edge | region memory only | no persist | | STORE-19 | edge | region memory only | no persist |
| STORE-20 | edge | same value written twice | map identity preserved (no subscriber wake) |
### App helpers ### App helpers

View file

@ -559,6 +559,10 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery
const [selectedStoreId, setSelectedStoreId] = useState<number | null>(null); const [selectedStoreId, setSelectedStoreId] = useState<number | null>(null);
const [tagVisibleCount, setTagVisibleCount] = useState(FLASH_PAGE_SIZE); const [tagVisibleCount, setTagVisibleCount] = useState(FLASH_PAGE_SIZE);
const [storeVisibleCount, setStoreVisibleCount] = 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
// background fetch (remount-after-stale, invalidation, version bump), which
// made the pull-to-refresh spinner pop up on its own.
const [isRefreshing, setIsRefreshing] = useState(false);
const router = useRouter(); const router = useRouter();
const productsQuery = useAllProducts(); const productsQuery = useAllProducts();
@ -652,6 +656,29 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery
const visibleTagProducts = tagProducts.slice(0, tagVisibleCount); const visibleTagProducts = tagProducts.slice(0, tagVisibleCount);
const visibleStoreProducts = storeProducts.slice(0, storeVisibleCount); const visibleStoreProducts = storeProducts.slice(0, storeVisibleCount);
const refetchProducts = productsQuery?.refetch;
const handleRefresh = React.useCallback(async () => {
setIsRefreshing(true);
try {
await Promise.all([refetchProducts?.(), refetchStores()]);
} finally {
setIsRefreshing(false);
}
}, [refetchProducts, refetchStores]);
// Memoised so the platform control is not rebuilt on every render.
const refreshControl = React.useMemo(
() => (
<RefreshControl
refreshing={isRefreshing}
onRefresh={handleRefresh}
tintColor={theme.colors.brand500}
colors={[theme.colors.brand500]}
/>
),
[isRefreshing, handleRefresh]
);
const renderProduct = (item: any) => ( const renderProduct = (item: any) => (
<View key={item.id} style={tw`mb-3`}> <View key={item.id} style={tw`mb-3`}>
<ProductCard <ProductCard
@ -724,17 +751,7 @@ export function FlashDeliveryProducts({ baseUrl, onProductPress }: FlashDelivery
<ScrollView <ScrollView
style={tw`flex-1 bg-white`} style={tw`flex-1 bg-white`}
contentContainerStyle={tw`pb-24`} contentContainerStyle={tw`pb-24`}
refreshControl={ refreshControl={refreshControl}
<RefreshControl
refreshing={productsQuery.isFetching}
onRefresh={() => {
productsQuery.refetch();
refetchStores();
}}
tintColor={theme.colors.brand500}
colors={[theme.colors.brand500]}
/>
}
> >
<View style={tw`px-4 pt-2`}> <View style={tw`px-4 pt-2`}>
<SearchBar <SearchBar

View file

@ -1,14 +1,20 @@
import * as React from 'react';
import { useFocusEffect } from '@react-navigation/native'; import { useFocusEffect } from '@react-navigation/native';
import { useAppStore } from '@/src/store/appStore'; import { useAppStore } from '@/src/store/appStore';
export const useHideTabNav = (caller: string = 'default') => { export const useHideTabNav = (caller: string = 'default') => {
const setHideTabsNavKey = useAppStore((state) => state.setHideTabsNavKey); const setHideTabsNavKey = useAppStore((state) => state.setHideTabsNavKey);
useFocusEffect(() => { // `useFocusEffect` re-runs whenever the callback identity changes, so an
setHideTabsNavKey(caller, true); // Hide tabs when screen focuses // inline callback would unsubscribe/resubscribe and write false -> true into
// the store on every render, flickering the tab bar.
useFocusEffect(
React.useCallback(() => {
setHideTabsNavKey(caller, true); // Hide tabs when screen focuses
return () => { return () => {
setHideTabsNavKey(caller, false); // Show tabs when screen blurs setHideTabsNavKey(caller, false); // Show tabs when screen blurs
}; };
}); }, [caller, setHideTabsNavKey])
}; );
};

View file

@ -7,8 +7,12 @@ interface AppStore {
export const useAppStore = create<AppStore>((set) => ({ export const useAppStore = create<AppStore>((set) => ({
hideTabsNav: {}, hideTabsNav: {},
// Writing the value a key already has would still allocate a new map and
// wake every subscriber (the tab navigator included), so bail out instead.
setHideTabsNavKey: (key: string, hide: boolean) => setHideTabsNavKey: (key: string, hide: boolean) =>
set((state) => ({ set((state) =>
hideTabsNav: { ...state.hideTabsNav, [key]: hide } state.hideTabsNav[key] === hide
})), ? state
: { hideTabsNav: { ...state.hideTabsNav, [key]: hide } }
),
})); }));

View file

@ -274,7 +274,7 @@ describe('trpc client link', () => {
}) })
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* HIDE-01..03 */ /* HIDE-01..04 */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
function HideProbe({ caller }: { caller?: string }) { function HideProbe({ caller }: { caller?: string }) {
@ -302,6 +302,17 @@ describe('useHideTabNav', () => {
act(() => view.unmount()) act(() => view.unmount())
expect(useAppStore.getState().hideTabsNav.cart).toBe(false) expect(useAppStore.getState().hideTabsNav.cart).toBe(false)
}) })
it('HIDE-04: re-rendering does not rewrite the store', () => {
const view = renderWithProviders(<HideProbe caller="cart" />, { auth: false })
const before = useAppStore.getState().hideTabsNav
act(() => view.rerender(<HideProbe caller="cart" />))
// An inline focus callback used to unsubscribe/resubscribe per render,
// writing false -> true and flickering the tab bar.
expect(useAppStore.getState().hideTabsNav).toBe(before)
})
}) })
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */

View file

@ -50,6 +50,16 @@ describe('stores', () => {
expect(useAppStore.getState().hideTabsNav).toEqual({ quick_delivery: false }) expect(useAppStore.getState().hideTabsNav).toEqual({ quick_delivery: false })
}) })
it('STORE-20: writing the value a hide key already has keeps the map identity', () => {
useAppStore.getState().setHideTabsNavKey('cart', true)
const hidden = useAppStore.getState().hideTabsNav
useAppStore.getState().setHideTabsNavKey('cart', true)
// Same object, so subscribers (the tab navigator) are not woken.
expect(useAppStore.getState().hideTabsNav).toBe(hidden)
})
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)

View file

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
/** /**
* Catalogue section 8.3 — Flash products (`tests/pages/flash-products.test.tsx`, * Catalogue section 8.3 — Flash products (`tests/pages/flash-products.test.tsx`,
* FLASH-01..21). * FLASH-01..25).
* *
* The page is `flash-delivery/(products)/index.tsx` plus `FlashDeliveryProducts` * The page is `flash-delivery/(products)/index.tsx` plus `FlashDeliveryProducts`
* in `components/SlotSpecificView.tsx`. The screen has two independently paged * in `components/SlotSpecificView.tsx`. The screen has two independently paged
@ -473,4 +473,35 @@ describe('Flash products (FLASH)', () => {
// The tag section still renders. // The tag section still renders.
expect(tagSection(view).getByText('Chicken Breast')).toBeTruthy() expect(tagSection(view).getByText('Chicken Breast')).toBeTruthy()
}) })
it('FLASH-24: a background refetch leaves the pull-to-refresh spinner idle', async () => {
const view = renderFlash()
await load()
const control = () => view.UNSAFE_getAllByType(RefreshControl)[0]
expect(control().props.refreshing).toBe(false)
// Something else refetches the catalogue (remount after the stale window,
// an invalidation, a version bump). It must not drive the user's control.
setJsonPending(productsCacheUrl())
await act(async () => {
void view.queryClient.refetchQueries({ queryKey: ['all-products'] })
})
await flush(2)
expect(control().props.refreshing).toBe(false)
})
it('FLASH-25: pulling down keeps the spinner up until the refresh settles', async () => {
const view = renderFlash()
await load()
setJsonPending(productsCacheUrl())
await act(async () => {
void view.UNSAFE_getAllByType(RefreshControl)[0].props.onRefresh()
})
await flush(1)
expect(view.UNSAFE_getAllByType(RefreshControl)[0].props.refreshing).toBe(true)
})
}) })

View file

@ -142,3 +142,31 @@ Web offers page already uses a 12px grid gap (gap-3), so no web change.
~ Same two sections, store tabs below the tag grid, independent 9-item paging. ~ Same two sections, store tabs below the tag grid, independent 9-item paging.
--- tests --- tests
~ Product assertions scoped to the tag grid; new store-section cases. ~ Product assertions scoped to the tag grid; new store-section cases.
[2026-11-01 14:06:51 IST] Flash delivery: the pull-to-refresh spinner no longer reports background fetches.
Diagnosed with a throwaway cache-subscription test (25s, real timers): the flash
screen itself starts no background fetches (only the 7 initial ones), and the
catalogue URL is built from assetsDomain/apiCacheKey/region/version — stable, so
the products query key does not churn. There is no 10s timer anywhere in
user-ui. What is wrong is the wiring: `refreshing` was bound to
`productsQuery.isFetching`, so *any* fetch (mount-after-stale, invalidation,
version bump, pull) drove the pull-to-refresh control, and the control element
was rebuilt on every render (re-attaching the platform control).
--- apps/user-ui/components/SlotSpecificView.tsx
~ FlashDeliveryProducts: `isRefreshing` local state + handleRefresh
(refetch products + stores, clear in finally), RefreshControl element
memoised. Mirrors home's handleRefresh. Background fetches are invisible.
--- apps/user-ui/src/hooks/useHideTabNav.ts
~ Focus callback memoised (was recreated every render, so useFocusEffect
unsubscribed/resubscribed per render and wrote false->true into the store,
flickering the tab bar on every render of every screen using it).
--- apps/user-ui/src/store/appStore.ts
~ setHideTabsNavKey no longer allocates when the key already has that value,
so it cannot notify subscribers spuriously.
--- tests
+ FLASH-24: a background refetch leaves the spinner idle.
+ FLASH-25: a pull keeps the spinner up until the refresh settles.
+ HIDE-04: re-render does not rewrite the store.
+ STORE-20: same-value write keeps the map identity.