Restaurant menu screen

You build the restaurant menu screen that fetches a restaurant's menu and displays orderable items in sections with their cheapest size prices. The configurator reuses the same cached menu query.

Ticket

Build the restaurant menu screen at app/app/restaurant/[id].tsx. It fetches the selected restaurant's menu from GET /v1/restaurants/{id}/menu, groups orderable items into sections, and displays each item's cheapest size price.

Customers need to compare menu items before configuring an order. The item configurator opens from this screen and uses the same cached menu query, so opening it does not require another request for the same restaurant.

Acceptance criteria

  • Define the menu data types in app/lib/menu.ts: MenuOption, MenuGroup, MenuSize, MenuItem and Menu. An option has id, name, in_stock and prices. A group has id, name, min_select, max_select and options. A size has size and price_paise. A menu item has id, name, optional category, sizes and groups. A menu contains items.
  • fetchMenu requests /v1/restaurants/{restaurantId}/menu through the API client and returns the menu data.
  • lowestPrice(item) returns the smallest price_paise among the item's sizes. It returns null when the item has no sizes.
  • toSections(menu) groups items by their trimmed category. It preserves the order in which categories first appear and uses "Menu" for missing or whitespace-only categories.
  • Every row returned by toSections includes fromPaise, calculated from the item's lowest size price. Items with no sizes are excluded because they cannot be ordered.
  • DEFAULT_SECTION is "Menu". When the API supplies no categories, all orderable items appear under one "Menu" section.
  • formatPaise(39900) returns "₹399", and formatPaise(39950) returns "₹399.50". Money remains in integer paise until formatted for display.
  • useMenu(api, restaurantId) uses the query key ["menu", restaurantId], with a staleTime of five minutes. The query is enabled only when restaurantId is a positive integer.
  • The configurator uses the same TanStack Query hook and query key. Opening an item from a menu already in cache does not trigger a second menu request.
  • The screen reads the restaurant id from its dynamic route parameter and converts it to a number. A value that is not a positive integer displays "This restaurant does not exist." without requesting a menu using an invalid id.
  • While the menu query is pending, the screen displays Loading. When the request fails, including a 404 for a missing restaurant, it displays ErrorState with a Try again button.
  • When the menu contains no orderable items, the screen displays EmptyState.
  • The screen renders a SectionList with sticky section headers. It uses the same virtualization approach as FlatList, so long menus do not require rendering every row inside a ScrollView.
  • Each menu row displays the item name, "Customisable" when the item has option groups, and "from ₹..." using its fromPaise value. The screen renders the calculated price instead of calculating prices itself.
  • Tapping an item pushes /item/[id] with both id and restaurantId in its route params.
  • Returning to the list and reopening the same restaurant within five minutes displays the cached menu without an initial loading spinner.
  • make test W=4B4 passes the eight menu-section tests in app-tests/lib/menu.test.ts, and npx tsc --noEmit passes in app/.

Constraints

  • Accept only positive integer restaurant ids for menu queries.
  • Set the menu query's staleTime to five minutes (5 * 60_000 milliseconds).
  • Keep all monetary values in integer paise until display formatting.
  • Exclude menu items with an empty sizes array from the orderable sections.

Design direction

Keep the menu data logic in app/lib/menu.ts. Define the API response types there, alongside the default section name, price helpers, section transformation and paise formatting. Keep the API request separate from the screen.

Put the query configuration in app/lib/useMenu.ts. The query key should identify the restaurant, and the enabled condition should prevent invalid route params from reaching the API.

Keep the screen responsible for route validation and presentation. Read the dynamic route parameter, convert it to a number, then handle the loading, error and empty states before rendering the menu.

Consider these trade-offs:

  • Use a Map to group sections. It preserves insertion order, so customers see categories in the order they first appear in the API response.
  • Filter items without sizes before rendering. An item without a price cannot be ordered, and displaying it would require inventing a price.
  • Keep price calculations in menu.ts. The screen should receive fromPaise rather than looping through sizes during rendering.
  • Use the same query key in the configurator. This lets both screens share the cached menu while keeping each restaurant's data separate.

Any layout that meets the contract passes.

How to check your work

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

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

Open a restaurant from the restaurant list and inspect its menu. Check the sticky section header, lowest-size prices, empty state and retry behaviour. Also open a menu using an invalid route such as restaurant/abc while signed in. Return to the list and reopen the same restaurant within five minutes to verify that the cached menu appears without a loading spinner. When a check fails, read the first failing assertion to identify the specific data transformation or contract that needs attention.

Finished Exercise 4.11?

Paged data lists

4 items

  • 4.8TanStack Query cachingnot completed
  • 4.9Virtualized list renderingnot completed
  • 4.10Restaurant list screennot completed
  • 4.11Restaurant menu screennot completed

Shopping cart

Up next · Module 4 · 2 items · about 1 h 30 min

Start module 4

Implementation

Run locally

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

  1. Define menu types and implement the API request.
  2. Implement lowest-price calculation and section grouping.
  3. Format integer paise for menu display.
  4. Add the shared menu query with id validation and caching.
  5. Build the menu screen with sticky section headers and item navigation.
  6. Run W4B4 tests and verify the screen on a device.

Commands

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

Tests

  • W4 B4 menu sections (8 tests) in app-tests/lib/menu.test.ts