Dough Git

clove/pkviewer

web/src/lib/components/admin/SectionAdmin.svelte

back to web/src/lib/components/admin

<!-- web/src/lib/components/admin/SectionAdmin.svelte -->
<script lang="ts">
  import { deleteSection, saveSection } from "$lib/admin.remote.ts";
  import type { ActionResult, AdminSection } from "$lib/types.ts";
  import Section from "../manage/Section.svelte";

  let {
    sections,
    pending,
    run,
  }: {
    sections: AdminSection[];
    pending: boolean;
    run: (action: () => Promise<ActionResult>, okText: string) => Promise<void>;
  } = $props();

  const EMPTY = {
    id: "",
    label: "",
    description: "",
    sortOrder: 100,
  };

  let draft = $state({ ...EMPTY });

  function add() {
    return run(async () => {
      const outcome = await saveSection({
        id: draft.id.trim(),
        label: draft.label.trim(),
        description: draft.description.trim(),
        sortOrder: draft.sortOrder,
      });
      if (outcome.ok) draft = { ...EMPTY };
      return outcome;
    }, "Section saved.");
  }
</script>

<Section title="Sections" description="The headings on the public credits page, in this order.">
  <ul class="mg-list">
    {#each sections as section (section.id)}
      <li class="mg-item">
        <div class="grow">
          <strong>{section.label}</strong>
          <div class="mg-item-meta muted">
            <code>{section.id}</code> · order {section.sortOrder}
          </div>
        </div>
        <button
          type="button"
          class="quiet"
          disabled={pending}
          onclick={() => run(() => deleteSection(section.id), "Section removed.")}
        >
          Remove
        </button>
      </li>
    {/each}
  </ul>

  <div class="mg-panel-inset">
    <div class="mg-row">
      <div class="mg-field">
        <label for="section-id">Identifier</label>
        <input id="section-id" bind:value={draft.id} placeholder="translators" />
      </div>
      <div class="mg-field grow">
        <label for="section-label">Heading</label>
        <input id="section-label" bind:value={draft.label} maxlength="60" placeholder="Translators" />
      </div>
      <div class="mg-field">
        <label for="section-order">Order</label>
        <input id="section-order" type="number" bind:value={draft.sortOrder} />
      </div>
    </div>

    <div class="mg-field">
      <label for="section-desc">Description (optional)</label>
      <input id="section-desc" bind:value={draft.description} maxlength="200" />
    </div>

    <div class="mg-actions">
      <button
        type="button"
        class="quiet"
        disabled={pending || !draft.id.trim() || !draft.label.trim()}
        onclick={add}
      >
        Add section
      </button>
    </div>
  </div>
</Section>