what

Edit without losing your work

Let the editor change a page's working copy, and make sure two open tabs can never silently overwrite each other's work.

Allow 20–30 minutes. This is an editorial estimate. Continue in the same my-site folder, signed in as the editor, with the draft from lesson 15.

Read

Start at L15. The editor can create drafts but not change any page. This lesson adds editing. Saving changes the working copy only; visitors keep reading the published copy until the page is published, which comes in a later lesson.

Every page has a revision, a number that grows with each save. The edit form remembers the revision it was opened with in a hidden _revision field. When you save, the server compares it with the page's current revision. If someone saved in between (you, in another tab, for example), the numbers differ and the server refuses the save with “This page changed since you opened it.” Nothing is overwritten, and your form comes back with what you typed so you can copy it before reloading.

The address (slug) was fixed when the page was created, so the form shows it but never sends it. A request that tries to change it anyway is refused.

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-L16.

2. Let editors change pages. Replace what.toml. Only the update line 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 and change pages; deleting them comes later. No two
# pages may share a slug: the database itself refuses the second one.
[collections.pages]
create = "editor"
update = "editor"
delete = "none"
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"
# 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. Link each page to its form. Replace site/admin/index.html. In All pages, each title now opens its edit page.

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>
<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.</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></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></tr>
</loop>
    </tbody>
  </table>
</div>
</if>

4. Add the edit page. Create site/admin/pages/edit/[id].html, with the square brackets in the file name: /admin/pages/edit/9 opens it with params.id set to 9. It reads that page's working copy. After a refused save it shows the values you typed and the revision you started from; otherwise it shows the stored page.

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>
<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. Start the server again with run-what dev. Nothing is published on this start.

Check

  1. Signed in, open /admin and follow your draft's title in All pages. Change the title and save. You land on /admin: the draft shows the new title and revision 2. /pages is unchanged.
  2. Open the same edit page in two tabs. In the first, change the summary and save. In the second, change the title and save: the page says “This page changed since you opened it” and still shows your new title. Save again: it conflicts again, because the form keeps the revision it was opened with. Reload the second tab: it shows the first tab's summary and the current revision.
  3. Clear the title and save: “This field is required.” appears, the other fields keep what you typed, and the revision does not change. Type a title and save: it works.

Exercise

Edit one of the eight published pages and save. Open its public address. Explain in two sentences why visitors still see the old text, and what has changed in the database.

If something goes wrong

  • “This page changed since you opened it.” Someone saved after you opened the form, maybe you in another tab. Copy what you typed, reload the page to get the latest version, then make your change again.
  • “Reload the page and try again.” The form was sent without its revision. Check the hidden _revision field in step 4.
  • Saving says “You don't have permission to update that item.” The update line still says none, or you are not signed in as the editor.
  • The edit page is “Page not found”. The id in the address does not match a page; open the page from All pages.
  • Start again from a known state. Download the L16 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 first.

Next: L17: preview what you are writing.