React · Chapter 25 of 26

The capstone: Parda's booking flow

By the end of this chapter you have built Parda's booking flow from the list of shows to the ticket, out of the pieces of the last 24 chapters: routes for every page, a cart kept by a reducer and shared through a context, data from the server with loading states and Retry, a hold timer, a booking sent with an action, and an error boundary around every page. You can also say, for every piece of state in an app, where it lives and how long it lasts. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Real apps
  • 5 tasks
  • React
  • In the all labs plan

The first 2 chapters of React are free. This one comes with the all labs plan.

Why you need this

Diwali week starts on Friday 6 November. Sayali wants one thing tested before then: that a person can go from the home page to a ticket on their phone without a single call to Datta. That is five pages in a row: the shows, a show with its times, the hall for one time, checkout, and the ticket. Every page you have built so far worked alone, given its data by App.jsx or by the Check. In the real flow the pages hand things to each other: the address says which show and which time, the seats picked on one page are paid for on the next, the booking that comes back decides the ticket's address.

Nothing new is needed for that, which is the point of this chapter. What is new is putting the pieces together so that each piece of state lives in the right place: long enough to reach the page that needs it, and no longer.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    CartContext.jsx: the cart, with a reducer and a context

    CartContext.jsx has three exports. cartReducer(state, action), exported by name, returns the next cart from a cart { showtime, seats } and an action, and never changes the cart it is given: { type: 'toggle', showtime, seat } adds the seat to seats, or removes it if it is there, keeping the order picked; a 7th seat is ignored (at most 6); a seat for another showtime starts a new cart with only that seat. { type: 'clear' } gives { showtime: null, seats: [] }. Any other type throws an Error with the message Unknown action: <type> (for 'pay', Unknown action: pay).

    CartProvider({ children }), the default export, keeps the cart with useReducer and cartReducer, starting at { showtime: null, seats: [] }, and gives its children, through a context, the value that useCart returns.

    useCart(), exported by name, returns { showtime, seats, toggle(showtime, seat), clear() } from the nearest CartProvider; toggle and clear send the actions above. Outside any CartProvider, it throws an Error with the message useCart must be used inside CartProvider. The Check calls cartReducer on its own, then uses CartProvider and useCart from a component of its own, and calls useCart with no provider.

  2. Task 2

    ShowPage.jsx: a show and its showtimes, from the server

    ShowPage is the page at /shows/:id. Read the id from the address and ask api.js for getShow(id) and getShowtimes(id) at the same time, then show a paragraph Loading the show... while they wait. When both have answered, show an <article> (className="show-page" for app.css) with a heading h2 with the title (The Tiffin Thief), a paragraph with the tagline (Someone at the office is eating lunches.), and a list with one item per showtime: a link to its seats, Sat 7 Nov, 4:00 pm at Rangdeep Studio Theatre, to /seats/T0038 (day, time and venue). A sold-out showtime (booked equals seats) has no link, only the text with : sold out at the end: Fri 6 Nov, 7:30 pm at Rangdeep Studio Theatre: sold out.

    If either call fails, show its error's message in an element with role="alert" and a button that says Retry, which asks the server again (and then shows the show). When the address changes to another show, ask again, and never show an answer that arrives for the old one. The Check answers when it wants to, with three showtimes for S14, then fails once and presses Retry.

  3. Task 3

    HoldTimer.jsx: the hold timer

    Make HoldTimer, with the props seconds and onExpire, show a paragraph that counts down once a second, in minutes and two-digit seconds: Seats held for 10:00 for seconds 600, then Seats held for 9:59; for seconds 3, Seats held for 0:03, 0:02, 0:01. At 0 it shows Your hold has run out. instead, calls onExpire() once, and stops counting. When HoldTimer is removed from the page, its timer stops too.

    The Check uses fake timers and moves time on one second at a time; it checks that onExpire is called once and not again 5 seconds later, and that no timer is left running after it removes HoldTimer.

  4. Task 4

    Checkout.jsx: checkout, with an action

    Checkout is the page at /checkout/:showtimeId. Read showtimeId from the address and the cart from useCart() (Task 1). When the cart has no seats, or is for another showtime, show a paragraph Your basket is empty. and a link Pick seats to /seats/T0038 (with the address's showtimeId). Otherwise show a <form> whose action comes from useActionState, holding a heading h2 Checkout, a paragraph Seats: E4, E5, an input labelled Name with name="name", an input labelled Phone with name="phone", and a button Book 2 seats (the number of seats) that says Booking... and is disabled while the action runs.

    Sending the form calls bookSeats({ showtime: showtimeId, seats, name, phone }) from api.js, once. When the booking comes back ({ id: 'PB81820' }), clear the cart and go to /tickets/PB81820, so that the ticket takes the checkout page's place in the history (Back from the ticket goes to the page before checkout). If bookSeats fails, show its message in an element with role="alert" and keep the name and phone that were typed. The Check gives Checkout a cart of its own with E4 and E5 for T0038; bookSeats fails once with Seat E4 was just taken, then answers.

  5. Task 5 · interview question

    Interview: AppRoutes.jsx, the basket that vanishes

    AppRoutes has every route of the site inside <Layout />: / ShowList, /shows/:id ShowPage, /seats/:showtimeId SeatsPage, /checkout/:showtimeId Checkout, /tickets/:id Ticket, and any other address NotFound. But the CartProvider is inside the seats route: pick seats in the Preview, press Checkout, and the checkout page says Something went wrong, useCart must be used inside CartProvider. Fix AppRoutes so that one CartProvider holds the cart for every page, and seats picked on /seats/T0038 are still in the cart on /checkout/T0038. Keep the routes as they are.

    The Check uses a CartProvider and pages of its own: it opens each address, then picks E4 on the seats page, follows the link to checkout and reads the cart there.

    Rukhsana will ask: why does the cart's provider go above the routes? What happens to a provider's state when its route is left?

What you will learn

  • CartContext.jsx: the cart, with a reducer and a context
  • ShowPage.jsx: a show and its showtimes, from the server
  • HoldTimer.jsx: the hold timer
  • Checkout.jsx: checkout, with an action
  • Interview: AppRoutes.jsx, the basket that vanishes

More in Real apps

Build The capstone: Parda's booking flow yourself

Parda sells tickets for Pune's own cinemas and stages, and its booking page broke on opening night. You rebuild it in React. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of React are free. This one comes with the all labs plan.