A new feature must fit the project's rules, not only its JSX. This one is an
archive: archived items leave the normal list but aren't deleted. Plan on
about 35 minutes in the browser. The SQLite version, with a real schema
migration, is a separate optional session described at the end.
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Plan before you type
Write down the answers to these first. They're the decisions the feature is
made of, and each has a rule from earlier in the week:
Who may archive? The same caller who may edit: canWrite (Day 4).
What if the item changed since the page loaded? The same version check
as edit: a stale version is a conflict, and nothing changes (Day 4).
What does a successful archive change?archived becomes true and
the version goes up by one, so an open edit form becomes stale.
Who sees archived items? Nobody in the default list; the list includes
them only when asked explicitly, with ?archived=1 (Day 1).
What happens after? Invalidate the list and the item page, then
redirect, in that order (Day 2).
Exercise
An archive operation
Add the feature in the browser project:
repository.archive(actor, id, version), returning the same results as
edit: not_found, conflict, or the updated public item.
repository.list(actor, q, options) leaves archived items out unless
options.showArchived is true.
An Archive item button on the item page, through a Server Action, and a
Show archived link on the list.
Acceptance checks: archive Test a stale edit and see it leave the list;
follow Show archived and see it marked archived at version 2. Then check
the rules in your head or with a test: archiving Grace's item (id 3) gives
not_found, and archiving item 2 again with version 1 gives conflict.
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
archive is edit with a different change. Its guards, in their order, are
the same two lines; only the row it builds differs. The list filter is a
separate decision from the write guard.
Hint 2: a first step
Copy edit into a new archive(actor, id, version) method, delete the draft
validation, and build next with archived: true instead of a new title. Then
call it from a new archiveItem(id, version) action, bound on the item page
like Remove item. Leave the list until the button works.
list gains (showArchived || !row.archived) inside the scoped filter, so the
owner check still runs first. publicItem adds archived because the page
shows it. The action binds both the id and the version the page rendered, so
archiving from a stale page is a conflict rather than a silent overwrite, and
it revalidates before redirecting. Compare each choice with yours; a different
shape that keeps the five decisions above is just as right.
Going further: the database version
In the downloaded SQLite project the same feature needs a schema change. Do it
as its own session of about an hour:
The project tracks its schema with PRAGMA user_version (it's 1 now). Add
a step to version 2 that runs
ALTER TABLE items ADD COLUMN archived INTEGER NOT NULL DEFAULT 0, and a test
that creates a version-1 database, opens it with the new code, and checks
every title and version survived.
Copy edit's conditional UPDATE, which already matches id, ownerId,
tenantId and version in one statement, and set archived=1 instead of
the title. As in edit, "no row changed" means not_found or conflict.
Run npm test and the project's checks from the previous lesson.
Editing the seed data instead of migrating only works on an empty database;
real users' data would never get the column.
Completion is saved in this browser only. It does not measure mastery.