what

Create your first draft

Let the editor create pages from the admin area. A new page is a draft: the editor sees it, visitors do not, and the server checks every field itself.

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

Read

Start at L14. The admin area is private and visitors read published copies, but nobody can write a page yet. This lesson opens one door: an editor may create pages. Changing them waits for lesson 16.

A form in the browser is only a convenience. Anyone can send the same request without it, so the rules that matter live on the server, in what.toml: which fields a page may have (fields.writable), which are required, how long they may be, that a slug uses lowercase letters, digits and single hyphens, and that it never changes once the page exists. The server trims spaces, checks every rule, and refuses anything else, whatever form sent it.

A new page is a working copy with no published copy yet. The admin page reads working copies with fetch.drafts.copy = "working", which only the roles in working_read may use; everyone else, and every public page, still sees published copies only.

This protects records, not files. The course never uploads a file, but if you add uploads later, know that an uploaded file is public at its /uploads/ address from the moment it is uploaded, even while the page that links to it is a draft. See File Uploads.

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

2. Let editors create checked pages. Replace what.toml. The create, working_read and fields.* lines are new.

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 now create pages; changing and deleting them come later. No
# two pages may share a slug: the database itself refuses the second one.
[collections.pages]
create = "editor"
update = "none"
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. Show every page to the editor. Replace site/admin/index.html. It keeps the published table and adds the working copies, with each page's revision.

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.</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>#page.title#</td><td><code>#page.slug#</code></td><td>#page._revision#</td></tr>
</loop>
    </tbody>
  </table>
</div>
</if>

4. Add the form. Create site/admin/pages/new.html. It is an ordinary form: it works without JavaScript, and when the server refuses it you come back to it with the errors and everything you typed.

site/admin/pages/new.html
<what>
title = "New page | Admin | My website"
description = "Create a draft page."
</what>

<h1>New page</h1>
<p>A new page is a draft: you see it in the admin area, visitors do not, until it 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.slug><li>Address: #errors.slug#</li></if>
    <if errors.summary><li>Summary: #errors.summary#</li></if>
    <if errors.body><li>Body: #errors.body#</li></if>
  </ul>
</div>
</if>
<form class="sign-in" method="post" action="/w-action/pages?w-redirect=/admin">
  <div class="field">
    <label for="title">Title</label>
    <input id="title" name="title" type="text" maxlength="120" required value="#old.title#" aria-describedby="title-error">
    <span id="title-error" class="field-error">#errors.title#</span>
  </div>
  <div class="field">
    <label for="slug">Address (slug)</label>
    <input id="slug" name="slug" type="text" required value="#old.slug#" aria-describedby="slug-help slug-error">
    <span id="slug-help">Lowercase letters, digits and single hyphens, for example my-first-draft. It cannot change later.</span>
    <span id="slug-error" class="field-error">#errors.slug#</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">Create draft</button>
</form>

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

Check

  1. Signed in, open /admin and follow Create a new page. Fill in a title such as My first draft, the address my-first-draft and a short summary, then create it. You land on /admin: the page is in All pages with revision 1, and not in Published pages.
  2. Open /pages and /pages/my-first-draft. The list is unchanged and the new address is “Page not found”: visitors cannot see a draft.
  3. Create another page with the address Bad Slug. You come back to the form with “Use lowercase letters, digits and single hyphens” next to the address, and every field still holds what you typed.

Exercise

The form says maxlength="120" and the server says max_length = { title = 120 }, but they count differently. The browser counts UTF-16 units, so an emoji uses two; the server counts characters, so it uses one. Explain why the browser may stop you from typing the whole of a title of 119 letters and one emoji while the server would accept it, and why the server's check still applies when a request does not come from this form.

If something goes wrong

  • The server stops with “needs fields.writable”. A fields.required, max_length, slug or immutable line is there without fields.writable. Replace the whole what.toml from step 2.
  • Creating a page says “You don't have permission to add to pages.” You are signed out, or signed in without the editor role. Sign in as editor.
  • “This slug is already used by another record.” Every page needs its own address. Choose another one.
  • /admin shows an error page. Check the two fetch.drafts lines in site/admin/index.html: copy must be exactly "working".
  • Start again from a known state. Download the L15 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.

Next: L16: edit without losing your work.