what

Delete with a clear consequence

Let the editor delete a page on purpose, after seeing exactly which page it is, with a Cancel that changes nothing.

Allow 15–25 minutes. This is an editorial estimate. Continue in the same my-site folder, signed in as the editor.

Read

Start at L18. The editor can publish and unpublish, but not delete. Unpublishing hides a page from visitors and keeps its working copy, so you can publish it again later. Deleting removes the page's working copy permanently: there is no undo and no bin. When in doubt, unpublish.

A published page cannot be deleted. Unpublish it from its preview first; then visitors have already lost it, and deleting only removes your working copy. The server enforces this order: a delete of a published page is refused with “Unpublish this page before deleting it.”

Deleting happens on its own confirmation page, which names the page and its address and has a real Cancel link. Its form sends the same two hidden numbers as Publish, _revision and _live_version: if the page was saved, published or unpublished after you opened the confirmation, the delete is refused and nothing is removed. After a refusal the Delete button is left out until you reload the confirmation.

After a delete you return to the admin page, which now shows the server's message, such as “Item deleted in pages”.

Change

1. Keep a copy. Stop the dev server with Ctrl+C. Copy the whole my-site folder to a new folder such as my-site-before-L19.

2. Let editors delete. Replace what.toml. The delete line now names the editor, and its comment changes.

what.toml
# What Project Configuration

[server]
port = 8085
host = "127.0.0.1"

[session]
enabled = true
cookie_name = "w_session"
max_age = 604800
database = "data/sessions.db"

# The database owns the pages now. On the first start after this change the
# server copies data/pages.json into the database once, keeps every id, and
# records that it did. It never reads the file again.
[import]
pages = "data/pages.json"

# Editors may create, change and delete pages. A published page is
# unpublished before it can be deleted. No two pages may share a slug: the
# database itself refuses the second one.
[collections.pages]
create = "editor"
update = "editor"
delete = "editor"
unique = ["slug"]
# Visitors read published copies of these fields only. A page created now
# stays private until it is published.
public = ["title", "slug", "summary", "body"]
# Editors read the working copies, drafts included, from the admin area.
working_read = "editor"
# Editors publish. Publish copies a page's last save (its public fields) into
# the published copy visitors read; Unpublish removes that copy. Nobody can
# publish until this line exists, and only signed-in accounts can hold it.
publish = "editor"
# The server checks every new or changed page itself, whatever form sent it.
fields.writable = ["title", "slug", "summary", "body"]
fields.required = ["title", "slug"]
fields.max_length = { title = 120, summary = 280, body = 20000 }
fields.slug = ["slug"]
fields.immutable = ["slug"]

# Sign-in. The site keeps its own editor accounts in data/app.db. You add one
# from the terminal with `run-what admin add`: there is no built-in account and
# no password in any file of the project.
[auth]
enabled = true
provider = "local"
login_path = "/login"
after_login = "/"

3. Add the confirmation page. Create site/admin/pages/delete/[id].html. For a published page it links to the preview instead of offering the button.

site/admin/pages/delete/[id].html
<what>
title = "Delete page | Admin | My website"
description = "Confirm that a page should be deleted."
fetch.page.url = "local:pages"
fetch.page.copy = "working"
fetch.page.filter = "id=#params.id#"
fetch.page.single = true
</what>

<h1>Delete page</h1>
<if flash.error>
<div class="form-error" role="alert">
  <p>#flash.error#</p>
  <p>This request did not delete the page. Read the details below again first.</p>
  <p><a href="/admin/pages/delete/#page.id#">Reload this page before trying again</a></p>
</div>
</if>
<p>You are about to delete <strong>#page.title#</strong>, at the address <strong>/pages/#page.slug#</strong>, last saved as revision #page._revision#. Deleting removes its working copy permanently. It cannot be undone.</p>
<if page._published>
<p>This page is published, so it cannot be deleted yet. <a href="/admin/pages/preview/#page.id#">Unpublish it from its preview</a>, then come back here.</p>
<else/>
<!-- The form sends the versions on screen. After a refusal it is left out: reload first. -->
<unless flash.error>
<form class="sign-in danger" method="post" action="/w-action/pages/#page.id#?w-action=delete&amp;w-redirect=/admin">
  <input type="hidden" name="_revision" value="#page._revision#">
  <input type="hidden" name="_live_version" value="#page._live_version#">
  <button type="submit">Delete this page permanently</button>
