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,MenuItemandMenu. An option hasid,name,in_stockandprices. A group hasid,name,min_select,max_selectandoptions. A size hassizeandprice_paise. A menu item hasid,name, optionalcategory,sizesandgroups. A menu containsitems. fetchMenurequests/v1/restaurants/{restaurantId}/menuthrough the API client and returns the menu data.lowestPrice(item)returns the smallestprice_paiseamong the item's sizes. It returnsnullwhen 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
toSectionsincludesfromPaise, calculated from the item's lowest size price. Items with no sizes are excluded because they cannot be ordered. DEFAULT_SECTIONis"Menu". When the API supplies no categories, all orderable items appear under one"Menu"section.formatPaise(39900)returns"₹399", andformatPaise(39950)returns"₹399.50". Money remains in integer paise until formatted for display.useMenu(api, restaurantId)uses the query key["menu", restaurantId], with astaleTimeof five minutes. The query is enabled only whenrestaurantIdis 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 a404for a missing restaurant, it displaysErrorStatewith 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 itsfromPaisevalue. The screen renders the calculated price instead of calculating prices itself. - Tapping an item pushes
/item/[id]with bothidandrestaurantIdin 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=4B4passes the eight menu-section tests inapp-tests/lib/menu.test.ts, andnpx tsc --noEmitpasses inapp/.
Constraints
- Accept only positive integer restaurant ids for menu queries.
- Set the menu query's
staleTimeto five minutes (5 * 60_000milliseconds). - Keep all monetary values in integer paise until display formatting.
- Exclude menu items with an empty
sizesarray 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
Mapto 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 receivefromPaiserather 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:
cd backend && make test W=4B4cd app && npx tsc --noEmitcd app && npx expo startOpen 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.
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.
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
Up next · Module 4 · 2 items · about 1 h 30 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Define menu types and implement the API request.
- Implement lowest-price calculation and section grouping.
- Format integer paise for menu display.
- Add the shared menu query with id validation and caching.
- Build the menu screen with sticky section headers and item navigation.
- Run W4B4 tests and verify the screen on a device.
Commands
cd backend && make test W=4B4cd app && npx tsc --noEmitcd app && npx expo startTests
W4 B4 menu sections (8 tests)inapp-tests/lib/menu.test.ts