Project
Bookstore
Goal: Turn the single-screen patterns from Stages 1-3 into a real, multi-page, router-driven app - a searchable book catalog, a detail page read from the URL, a favorites list that survives navigating around the app, and a protected account page behind a mock login - organized in feature folders instead of one flat
components/dump. Time: ~8-10 hours · Uses: Part 15 (React Router, Declarative mode), Part 16 (feature folders, module boundaries), Part 11 (loading/error/success fetching), Part 9-10 (context + reducer), Part 14 (Testing Library) - the course's main capstone, the thing every Stage 5 track builds on next
Project brief
This is Stage 3's search app, extended conceptually rather than reused literally (see the repo's DESIGN-NOTES.md, "Capstone continuity"): same idea of a searchable list backed by a fetch, now split across real routes instead of living on one screen. The catalog talks to a data source modeled on the real Open Library search API shape - but, exactly like every fetch in this course, no test ever makes a real network call; global.fetch is stubbed by hand everywhere.
Land on "/" → "Loading books..." then a list of books for the
default search "fiction"
Search "tolkien", press Search → the URL becomes "/?q=tolkien"; a new list of
results replaces the old one
Tap "☆ Favorite" on a book → the button flips to "★ Favorited"
Click "Favorites" in the nav → that exact same book is already there - no refetch,
nothing passed through the router
Click a book's title → "/books/OL1W" shows its own detail page, read from
useParams(), not a prop
Visit "/account" signed out → redirected to "/login?next=%2Faccount"
Click "Sign in (mock)" → lands back on "/account", now showing real content
Visit "/this-page-does-not-exist" → a real 404 page, with a link back to the catalog
Files (feature folders, Part 16's convention)
project-bookstore/
├── app/ # the router/layout/root - not a feature itself
│ ├── Layout.jsx # shared nav chrome + <Outlet/>
│ ├── NotFoundPage.jsx # the catch-all 404
│ ├── AppRoutes.jsx # <Routes>/<Route> only - no <BrowserRouter> (testable)
│ ├── AppRoutes.test.jsx # the whole app wired together, MemoryRouter-tested
│ ├── AppProviders.jsx # every context provider, wrapping the routed tree once
│ └── AppRoot.jsx # the real app: exactly one <BrowserRouter>
├── features/
│ ├── catalog/ # the "/" list route + "/books/:id" detail route
│ │ ├── catalogApi.js # fetch + Open-Library-shape mapping (internal)
│ │ ├── useBookSearch.js # Part 11's loading/error/success hook (internal)
│ │ ├── CatalogContext.jsx # last-loaded-books-by-id cache (internal)
│ │ ├── CatalogPage.jsx # route: "/"
│ │ ├── BookDetailPage.jsx # route: "/books/:id"
│ │ ├── catalogApi.test.jsx
│ │ ├── CatalogPage.test.jsx
│ │ └── index.js # this feature's PUBLIC API
│ ├── favorites/ # the shared favorite/cart-like state
│ │ ├── favoritesReducer.js # pure reducer (Part 10), tested with no component
│ │ ├── FavoritesContext.jsx# useReducer + useContext, app-wide
│ │ ├── FavoritesPage.jsx # route: "/favorites"
│ │ ├── favoritesReducer.test.jsx
│ │ └── index.js # this feature's PUBLIC API
│ └── auth/ # the mock login + protected route
│ ├── AuthContext.jsx # a signed-in flag, default signed-out
│ ├── RequireAuth.jsx # Part 15 Section 7's exact RequireAuth shape
│ ├── LoginPage.jsx # route: "/login"
│ ├── AccountPage.jsx # route: "/account" (protected)
│ └── index.js # this feature's PUBLIC API
└── README.md # this file
How to run
This project reuses the one repo-level harness (see ../../TOOLING.md) - no separate install, no separate package.json. From the repo root:
npx vitest run stage-4-routing-and-architecture/project-bookstore
To actually see it render, import AppRoot from app/AppRoot.jsx into harness/src/App.jsx temporarily and run npm run dev - then remove the import again once done, so the harness keeps showing whichever part it's currently wired for.
Features
| Feature | What it does |
|---|---|
Searchable catalog (/) |
A controlled search form whose term lives in the URL (?q=...), fetched with Part 11's exact loading/error/success modeling |
Dynamic detail route (/books/:id) |
Reads id with useParams, looks the book up in a small in-memory cache, and shows a graceful "not found" link instead of crashing on an unknown or not-yet-loaded id |
| Favorite toggle | Every book row (catalog or detail) can be favorited/unfavorited with one button |
Shared favorites (/favorites) |
A useReducer + useContext pair, wrapping the whole routed app, so favoriting on / and reading the list on /favorites share one state - proven across a real route change in the test suite |
Protected account (/account) |
A RequireAuth layout route redirects a signed-out visitor to /login?next=... and renders real content once signed in |
Mock login (/login) |
A single "Sign in" button (no real credentials), with an open-redirect-safe next (must start with /, never // or a full URL) |
404 (*) |
A catch-all route for any URL nothing else matches |
| Feature-folder layout | Each feature exposes only its index.js to the others - catalogApi.js, favoritesReducer.js, and AuthContext.jsx's internals stay private to their own folder |
Which Parts each feature uses
| Feature | Skills | Part |
|---|---|---|
AppRoutes.jsx - <Routes>/<Route>, nested layout route, <Outlet/> |
Declarative-mode routing, layout routes | Part 15 |
BookDetailPage.jsx - useParams, not-found fallback |
Dynamic segments | Part 15 |
Layout.jsx - NavLink with end on the catalog tab |
Active-tab highlighting | Part 15 |
CatalogPage.jsx - useSearchParams for a shareable search term |
URL-held filter state | Part 15 |
LoginPage.jsx - useNavigate after the mock sign-in action completes |
Post-action navigation, not a link substitute | Part 15 |
RequireAuth.jsx - <Navigate to="/login?next=...">, encodeURIComponent-safe next |
Protected routes, open-redirect safety | Part 15 |
AppRoutes.test.jsx, CatalogPage.test.jsx - <MemoryRouter initialEntries={[...]}> |
Testing routed components | Part 15 §8 |
useBookSearch.js + catalogApi.js - { phase: "loading"|"error"|"success" }, AbortController cleanup |
Fetching-as-explicit-state, no boolean soup | Part 11 |
favoritesReducer.js + FavoritesContext.jsx - useReducer for updates, useContext for sharing |
The "poor man's Redux" pattern | Part 9, Part 10 |
AuthContext.jsx - a safety-throwing useX hook outside its provider |
createContext/Provider/custom hook |
Part 9 |
Feature folders + each index.js public surface |
Grouping by feature, not by type; module boundaries | Part 16 |
Every *.test.jsx file |
describe/it/expect, Testing Library queries, userEvent |
Part 14 |
Deploying (documentation only - nothing below was actually run)
This is a fully client-rendered SPA with no backend, so it ships as static files to any static host, in one command after a production build:
npm run build # builds the Vite harness into harness/dist (or dist/, depending on how main.jsx wires AppRoot)
npx gh-pages -d dist # OR: netlify deploy --prod --dir=dist OR: vercel --prod
Whichever host is used, one setting matters for client-side routing: the host must rewrite every unknown path back to index.html (a "SPA fallback" / _redirects rule), or a hard refresh on /books/OL1W will 404 at the server before React Router ever gets a chance to render its own catch-all route.
Build it step by step
- Favorites first (Parts 9-10). Write
favoritesReducer.jsas a puretoggleaction, test it directly with no component, then wrap it inFavoritesProvider/useFavorites- this is the state every other feature will read. - The catalog (Part 11). Write
catalogApi.js'ssearchBooks, mapping Open Library's doc shape to this project's own{ id, title, author, year }. ExtractuseBookSearchas a custom hook holding exactly one{ phase, ... }value, with anAbortControllercancelled on cleanup. - Routing (Part 15). Wire
AppRoutes.jsx: an index route for the catalog,books/:idfor the detail page,favorites,login, and aRequireAuth-guardedaccount, plus the catch-all*. KeepAppRoutesfree of<BrowserRouter>so it can be rendered inside a test's own<MemoryRouter>. - Auth (Part 15, Section 7).
AuthContextstarts signed-out.RequireAuthredirects to/login?next=..., built withencodeURIComponenton the current pathname only.LoginPagevalidatesnextbefore ever navigating to it. - Wrap and verify.
AppProvidersnestsAuthProvider→FavoritesProvider→CatalogCacheProvideraroundAppRoutes;AppRootadds the one real<BrowserRouter>around all of it. Run the full suite (below) and confirm every scenario in the project brief's transcript actually happens.
Checklist
- At least 4 routes, including one dynamic (
/books/:id) and one protected (/account) - this project ships 6:/,/books/:id,/favorites,/login,/account,*. - Favorites persist across a route change - proven by favoriting on
/and reading it back on/favoritesin the same test. - Organized in feature folders (
features/catalog,features/favorites,features/auth), each with its ownindex.jspublic surface - not a flatcomponents/dump. - A documented one-command deploy to a static host (above) - not actually run.
- The test suite passes and covers the protected-route redirect, plus the list, detail, and 404 routes.
Next
🎉 Stage 4 done! Go back to the roadmap and start Stage 5: Specialization.