</form>
</unless>
</if>
<p><a href="/admin/pages/edit/#page.id#">Cancel</a></p>

4. Link the edit page to it. Replace site/admin/pages/edit/[id].html. Only the paragraph with the delete link is new.

site/admin/pages/edit/[id].html
<what>
title = "Edit page | Admin | My website"
description = "Edit a page's working copy."
fetch.page.url = "local:pages"
fetch.page.copy = "working"
fetch.page.filter = "id=#params.id#"
fetch.page.single = true
</what>

<h1>Edit page</h1>
<p>You are editing the working copy of <strong>#page.title#</strong> (revision #page._revision#). Saving changes the working copy only: visitors keep seeing the published copy until the page is published.</p>
<p><a href="/admin/pages/preview/#page.id#">Preview your last save</a>: it draws this page as you last saved it with the public page's component. Anything typed below and not saved yet is not included.</p>
<p><a href="/admin/pages/delete/#page.id#">Delete this page</a>: the next page asks you to confirm.</p>
<if flash.error>
<div class="form-error" role="alert">
  <p>#flash.error#</p>
  <ul>
    <if errors.title><li>Title: #errors.title#</li></if>
    <if errors.summary><li>Summary: #errors.summary#</li></if>
    <if errors.body><li>Body: #errors.body#</li></if>
  </ul>
</div>
</if>
<!-- After a refused save, the form shows what was typed and the revision it was opened with. -->
<if old>
<form class="sign-in" method="post" action="/w-action/pages/#page.id#?w-redirect=/admin">
  <input type="hidden" name="_revision" value="#old._revision#">
  <p>Address: <strong>/pages/#page.slug#</strong> (fixed when the page was created)</p>
  <div class="field">
    <label for="title">Title</label>
    <input id="title" name="title" type="text" maxlength="120" value="#old.title#" aria-describedby="title-error">
    <span id="title-error" class="field-error">#errors.title#</span>
  </div>
  <div class="field">
    <label for="summary">Summary</label>
    <input id="summary" name="summary" type="text" maxlength="280" value="#old.summary#" aria-describedby="summary-error">
    <span id="summary-error" class="field-error">#errors.summary#</span>
  </div>
  <div class="field">
    <label for="body">Body</label>
    <textarea id="body" name="body" rows="8" maxlength="20000" aria-describedby="body-error">#old.body#</textarea>
    <span id="body-error" class="field-error">#errors.body#</span>
  </div>
  <button type="submit">Save changes</button>
</form>
<else/>
<form class="sign-in" method="post" action="/w-action/pages/#page.id#?w-redirect=/admin">
  <input type="hidden" name="_revision" value="#page._revision#">
  <p>Address: <strong>/pages/#page.slug#</strong> (fixed when the page was created)</p>
  <div class="field">
    <label for="title">Title</label>
    <input id="title" name="title" type="text" maxlength="120" value="#page.title#" aria-describedby="title-error">
    <span id="title-error" class="field-error">#errors.title#</span>
  </div>
  <div class="field">
    <label for="summary">Summary</label>
    <input id="summary" name="summary" type="text" maxlength="280" value="#page.summary#" aria-describedby="summary-error">
    <span id="summary-error" class="field-error">#errors.summary#</span>
  </div>
  <div class="field">
    <label for="body">Body</label>
    <textarea id="body" name="body" rows="8" maxlength="20000" aria-describedby="body-error">#page.body#</textarea>
    <span id="body-error" class="field-error">#errors.body#</span>
  </div>
  <button type="submit">Save changes</button>
</form>
</if>

5. Show the result on the admin page. Replace site/admin/index.html. Only the line under the heading is new: it shows the server's message after a change.

site/admin/index.html
<what>
title = "Admin | My website"
description = "The editing area: every page, published or draft."
fetch.pages = "local:pages"
fetch.drafts.url = "local:pages"
fetch.drafts.copy = "working"
</what>

