Item configurator screen
You build an item configurator screen that takes a menu item and its selections, then displays the server's quote. Add to cart stays disabled until that quote matches the current selection and has no errors.
Ticket
Build the item configurator screen in app/app/item/[id].tsx and its selection and pricing helpers in app/lib/configurator.ts. Customers need to choose a size, configure each option group, and see the price returned by the server before adding the item to their cart.
The menu screen already loads the item's menu. Reuse that cached menu rather than making another request. The configurator feeds the cart from exercise 4.13, which the checkout exercise will use to place an order.
Acceptance criteria
controlKind(group)returns"radio"whenmax_selectis1and"checkbox"otherwise.isAvailable(option, size)returnstrueonly when the option is in stock and has a price for the selected size or for every size through"*".optionPrice(option, size)prefers the selected size's price over the"*"price.toggle(group, chosen, optionId)returns a new object. In a radio group, picking an option replaces the previous choice, and picking the selected option again removes it. In a checkbox group, picking an option beyondmax_selectleaves the existing choices unchanged.initialChosen(item, size)selects the first available option in every required single-choice group wheremin_selectandmax_selectare both1.dropUnavailable(item, size, chosen)removes choices that are unavailable for the new size.priceRequest(selection)produces the request fieldsitem_id,size,chosenandqty.fetchQuotesends that request through the API client toPOST /v1/menu/priceand returns the server's quote, includingdescription,unit_paise,total_paiseanderrors.- The quote's
errorsfield is always an array ofSelectionErrorobjects containingcodeandmessage, withgroup_idandoption_idwhere applicable. When the array is not empty,unit_paiseandtotal_paiseare both0. errorsByGroup(item, errors)places each error'smessageunderbyGroup[String(group_id)]when that group belongs to the item. Messages without agroup_idor with agroup_idnot belonging to the item go intogeneral. Both groups preserve the original order of their messages.- The item screen reads
idandrestaurantIdfrom its route params and finds the item throughuseMenu(api, restaurantId). Opening an item already present in the menu cache shows its sizes and groups without a loading spinner or an additional menu request. - The item screen shows loading and error states while the menu is unavailable. It shows an empty state when the item is missing or has no sizes.
- The configurator shows size chips first, followed by every option group. Each group displays
"Pick n"when its minimum and maximum are equal, or"Pick a to b"when they differ. - Each option row displays its price for the selected size. An unavailable option is greyed out, disabled, and labelled
"Not in this size". Rows expose the correctaccessibilityRole, checked state and disabled state. - Changing the size removes unavailable choices. The next quote uses the updated selection.
- The selection is debounced by
300ms. Several quick option changes produce one/v1/menu/pricerequest after the changes settle. The quote query key is["quote", debounced], andkeepPreviousDatakeeps the previous price visible while the next quote loads. - Server error messages are displayed under their matching option groups using
group_id. Errors without a matching group are displayed at the bottom. Read the errors fromquote.data?.errors ?? []when the quote has not arrived yet. Once returned,errorsis always an array. A network failure displays its own message. - Add to cart is enabled only when the debounced selection matches the current selection, the quote request is no longer in flight, and the quote contains no selection errors.
- The bottom bar displays the quote's
unit_paiseand places Add to cart inside the safe area. - The cart line uses the current
itemId,size,chosen,qty: 1, and the quote'sunit_paiseanddescription. Adding it uses the Zustand store from exercise 4.13. - When adding the line returns
"ok", the screen closes withrouter.back(). When it returns"other-restaurant", the screen asks withAlert.alert("Start a new cart?", ...). Keep it cancels. Start new cart callsreplaceWithand closes the screen. - Temporarily allow a second Crust choice to reach server validation. The resulting error appears under Crust and Add to cart is disabled. Undo this temporary change afterward.
- Adding an item from the same restaurant closes the configurator and updates the Cart badge. Adding an item from another restaurant asks before replacing the cart.
make test W=4B5passes all 11 configurator tests, andnpx tsc --noEmitpasses inapp/.
Constraints
- The debounce delay is
300ms. - The quote request uses
qty: 1. - The selected option count cannot exceed a group's
max_selectthrough the normal controls. - The phone does not calculate item prices. Only the server's quote provides the price and validation result.
Design direction
Keep the selection rules in pure functions in app/lib/configurator.ts. Use the menu types and API client types already present in the app. This keeps the rules independently testable without rendering a screen.
Keep the generic debounce hook in app/lib/useDebounced.ts. In the screen, use local state for size and chosen, derive the selection with useMemo, and pass it through the debounce hook before creating the quote query.
Use useMenu for the cached menu and TanStack Query for the quote. Keep the quote query key tied to the debounced selection. Use keepPreviousData so the last price remains visible during a new request.
There are a few trade-offs to consider:
- Local availability and selection rules prevent impossible taps, but they do not replace server-side validation. The server remains authoritative.
- Keeping the previous quote avoids an empty price during a request, but that price may belong to an earlier selection. Gate Add to cart on the settled selection rather than on whether a quote exists.
- Mapping errors by
group_idkeeps messages close to the relevant controls without depending on group or option names. Messages without a matching group still need a place to appear. - The cart can store the quote's unit price as a display estimate. The order flow reprices the order later.
Any layout that meets the contract passes.
How to check your work
Run these commands:
cd backend && make test W=4B5
cd app && npx tsc --noEmit
cd app && npx expo startRead the first failing assertion carefully. It identifies the behaviour that differs from the contract. Fix that behaviour before investigating later failures, then open the configurator on an emulator or phone to check the screen and cart interactions.
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.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Implement the configurator types and pure selection helpers.
- Add the debounced quote request using TanStack Query.
- Build the size and option controls with availability states.
- Display grouped server errors and the safe-area price bar.
- Add cart integration and the other-restaurant confirmation.
- Pass all 11 tests and verify the screen in Expo.
Commands
cd backend && make test W=4B5cd app && npx tsc --noEmitcd app && npx expo startTests
W4 B5 configurator (11 tests)inapp-tests/lib/configurator.test.ts