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 with restaurant_id and lines. Each line contains item_id, size, chosen and qty. It does not send prices because the server reprices every line.
  • placeOrder(api, cart, clear) rejects a cart with a null restaurant ID, no lines or no idempotency key. It does not call the API for an invalid cart.
  • A valid cart produces a POST /v1/orders request through the API client. The request includes the cart's key in the Idempotency-Key header and uses the body produced by orderRequest.
  • placeOrder calls clear() 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 409 response displays "This order is still being placed. Wait a moment, then tap Place order again.".
  • A 429 response displays the wait in seconds using the retry delay, falling back to 1 second when no retry delay is available.
  • A 400 response displays the server-provided error text.
  • The cart screen reads lines, setQty and total() 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 setQty and 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 clear function 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-Key header contains the cart's existing key, which must be between 1 and 36 characters.
  • 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:

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

The 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:

  1. Add items to the cart and verify the quantity controls and total.
  2. Place an order and verify that the screen opens the new order, the cart empties and the Orders tab updates.
  3. Stop the API and tap Place order. Confirm that the cart remains and displays the saved-cart message. Restart the API and retry.
  4. Verify that SELECT COUNT(*) FROM orders WHERE user_id = ? increases by exactly one.
  5. 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.
Finished Exercise 4.16?

Menu configuration and checkout

3 items

  • 4.14Live server validationnot completed
  • 4.15Item configurator screennot completed
  • 4.16Cart screennot completed

Order history

Up next · Module 6 · 1 items · about 1 h 15 min

Start module 6

Implementation

Run locally

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

  1. Implement cart snapshot validation and order request construction.
  2. Send checkout requests with the existing idempotency key.
  3. Clear the cart only after a successful response.
  4. Build the cart screen with quantity controls and total.
  5. Add pending, error and successful checkout handling.
  6. Verify retries, lost responses and order history updates.

Commands

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

Tests

  • W4 B7 checkout (8 tests) in app-tests/lib/checkout.test.ts