<h1>Admin</h1>
<if flash.success><p class="form-success" role="status">#flash.success#</p></if>
<p>Only a signed-in editor can open this page. <a href="/admin/pages/new">Create a new page</a>: it starts as a draft that only editors see.</p>
<if pages>
<div class="table-wrap">
  <table class="inventory">
    <caption>Published pages</caption>
    <thead>
      <tr><th scope="col">Id</th><th scope="col">Title</th><th scope="col">Slug</th></tr>
    </thead>
    <tbody>
<loop data="#pages#" as="page">
      <tr><td>#page.id#</td><td><a href="/pages/#page.slug|urlencode#">#page.title#</a></td><td><code>#page.slug#</code></td></tr>
</loop>
    </tbody>
  </table>
</div>
<else/>
<p class="empty-state">No pages yet.</p>
</if>

<h2>All pages</h2>
<p>The working copies, drafts included. Visitors never see this list. Follow a title to edit that page; its preview publishes it.</p>
<if drafts>
<div class="table-wrap">
  <table class="inventory">
    <caption>All pages (working copies)</caption>
    <thead>
      <tr><th scope="col">Id</th><th scope="col">Title</th><th scope="col">Slug</th><th scope="col">Revision</th><th scope="col">Published</th></tr>
    </thead>
    <tbody>
<loop data="#drafts#" as="page">
      <tr><td>#page.id#</td><td><a href="/admin/pages/edit/#page.id#">#page.title#</a></td><td><code>#page.slug#</code></td><td>#page._revision#</td><td><if page._published>revision #page._published_revision#<else/>not published</if></td></tr>
</loop>
    </tbody>
  </table>
</div>
</if>

6. Style the message and the button. Replace static/styles.css. Only the last two rules are new.

