Restaurant list screen

You build the restaurant list screen with cached, paged restaurant data, pull-to-refresh and automatic refetch when the app returns online or to the foreground.

Ticket

Build the restaurant list screen in app/. It loads restaurants near the fixed Week 2 seed point from GET /v1/restaurants, one page at a time. Customers can scroll through the results, pull to refresh, and open a restaurant's menu.

The screen gives customers the starting point for choosing what to order. The next exercise, W4B4, builds the menu screen that opens when a customer selects a restaurant. The backend does not change.

Acceptance criteria

  • Page<T> represents a page with items and next_cursor. nextCursor returns undefined when next_cursor is empty, and returns the cursor otherwise.
  • flatten combines the items from all pages in their original order. It returns an empty array when there are no pages.
  • restaurantsPath always includes lat=28.46, lng=77.03, radius_km=5 and limit=20 for the fixed seed point. It adds cursor only when the cursor is not empty, using URLSearchParams to encode it.
  • fetchRestaurants sends the path through the API client. It does not call fetch directly.
  • openLabel returns "Open" when is_open is true, "Closed" when it is false, and null when the API does not provide is_open.
  • The infinite query uses the query key ["restaurants", lat, lng, radiusKm], initialPageParam: "", getNextPageParam: nextCursor and staleTime: 60_000.
  • The shared TanStack Query client connects focusManager to React Native AppState and onlineManager to expo-network. Queries retry network failures and 5xx responses at most twice, and never retry 4xx responses.
  • The query client is created once at module level in app/lib/queryClient.ts. The root layout uses that instance.
  • The screen uses FlatList for virtualization, not a ScrollView with map. Rows use the restaurant id as their key. renderItem and the row-opening callback use stable useCallback references, and RestaurantRow uses React.memo.
  • Every restaurant row is exactly 76 dp high, including its bottom border. getItemLayout uses that height for its item length and offset calculations.
  • onEndReached uses a threshold of 0.5. It calls fetchNextPage() only when hasNextPage is true and isFetchingNextPage is false.
  • The pull-to-refresh control stays active during a refetch, but not during a next-page fetch. A footer spinner appears while the next page loads.
  • The first pending load displays eight skeleton rows. An error with no data displays the full-screen error and a Try again button. An empty result displays the centred empty state.
  • A failed background refetch or pull-to-refresh keeps the existing restaurants visible. The full-screen error is reserved for an error with no data.
  • Tapping a restaurant pushes /restaurant/[id].
  • The root layout imports the shared query client. The tab layout places Sign out in the tab header's headerRight.

Constraints

  • Use latitude 28.46, longitude 77.03 and radius 5 km for the fixed seed point.
  • Set PAGE_SIZE to 20.
  • Set staleTime to 60_000 milliseconds.
  • Limit query retries to two for network failures and 5xx responses. Never retry 4xx responses.
  • Set the row height to 76 dp and onEndReachedThreshold to 0.5.
  • Show eight skeleton rows during the first pending load.
  • The Week 2 seed contains 50 restaurants, which produces three pages at a page size of 20.

Design direction

Keep the data transformations separate from React components. Put Page<T>, nextCursor and flatten in app/lib/pages.ts. Put the restaurant types, path construction, API request and open label in app/lib/restaurants.ts. These functions are easier to test without rendering a screen.

Put the paging configuration in app/lib/useRestaurants.ts. Keep the shared query client and the app's focus and online event wiring in app/lib/queryClient.ts. Let the root layout provide that client to the app.

Keep the screen focused on presentation. Put the row and its skeleton in app/components/RestaurantRow.tsx, and the loading, error and empty states in app/components/StateViews.tsx. The screen should flatten the query's pages and pass the resulting restaurants to FlatList.

Consider these trade-offs:

  • Keep the query's pages intact for pagination, then flatten them for rendering. This preserves the server's page boundaries without making the list manage cursors.
  • Include every location input in the query key. A different radius or coordinate must not reuse another area's cached result.
  • A fixed-height row makes getItemLayout predictable. Keep the bottom border inside the row so it does not add to the measured height.
  • Keep background errors separate from initial-load errors. A failed refresh should not discard restaurants the customer can already see.
  • Connect focus and network events once through the shared query client. Later screens can then use the same behaviour without installing separate listeners.

Any layout that meets the contract passes.

How to check your work

Run the library tests first, then check TypeScript and launch the app:

bash
cd backend && make test W=4B3
bash
cd app && npx tsc --noEmit
bash
cd app && npx expo start

On your phone, check the initial skeletons, restaurant rows, next-page spinner, pull-to-refresh, offline recovery and menu navigation. To see pagination more clearly, temporarily set the page size to 5, then restore it to 20. When a check fails, read the first failing assertion to identify the specific contract that your implementation violates before changing anything else.

Finished Exercise 4.10?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Add page helpers and restaurant request functions.
  2. Configure the shared query client and infinite query.
  3. Build the memoised 76 dp restaurant row and state views.
  4. Implement the paginated FlatList and pull-to-refresh behaviour.
  5. Verify focus, reconnect and background error handling.
  6. Run the W4B3 tests and check the app on a device.

Commands

terminal
cd backend && make test W=4B3
terminal
cd app && npx tsc --noEmit
terminal
cd app && npx expo start

Tests

  • W4 B3 restaurant list (6 tests) in app-tests/lib/restaurants.test.ts