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=20and adds thecursorparameter only when the cursor is not empty.statusLabelmapsPLACEDto "Placed",ACCEPTEDto "Accepted by the restaurant",PREPARINGto "Being prepared",READYto "Ready for pickup",PICKED_UPto "On the way",DELIVEREDto "Delivered",CANCELLEDto "Cancelled" andREJECTEDto "Rejected by the restaurant".isFinalreturnstrueforDELIVERED,CANCELLEDandREJECTED, andfalsefor other statuses.timeline(order)maps the order's events in their original order to{ status, label, at }, using each event'sto_status, its human-readable status label and itscreated_at.pollInterval(order)returns10_000when the order has not loaded or can still change. It returnsfalsewhen the order isDELIVERED,CANCELLEDorREJECTED.useOrdersuses an infinite query with the query key["orders"],initialPageParam: ""andgetNextPageParam: nextCursor.useOrder(api, id)uses the query key["orders", id]and is enabled only whenidis a positive integer. ItsrefetchIntervalusespollInterval(query.state.data)while the screen is focused and returnsfalsewhile 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 throughRefreshControl, 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}/eventswith{ "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
404response. make test W=4B8passes all 6 tests inapp-tests/lib/orders.test.ts, andnpx tsc --noEmitpasses inapp/.
Constraints
- Request 20 orders per page.
- Use a polling interval of
10_000milliseconds. - 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
onEndReachedso 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:
cd backend && make test W=4B8
cd app && npx tsc --noEmit
cd app && npx expo startOpen 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.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Order history
1 items
- 4.17Orders tab and order detail screensnot completed
Up next · Module 7 · 1 items · about 50 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Add order types and pure functions in
app/lib/orders.ts. - Implement the list and detail queries in
app/lib/useOrders.ts. - Build the Orders tab with pagination and refresh.
- Build the order detail screen with its event timeline.
- Verify focus-aware polling and final-state behaviour.
- Run the W4B8 tests and TypeScript check.
Commands
cd backend && make test W=4B8cd app && npx tsc --noEmitcd app && npx expo startTests
W4 B8 order history (6 tests)inapp-tests/lib/orders.test.ts