static/styles.css
/* Framework styles are supplied automatically. These rules style our site. */
.site-body {
  margin: 0;
  color: #26372c;
  background: #faf8f2;
  font-family: system-ui, sans-serif;
  line-height: 1.65;
}
.site-header, .site-main, .site-footer {
  width: min(100% - 2rem, 48rem);
  margin-inline: auto;
}
.site-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding-block: 1.5rem;
  border-bottom: 1px solid #c9cec4;
}
.site-name { font-size: 1.2rem; font-weight: 750; }
.site-nav { display: flex; flex-wrap: wrap; gap: 1rem; }
.site-body a { color: #315f40; text-underline-offset: 0.2em; }
.site-body a:focus-visible {
  outline: 3px solid #825414;
  outline-offset: 4px;
}
.site-main { padding-block: 2.5rem; }
.site-main h1 { font-size: clamp(2rem, 7vw, 3rem); line-height: 1.15; }
.site-main p { max-width: 65ch; }
.site-main figure { margin: 2rem 0; }
.site-image { display: block; max-width: 100%; height: auto; border-radius: 1rem; }
.site-main figcaption { margin-top: 0.6rem; color: #4b594e; }
.site-footer { border-top: 1px solid #c9cec4; padding-block: 1.5rem; }
.skip-link {
  position: absolute;
  top: -10rem;
  left: 1rem;
  padding: 0.6rem 1rem;
  background: #fff;
  z-index: 10;
}
.skip-link:focus { top: 1rem; }

.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); gap: 1.25rem; }
.content-card { padding: 1.5rem; border: 1px solid #c9cec4; border-radius: 1rem; background: #fffdf8; }
.content-card h2 { margin-top: 0; font-size: 1.4rem; }
.card-note { font-size: 0.9rem; color: #4b594e; }
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.empty-state { padding: 1rem 1.25rem; border: 1px dashed #c9cec4; border-radius: 1rem; }
.table-wrap { overflow-x: auto; }
.inventory { border-collapse: collapse; width: 100%; min-width: 28rem; }
.inventory caption { text-align: left; padding-bottom: 0.6rem; color: #4b594e; }
.inventory th, .inventory td { text-align: left; padding: 0.6rem 0.8rem; border-bottom: 1px solid #c9cec4; vertical-align: top; }
.inventory thead th { border-bottom-width: 2px; }

.listing-controls { display: flex; flex-wrap: wrap; align-items: end; gap: 0.75rem 1rem; margin-bottom: 1rem; }
.listing-controls .field { display: grid; gap: 0.25rem; }
.listing-controls input, .listing-controls select, .listing-controls button { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.7rem; border: 1px solid #8a9586; border-radius: 0.5rem; background: #fffdf8; color: inherit; }
.listing-controls input { min-width: min(100%, 16rem); }
.listing-controls button { background: #315f40; border-color: #315f40; color: #fff; cursor: pointer; }
.listing-controls :focus-visible { outline: 3px solid #825414; outline-offset: 2px; }
.result-count { color: #4b594e; }
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 1rem; margin-top: 1.5rem; }
.pager [aria-current] { font-weight: 650; }

/* L13: the account corner and the sign-in form */
.site-account { display: flex; flex-wrap: wrap; align-items: center; gap: 0.75rem; }
.site-account form { margin: 0; }
.site-account button, .sign-in button { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.9rem; border: 1px solid #315f40; border-radius: 0.5rem; background: #315f40; color: #fff; cursor: pointer; }
.sign-in { display: grid; gap: 1rem; max-width: 24rem; }
.sign-in .field { display: grid; gap: 0.25rem; }
.sign-in input { box-sizing: border-box; min-height: 2.75rem; font: inherit; padding: 0.45rem 0.7rem; border: 1px solid #8a9586; border-radius: 0.5rem; background: #fffdf8; color: inherit; }
.form-error { padding: 0.75rem 1rem; border-left: 4px solid #a33a2e; background: #fbeeec; }

/* L17: the note above a preview */
.preview-note { padding: 0.75rem 1rem; border-left: 4px solid #315f40; background: #edf3ec; }

/* L18: Publish and Unpublish side by side; Unpublish is the quieter button */
.publish-controls { display: flex; flex-wrap: wrap; gap: 0.75rem; margin-block: 1.5rem; }
.publish-controls .unpublish button { background: #fffdf8; color: #315f40; }

/* L19: the message after a change, and the delete button */
.form-success { padding: 0.75rem 1rem; border-left: 4px solid #315f40; background: #edf3ec; }
.danger button { background: #a33a2e; border-color: #a33a2e; }

7. Start the server again with run-what dev. Nothing is published on this start.

Check

  1. Signed in, create a draft with the form from lesson 15. Open it from All pages and follow Delete this page. The confirmation names its title and address. Follow Cancel: you are back on the edit page and nothing changed.
  2. Open the confirmation again and choose Delete this page permanently. You land on /admin, which says “Item deleted in pages”, and the page is no longer in All pages.
  3. Open the confirmation for a published page: it has no Delete button, only a link to the preview. Unpublish the page there, open the confirmation again, and delete it. Its public address now answers “Page not found”.
  4. Open a confirmation, then save the page from another tab. Delete from the confirmation: it says “This page changed since you opened it.”, the page is still there, and the button is gone until you follow Reload this page before trying again.

Exercise

Create a page with the address practice, publish it, unpublish it and delete it. Then create a new page with the same address. Explain in two sentences why the new page is not published, and why it has a different id.

If something goes wrong

  • “You don't have permission to delete that item.” The delete line still says none, the server was not restarted after you changed it, or you are not signed in as the editor.
  • “Unpublish this page before deleting it.” The page is published. Open its preview, choose Unpublish, then open the confirmation again.
  • “This page changed since you opened it.” The page was saved, published or unpublished after you opened the confirmation. Follow Reload this page before trying again and check it is still the page you mean to delete.
  • “Reload the page and try again.” A hidden _revision or _live_version field is missing; check step 3.
  • Start again from a known state. Download the L19 checkpoint into a new, empty folder; never unpack it over your project. It has no database and no account: run run-what admin add editor in the new folder, then run-what dev. The first start imports the eight pages and publishes them once; it has no drafts, so create one with the form from lesson 15 before deleting.

Next: L20: make one edit flow feel immediate.