Work in the lab below, or in your downloaded project with npm run dev. Each
feature uses a different day's idea.
npm install and npm run dev. 1. Search
Add a Search box to the list that filters notes by title as you type, and
works together with the tag filter: on #routing, typing params gives
/?tag=routing&q=params.
npm install and npm run dev. Hint 1: the idea
Day 3's SearchBox does exactly this: it copies the current search params, sets
q, and calls router.replace. Copy it into app/SearchBox.jsx.
Hint 2: a first step
Read q from searchParams alongside tag (default ""), and filter the
loaded notes by title.
Show one solution
In app/page.jsx:
import SearchBox from "./SearchBox.jsx";const { tag, q } = await searchParams;
const query = Array.isArray(q) ? q[0] : (q ?? "");
const notes = (await getNotes(tag)).filter((note) => note.title.toLowerCase().includes(query.toLowerCase()));<SearchBox />app/SearchBox.jsx is Day 3's, unchanged:
"use client";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
export default function SearchBox() {
const router = useRouter();
const pathname = usePathname();
const searchParams = useSearchParams();
function handleChange(event) {
const params = new URLSearchParams(searchParams);
if (event.target.value) params.set("q", event.target.value);
else params.delete("q");
const query = params.toString();
router.replace(query ? `${pathname}?${query}` : pathname);
}
return (
<label>
Search <input defaultValue={searchParams.get("q") ?? ""} onChange={handleChange} />
</label>
);
}2. One note from the API
Add GET /api/notes/[id], answering with one note, or a 404 with
{ "error": "No note 42" }.
npm install and npm run dev. Hint 1: the idea
An API route is a request handler, not a page component. Resolve its dynamic id before loading the record.
Hint 2: a first step
app/api/notes/[id]/route.js, with GET(request, { params }), as in Day 6.
Remember Number(id): ids in lib/notes.js are numbers.
Show one solution
import { getNote } from "@/lib/notes.js";
export async function GET(request, { params }) {
const { id } = await params;
const note = await getNote(Number(id));
if (!note) return Response.json({ error: `No note ${id}` }, { status: 404 });
return Response.json(note);
}In the downloaded project, open http://localhost:3000/api/notes/1 to see it.
3. Tag counts
Show how many notes each tag has: #routing (2) #components (1) #data (0), whatever tag is selected.
npm install and npm run dev. Hint 1: the idea
A filtered list cannot tell you the totals for every tag. Counts need the full data set.
Hint 2: a first step
The page already has the notes for the selected tag. Load all notes too,
with getNotes(), and count each tag from those.
Show one solution
const notes = await getNotes(tag);
const all = await getNotes();
const count = (t) => all.filter((note) => note.tag === t).length;<Link key={t} href={`/?tag=${t}`}>
#{t} ({count(t)})
</Link>The two queries don't depend on each other. For a real database, load them
together with Promise.all, as on Day 4.
Completion is saved in this browser only. It does not measure mastery.