Catalog UI integration
Scope: Next.js 16.x candidate, App Router, Cache Components disabled, TypeScript. Source walkthroughs and expected outcomes; no framework build or browser execution has been verified.
What you will learn
- Compose shared navigation and book details
- Check semantic structure
app/catalog/page.tsx — complete list
import Link from 'next/link';import { getBooks } from '../../lib/books';import { parseQuery } from '../../lib/catalog-query';import BookCard from './BookCard';import styles from './catalog.module.css';export default async function Catalog({ searchParams }: { searchParams: Promise<Record<string, string | string[] | undefined>> }) { const { sort, q, page } = parseQuery(await searchParams); const books = getBooks().filter(b => b.title.toLowerCase().includes(q)).sort((a,b) => sort === 'price' ? a.price - b.price : a.title < b.title ? -1 : a.title > b.title ? 1 : 0); const visible = books.slice((page - 1) * 2, page * 2); return <><h1>Catalog</h1> <form action="/catalog" method="get" className={styles.controls}> <label htmlFor="q">Search titles</label><input id="q" name="q" defaultValue={q} maxLength={60} /> <label htmlFor="sort">Sort by</label><select id="sort" name="sort" defaultValue={sort}><option value="title">Title</option><option value="price">Price</option></select> <button type="submit">Apply filters</button> </form> {visible.length ? <div className={styles.grid}>{visible.map(book => <BookCard key={book.slug} book={book} />)}</div> : <p>No books on this page.</p>} <nav aria-label="Catalog pages"> {page > 1 && <Link href={{ pathname: '/catalog', query: { q, sort, page: page - 1 } }}>Previous page</Link>} {page * 2 < books.length && <Link href={{ pathname: '/catalog', query: { q, sort, page: page + 1 } }}>Next page</Link>} </nav></>;}Default title order: Fern Field then Moss Garden, with Tide Notes on page 2. Price order: Tide Notes then Fern Field, with Moss Garden on page 2.
How it works
The catalog form uses ordinary GET submission. The server reads asynchronous searchParams, validates repeated or malformed options, filters a copied array and sorts it. For equal prices this comparator returns zero, preserving the input order under modern JavaScript stable sort. That is deterministic for the fixed fixture, but a database-backed catalog needs an explicit secondary stable key and stable pagination strategy. Two items appear per page. q is bounded and normalized; sort accepts price only when it is a single exact value. Empty results show a message. Previous and Next links preserve the validated options. No purchase occurs, and the quantity widget changes local state only. The project keeps one main landmark in the root layout, labels inputs and uses descriptive links.
lib/catalog-query.ts — complete helper
export function parseQuery(query: Record<string, string | string[] | undefined>) { const sort = query.sort === 'price' ? 'price' : 'title'; const topic = query.q; const q = typeof topic === 'string' && topic.length <= 60 ? topic.trim().toLowerCase() : ''; const p = query.page; const page = typeof p === 'string' && /^[1-9]\d{0,2}$/.test(p) && Number(p) <= 100 ? Number(p) : 1; return { sort, q, page };}Malformed page or repeated page defaults to 1; repeated sort defaults to title. A repeated or oversized q becomes empty. No query text is executed.
Common mistake
Do not filter after slicing the page; doing so can hide matches on other pages. Filter first, then sort, then paginate. A UI message that a quantity changed does not mean an order was stored.
Try it yourself
Trace /catalog?q=fern&sort=price: one card should appear. Then try /catalog?page=2 and explain why the list contains only Tide Notes. Test the links preserve the filter.
Further reading
Next lesson: Catalog milestone testing.