ferst-core 0.13.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/ferst-modules.mjs +15 -1
- package/cms/config.template.yml +11 -0
- package/components/SiteHeader.astro +1360 -1273
- package/components/recipes/Book.astro +362 -31
- package/components/recipes/Hero.astro +536 -533
- package/content/blocks.ts +20 -6
- package/content/schemas.ts +31 -0
- package/integration.mjs +263 -249
- package/layouts/Base.astro +21 -2
- package/lib/book.ts +187 -12
- package/lib/image-size.mjs +73 -0
- package/package.json +1 -1
|
@@ -15,41 +15,137 @@
|
|
|
15
15
|
* The offer's name, length, price and who gives it are read from the platform at build time
|
|
16
16
|
* (lib/book); its free times and places in the browser, since they change by the minute. Needs
|
|
17
17
|
* JavaScript: choosing among live times can't be a plain form. Without it, the block says so.
|
|
18
|
+
*
|
|
19
|
+
* Or a person or a place (0.14.0; the maintainer 2026-11-24: "we need both and its up to the person
|
|
20
|
+
* configuring the module to pick what is suitable"). `resource` alone: the head introduces them (Sam,
|
|
21
|
+
* the Hall), then the offers they provide online as choices, and below, the chosen one's calendar,
|
|
22
|
+
* booked with them only. `service` and `resource`: that offer, with them only.
|
|
23
|
+
*
|
|
24
|
+
* Shown outside a site (the Ferst portal's preview, the maintainer 2026-11-24: "utilise the core
|
|
25
|
+
* ferst events and bookings module block as it would be applied … to visualise it before someone
|
|
26
|
+
* clicks 'add it to my site'"): the host says which site and platform through `Astro.locals.ferstBook`,
|
|
27
|
+
* and `demo` plays every step with the real free times but never holds or books anything.
|
|
18
28
|
*/
|
|
19
29
|
import ContactForm from './ContactForm.astro';
|
|
20
30
|
import Heading from '../blocks/Heading.astro';
|
|
21
|
-
import { loadBookService, metaLine, whereText } from '../../lib/book';
|
|
31
|
+
import { bookTarget, eventsData, ldJson, loadBookResource, loadBookService, loadEventDates, metaLine, whereText, type BookService, type BookWho, type EventsContext } from '../../lib/book';
|
|
22
32
|
import { resolveGitBase, resolveSiteId } from '../../lib/git-base.mjs';
|
|
33
|
+
import { canonicalUrl } from '../../lib/seo.mjs';
|
|
34
|
+
import { loadSiteSettings } from '../../lib/siteSettings';
|
|
23
35
|
|
|
24
36
|
interface Props {
|
|
25
|
-
service
|
|
37
|
+
service?: string;
|
|
38
|
+
resource?: string;
|
|
26
39
|
event?: string;
|
|
40
|
+
/** `button`: "Book now" for one date of a class or an event (its next with places, or `date`). */
|
|
41
|
+
look?: 'button';
|
|
42
|
+
date?: string;
|
|
43
|
+
/** Dates across chosen classes and events, as one list: each an offer booked by places, or one event of it. */
|
|
44
|
+
list?: Array<{ service: string; event?: string }>;
|
|
27
45
|
title?: string;
|
|
28
46
|
intro?: string;
|
|
29
47
|
note?: string;
|
|
30
48
|
buttonLabel?: string;
|
|
31
49
|
}
|
|
32
|
-
const {
|
|
33
|
-
const
|
|
34
|
-
const
|
|
50
|
+
const { event, title, intro, note, buttonLabel = 'Book', look, date } = Astro.props;
|
|
51
|
+
const list = Astro.props.list?.length ? Astro.props.list : null;
|
|
52
|
+
const target = list ? { service: null, resource: null } : bookTarget(Astro.props);
|
|
53
|
+
const host = (Astro.locals as { ferstBook?: { siteId?: string; platform?: string; demo?: boolean } } | undefined)?.ferstBook;
|
|
54
|
+
const base = host?.platform ?? resolveGitBase();
|
|
55
|
+
const siteId = host?.siteId ?? resolveSiteId();
|
|
56
|
+
const demo = !!host?.demo;
|
|
57
|
+
let who: BookWho | null = null;
|
|
58
|
+
let offers: BookService[];
|
|
59
|
+
if (list) {
|
|
60
|
+
// A custom list (the maintainer, 2026-11-24): the dates of each, as one list.
|
|
61
|
+
offers = await Promise.all(list.map((l) => loadBookService(l.service, { base, siteId, event: l.event ?? null })));
|
|
62
|
+
const timed = offers.find((o) => o.mode !== 'places');
|
|
63
|
+
if (timed) throw new Error(`Book block: a list is of classes and events, booked by places; "${timed.name}" is booked by time. Give it a block of its own, or run \`npx ferst-modules\` to list what's booked by places.`);
|
|
64
|
+
} else if (target.service) {
|
|
65
|
+
offers = [await loadBookService(target.service, { base, siteId, event: event ?? null, resource: target.resource })];
|
|
66
|
+
} else {
|
|
67
|
+
const got = await loadBookResource(target.resource!, { base, siteId });
|
|
68
|
+
who = got.resource;
|
|
69
|
+
offers = got.services;
|
|
70
|
+
}
|
|
71
|
+
const lead = offers[0];
|
|
72
|
+
// "Book now" (the maintainer's fourth way, 2026-11-24): one date of a class or an event, nothing to pick
|
|
73
|
+
// but how many places. A haircut or a hire becomes one date only when it's booked, so it has no button.
|
|
74
|
+
const one = look === 'button';
|
|
75
|
+
if (one && (who || lead.mode !== 'places')) {
|
|
76
|
+
throw new Error(`Book block: a "Book now" button is for a class or an event, booked by places; "${lead.name}" is booked by time. Leave out "look" to show its calendar, or run \`npx ferst-modules\` to list what's booked by places.`);
|
|
77
|
+
}
|
|
78
|
+
const anchor = `book-${who ? who.id : `${lead.id}${lead.event ? `-${lead.event}` : ''}${lead.resource ? `-${lead.resource}` : ''}`}`;
|
|
35
79
|
const { align: blockAlign, headingAlign } = Astro.props as { align?: 'start' | 'center' | 'end'; headingAlign?: 'start' | 'center' | 'end' };
|
|
36
80
|
const headAlign = headingAlign ?? blockAlign ?? 'center';
|
|
37
|
-
|
|
38
|
-
const
|
|
81
|
+
// The head: the offer (its length, with whom or where, its price), or the person or place it names.
|
|
82
|
+
const heading = title ?? (list ? 'Coming up' : who ? who.name : lead.name);
|
|
83
|
+
const meta = list ? '' : who ? (offers.length === 1 ? metaLine({ ...lead, resources: [] }) : '') : metaLine(lead);
|
|
84
|
+
const lines = intro ?? (list ? '' : who ? who.description : lead.description);
|
|
85
|
+
const many = !list && offers.length > 1;
|
|
86
|
+
const panels = list ? [lead] : offers;
|
|
87
|
+
const sources = list ? JSON.stringify(offers.map((o) => ({ book: o.definition, event: o.event ?? '' }))) : undefined;
|
|
88
|
+
const placeOf = (d: BookService) => d.resources.find((r) => r.kind === 'place')?.name ?? '';
|
|
89
|
+
|
|
90
|
+
// For search engines (0.15.0; myai4-webdev docs/EVENTS.md slice 6d): each coming date of a class or an
|
|
91
|
+
// event as a schema.org Event, once the site has an address (as all its structured data, Base.astro).
|
|
92
|
+
// Read at build, and brought up to date as the page loads. A preview (no site address) has none.
|
|
93
|
+
const siteUrl = (import.meta.env.FERST_SITE_URL as string | undefined) || '';
|
|
94
|
+
let ldContext: EventsContext | null = null;
|
|
95
|
+
if (siteUrl) {
|
|
96
|
+
const settings = await loadSiteSettings().catch(() => null);
|
|
97
|
+
ldContext = {
|
|
98
|
+
url: `${canonicalUrl(siteUrl, Astro.url?.pathname ?? '/')}#${anchor}`,
|
|
99
|
+
organisation: { name: settings?.identity?.siteTitle, url: `${siteUrl.replace(/\/+$/, '')}/` },
|
|
100
|
+
places: (settings?.places ?? []) as EventsContext['places'],
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const ldOf = async (def: BookService): Promise<string | null> => {
|
|
104
|
+
if (!ldContext || def.mode !== 'places') return null;
|
|
105
|
+
const dates = list
|
|
106
|
+
? (await Promise.all(offers.map((o) => loadEventDates(o.definition, { event: o.event })))).flat().sort((a, b) => a.start.localeCompare(b.start))
|
|
107
|
+
: await loadEventDates(def.definition, { event: def.event, resource: def.resource });
|
|
108
|
+
const data = eventsData(dates, ldContext);
|
|
109
|
+
return data ? ldJson(data) : null;
|
|
110
|
+
};
|
|
111
|
+
const lds = await Promise.all(panels.map(ldOf));
|
|
39
112
|
---
|
|
40
113
|
|
|
41
|
-
<section class="b-book" id={anchor} data-book
|
|
42
|
-
|
|
114
|
+
<section class="b-book" id={anchor} data-book-block data-demo={demo ? '1' : undefined} data-find={`${base}/sites/${encodeURIComponent(siteId ?? '')}/book/find`}>
|
|
115
|
+
{demo && <p class="b-book__notice" data-book-demo>A preview: try it as a visitor would. The free times are real; nothing is held or booked.</p>}
|
|
43
116
|
<div class="b-book__head" data-recipe-head data-content-align={headAlign}>
|
|
44
|
-
<Heading text={
|
|
45
|
-
<p class="b-book__meta" data-book-meta>{
|
|
46
|
-
{
|
|
117
|
+
<Heading text={heading} level={2} />
|
|
118
|
+
{meta && <p class="b-book__meta" data-book-meta>{meta}</p>}
|
|
119
|
+
{lines && <p class="b-book__intro">{lines}</p>}
|
|
47
120
|
</div>
|
|
48
121
|
|
|
49
122
|
<noscript><p class="b-book__notice">Booking needs JavaScript switched on in your browser.</p></noscript>
|
|
123
|
+
|
|
124
|
+
{many && (
|
|
125
|
+
<div class="b-book__offers" role="group" aria-label="What would you like to book?" data-book-offers>
|
|
126
|
+
{offers.map((d, i) => (
|
|
127
|
+
<button type="button" class="b-book__offer" data-book-offer={d.id} aria-pressed={String(i === 0)}>
|
|
128
|
+
<strong>{d.name}</strong>
|
|
129
|
+
<span>{metaLine({ ...d, resources: [] })}</span>
|
|
130
|
+
</button>
|
|
131
|
+
))}
|
|
132
|
+
</div>
|
|
133
|
+
)}
|
|
134
|
+
|
|
135
|
+
{panels.map((def, i) => {
|
|
136
|
+
const where = placeOf(def);
|
|
137
|
+
const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
138
|
+
return (
|
|
139
|
+
<div class="b-book__panel" data-book={def.definition} data-holds={def.holds} data-name={def.name} data-label={buttonLabel} data-offer={def.id}
|
|
140
|
+
data-mode={def.mode} data-event={list ? '' : def.event ?? ''} data-resource={def.resource ?? ''} data-minutes={def.minutes} data-step={def.step} data-where={list ? '' : where}
|
|
141
|
+
data-list={sources}
|
|
142
|
+
data-look={one ? 'button' : undefined} data-date={one ? date ?? '' : undefined}
|
|
143
|
+
data-with={whereText(def.resources)} hidden={i > 0}>
|
|
144
|
+
{lds[i] && <script type="application/ld+json" is:inline data-book-ld data-ctx={JSON.stringify(ldContext)} set:html={lds[i]} />}
|
|
145
|
+
{many && def.description && <p class="b-book__intro">{def.description}</p>}
|
|
50
146
|
<div class="b-book__notice" role="alert" data-book-error hidden></div>
|
|
51
147
|
|
|
52
|
-
|
|
148
|
+
{/* Step 1: what to book */}
|
|
53
149
|
<div class="b-book__pick" data-book-pick hidden>
|
|
54
150
|
{picksWho && (
|
|
55
151
|
<label class="b-book__who">
|
|
@@ -60,7 +156,7 @@ const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
|
60
156
|
</select>
|
|
61
157
|
</label>
|
|
62
158
|
)}
|
|
63
|
-
<div class="b-book__days-bar">
|
|
159
|
+
<div class="b-book__days-bar" hidden={one}>
|
|
64
160
|
<button type="button" class="b-book__nav" data-book-earlier aria-label="Earlier dates" disabled>‹</button>
|
|
65
161
|
<div class="b-book__days" role="group" aria-label="Choose a day" data-book-days></div>
|
|
66
162
|
<button type="button" class="b-book__nav" data-book-later aria-label="Later dates">›</button>
|
|
@@ -80,12 +176,12 @@ const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
|
80
176
|
<p class="b-book__chosen" data-book-chosen></p>
|
|
81
177
|
<label class="b-book__who"><span>How many places</span><input type="number" min="1" value="1" inputmode="numeric" data-book-count /></label>
|
|
82
178
|
<p class="b-book__quote" data-book-quote aria-live="polite"></p>
|
|
83
|
-
<button type="button" class="b-book__go" data-book-hold>Hold these places</button>
|
|
179
|
+
<button type="button" class="b-book__go" data-book-hold>{one ? 'Book now' : 'Hold these places'}</button>
|
|
84
180
|
</div>
|
|
85
181
|
)}
|
|
86
182
|
</div>
|
|
87
183
|
|
|
88
|
-
|
|
184
|
+
{/* Step 2: who they are */}
|
|
89
185
|
<div class="b-book__details" data-book-details hidden>
|
|
90
186
|
<p class="b-book__held" data-book-held></p>
|
|
91
187
|
<ContactForm
|
|
@@ -100,32 +196,53 @@ const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
|
100
196
|
{ name: 'notes', label: 'Anything we should know? (optional)', type: 'textarea' },
|
|
101
197
|
]}
|
|
102
198
|
honeypot
|
|
103
|
-
idPrefix={anchor}
|
|
199
|
+
idPrefix={many ? `${anchor}-${def.id}` : anchor}
|
|
104
200
|
/>
|
|
105
201
|
<button type="button" class="b-book__link" data-book-again>{def.mode === 'places' ? 'Choose another date' : 'Choose another time'}</button>
|
|
106
202
|
</div>
|
|
107
203
|
|
|
108
|
-
|
|
204
|
+
{/* Step 3: booked, and "Add to calendar" */}
|
|
109
205
|
<div class="b-book__notice b-book__notice--done" role="status" data-book-done hidden></div>
|
|
206
|
+
</div>
|
|
207
|
+
);
|
|
208
|
+
})}
|
|
209
|
+
|
|
210
|
+
{/* Their own bookings: the links, emailed to the address they booked with (2026-11-24). */}
|
|
211
|
+
<details class="b-book__find" data-book-find>
|
|
212
|
+
<summary>Change or cancel a booking</summary>
|
|
213
|
+
<form class="b-book__find-form" data-book-find-form>
|
|
214
|
+
<label class="b-book__who"><span>The email you booked with</span><input type="email" name="email" required autocomplete="email" id={`${anchor}-find-email`} /></label>
|
|
215
|
+
<button type="submit" class="b-book__go">Email me my bookings</button>
|
|
216
|
+
</form>
|
|
217
|
+
<p class="b-book__find-said" role="status" data-book-find-said></p>
|
|
218
|
+
</details>
|
|
110
219
|
</section>
|
|
111
220
|
|
|
112
221
|
<script>
|
|
113
|
-
import { addDays, bookingMessage, calendarFile, dayChip, endChoices, googleCalendarLink, londonDay, whenText, type EventDate, type TimesAnswer } from '../../lib/book';
|
|
222
|
+
import { addDays, bookingMessage, calendarFile, dayChip, endChoices, eventsData, googleCalendarLink, londonDay, whenText, whichFree, type EventDate, type EventsContext, type TimesAnswer } from '../../lib/book';
|
|
114
223
|
|
|
115
224
|
const SPAN = 14; // days shown at once (dates for a class: four weeks)
|
|
116
225
|
const AHEAD = 60; // how far ahead the platform takes bookings
|
|
117
226
|
|
|
118
227
|
type Hold = { id: string; start: string; end: string; places?: number; title?: string; resource: { name: string }; price: { label: string; note?: string | null } | null };
|
|
119
228
|
|
|
229
|
+
/** One offer's way to book, in its panel. Returns how to start it (once shown) and to leave it. */
|
|
120
230
|
function setUp(block: HTMLElement) {
|
|
121
231
|
const src = block.dataset.book!;
|
|
122
232
|
const holds = block.dataset.holds!;
|
|
123
233
|
const mode = (block.dataset.mode ?? 'times') as 'times' | 'chosen' | 'places';
|
|
124
234
|
const onlyEvent = block.dataset.event || '';
|
|
235
|
+
const onlyWith = block.dataset.resource || ''; // the person or place the block names: booked with them only
|
|
236
|
+
const demo = !!block.closest('[data-book-block]')?.hasAttribute('data-demo'); // a preview: nothing is held or booked
|
|
125
237
|
const minutes = Number(block.dataset.minutes ?? '30');
|
|
126
238
|
const step = Number(block.dataset.step ?? '30');
|
|
127
239
|
const where = block.dataset.where ?? '';
|
|
128
|
-
const
|
|
240
|
+
const one = block.dataset.look === 'button'; // "Book now": one date, only how many places left to choose
|
|
241
|
+
// A custom list: each source's dates, as one list; each date booked through its own offer.
|
|
242
|
+
const sources = block.dataset.list ? (JSON.parse(block.dataset.list) as Array<{ book: string; event: string }>) : null;
|
|
243
|
+
const srcOf = (d: EventDate | null) => (d as (EventDate & { src?: string }) | null)?.src ?? src;
|
|
244
|
+
const pinned = block.dataset.date || '';
|
|
245
|
+
const span = one ? 31 : mode === 'places' ? SPAN * 2 : SPAN;
|
|
129
246
|
const $ = <T extends HTMLElement>(sel: string) => block.querySelector<T>(sel)!;
|
|
130
247
|
const pick = $('[data-book-pick]');
|
|
131
248
|
const details = $('[data-book-details]');
|
|
@@ -190,8 +307,19 @@ const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
|
190
307
|
const times = day ? free(day) : [];
|
|
191
308
|
if (!day) { timesEl.innerHTML = `<p class="b-book__empty">${answer?.days.some((d) => free(d.day).length) ? 'Choose a day.' : 'Nothing free in these dates. Try later dates.'}</p>`; return; }
|
|
192
309
|
if (!times.length) { timesEl.innerHTML = '<p class="b-book__empty">Nothing free that day.</p>'; return; }
|
|
310
|
+
// Several places, "Anywhere free" (the maintainer's second way, 2026-11-24: "hall hire showing any
|
|
311
|
+
// hall available"): each time says which are free then.
|
|
312
|
+
const places = answer?.resources ?? [];
|
|
193
313
|
timesEl.replaceChildren(...times.map((t) => {
|
|
194
314
|
const b = button('b-book__time', t.time, () => (mode === 'chosen' ? chooseStart(t.time) : void take({ start: t.start })));
|
|
315
|
+
const which = who?.value ? null : whichFree(t.resources, places);
|
|
316
|
+
if (which) {
|
|
317
|
+
b.innerHTML = '<strong></strong><span></span>';
|
|
318
|
+
b.querySelector('strong')!.textContent = t.time;
|
|
319
|
+
b.querySelector('span')!.textContent = which;
|
|
320
|
+
b.setAttribute('aria-label', `${t.time}, free: ${which}`);
|
|
321
|
+
b.classList.add('b-book__time--which');
|
|
322
|
+
}
|
|
195
323
|
if (mode === 'chosen') b.setAttribute('aria-pressed', String(t.time === start));
|
|
196
324
|
return b;
|
|
197
325
|
}));
|
|
@@ -210,6 +338,23 @@ const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
|
210
338
|
// ── A class or a concert: its coming dates, then how many places ──────────
|
|
211
339
|
function drawDates() {
|
|
212
340
|
const dates = answer?.events ?? [];
|
|
341
|
+
if (one) {
|
|
342
|
+
const d = dates.find((x) => (pinned ? x.start === pinned : x.left > 0));
|
|
343
|
+
if (!d || d.left === 0) { timesEl.innerHTML = `<p class="b-book__empty">${d ? 'It’s full.' : 'No dates with places just now.'}</p>`; return; }
|
|
344
|
+
const card = document.createElement('div');
|
|
345
|
+
card.className = 'b-book__one';
|
|
346
|
+
card.innerHTML = '<strong></strong><span></span>';
|
|
347
|
+
card.querySelector('strong')!.textContent = d.title;
|
|
348
|
+
card.querySelector('span')!.textContent = `${whenText(d.start)}${where ? `, at ${where}` : ''} · ${d.left} ${d.left === 1 ? 'place' : 'places'} left · ${d.price}`;
|
|
349
|
+
timesEl.replaceChildren(card);
|
|
350
|
+
date = d;
|
|
351
|
+
chosenEl!.textContent = '';
|
|
352
|
+
count!.max = String(d.left);
|
|
353
|
+
if (Number(count!.value) > d.left) count!.value = String(d.left);
|
|
354
|
+
more!.hidden = false;
|
|
355
|
+
void quote();
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
213
358
|
if (!dates.length) { timesEl.innerHTML = `<p class="b-book__empty">${onlyEvent ? 'No dates to book in these weeks.' : 'Nothing to book in these weeks. Try later dates.'}</p>`; return; }
|
|
214
359
|
timesEl.replaceChildren(...dates.map((d) => {
|
|
215
360
|
const chip = dayChip(d.start.slice(0, 10));
|
|
@@ -242,35 +387,67 @@ const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
|
242
387
|
q = `start=${date.start}&end=${date.end}&places=${Math.max(1, Number(count!.value) || 1)}&event=${encodeURIComponent(date.event_id)}`;
|
|
243
388
|
}
|
|
244
389
|
if (!q) return;
|
|
245
|
-
const res = await fetch(`${
|
|
390
|
+
const res = await fetch(`${srcOf(date)}/quote?${q}`, { headers: { Accept: 'application/json' } }).catch(() => null);
|
|
246
391
|
const body = res?.ok ? await res.json().catch(() => null) : null;
|
|
247
392
|
if (!body) return;
|
|
248
393
|
quoteEl.textContent = body.price ? `${body.price.label} · ${body.price.lines.join('; ')} · paid on the day` : 'Free';
|
|
249
|
-
if (holdBtn && mode === 'places') { const n = Math.max(1, Number(count!.value) || 1); holdBtn.textContent = `Hold ${n === 1 ? 'this place' : `${n} places`}`; }
|
|
394
|
+
if (holdBtn && mode === 'places' && !one) { const n = Math.max(1, Number(count!.value) || 1); holdBtn.textContent = `Hold ${n === 1 ? 'this place' : `${n} places`}`; }
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** The page's dates for search engines, brought up to date (the places left) from what's just been read. */
|
|
398
|
+
function freshForSearch() {
|
|
399
|
+
const ld = block.querySelector<HTMLScriptElement>('script[data-book-ld]');
|
|
400
|
+
if (!ld || !answer || from !== today) return;
|
|
401
|
+
const data = eventsData(answer.events ?? [], JSON.parse(ld.dataset.ctx ?? '{}') as EventsContext);
|
|
402
|
+
if (data) ld.textContent = JSON.stringify(data);
|
|
403
|
+
else ld.remove();
|
|
250
404
|
}
|
|
251
405
|
|
|
252
406
|
async function load() {
|
|
253
407
|
say(null);
|
|
254
408
|
timesEl.innerHTML = `<p class="b-book__empty">${mode === 'places' ? 'Loading dates…' : 'Loading free times…'}</p>`;
|
|
255
409
|
try {
|
|
256
|
-
|
|
410
|
+
if (sources) {
|
|
411
|
+
const all = await Promise.all(sources.map(async (s) => {
|
|
412
|
+
const r = await fetch(`${s.book}?from=${from}&to=${addDays(from, span)}${s.event ? `&event=${encodeURIComponent(s.event)}` : ''}`, { headers: { Accept: 'application/json' } });
|
|
413
|
+
if (!r.ok) { const b = await r.json().catch(() => ({})); throw new Error(b.error ?? String(r.status)); }
|
|
414
|
+
return ((await r.json()) as TimesAnswer).events?.map((e) => ({ ...e, src: s.book })) ?? [];
|
|
415
|
+
}));
|
|
416
|
+
answer = { service: { id: '', name: block.dataset.name ?? '', minutes }, resources: [], days: [], events: all.flat().sort((a, b) => a.start.localeCompare(b.start)) };
|
|
417
|
+
start = null; date = null;
|
|
418
|
+
drawDays(); drawTimes(); freshForSearch();
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const res = await fetch(`${src}?from=${from}&to=${addDays(from, span)}${onlyEvent ? `&event=${encodeURIComponent(onlyEvent)}` : ''}${onlyWith ? `&resource=${encodeURIComponent(onlyWith)}` : ''}`, { headers: { Accept: 'application/json' } });
|
|
257
422
|
if (!res.ok) { const b = await res.json().catch(() => ({})); throw new Error(b.error ?? String(res.status)); }
|
|
258
423
|
answer = (await res.json()) as TimesAnswer;
|
|
259
424
|
day = answer.days.find((d) => free(d.day).length)?.day ?? null;
|
|
260
425
|
start = null; date = null;
|
|
261
|
-
drawDays(); drawTimes();
|
|
426
|
+
drawDays(); drawTimes(); freshForSearch();
|
|
262
427
|
} catch (e) {
|
|
263
428
|
timesEl.innerHTML = '';
|
|
264
429
|
say(bookingMessage(e instanceof Error ? e.message : ''));
|
|
265
430
|
}
|
|
266
431
|
}
|
|
267
432
|
|
|
433
|
+
/** A preview's hold: what a visitor would see held, with nothing sent. */
|
|
434
|
+
function pretend(body: Record<string, unknown>) {
|
|
435
|
+
const start = String(body.start);
|
|
436
|
+
const plus = (local: string, m: number) => { const t = Number(local.slice(11, 13)) * 60 + Number(local.slice(14, 16)) + m; return `${local.slice(0, 11)}${String(Math.floor(t / 60) % 24).padStart(2, '0')}:${String(t % 60).padStart(2, '0')}`; };
|
|
437
|
+
hold = { id: '', start, end: typeof body.end === 'string' ? body.end : date ? date.end : plus(start, minutes), places: typeof body.places === 'number' ? body.places : undefined, title: date?.title, resource: { name: '' }, price: null };
|
|
438
|
+
const h = hold;
|
|
439
|
+
const what = h.places ? `${h.title ?? block.dataset.name}, ${whenText(h.start)}, ${h.places} ${h.places === 1 ? 'place' : 'places'}` : `${whenText(h.start)} to ${h.end.slice(11)}`;
|
|
440
|
+
$('[data-book-held]').textContent = `${what}. On your website, this is kept for the visitor for 10 minutes while they give their details. In this preview, nothing is kept.`;
|
|
441
|
+
show('details');
|
|
442
|
+
}
|
|
443
|
+
|
|
268
444
|
/** Hold what was picked: a time (and an end), or places at a date. */
|
|
269
445
|
async function take(body: Record<string, unknown>) {
|
|
270
446
|
say(null);
|
|
271
|
-
|
|
447
|
+
if (demo) { pretend(body); return; }
|
|
448
|
+
const res = await fetch(`${mode === 'places' ? srcOf(date) : src}/hold`, {
|
|
272
449
|
method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
|
273
|
-
body: JSON.stringify({ ...body, ...(who?.value ? { resource_id: who
|
|
450
|
+
body: JSON.stringify({ ...body, ...(who?.value || onlyWith ? { resource_id: who?.value || onlyWith } : {}) }),
|
|
274
451
|
}).catch(() => null);
|
|
275
452
|
const answerBody = res ? await res.json().catch(() => ({})) : {};
|
|
276
453
|
if (!res || res.status !== 201) { say(bookingMessage(answerBody.error ?? '')); await load(); return; }
|
|
@@ -297,6 +474,16 @@ const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
|
297
474
|
ev.preventDefault();
|
|
298
475
|
if (!hold) return;
|
|
299
476
|
say(null);
|
|
477
|
+
if (demo) {
|
|
478
|
+
done.innerHTML = '';
|
|
479
|
+
const strong = document.createElement('strong');
|
|
480
|
+
strong.textContent = 'This is a preview: nothing was booked.';
|
|
481
|
+
done.append(strong, ' On your website, the visitor would now be booked, emailed the booking with an invitation for their calendar, and offered “Add to calendar”.');
|
|
482
|
+
hold = null;
|
|
483
|
+
show('done');
|
|
484
|
+
done.focus();
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
300
487
|
const data = Object.fromEntries(new FormData(form).entries());
|
|
301
488
|
if (submit) submit.disabled = true;
|
|
302
489
|
const res = await fetch(`${holds}/${hold.id}`, {
|
|
@@ -309,10 +496,19 @@ const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
|
309
496
|
if (body.error === 'hold_expired' || body.error === 'already_booked') { hold = null; show('pick'); await load(); }
|
|
310
497
|
return;
|
|
311
498
|
}
|
|
312
|
-
const b = body.booking as { id: string; start: string; end: string; starts_at?: string; ends_at?: string; service: string; resource: string; places?: number };
|
|
499
|
+
const b = body.booking as { id: string; status?: string; start: string; end: string; starts_at?: string; ends_at?: string; service: string; resource: string; places?: number };
|
|
313
500
|
done.innerHTML = '';
|
|
314
501
|
const strong = document.createElement('strong');
|
|
315
502
|
const withWhom = b.resource ? `, ${where ? 'at' : 'with'} ${b.resource}` : '';
|
|
503
|
+
// On request (platform 2026-11-24): asked for, its time kept, until the organisation confirms it.
|
|
504
|
+
if (b.status === 'requested') {
|
|
505
|
+
strong.textContent = `Requested: ${b.service}, ${whenText(b.start)}${b.places ? `, ${b.places} ${b.places === 1 ? 'place' : 'places'}` : ''}${withWhom}.`;
|
|
506
|
+
done.append(strong, ' The time is kept for you while they answer. We’ve emailed you, and you’ll get the booking, with an invitation for your calendar, once it’s confirmed.');
|
|
507
|
+
hold = null;
|
|
508
|
+
show('done');
|
|
509
|
+
done.focus();
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
316
512
|
strong.textContent = `Booked: ${b.service}, ${whenText(b.start)}${b.places ? `, ${b.places} ${b.places === 1 ? 'place' : 'places'}` : ''}${withWhom}.`;
|
|
317
513
|
done.append(strong, ' We’ve emailed you the booking, with an invitation for your calendar.');
|
|
318
514
|
if (b.starts_at && b.ends_at) {
|
|
@@ -332,7 +528,7 @@ const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
|
332
528
|
});
|
|
333
529
|
|
|
334
530
|
$('[data-book-again]').addEventListener('click', async () => {
|
|
335
|
-
if (hold) await fetch(`${holds}/${hold.id}`, { method: 'DELETE' }).catch(() => null);
|
|
531
|
+
if (hold?.id) await fetch(`${holds}/${hold.id}`, { method: 'DELETE' }).catch(() => null);
|
|
336
532
|
hold = null;
|
|
337
533
|
show('pick');
|
|
338
534
|
await load();
|
|
@@ -342,11 +538,49 @@ const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
|
342
538
|
who?.addEventListener('change', () => { day = answer?.days.find((d) => free(d.day).length)?.day ?? null; start = null; drawDays(); drawTimes(); });
|
|
343
539
|
done.tabIndex = -1;
|
|
344
540
|
|
|
345
|
-
|
|
346
|
-
|
|
541
|
+
let started = false;
|
|
542
|
+
return {
|
|
543
|
+
/** Shown: load its times the first time. */
|
|
544
|
+
start() { if (!started) { started = true; show('pick'); void load(); } },
|
|
545
|
+
/** Another offer chosen: let go of anything held here. */
|
|
546
|
+
async leave() {
|
|
547
|
+
if (hold?.id) await fetch(`${holds}/${hold.id}`, { method: 'DELETE' }).catch(() => null);
|
|
548
|
+
hold = null;
|
|
549
|
+
if (started) { show('pick'); say(null); }
|
|
550
|
+
},
|
|
551
|
+
};
|
|
347
552
|
}
|
|
348
553
|
|
|
349
|
-
|
|
554
|
+
// A block for a person or a place shows each offer they provide as a choice, and the chosen one's panel.
|
|
555
|
+
document.querySelectorAll<HTMLElement>('[data-book-block]').forEach((section) => {
|
|
556
|
+
const panels = [...section.querySelectorAll<HTMLElement>('[data-book]')].map((el) => ({ el, run: setUp(el) }));
|
|
557
|
+
const choices = [...section.querySelectorAll<HTMLButtonElement>('[data-book-offer]')];
|
|
558
|
+
const choose = (offer: string) => {
|
|
559
|
+
for (const p of panels) {
|
|
560
|
+
const on = p.el.dataset.offer === offer;
|
|
561
|
+
if (!on && !p.el.hidden) void p.run.leave();
|
|
562
|
+
p.el.hidden = !on;
|
|
563
|
+
if (on) p.run.start();
|
|
564
|
+
}
|
|
565
|
+
for (const c of choices) c.setAttribute('aria-pressed', String(c.dataset.bookOffer === offer));
|
|
566
|
+
};
|
|
567
|
+
for (const c of choices) c.addEventListener('click', () => choose(c.dataset.bookOffer!));
|
|
568
|
+
if (panels[0]) choose(panels[0].el.dataset.offer!);
|
|
569
|
+
|
|
570
|
+
// "Change or cancel a booking": the same answer whatever the address, from the platform.
|
|
571
|
+
const findForm = section.querySelector<HTMLFormElement>('[data-book-find-form]');
|
|
572
|
+
const said = section.querySelector<HTMLElement>('[data-book-find-said]');
|
|
573
|
+
findForm?.addEventListener('submit', async (ev) => {
|
|
574
|
+
ev.preventDefault();
|
|
575
|
+
if (!said) return;
|
|
576
|
+
const email = String(new FormData(findForm).get('email') ?? '').trim();
|
|
577
|
+
if (section.hasAttribute('data-demo')) { said.textContent = `In this preview, nothing is sent. On your website, ${email || 'they'} would be emailed a link to each of their bookings.`; return; }
|
|
578
|
+
said.textContent = 'One moment…';
|
|
579
|
+
const res = await fetch(section.dataset.find!, { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, body: JSON.stringify({ email }) }).catch(() => null);
|
|
580
|
+
const body = res ? await res.json().catch(() => ({})) : {};
|
|
581
|
+
said.textContent = res?.ok ? (body.message ?? 'If you have bookings with us under that address, we’ve emailed you a link to each.') : bookingMessage(body.error ?? '');
|
|
582
|
+
});
|
|
583
|
+
});
|
|
350
584
|
</script>
|
|
351
585
|
|
|
352
586
|
<style>
|
|
@@ -584,6 +818,103 @@ const picksWho = def.mode !== 'places' && def.resources.length > 1;
|
|
|
584
818
|
.b-book__add {
|
|
585
819
|
margin: 0.6rem 0 0;
|
|
586
820
|
}
|
|
821
|
+
.b-book__find summary {
|
|
822
|
+
cursor: pointer;
|
|
823
|
+
color: var(--fg);
|
|
824
|
+
text-decoration: underline;
|
|
825
|
+
}
|
|
826
|
+
.b-book__find-form {
|
|
827
|
+
display: flex;
|
|
828
|
+
flex-direction: column;
|
|
829
|
+
gap: 0.75rem;
|
|
830
|
+
margin-top: 0.75rem;
|
|
831
|
+
}
|
|
832
|
+
.b-book__find-form input {
|
|
833
|
+
font: inherit;
|
|
834
|
+
color: var(--fg);
|
|
835
|
+
background: var(--field-bg, var(--bg-surface));
|
|
836
|
+
border: var(--field-border, 1px solid var(--border));
|
|
837
|
+
border-radius: var(--field-radius, var(--radius-sm));
|
|
838
|
+
padding: 0.6rem 0.75rem;
|
|
839
|
+
max-width: 20rem;
|
|
840
|
+
}
|
|
841
|
+
.b-book__find-said {
|
|
842
|
+
margin: 0.5rem 0 0;
|
|
843
|
+
color: var(--muted);
|
|
844
|
+
}
|
|
845
|
+
.b-book__time--which {
|
|
846
|
+
display: flex;
|
|
847
|
+
flex-direction: column;
|
|
848
|
+
align-items: center;
|
|
849
|
+
gap: 0.1rem;
|
|
850
|
+
}
|
|
851
|
+
.b-book__time--which span {
|
|
852
|
+
font-size: 0.75rem;
|
|
853
|
+
font-weight: 400;
|
|
854
|
+
color: var(--muted);
|
|
855
|
+
}
|
|
856
|
+
.b-book__times:has(.b-book__time--which) {
|
|
857
|
+
grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
|
|
858
|
+
}
|
|
859
|
+
.b-book__days-bar[hidden] {
|
|
860
|
+
display: none;
|
|
861
|
+
}
|
|
862
|
+
.b-book__one {
|
|
863
|
+
display: flex;
|
|
864
|
+
flex-direction: column;
|
|
865
|
+
gap: 0.15rem;
|
|
866
|
+
padding: 0.85rem 1rem;
|
|
867
|
+
border: 1px solid var(--border);
|
|
868
|
+
border-radius: var(--radius-sm);
|
|
869
|
+
background: var(--bg-surface);
|
|
870
|
+
grid-column: 1 / -1;
|
|
871
|
+
}
|
|
872
|
+
.b-book__one span {
|
|
873
|
+
color: var(--muted);
|
|
874
|
+
}
|
|
875
|
+
.b-book__panel {
|
|
876
|
+
display: flex;
|
|
877
|
+
flex-direction: column;
|
|
878
|
+
gap: 1rem;
|
|
879
|
+
}
|
|
880
|
+
.b-book__panel[hidden] {
|
|
881
|
+
display: none;
|
|
882
|
+
}
|
|
883
|
+
.b-book__offers {
|
|
884
|
+
display: grid;
|
|
885
|
+
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
|
|
886
|
+
gap: 0.5rem;
|
|
887
|
+
}
|
|
888
|
+
.b-book__offer {
|
|
889
|
+
display: flex;
|
|
890
|
+
flex-direction: column;
|
|
891
|
+
align-items: flex-start;
|
|
892
|
+
gap: 0.15rem;
|
|
893
|
+
padding: 0.75rem 0.9rem;
|
|
894
|
+
font: inherit;
|
|
895
|
+
text-align: start;
|
|
896
|
+
color: var(--fg);
|
|
897
|
+
background: var(--bg-surface);
|
|
898
|
+
border: 1px solid var(--border);
|
|
899
|
+
border-radius: var(--radius-sm);
|
|
900
|
+
cursor: pointer;
|
|
901
|
+
transition: border-color 0.15s ease, background 0.15s ease;
|
|
902
|
+
}
|
|
903
|
+
.b-book__offer span {
|
|
904
|
+
color: var(--muted);
|
|
905
|
+
font-size: 0.92rem;
|
|
906
|
+
}
|
|
907
|
+
.b-book__offer:hover {
|
|
908
|
+
border-color: var(--gold);
|
|
909
|
+
}
|
|
910
|
+
.b-book__offer[aria-pressed='true'] {
|
|
911
|
+
border-color: var(--gold);
|
|
912
|
+
background: var(--gold-light);
|
|
913
|
+
}
|
|
914
|
+
.b-book__offer:focus-visible {
|
|
915
|
+
outline: 2px solid var(--gold);
|
|
916
|
+
outline-offset: 2px;
|
|
917
|
+
}
|
|
587
918
|
.b-book__add a {
|
|
588
919
|
color: var(--fg);
|
|
589
920
|
font-weight: 600;
|