Admin UI consistency: DaisyUI 5 forms, shared modal and setting rows
- Migrate all DaisyUI 4 form markup (form-control, label-text, *-bordered — removed in v5) to v5 fieldset/legend; labels sat inline beside inputs and inputs fell back to a fixed 20rem width. - One Modal component (native <dialog>: Escape, focus trap) for page, event, item and slot editors; destructive actions confirmed everywhere. The page Delete confirm was ignored — returning false from onsubmit doesn't cancel — so Cancel still deleted the page. - Block editor follows the background-picker style throughout: SegmentedControl rows (title-cased options, clearer selected state) and a new ColorField row with reset; image position is Left/Right like the alignment controls. - Globals keep their layout; readable field labels, save feedback without reload, hints wrap. - Pages list: fixed broken Preview link (VITE_PUBLIC_SITE_URL was undefined → "undefined/slug"), shown for published pages only; action column no longer breaks row alignment; empty states on all tables. - Items: image picker instead of a raw media-id field, upload errors shown (were swallowed), prices in the shop currency instead of hard-coded EUR. - Slots/orders/calendar: times shown in the site timezone with a fixed locale (server and browser render the same text). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
This commit is contained in:
parent
cfe4e2062d
commit
af7b4af5de
16 changed files with 1237 additions and 1211 deletions
|
|
@ -1,10 +1,15 @@
|
|||
<script lang="ts">
|
||||
import { uploadMedia } from '$lib/pb';
|
||||
import { confirmSubmit } from '$lib/admin/confirm';
|
||||
import Modal from '$lib/admin/Modal.svelte';
|
||||
import type { ItemRecord, ItemType } from '$lib/types';
|
||||
|
||||
let { data, form } = $props();
|
||||
let items = $derived(data.items || []);
|
||||
let showEditor = $state(false);
|
||||
let uploading = $state(false);
|
||||
let uploadError = $state('');
|
||||
let fileInput: HTMLInputElement | undefined = $state();
|
||||
|
||||
type ItemDraft = {
|
||||
id: string;
|
||||
|
|
@ -22,7 +27,7 @@
|
|||
let draft = $state<ItemDraft>(emptyDraft());
|
||||
let action = $derived(draft.id ? '?/update' : '?/create');
|
||||
|
||||
const eur = new Intl.NumberFormat(undefined, { style: 'currency', currency: 'EUR' });
|
||||
let money = $derived(new Intl.NumberFormat('en-GB', { style: 'currency', currency: data.currency }));
|
||||
|
||||
function emptyDraft(): ItemDraft {
|
||||
return { id: '', name: '', description: '', price: 0, image: '', imageUrl: '', type: 'item', stock: '', duration: '', active: true };
|
||||
|
|
@ -30,6 +35,7 @@
|
|||
|
||||
function createItem() {
|
||||
draft = emptyDraft();
|
||||
uploadError = '';
|
||||
showEditor = true;
|
||||
}
|
||||
|
||||
|
|
@ -46,6 +52,7 @@
|
|||
duration: item.duration == null ? '' : String(item.duration),
|
||||
active: item.active
|
||||
};
|
||||
uploadError = '';
|
||||
showEditor = true;
|
||||
}
|
||||
|
||||
|
|
@ -54,16 +61,12 @@
|
|||
const file = input.files?.[0];
|
||||
if (!file) return;
|
||||
uploading = true;
|
||||
uploadError = '';
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('alt', file.name.replace(/\.[^/.]+$/, '').replace(/[-_]/g, ' '));
|
||||
const res = await fetch('/api/upload', { method: 'POST', body: formData });
|
||||
if (!res.ok) throw new Error(`Upload failed (${res.status})`);
|
||||
const result = await res.json();
|
||||
const result = await uploadMedia(file);
|
||||
draft = { ...draft, image: result.record.id, imageUrl: result.url };
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
uploadError = err instanceof Error ? err.message : 'Upload failed.';
|
||||
} finally {
|
||||
uploading = false;
|
||||
input.value = '';
|
||||
|
|
@ -82,30 +85,34 @@
|
|||
<button class="btn btn-primary" type="button" onclick={() => createItem()}>Create item</button>
|
||||
</div>
|
||||
|
||||
{#if form?.message}<div class="alert alert-error">{form.message}</div>{/if}
|
||||
{#if form?.message}<div class="alert alert-error" role="alert">{form.message}</div>{/if}
|
||||
|
||||
<div class="rounded-box bg-base-100 p-5 shadow">
|
||||
<div class="overflow-x-auto">
|
||||
<table class="table">
|
||||
<thead><tr><th>Name</th><th>Type</th><th>Price</th><th>Stock</th><th>Duration</th><th>Status</th><th></th></tr></thead>
|
||||
<tbody>
|
||||
{#each items as item}
|
||||
{#each items as item (item.id)}
|
||||
<tr>
|
||||
<td>
|
||||
<div class="flex items-center gap-3">
|
||||
{#if item.imageUrl}
|
||||
<img class="h-10 w-10 rounded-box object-cover" src={item.imageUrl} alt="" />
|
||||
<img class="size-10 rounded-box object-cover" src={item.imageUrl} alt="" />
|
||||
{:else}
|
||||
<div class="size-10 rounded-box bg-base-200"></div>
|
||||
{/if}
|
||||
<span class="font-semibold">{item.name}</span>
|
||||
</div>
|
||||
</td>
|
||||
<td><span class="badge">{item.type === 'item_with_time' ? 'Service' : 'Product'}</span></td>
|
||||
<td>{eur.format(item.price)}</td>
|
||||
<td>{item.type === 'item' ? (item.stock ?? '—') : '—'}</td>
|
||||
<td>{money.format(item.price)}</td>
|
||||
<td>{item.type === 'item' ? (item.stock ?? '∞') : '—'}</td>
|
||||
<td>{item.type === 'item_with_time' ? `${item.duration ?? 0} min` : '—'}</td>
|
||||
<td><span class="badge" class:badge-success={item.active}>{item.active ? 'Active' : 'Inactive'}</span></td>
|
||||
<td class="text-right"><button class="btn btn-sm" type="button" onclick={() => editItem(item)}>Edit</button></td>
|
||||
</tr>
|
||||
{:else}
|
||||
<tr><td colspan="7" class="py-10 text-center text-base-content/60">No items yet — create a service or product.</td></tr>
|
||||
{/each}
|
||||
</tbody>
|
||||
</table>
|
||||
|
|
@ -113,87 +120,84 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
{#if showEditor}
|
||||
<div class="fixed inset-0 z-50 bg-black/50 p-4">
|
||||
<div class="mx-auto max-h-full max-w-2xl overflow-auto rounded-box bg-base-100 p-6 shadow-xl">
|
||||
<div class="mb-4 flex items-center justify-between gap-3">
|
||||
<h2 class="text-2xl font-bold">{draft.id ? 'Edit item' : 'Create item'}</h2>
|
||||
<button class="btn btn-ghost btn-sm" type="button" onclick={() => (showEditor = false)}>Close</button>
|
||||
</div>
|
||||
<Modal bind:open={showEditor} title={draft.id ? 'Edit item' : 'Create item'} wide>
|
||||
<form method="POST" action={action} class="space-y-1">
|
||||
<input type="hidden" name="id" value={draft.id} />
|
||||
<input type="hidden" name="active" value={draft.active ? 'true' : 'false'} />
|
||||
<input type="hidden" name="image" value={draft.image} />
|
||||
|
||||
<form method="POST" action={action} class="space-y-4">
|
||||
<input type="hidden" name="id" value={draft.id} />
|
||||
<input type="hidden" name="active" value={draft.active ? 'true' : 'false'} />
|
||||
<label class="fieldset">
|
||||
<span class="fieldset-legend">Name</span>
|
||||
<input class="input w-full" name="name" required bind:value={draft.name} />
|
||||
</label>
|
||||
|
||||
<label class="form-control w-full">
|
||||
<div class="label"><span class="label-text">Name</span></div>
|
||||
<input class="input input-bordered w-full" name="name" required bind:value={draft.name} />
|
||||
</label>
|
||||
<label class="fieldset">
|
||||
<span class="fieldset-legend">Description</span>
|
||||
<textarea class="textarea min-h-28 w-full" name="description" bind:value={draft.description}></textarea>
|
||||
</label>
|
||||
|
||||
<label class="form-control w-full">
|
||||
<div class="label"><span class="label-text">Description</span></div>
|
||||
<textarea class="textarea textarea-bordered min-h-28" name="description" bind:value={draft.description}></textarea>
|
||||
</label>
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-2">
|
||||
<label class="form-control w-full">
|
||||
<div class="label"><span class="label-text">Price</span></div>
|
||||
<input class="input input-bordered w-full" type="number" step="0.01" min="0" name="price" required bind:value={draft.price} />
|
||||
</label>
|
||||
<label class="form-control w-full">
|
||||
<div class="label"><span class="label-text">Type</span></div>
|
||||
<select class="select select-bordered w-full" name="type" bind:value={draft.type}>
|
||||
<option value="item">Product</option>
|
||||
<option value="item_with_time">Service</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{#if draft.type === 'item'}
|
||||
<label class="form-control w-full">
|
||||
<div class="label"><span class="label-text">Stock</span></div>
|
||||
<input class="input input-bordered w-full" type="number" min="0" step="1" name="stock" placeholder="Unlimited when empty" bind:value={draft.stock} />
|
||||
</label>
|
||||
{:else}
|
||||
<label class="form-control w-full">
|
||||
<div class="label"><span class="label-text">Duration minutes</span></div>
|
||||
<input class="input input-bordered w-full" type="number" min="0" step="1" name="duration" placeholder="60" bind:value={draft.duration} />
|
||||
</label>
|
||||
{/if}
|
||||
|
||||
<div class="form-control w-full">
|
||||
<div class="label"><span class="label-text">Image</span></div>
|
||||
<div class="flex items-start gap-3">
|
||||
{#if draft.imageUrl}
|
||||
<img class="h-16 w-16 rounded-box object-cover" src={draft.imageUrl} alt="" />
|
||||
{/if}
|
||||
<div class="flex-1 space-y-2">
|
||||
<input class="input input-bordered w-full" name="image" placeholder="Media record id" bind:value={draft.image} />
|
||||
<input class="file-input file-input-bordered file-input-sm w-full" type="file" accept="image/*" disabled={uploading} onchange={onImageSelected} />
|
||||
<p class="text-xs text-base-content/60">
|
||||
{uploading ? 'Uploading…' : 'Upload a file or paste a media record id. A media picker will replace this in a later iteration.'}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<label class="label cursor-pointer justify-start gap-3">
|
||||
<input class="toggle toggle-success" type="checkbox" bind:checked={draft.active} />
|
||||
<span class="label-text">Active</span>
|
||||
</label>
|
||||
|
||||
<div class="flex justify-between gap-2 pt-2">
|
||||
{#if draft.id}
|
||||
<button class="btn btn-error" type="submit" formaction="?/delete">Delete</button>
|
||||
{:else}
|
||||
<span></span>
|
||||
{/if}
|
||||
<div class="flex gap-2">
|
||||
<button class="btn" type="button" onclick={() => (showEditor = false)}>Cancel</button>
|
||||
<button class="btn btn-primary" type="submit">Save item</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<div class="grid gap-x-4 md:grid-cols-2">
|
||||
<label class="fieldset">
|
||||
<span class="fieldset-legend">Price ({data.currency})</span>
|
||||
<input class="input w-full" type="number" step="0.01" min="0" name="price" required bind:value={draft.price} />
|
||||
</label>
|
||||
<label class="fieldset">
|
||||
<span class="fieldset-legend">Type</span>
|
||||
<select class="select w-full" name="type" bind:value={draft.type}>
|
||||
<option value="item">Product</option>
|
||||
<option value="item_with_time">Service (bookable)</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if draft.type === 'item'}
|
||||
<label class="fieldset">
|
||||
<span class="fieldset-legend">Stock</span>
|
||||
<input class="input w-full" type="number" min="0" step="1" name="stock" placeholder="Unlimited when empty" bind:value={draft.stock} />
|
||||
</label>
|
||||
{:else}
|
||||
<label class="fieldset">
|
||||
<span class="fieldset-legend">Duration (minutes)</span>
|
||||
<input class="input w-full" type="number" min="1" step="1" name="duration" placeholder="60" required bind:value={draft.duration} />
|
||||
</label>
|
||||
{/if}
|
||||
|
||||
<div class="fieldset">
|
||||
<span class="fieldset-legend">Image</span>
|
||||
<div class="flex items-center gap-4">
|
||||
{#if draft.imageUrl}
|
||||
<img class="size-20 rounded-box object-cover shadow" src={draft.imageUrl} alt="" />
|
||||
{:else}
|
||||
<div class="grid size-20 place-items-center rounded-box border-2 border-dashed border-base-300 text-xs text-base-content/40">No image</div>
|
||||
{/if}
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<button class="btn btn-sm" type="button" disabled={uploading} onclick={() => fileInput?.click()}>
|
||||
{#if uploading}<span class="loading loading-spinner loading-xs"></span> Uploading…{:else}{draft.imageUrl ? 'Replace image' : 'Upload image'}{/if}
|
||||
</button>
|
||||
{#if draft.image}
|
||||
<button class="btn btn-ghost btn-sm text-error" type="button" onclick={() => (draft = { ...draft, image: '', imageUrl: '' })}>Remove</button>
|
||||
{/if}
|
||||
</div>
|
||||
<input class="hidden" type="file" accept="image/*" bind:this={fileInput} onchange={onImageSelected} />
|
||||
</div>
|
||||
{#if uploadError}<span class="label text-error">{uploadError}</span>{/if}
|
||||
</div>
|
||||
|
||||
<label class="label mt-2 cursor-pointer gap-3 text-base-content">
|
||||
<input class="toggle toggle-success" type="checkbox" bind:checked={draft.active} />
|
||||
Active <span class="text-base-content/60">— inactive items are hidden from the shop</span>
|
||||
</label>
|
||||
|
||||
<div class="modal-action justify-between">
|
||||
{#if draft.id}
|
||||
<button class="btn btn-error btn-outline" type="submit" formaction="?/delete" formnovalidate onclick={confirmSubmit(`Delete "${draft.name}"?`)}>Delete</button>
|
||||
{:else}
|
||||
<span></span>
|
||||
{/if}
|
||||
<div class="flex gap-2">
|
||||
<button class="btn" type="button" onclick={() => (showEditor = false)}>Cancel</button>
|
||||
<button class="btn btn-primary" type="submit" disabled={uploading}>Save item</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue