Orders tab and order detail screens

You build the Orders tab and order detail screen, with paginated history and a timeline that polls while the order is visible and unfinished.

Ticket

Build the Orders tab and order detail screen. The tab pages through the customer's past orders. Each row opens a dynamic route for one order, where you display its status and the events recorded by the API.

Customers need to check previous orders and follow an active order without repeatedly refreshing the screen themselves. Week 5 replaces Polling with WebSockets. The screens and order data functions you build here provide the client-side foundation for that change.

Acceptance criteria

  • ordersPath(cursor) requests /v1/orders?limit=20 and adds the cursor parameter only when the cursor is not empty.
  • statusLabel maps PLACED to "Placed", ACCEPTED to "Accepted by the restaurant", PREPARING to "Being prepared", READY to "Ready for pickup", PICKED_UP to "On the way", DELIVERED to "Delivered", CANCELLED to "Cancelled" and REJECTED to "Rejected by the restaurant".
  • isFinal returns true for DELIVERED, CANCELLED and REJECTED, and false for other statuses.
  • timeline(order) maps the order's events in their original order to { status, label, at }, using each event's to_status, its human-readable status label and its created_at.
  • pollInterval(order) returns 10_000 when the order has not loaded or can still change. It returns false when the order is DELIVERED, CANCELLED or REJECTED.
  • useOrders uses an infinite query with the query key ["orders"], initialPageParam: "" and getNextPageParam: nextCursor.
  • useOrder(api, id) uses the query key ["orders", id] and is enabled only when id is a positive integer. Its refetchInterval uses pollInterval(query.state.data) while the screen is focused and returns false while it is not focused.
  • Both query keys share the ["orders"] prefix, so checkout's query invalidation refreshes the order list and any open order detail.
  • The Orders tab renders a FlatList with guarded onEndReached, pull-to-refresh through RefreshControl, a footer spinner while fetching another page and the empty state "No orders yet". Each row shows "Order #id", the local date and time, the status label and the total. Tapping a row opens /orders/[id].
  • The Orders tab shows a loading state while the first page loads. When loading fails and there are no orders, it shows an error state with a retry action.
  • The detail screen rejects an invalid order id. For a valid order, it displays the status as a large title, the total and either "Updates every 10 seconds" or "Finished".
  • The detail screen renders one timeline dot for each event, in event order. The latest event has a red dot. Each step shows its status label and local time.
  • Both screens provide loading and error states with a retry action.
  • After placing two orders, both appear in the Orders tab newest first without pulling to refresh.
  • When you send POST /v1/orders/{id}/events with { "event_id": "...", "event": "ACCEPT" } from another terminal as a restaurant user, the open detail screen shows the updated status and a new timeline step within 10 seconds.
  • When another screen is pushed over the order detail screen, requests for that order stop. They resume when you return to the detail screen.
  • After an order is cancelled or delivered, the detail screen displays "Finished" and polling stops.
  • Opening another customer's order id through a deep link shows the error state for the API's 404 response.
  • make test W=4B8 passes all 6 tests in app-tests/lib/orders.test.ts, and npx tsc --noEmit passes in app/.

Constraints

  • Request 20 orders per page.
  • Use a polling interval of 10_000 milliseconds.
  • Poll only while the detail screen is focused and the order can still change.
  • Enable the detail query only for a positive integer order id.
  • Guard onEndReached so you do not fetch another page when there is no next page or a page is already being fetched.

Design direction

Keep the order types, status labels, final-state rule, URL construction, fetching functions, timeline mapping and polling interval in app/lib/orders.ts. These functions describe the API contract and remain independent of React components.

Put useOrders and useOrder in app/lib/useOrders.ts. Use TanStack Query for pagination and refetching. Let useIsFocused() from Expo Router control whether the detail query polls. A screen covered by another screen in the navigation stack can remain mounted, so mounting alone is not a sufficient visibility check.

Keep the two screens responsible for rendering their data and handling navigation. Use FlatList for the order history rather than rendering every row inside a ScrollView. Keep the timeline as a rendering of the server's events rather than inferring previous statuses from the current status.

Think about where to place the status vocabulary and how to share query keys. A single query key prefix makes checkout invalidation easier to reason about. A pure polling decision keeps final-state behaviour separate from screen focus. Any layout that meets the contract passes.

How to check your work

Run the commands from the repository root:

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

Open the app on an emulator or phone. Place two orders and check that the Orders tab shows both without a manual refresh. Open an order detail, change its status through the Week 3 events endpoint, then check that polling pauses when the screen is covered and stops permanently for a final status. Test a deep link to another customer's order as well.

When a test fails, start with its first failing assertion. It identifies the observed value that differs from the contract. Fix that behaviour before investigating later failures.

Finished Exercise 4.17?

Order history

1 items

  • 4.17Orders tab and order detail screensnot completed

Mobile API capacity

Up next · Module 7 · 1 items · about 50 min

Start module 7

Implementation

Run locally

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

  1. Add order types and pure functions in app/lib/orders.ts.
  2. Implement the list and detail queries in app/lib/useOrders.ts.
  3. Build the Orders tab with pagination and refresh.
  4. Build the order detail screen with its event timeline.
  5. Verify focus-aware polling and final-state behaviour.
  6. Run the W4B8 tests and TypeScript check.

Commands

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

Tests

  • W4 B8 order history (6 tests) in app-tests/lib/orders.test.ts