domaino/app-admin/src/lib/admin/slots/GenerateSlotsModal.svelte
agent a22d9f2348 Admin slots: places, blocked/full states, edits limited once booked
Calendar, day panel and list show available / in a basket / booked /
full / blocked (with what blocks it) and places taken. Services, Generate
and single slots take places (default from the service); Copy keeps
them. Once any place is taken only places (not below taken) and notes
can change. All reads go through the shop hook's slot details.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VaXE9U7AmW3dfD2w6cbLfk
2026-10-24 08:26:37 +00:00

145 lines
5.2 KiB
Svelte
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script lang="ts">
import { untrack } from 'svelte';
import { enhance } from '$app/forms';
import Modal from '$lib/admin/Modal.svelte';
import { WEEKDAYS, addDaysToKey, startTimesInWindows, type TimeWindow } from '$lib/slots';
import BulkActions from './BulkActions.svelte';
import { bulkForm } from './bulkForm.svelte';
type Service = { id: string; name: string; duration: number; bufferMinutes: number; capacity: number };
let {
open = $bindable(false),
items,
timezone,
today
}: { open?: boolean; items: Service[]; timezone: string; today: string } = $props();
// Kept between openings, so a second run starts from the last settings.
let draft = $state({
item: '',
fromDate: '',
toDate: '',
weekdays: [1, 2, 3, 4, 5],
windows: [{ from: '09:00', to: '17:00' }] as TimeWindow[],
bufferMinutes: '',
capacity: '',
notes: ''
});
const bulk = bulkForm(
() => JSON.stringify(draft),
() => (open = false)
);
let service = $derived(items.find((item) => item.id === draft.item));
let startTimes = $derived(
service ? startTimesInWindows(draft.windows, service.duration, Number(draft.bufferMinutes) || 0) : []
);
// Each opening starts without a stale preview; the first one also fills in dates.
$effect(() => {
if (!open) return;
untrack(() => {
bulk.reset();
if (!draft.fromDate) {
draft.fromDate = today;
draft.toDate = addDaysToKey(today, 27);
}
});
});
function selectService(id: string) {
draft.item = id;
// The service's own get-ready time and places are the defaults; both can be overridden here.
const item = items.find((item) => item.id === id);
draft.bufferMinutes = String(item?.bufferMinutes ?? 0);
draft.capacity = String(item?.capacity ?? 1);
}
function addWindow() {
const last = draft.windows.at(-1);
draft.windows.push(last ? { from: last.to, to: last.to } : { from: '09:00', to: '17:00' });
}
</script>
<Modal bind:open title="Generate slots" wide>
<form method="POST" action="?/generate" use:enhance={bulk.submit} class="space-y-1">
<label class="fieldset">
<span class="fieldset-legend">Service</span>
<select class="select w-full" name="item" required value={draft.item} onchange={(e) => selectService(e.currentTarget.value)}>
<option value="" disabled>Select a service…</option>
{#each items as item (item.id)}
<option value={item.id}>{item.name} ({item.duration} min)</option>
{/each}
</select>
</label>
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
<span class="fieldset-legend">From</span>
<input class="input w-full" type="date" name="fromDate" required bind:value={draft.fromDate} />
</label>
<label class="fieldset">
<span class="fieldset-legend">To</span>
<input class="input w-full" type="date" name="toDate" required min={draft.fromDate} bind:value={draft.toDate} />
</label>
</div>
<div class="fieldset">
<span class="fieldset-legend">Days</span>
<div class="join grid grid-cols-7">
{#each WEEKDAYS as weekday (weekday.day)}
<input class="btn join-item btn-sm px-1" type="checkbox" name="weekday" value={weekday.day} aria-label={weekday.label} bind:group={draft.weekdays} />
{/each}
</div>
</div>
<div class="fieldset">
<span class="fieldset-legend">Opening hours ({timezone})</span>
<div class="space-y-2">
{#each draft.windows as window, i}
<div class="flex items-center gap-2">
<input class="input w-32" type="time" name="windowFrom" required aria-label="From" bind:value={window.from} />
<span>–</span>
<input class="input w-32" type="time" name="windowTo" required aria-label="To" bind:value={window.to} />
{#if draft.windows.length > 1}
<button class="btn btn-ghost btn-sm" type="button" aria-label="Remove these hours" onclick={() => draft.windows.splice(i, 1)}>✕</button>
{/if}
</div>
{/each}
</div>
<button class="btn btn-ghost btn-sm mt-1 w-fit" type="button" onclick={addWindow}>+ Add hours (e.g. after a lunch break)</button>
</div>
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
<span class="fieldset-legend">Get-ready time (minutes)</span>
<input class="input w-32" type="number" min="0" step="1" name="bufferMinutes" bind:value={draft.bufferMinutes} />
<span class="label">Gap after each appointment. Defaults to the service’s own.</span>
</label>
<label class="fieldset">
<span class="fieldset-legend">Places per slot</span>
<input class="input w-32" type="number" min="1" max="500" step="1" name="capacity" bind:value={draft.capacity} />
<span class="label">More than 1 for a group session. Defaults to the service’s own.</span>
</label>
</div>
{#if service}
<p class="rounded-box bg-base-200 px-4 py-2 text-sm">
{#if startTimes.length}
<span class="font-semibold">{startTimes.length} per day:</span> {startTimes.join(', ')}
{:else}
No {service.duration}-minute appointment fits in these hours.
{/if}
</p>
{/if}
<label class="fieldset">
<span class="fieldset-legend">Notes <span class="font-normal text-base-content/50">(optional, added to every slot)</span></span>
<input class="input w-full" name="notes" bind:value={draft.notes} />
</label>
<BulkActions {bulk} {timezone} onCancel={() => (open = false)} />
</form>
</Modal>