Generate fills a date range (weekdays x opening hours) spaced by the service's duration + get-ready time (overridable). Copy repeats a week into the next 1-12 weeks; Clear deletes free slots in a range. Each previews first, skips past slots and overlaps with the same service, and never touches booked or in-basket slots. Planning is pure and DST-safe in $shared/slots. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VaXE9U7AmW3dfD2w6cbLfk
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import { applyAction } from '$app/forms';
|
|
import type { SubmitFunction } from '@sveltejs/kit';
|
|
import type { BulkResult } from '$lib/slots';
|
|
|
|
/**
|
|
* State for a /slots bulk form (generate, copy, clear), which is always
|
|
* submitted twice: first `intent=preview` (the server says what it would
|
|
* do), then `intent=create` to do it. The preview only counts while the
|
|
* inputs are unchanged — `inputsKey` is a snapshot of them — so the admin
|
|
* can't confirm numbers for a form they have since edited.
|
|
*
|
|
* Previews and errors stay inside the modal; a finished run closes it and
|
|
* reloads the page, whose `form.bulk` then shows the outcome.
|
|
*/
|
|
export function bulkForm(inputsKey: () => string, close: () => void) {
|
|
let preview = $state<BulkResult | null>(null);
|
|
let previewKey = $state('');
|
|
let error = $state('');
|
|
let busy = $state(false);
|
|
|
|
const submit: SubmitFunction = () => {
|
|
const key = inputsKey();
|
|
busy = true;
|
|
error = '';
|
|
return async ({ result, update }) => {
|
|
busy = false;
|
|
if (result.type === 'success' && result.data?.bulk?.intent === 'preview') {
|
|
preview = result.data.bulk as BulkResult;
|
|
previewKey = key;
|
|
} else if (result.type === 'failure') {
|
|
error = String(result.data?.message || 'Something went wrong.');
|
|
} else if (result.type === 'success') {
|
|
preview = null;
|
|
close();
|
|
await update({ reset: false });
|
|
} else {
|
|
await applyAction(result);
|
|
}
|
|
};
|
|
};
|
|
|
|
return {
|
|
get preview() {
|
|
return preview && previewKey === inputsKey() ? preview : null;
|
|
},
|
|
get error() {
|
|
return error;
|
|
},
|
|
get busy() {
|
|
return busy;
|
|
},
|
|
reset() {
|
|
preview = null;
|
|
error = '';
|
|
},
|
|
submit
|
|
};
|
|
}
|
|
|
|
export type BulkForm = ReturnType<typeof bulkForm>;
|