Cart screen
You build the cart screen that submits the customer's cart as an order and opens the returned order. The cart and its idempotency key remain unchanged until the server confirms success.
Ticket
Build the cart screen in app/app/(tabs)/cart.tsx and the checkout helpers in app/lib/checkout.ts. Customers need to review cart lines, change quantities, see the total, and place an order through POST /v1/orders.
A request can create an order even when its response never reaches the phone. Keep the cart and its idempotency key until the server confirms the order. The order history screen depends on this flow to show the newly created order without a manual refresh.
Acceptance criteria
orderRequest(cart)produces a body withrestaurant_idandlines. Each line containsitem_id,size,chosenandqty. It does not send prices because the server reprices every line.placeOrder(api, cart, clear)rejects a cart with anullrestaurant ID, no lines or no idempotency key. It does not call the API for an invalid cart.- A valid cart produces a
POST /v1/ordersrequest through the API client. The request includes the cart's key in theIdempotency-Keyheader and uses the body produced byorderRequest. placeOrdercallsclear()only after the server returns the placed order. A network error, HTTP error or validation error leaves the cart and its idempotency key unchanged.checkoutErrorMessage(error)handles a network failure with the message"No connection. Your cart is saved, so tapping Place order again is safe.".- A
409response displays"This order is still being placed. Wait a moment, then tap Place order again.". - A
429response displays the wait in seconds using the retry delay, falling back to1second when no retry delay is available. - A
400response displays the server-provided error text. - The cart screen reads
lines,setQtyandtotal()from the Zustand store. When there are no lines, it displays an empty state. - Each cart line displays its description, quantity controls and line total. The minus and plus controls call
setQtyand are disabled while an order is being placed. - The total appears at the bottom of the screen. The displayed prices use formatted rupees, while the underlying values remain in paise.
- Place order uses a TanStack Query mutation. It reads the current cart and its
clearfunction from the Zustand store when the mutation runs, rather than using a cart value captured during rendering. - While the mutation is pending, the button displays
"Placing order..."and is disabled. Tapping Place order twice quickly sends only one request. - On success, the screen invalidates the
["orders"]query and navigates to/orders/[id]using the returned order ID. - On failure, the screen displays
checkoutErrorMessage(error)above the total. The cart remains available for another attempt. - After a successful order, the cart and its badge are empty, and the Orders tab displays the new order without a manual refresh.
- Retrying after a lost response sends the same idempotency key. The server returns the existing order rather than creating another one.
Constraints
- The
Idempotency-Keyheader contains the cart's existing key, which must be between1and36characters. - The request body contains no client-side prices.
- The cart is cleared only after a successful server response.
- Only one checkout mutation can be pending at a time.
- The idempotency key remains unchanged across failed attempts.
Design direction
Keep request construction, validation and error formatting in app/lib/checkout.ts. Use the existing ApiClient and cart types rather than introducing another API layer.
Keep the screen in app/app/(tabs)/cart.tsx. Use Zustand selectors for rendering cart state and a TanStack Query mutation for placing orders. Read the store through getState() inside the mutation so the submitted snapshot reflects the cart at the moment of the tap.
Use the existing cart store and its persistence. The idempotency key belongs to the persisted cart state, not to temporary screen state.
Consider these trade-offs:
- Clearing the cart before the request finishes makes the UI look responsive, but destroys the information needed to retry after a lost response. Clear it only after confirmation.
- The client prevents a fast double tap with the mutation's pending state. Server-side idempotency handles retries that happen after a timeout or an uncertain response.
- The app displays cart prices for review, but the order request sends only item selections and quantities. The server remains responsible for calculating the final price.
Any layout that meets the contract passes.
How to check your work
Run these commands:
cd backend && make test W=4B7
cd app && npx tsc --noEmit
cd app && npx expo startThe backend command runs the 8 checkout tests in app-tests/lib/checkout.test.ts. Read the first failing assertion to identify whether the request shape, idempotency key, cart clearing or error handling differs from the contract.
Then check the screen manually:
- Add items to the cart and verify the quantity controls and total.
- Place an order and verify that the screen opens the new order, the cart empties and the Orders tab updates.
- Stop the API and tap Place order. Confirm that the cart remains and displays the saved-cart message. Restart the API and retry.
- Verify that
SELECT COUNT(*) FROM orders WHERE user_id = ?increases by exactly one. - For the lost-response check, temporarily add a 3-second sleep after the commit in the Go order handler. Tap Place order, enable airplane mode before the response arrives, reconnect and retry. Confirm that the same order opens and the count increases by exactly one. Remove the sleep afterward.
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.
Menu configuration and checkout
3 items
- 4.14Live server validationnot completed
- 4.15Item configurator screennot completed
- 4.16Cart screennot completed
Up next · Module 6 · 1 items · about 1 h 15 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Implement cart snapshot validation and order request construction.
- Send checkout requests with the existing idempotency key.
- Clear the cart only after a successful response.
- Build the cart screen with quantity controls and total.
- Add pending, error and successful checkout handling.
- Verify retries, lost responses and order history updates.
Commands
cd backend && make test W=4B7cd app && npx tsc --noEmitcd app && npx expo startTests
W4 B7 checkout (8 tests)inapp-tests/lib/checkout.test.ts