@profitstudio/booking-engine 0.0.0-stage → 2.0.0-beta.1
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/CHANGELOG.md +562 -0
- package/LICENSE +21 -0
- package/README.md +18 -2
- package/THIRD_PARTY_NOTICES.md +41 -0
- package/bin/booking-engine.mjs +575 -0
- package/bin/connect.mjs +408 -0
- package/bin/photos.mjs +82 -0
- package/dist/admin/BookingAdmin.d.ts +22 -0
- package/dist/admin/BookingAdmin.js +337 -0
- package/dist/admin/admin.css +1423 -0
- package/dist/admin/alerts.d.ts +10 -0
- package/dist/admin/alerts.js +11 -0
- package/dist/admin/booking-detail.d.ts +37 -0
- package/dist/admin/booking-detail.js +149 -0
- package/dist/admin/content-inputs.d.ts +15 -0
- package/dist/admin/content-inputs.js +105 -0
- package/dist/admin/context.d.ts +37 -0
- package/dist/admin/context.js +8 -0
- package/dist/admin/drawer.d.ts +14 -0
- package/dist/admin/drawer.js +56 -0
- package/dist/admin/feedback.d.ts +32 -0
- package/dist/admin/feedback.js +70 -0
- package/dist/admin/fields.d.ts +27 -0
- package/dist/admin/fields.js +146 -0
- package/dist/admin/fonts/OFL-Geist-Mono.txt +93 -0
- package/dist/admin/fonts/OFL-Geist.txt +93 -0
- package/dist/admin/fonts/geist-latin-wght-normal.woff2 +0 -0
- package/dist/admin/fonts/geist-mono-latin-wght-normal.woff2 +0 -0
- package/dist/admin/icons.d.ts +66 -0
- package/dist/admin/icons.js +72 -0
- package/dist/admin/image-upload.d.ts +58 -0
- package/dist/admin/image-upload.js +111 -0
- package/dist/admin/index.d.ts +6 -0
- package/dist/admin/index.js +6 -0
- package/dist/admin/lucide.generated.d.ts +521 -0
- package/dist/admin/lucide.generated.js +76 -0
- package/dist/admin/sections/BookingsSection.d.ts +1 -0
- package/dist/admin/sections/BookingsSection.js +137 -0
- package/dist/admin/sections/EmailSection.d.ts +8 -0
- package/dist/admin/sections/EmailSection.js +182 -0
- package/dist/admin/sections/HoursSection.d.ts +1 -0
- package/dist/admin/sections/HoursSection.js +110 -0
- package/dist/admin/sections/OverviewSection.d.ts +1 -0
- package/dist/admin/sections/OverviewSection.js +427 -0
- package/dist/admin/sections/ServicesSection.d.ts +1 -0
- package/dist/admin/sections/ServicesSection.js +236 -0
- package/dist/admin/sections/SettingsSection.d.ts +10 -0
- package/dist/admin/sections/SettingsSection.js +96 -0
- package/dist/admin/sections/WebsiteSection.d.ts +1 -0
- package/dist/admin/sections/WebsiteSection.js +228 -0
- package/dist/admin/sections.d.ts +22 -0
- package/dist/admin/sections.js +29 -0
- package/dist/admin/setup.d.ts +1 -0
- package/dist/admin/setup.js +45 -0
- package/dist/admin/today.d.ts +43 -0
- package/dist/admin/today.js +115 -0
- package/dist/admin/ui.d.ts +134 -0
- package/dist/admin/ui.js +220 -0
- package/dist/calendar.d.ts +64 -0
- package/dist/calendar.js +139 -0
- package/dist/client.d.ts +147 -0
- package/dist/client.js +507 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.js +120 -0
- package/dist/content/image.d.ts +20 -0
- package/dist/content/image.js +90 -0
- package/dist/content/resolve.d.ts +12 -0
- package/dist/content/resolve.js +73 -0
- package/dist/content/schema.d.ts +119 -0
- package/dist/content/schema.js +53 -0
- package/dist/content/store.d.ts +48 -0
- package/dist/content/store.js +123 -0
- package/dist/content/validate.d.ts +33 -0
- package/dist/content/validate.js +177 -0
- package/dist/errors.d.ts +19 -0
- package/dist/errors.js +94 -0
- package/dist/generated/manifest.d.ts +6 -0
- package/dist/generated/manifest.js +7 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +20 -0
- package/dist/presets/hair-salon.d.ts +8 -0
- package/dist/presets/hair-salon.js +94 -0
- package/dist/presets/index.d.ts +5 -0
- package/dist/presets/index.js +6 -0
- package/dist/preview.d.ts +18 -0
- package/dist/preview.js +134 -0
- package/dist/semver.d.ts +16 -0
- package/dist/semver.js +55 -0
- package/dist/time.d.ts +31 -0
- package/dist/time.js +138 -0
- package/dist/types.d.ts +477 -0
- package/dist/types.js +3 -0
- package/dist/vite.d.ts +52 -0
- package/dist/vite.js +150 -0
- package/docs/BUILD-A-SITE.md +203 -0
- package/docs/EMAIL-SETUP.md +224 -0
- package/docs/INTEGRATION.md +1109 -0
- package/engine.manifest.json +90 -0
- package/functions/booking-mailer/deno.json +6 -0
- package/functions/booking-mailer/index.ts +44 -0
- package/functions/booking-mailer/mailer.js +156 -0
- package/functions/booking-mailer/providers/index.js +42 -0
- package/functions/booking-mailer/providers/resend.js +75 -0
- package/functions/booking-mailer/templates.js +345 -0
- package/migrations/0001_core.sql +1673 -0
- package/migrations/0002_security_hardening.sql +711 -0
- package/migrations/0003_private_definers_and_holds.sql +808 -0
- package/migrations/0004_verified_requests.sql +1702 -0
- package/migrations/0005_scoped_mailer_credential.sql +347 -0
- package/migrations/0006_trusted_client_ip.sql +56 -0
- package/migrations/0007_site_content.sql +380 -0
- package/migrations/0008_service_images_opening_times.sql +328 -0
- package/package.json +85 -4
- package/templates/BOOKING_ENGINE.md +61 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// "Bookings": every appointment and request, by day. Views for what is coming,
|
|
3
|
+
// what needs an answer, what happened and what was cancelled; a search over
|
|
4
|
+
// past and upcoming bookings; and a detail panel with the answers that are
|
|
5
|
+
// valid for each booking.
|
|
6
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
7
|
+
import { addDays, formatClockTime, formatDate, formatDateTime, timeZoneLabel, todayInTimeZone } from '../../time.js';
|
|
8
|
+
import { BookingDrawer, priceText } from '../booking-detail.js';
|
|
9
|
+
import { useAdmin } from '../context.js';
|
|
10
|
+
import { Icon } from '../icons.js';
|
|
11
|
+
import { holdState } from '../today.js';
|
|
12
|
+
import { Button, EmptyState, ErrorNotice, IconButton, PageHeader, Segmented, SkeletonRows, StatusBadge, formatSpan, useMediaQuery, useNow, useResult } from '../ui.js';
|
|
13
|
+
const VIEWS = [
|
|
14
|
+
{ id: 'upcoming', label: 'Upcoming', sub: 'Appointments and requests for the next 90 days, by day', range: (t) => [t, addDays(t, 90)], statuses: ['pending', 'expired', 'confirmed'] },
|
|
15
|
+
{ id: 'pending', label: 'Needs your answer', sub: 'Requests waiting for you to confirm or decline', range: (t) => [t, addDays(t, 365)], statuses: ['pending', 'expired'] },
|
|
16
|
+
{ id: 'past', label: 'Past 30 days', sub: 'What happened over the last 30 days, newest first', range: (t) => [addDays(t, -30), t] },
|
|
17
|
+
{ id: 'cancelled', label: 'Cancelled', sub: 'Cancelled and declined bookings', range: (t) => [addDays(t, -30), addDays(t, 365)], statuses: ['cancelled'] },
|
|
18
|
+
];
|
|
19
|
+
const digits = (s) => s.replace(/\D/g, '');
|
|
20
|
+
/** Does a booking match what the owner typed? Names, email, phone, reference and service. */
|
|
21
|
+
function matches(b, query) {
|
|
22
|
+
const q = query.trim().toLowerCase();
|
|
23
|
+
if (!q)
|
|
24
|
+
return true;
|
|
25
|
+
const text = [b.customer.name, b.customer.email, b.reference, b.serviceName, b.notes ?? ''].join(' ').toLowerCase();
|
|
26
|
+
if (q.split(/\s+/).every((word) => text.includes(word)))
|
|
27
|
+
return true;
|
|
28
|
+
const qd = digits(q);
|
|
29
|
+
return qd.length >= 3 && Boolean(b.customer.phone && digits(b.customer.phone).includes(qd));
|
|
30
|
+
}
|
|
31
|
+
export function BookingsSection() {
|
|
32
|
+
const { engine, settings, view: askedView, pendingCount } = useAdmin();
|
|
33
|
+
const [viewId, setViewId] = useState(() => (VIEWS.some((v) => v.id === askedView) ? askedView : 'upcoming'));
|
|
34
|
+
const [query, setQuery] = useState('');
|
|
35
|
+
const [selected, setSelected] = useState(null);
|
|
36
|
+
const searchRef = useRef(null);
|
|
37
|
+
const docked = useMediaQuery('(min-width: 1200px)');
|
|
38
|
+
const now = useNow(30_000);
|
|
39
|
+
const view = VIEWS.find((v) => v.id === viewId) ?? VIEWS[0];
|
|
40
|
+
const tz = settings.timezone;
|
|
41
|
+
const locale = settings.locale;
|
|
42
|
+
const today = todayInTimeZone(tz);
|
|
43
|
+
const [from, to] = view.range(today);
|
|
44
|
+
const [load, reload] = useResult(() => engine.admin.listBookings({ from, to, statuses: view.statuses }), [viewId, from, to]);
|
|
45
|
+
const [services] = useResult(() => engine.admin.listServices(), []);
|
|
46
|
+
const searching = query.trim().length > 0;
|
|
47
|
+
const [everything, setEverything] = useState(null);
|
|
48
|
+
const [searchTick, setSearchTick] = useState(0);
|
|
49
|
+
// The search looks at a year back and a year ahead (two calls: one range is at most 400 days).
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (!searching || everything)
|
|
52
|
+
return;
|
|
53
|
+
let alive = true;
|
|
54
|
+
Promise.all([
|
|
55
|
+
engine.admin.listBookings({ from: addDays(today, -365), to: addDays(today, -1) }),
|
|
56
|
+
engine.admin.listBookings({ from: today, to: addDays(today, 365) }),
|
|
57
|
+
]).then(([past, ahead]) => {
|
|
58
|
+
if (alive && past.ok && ahead.ok)
|
|
59
|
+
setEverything([...ahead.data, ...[...past.data].reverse()]);
|
|
60
|
+
});
|
|
61
|
+
return () => {
|
|
62
|
+
alive = false;
|
|
63
|
+
};
|
|
64
|
+
}, [searching, everything, engine, today, searchTick]);
|
|
65
|
+
// ⌘K and the search buttons open this page with the field focused.
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (askedView === 'search')
|
|
68
|
+
searchRef.current?.focus();
|
|
69
|
+
}, [askedView]);
|
|
70
|
+
const svc = services.state === 'ready' ? services.data : null;
|
|
71
|
+
const list = useMemo(() => {
|
|
72
|
+
if (searching)
|
|
73
|
+
return (everything ?? []).filter((b) => matches(b, query));
|
|
74
|
+
if (load.state !== 'ready')
|
|
75
|
+
return [];
|
|
76
|
+
if (view.id === 'past')
|
|
77
|
+
return [...load.data].filter((b) => Date.parse(b.startsAt) <= now).reverse();
|
|
78
|
+
return load.data;
|
|
79
|
+
}, [searching, everything, query, load, view.id, now]);
|
|
80
|
+
const groups = useMemo(() => {
|
|
81
|
+
const map = new Map();
|
|
82
|
+
for (const b of list)
|
|
83
|
+
map.set(b.date, [...(map.get(b.date) ?? []), b]);
|
|
84
|
+
return [...map.entries()];
|
|
85
|
+
}, [list]);
|
|
86
|
+
// Keep the open booking in step with the list (after an answer, a reload).
|
|
87
|
+
const current = selected ? (list.find((b) => b.id === selected.id) ?? selected) : null;
|
|
88
|
+
const index = current ? list.findIndex((b) => b.id === current.id) : -1;
|
|
89
|
+
function changed(b) {
|
|
90
|
+
setSelected(b);
|
|
91
|
+
reload();
|
|
92
|
+
setEverything(null);
|
|
93
|
+
setSearchTick((t) => t + 1);
|
|
94
|
+
}
|
|
95
|
+
function close() {
|
|
96
|
+
const id = current?.id;
|
|
97
|
+
setSelected(null);
|
|
98
|
+
if (docked && id) {
|
|
99
|
+
window.requestAnimationFrame(() => document.querySelector(`[data-booking-id="${id}"] .pbe-who-name`)?.focus());
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const time = (hhmm) => formatClockTime(hhmm, locale);
|
|
103
|
+
const loading = searching ? everything === null : load.state === 'loading';
|
|
104
|
+
const error = !searching && load.state === 'error' ? load.error : null;
|
|
105
|
+
const statusNote = (b) => {
|
|
106
|
+
if (b.status === 'pending' || b.status === 'expired') {
|
|
107
|
+
const hold = holdState(b, settings.pendingHoldMinutes, now);
|
|
108
|
+
if (!hold || hold.ended)
|
|
109
|
+
return _jsx("span", { className: "pbe-status-note pbe-status-note--ended", children: "Hold ended \u00B7 confirm if free" });
|
|
110
|
+
const sameDay = todayInTimeZone(tz, new Date(Date.parse(b.holdExpiresAt))) === today;
|
|
111
|
+
return (_jsxs("span", { className: "pbe-status-note", children: ["Held until ", formatDateTime(b.holdExpiresAt, tz, locale, sameDay ? { hour: 'numeric', minute: '2-digit' } : { weekday: 'short', hour: 'numeric', minute: '2-digit' })] }));
|
|
112
|
+
}
|
|
113
|
+
if (b.status === 'confirmed' && Date.parse(b.startsAt) <= now && now < Date.parse(b.endsAt)) {
|
|
114
|
+
return _jsxs("span", { className: "pbe-status-note pbe-status-note--now", children: ["In progress \u00B7 ", formatSpan(Date.parse(b.endsAt) - now), " left"] });
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
};
|
|
118
|
+
const empty = searching ? (_jsx(EmptyState, { icon: "search", title: `No bookings match “${query.trim()}”`, variant: "card", children: "Search looks at names, email addresses, phone numbers, references and services, a year back and a year ahead." })) : view.id === 'pending' ? (_jsx(EmptyState, { icon: "inbox", title: "Nothing waiting for you", variant: "card", action: _jsx(Button, { onClick: () => setViewId('upcoming'), children: "See upcoming bookings" }), children: "New requests from your website land here, with their time held until you answer." })) : view.id === 'upcoming' ? (_jsx(EmptyState, { icon: "bookings", title: "No upcoming bookings", variant: "card", children: "Bookings made on your website appear here, by day." })) : view.id === 'past' ? (_jsx(EmptyState, { icon: "bookings", title: "No bookings in the past 30 days", variant: "card" })) : (_jsx(EmptyState, { icon: "bookings", title: "No cancelled bookings", variant: "card" }));
|
|
119
|
+
const page = (_jsxs("section", { "aria-labelledby": "pbe-bookings-title", className: "pbe-page", children: [_jsx(PageHeader, { id: "pbe-bookings-title", title: "Bookings", sub: `${searching ? 'Search results across past and upcoming bookings' : view.sub} · Times in ${timeZoneLabel(tz, locale)}`, actions: _jsxs("div", { className: "pbe-search", children: [_jsx(Icon, { name: "search" }), _jsx("input", { ref: searchRef, type: "search", "aria-label": "Search bookings", placeholder: "Name, email, phone or reference", value: query, onChange: (e) => setQuery(e.target.value), onKeyDown: (e) => {
|
|
120
|
+
if (e.key === 'Escape' && query) {
|
|
121
|
+
e.preventDefault();
|
|
122
|
+
setQuery('');
|
|
123
|
+
}
|
|
124
|
+
} }), query ? _jsx(IconButton, { icon: "close", label: "Clear search", onClick: () => setQuery('') }) : null] }) }), _jsx("div", { className: "pbe-toolbar", children: _jsx(Segmented, { label: "Booking views", value: searching ? '' : viewId, onChange: (id) => {
|
|
125
|
+
setViewId(id);
|
|
126
|
+
setQuery('');
|
|
127
|
+
}, options: VIEWS.map((v) => ({ id: v.id, label: v.label, count: v.id === 'pending' ? pendingCount : null })) }) }), searching && everything ? (_jsxs("p", { className: "pbe-results-note", role: "status", children: [list.length === 1 ? '1 booking' : `${list.length} bookings`, " found"] })) : null, error ? _jsx(ErrorNotice, { error: error, onRetry: reload }) : null, loading && !error ? (_jsx("div", { className: "pbe-card", children: _jsx(SkeletonRows, { rows: 5, label: "Loading bookings\u2026" }) })) : null, !loading && !error && list.length === 0 ? empty : null, !loading && list.length ? (_jsx("div", { className: "pbe-table-wrap", children: _jsxs("table", { className: "pbe-table pbe-table--card pbe-btable", children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { scope: "col", className: "pbe-col-time", children: "Time" }), _jsx("th", { scope: "col", children: "Customer" }), _jsx("th", { scope: "col", children: "Service" }), _jsx("th", { scope: "col", className: "pbe-num pbe-col-value", children: "Value" }), _jsx("th", { scope: "col", children: "Status" }), _jsx("th", { scope: "col", className: "pbe-col-ref", children: "Reference" })] }) }), groups.map(([date, items]) => (_jsxs("tbody", { children: [_jsx("tr", { className: "pbe-group", children: _jsxs("th", { scope: "rowgroup", colSpan: 6, children: [date === today ? _jsx("span", { className: "pbe-today-tag", children: "Today" }) : null, formatDate(date, locale, { weekday: 'long', month: 'long', day: 'numeric', ...(date.slice(0, 4) !== today.slice(0, 4) ? { year: 'numeric' } : {}) }), _jsx("span", { className: "pbe-group-meta", children: items.length === 1 ? '1 booking' : `${items.length} bookings` })] }) }), items.map((b) => {
|
|
128
|
+
const past = Date.parse(b.endsAt) <= now && view.id !== 'past';
|
|
129
|
+
const isOpen = current?.id === b.id;
|
|
130
|
+
return (_jsxs("tr", { className: `pbe-brow${past ? ' pbe-row--past' : ''}${b.status === 'cancelled' ? ' pbe-row--cancelled' : ''}`, "data-booking-id": b.id, "data-selected": isOpen || undefined, onClick: () => setSelected(b), children: [_jsx("td", { className: "pbe-col-time", children: _jsxs("span", { className: "pbe-time", children: [_jsx("b", { children: time(b.time) }), _jsxs("span", { children: ["to ", time(b.endTime)] })] }) }), _jsx("td", { className: "pbe-col-who", children: _jsxs("div", { className: "pbe-who", children: [_jsx("button", { type: "button", className: "pbe-who-name", "aria-current": isOpen || undefined, onClick: (e) => {
|
|
131
|
+
e.stopPropagation();
|
|
132
|
+
setSelected(b);
|
|
133
|
+
}, children: b.customer.name }), _jsx("span", { className: "pbe-who-mail", children: b.customer.email })] }) }), _jsx("td", { className: "pbe-col-svc", children: _jsx("span", { className: "pbe-svc", children: b.serviceName }) }), _jsx("td", { className: "pbe-num pbe-col-value", children: priceText(b, svc, locale) }), _jsx("td", { className: "pbe-col-status", children: _jsxs("div", { className: "pbe-status-cell", children: [_jsx(StatusBadge, { status: b.status }), statusNote(b)] }) }), _jsx("td", { className: "pbe-col-ref pbe-ref-cell", children: b.reference })] }, b.id));
|
|
134
|
+
})] }, date)))] }) })) : null] }));
|
|
135
|
+
const drawer = current ? (_jsx(BookingDrawer, { booking: current, services: svc, docked: docked, onClose: close, onChanged: changed, onPrevious: index > 0 ? () => setSelected(list[index - 1]) : null, onNext: index >= 0 && index < list.length - 1 ? () => setSelected(list[index + 1]) : null }, docked ? 'docked' : 'modal')) : null;
|
|
136
|
+
return (_jsxs("div", { className: `pbe-bookings${docked && current ? ' pbe-bookings--docked' : ''}`, children: [page, drawer] }));
|
|
137
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { EmailStatus } from '../../types.js';
|
|
2
|
+
/** What the last start of the mailer means, in the owner's words. */
|
|
3
|
+
export declare function describeLastStart(start: EmailStatus['wiring']['lastStart']): {
|
|
4
|
+
ok: boolean;
|
|
5
|
+
text: string;
|
|
6
|
+
} | null;
|
|
7
|
+
/** Email health, test email, the online-booking switch and email settings. */
|
|
8
|
+
export declare function EmailSection(): import("react").JSX.Element;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { formatDateTime } from '../../time.js';
|
|
4
|
+
import { ALERTS_VIEW, TEST_EMAIL_VIEW, alertsAddress } from '../alerts.js';
|
|
5
|
+
import { useAdmin } from '../context.js';
|
|
6
|
+
import { EMAIL_GROUP } from '../fields.js';
|
|
7
|
+
import { Icon } from '../icons.js';
|
|
8
|
+
import { Badge, Button, ErrorNotice, Field, Notice, PageHeader, SkeletonBlock, formatAgo, useResult } from '../ui.js';
|
|
9
|
+
import { SettingsSection } from './SettingsSection.js';
|
|
10
|
+
const KIND_LABELS = {
|
|
11
|
+
verification_code: 'Booking code',
|
|
12
|
+
request_received: 'Request received',
|
|
13
|
+
booking_confirmed: 'Booking confirmed',
|
|
14
|
+
booking_declined: 'Request declined',
|
|
15
|
+
booking_cancelled: 'Booking cancelled',
|
|
16
|
+
request_expired: 'Request expired',
|
|
17
|
+
owner_new_request: 'New request alert (to you)',
|
|
18
|
+
test: 'Test email',
|
|
19
|
+
};
|
|
20
|
+
const DELIVERY = {
|
|
21
|
+
pending: { label: 'Queued', tone: 'warn' },
|
|
22
|
+
sending: { label: 'Sending', tone: 'warn' },
|
|
23
|
+
sent: { label: 'Sent', tone: 'ok' },
|
|
24
|
+
failed: { label: 'Failed', tone: 'danger' },
|
|
25
|
+
cancelled: { label: 'Withdrawn', tone: 'neutral' },
|
|
26
|
+
};
|
|
27
|
+
const WIRING = [
|
|
28
|
+
{ key: 'configured', label: 'The mailer’s address and token are stored (booking.configure_mailer)' },
|
|
29
|
+
{ key: 'pgNet', label: 'The pg_net extension is enabled (emails go out at once)' },
|
|
30
|
+
{ key: 'schedule', label: 'The every-minute job is scheduled (retries and expiry notices)' },
|
|
31
|
+
];
|
|
32
|
+
/** What the last start of the mailer means, in the owner's words. */
|
|
33
|
+
export function describeLastStart(start) {
|
|
34
|
+
if (!start)
|
|
35
|
+
return null;
|
|
36
|
+
if (start.status === null) {
|
|
37
|
+
return start.error
|
|
38
|
+
? { ok: false, text: `the mailer could not be reached (${start.error})` }
|
|
39
|
+
: { ok: true, text: 'waiting for the mailer to answer' };
|
|
40
|
+
}
|
|
41
|
+
if (start.status >= 200 && start.status < 300)
|
|
42
|
+
return { ok: true, text: 'the mailer ran' };
|
|
43
|
+
if (start.status === 401)
|
|
44
|
+
return { ok: false, text: 'HTTP 401 — the token does not match the Edge Function secret BOOKING_MAILER_TOKEN' };
|
|
45
|
+
if (start.status === 503)
|
|
46
|
+
return { ok: false, text: 'HTTP 503 — the Edge Function secret BOOKING_MAILER_TOKEN is not set' };
|
|
47
|
+
if (start.status === 404)
|
|
48
|
+
return { ok: false, text: 'HTTP 404 — booking-mailer is not deployed at the configured address' };
|
|
49
|
+
return { ok: false, text: `HTTP ${start.status} — see the booking-mailer logs in Supabase` };
|
|
50
|
+
}
|
|
51
|
+
/** The alerts address field (in the email settings at the end of the page). */
|
|
52
|
+
const ALERTS_FIELD = 'set-ownerNotificationEmail';
|
|
53
|
+
/** Scroll a field into view and put the cursor in it. */
|
|
54
|
+
function reveal(id) {
|
|
55
|
+
const field = document.getElementById(id);
|
|
56
|
+
field?.scrollIntoView({ block: 'center' });
|
|
57
|
+
field?.focus({ preventScroll: true });
|
|
58
|
+
}
|
|
59
|
+
/** Email health, test email, the online-booking switch and email settings. */
|
|
60
|
+
export function EmailSection() {
|
|
61
|
+
const { engine, settings, refreshSettings, toast, view } = useAdmin();
|
|
62
|
+
const [load, reload] = useResult(() => engine.admin.emailStatus(), []);
|
|
63
|
+
// Where new-request alerts go (the alerts address, else the public email); the test email goes there by default.
|
|
64
|
+
const alerts = alertsAddress(settings);
|
|
65
|
+
const [recipient, setRecipient] = useState(alerts ?? '');
|
|
66
|
+
const [test, setTest] = useState(null);
|
|
67
|
+
const [busy, setBusy] = useState(null);
|
|
68
|
+
const [showAll, setShowAll] = useState(false);
|
|
69
|
+
const [problem, setProblem] = useState(null);
|
|
70
|
+
const alive = useRef(true);
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
alive.current = true;
|
|
73
|
+
return () => {
|
|
74
|
+
alive.current = false;
|
|
75
|
+
};
|
|
76
|
+
}, []);
|
|
77
|
+
// A newly saved alerts address becomes the test recipient, unless the owner typed another one.
|
|
78
|
+
const shownAlerts = useRef(alerts);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
setRecipient((r) => (r === '' || r === shownAlerts.current ? (alerts ?? '') : r));
|
|
81
|
+
shownAlerts.current = alerts;
|
|
82
|
+
}, [alerts]);
|
|
83
|
+
// From the setup checklist: bring the alerts address or the test email into view (after go() has scrolled up).
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
const id = view === ALERTS_VIEW ? ALERTS_FIELD : view === TEST_EMAIL_VIEW ? 'pbe-test-recipient' : null;
|
|
86
|
+
if (!id)
|
|
87
|
+
return;
|
|
88
|
+
let frame = requestAnimationFrame(() => {
|
|
89
|
+
frame = requestAnimationFrame(() => reveal(id));
|
|
90
|
+
});
|
|
91
|
+
return () => cancelAnimationFrame(frame);
|
|
92
|
+
}, [view]);
|
|
93
|
+
const when = (iso) => iso ? formatDateTime(iso, settings.timezone, settings.locale, { weekday: 'short', hour: 'numeric', minute: '2-digit' }) : '';
|
|
94
|
+
async function sendTest(event) {
|
|
95
|
+
event.preventDefault();
|
|
96
|
+
if (busy === 'test')
|
|
97
|
+
return;
|
|
98
|
+
setBusy('test');
|
|
99
|
+
setTest(null);
|
|
100
|
+
const queuedAt = Date.now() - 5000;
|
|
101
|
+
const r = await engine.admin.sendTestEmail(recipient);
|
|
102
|
+
if (!r.ok) {
|
|
103
|
+
setBusy(null);
|
|
104
|
+
setTest({ kind: 'error', text: r.error.message });
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
setTest({ kind: 'info', text: 'Test email queued. Waiting for delivery…' });
|
|
108
|
+
// Follow the email until the mailer reports it (the database starts the mailer at once).
|
|
109
|
+
for (let i = 0; i < 30 && alive.current; i++) {
|
|
110
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
111
|
+
const s = await engine.admin.emailStatus();
|
|
112
|
+
if (!s.ok)
|
|
113
|
+
continue;
|
|
114
|
+
const mail = s.data.recent.find((m) => m.kind === 'test' && Date.parse(m.createdAt) >= queuedAt);
|
|
115
|
+
if (mail?.status === 'sent') {
|
|
116
|
+
await refreshSettings();
|
|
117
|
+
reload();
|
|
118
|
+
setBusy(null);
|
|
119
|
+
setTest({ kind: 'success', text: `Delivered to the email provider. Check the inbox of ${recipient} (and the spam folder).` });
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (mail?.status === 'failed') {
|
|
123
|
+
reload();
|
|
124
|
+
setBusy(null);
|
|
125
|
+
setTest({ kind: 'error', text: `Sending failed: ${mail.lastError ?? 'unknown error'}` });
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (!alive.current)
|
|
130
|
+
return;
|
|
131
|
+
reload();
|
|
132
|
+
setBusy(null);
|
|
133
|
+
setTest({
|
|
134
|
+
kind: 'warning',
|
|
135
|
+
text: 'The test email is still waiting. Automatic sending may not be set up yet: check the technical details below, then try again.',
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
async function setOnline(on) {
|
|
139
|
+
setProblem(null);
|
|
140
|
+
setBusy('online');
|
|
141
|
+
const r = await engine.admin.updateSettings({ onlineBookingEnabled: on });
|
|
142
|
+
if (!r.ok) {
|
|
143
|
+
setBusy(null);
|
|
144
|
+
setProblem(r.error.message);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
await refreshSettings();
|
|
148
|
+
setBusy(null);
|
|
149
|
+
toast(on
|
|
150
|
+
? { title: 'Online booking is on.', detail: 'Customers can request appointments on your website.' }
|
|
151
|
+
: { title: 'Online booking is off.', detail: 'Your website shows a contact message instead of the form.', kind: 'info' });
|
|
152
|
+
}
|
|
153
|
+
async function retryFailed() {
|
|
154
|
+
setProblem(null);
|
|
155
|
+
setBusy('retry');
|
|
156
|
+
const r = await engine.admin.retryFailedEmails();
|
|
157
|
+
setBusy(null);
|
|
158
|
+
if (!r.ok) {
|
|
159
|
+
setProblem(r.error.message);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
reload();
|
|
163
|
+
toast(r.data.requeued
|
|
164
|
+
? { title: `${r.data.requeued} email${r.data.requeued === 1 ? '' : 's'} back in the queue`, detail: 'They are sent within a minute.' }
|
|
165
|
+
: { title: 'Nothing to retry.', kind: 'info' });
|
|
166
|
+
}
|
|
167
|
+
const status = load.state === 'ready' ? load.data : null;
|
|
168
|
+
const lastStart = status ? describeLastStart(status.wiring.lastStart) : null;
|
|
169
|
+
const working = Boolean(status?.mailer.lastSuccessAt);
|
|
170
|
+
const failing = Boolean(status?.mailer.lastFailureAt && (!status.mailer.lastSuccessAt || Date.parse(status.mailer.lastFailureAt) > Date.parse(status.mailer.lastSuccessAt)));
|
|
171
|
+
const wired = status ? WIRING.every((w) => status.wiring[w.key]) : true;
|
|
172
|
+
const online = settings.onlineBookingEnabled;
|
|
173
|
+
// Going live needs both: customers receive their codes, and the owner hears about every request.
|
|
174
|
+
const canGoLive = working && Boolean(alerts);
|
|
175
|
+
return (_jsxs("section", { "aria-labelledby": "pbe-email-title", className: "pbe-page", children: [_jsx(PageHeader, { id: "pbe-email-title", title: "Email & online booking", sub: `Customers confirm their email address with a code before a request reaches you, and get an email when you confirm, decline or cancel.${alerts ? ` You are emailed about every new request at ${alerts}.` : ''}` }), problem ? _jsx(Notice, { kind: "error", children: problem }) : null, !alerts ? (_jsxs(Notice, { kind: "warning", children: [_jsx("strong", { children: "New-request alerts have no address." }), " Nobody is emailed when a customer asks for an appointment, so a request could wait unanswered until its hold ends. Choose an address under \u201CSend new-request alerts to\u201D, or add a public email in Business.", ' ', _jsx("button", { type: "button", className: "pbe-link-button", onClick: () => reveal(ALERTS_FIELD), children: "Choose an address" })] })) : null, _jsxs("div", { className: "pbe-grid-2", children: [_jsxs("div", { className: "pbe-card pbe-card--pad pbe-stack", "aria-labelledby": "pbe-online-title", role: "region", children: [_jsxs("div", { className: "pbe-status-head", children: [_jsx("span", { className: `pbe-status-icon${online ? ' pbe-status-icon--ok' : ''}`, "aria-hidden": "true", children: _jsx(Icon, { name: "online", size: 20 }) }), _jsxs("div", { children: [_jsxs("h2", { id: "pbe-online-title", className: "pbe-card-title", children: ["Online booking is ", online ? 'on' : 'off'] }), _jsx("p", { className: "pbe-card-sub", children: online
|
|
176
|
+
? 'Customers can request appointments on your website.'
|
|
177
|
+
: !working
|
|
178
|
+
? 'Send a test email first: customers must be able to receive their code before they can book.'
|
|
179
|
+
: !alerts
|
|
180
|
+
? 'Email works. Choose where new-request alerts go, then switch online booking on.'
|
|
181
|
+
: 'Email works. Switch online booking on when you are ready for requests.' })] })] }), _jsx("div", { className: "pbe-form-actions", children: online ? (_jsx(Button, { loading: busy === 'online', onClick: () => void setOnline(false), children: "Turn online booking off" })) : (_jsx(Button, { variant: "primary", loading: busy === 'online', disabled: !canGoLive, onClick: () => void setOnline(true), children: "Turn online booking on" })) })] }), _jsxs("div", { className: "pbe-card pbe-card--pad pbe-stack", "aria-labelledby": "pbe-sending-title", role: "region", children: [load.state === 'loading' ? _jsx(SkeletonBlock, { lines: 2, label: "Checking booking emails\u2026" }) : null, load.state === 'error' ? _jsx(ErrorNotice, { error: load.error, onRetry: reload }) : null, status ? (_jsxs(_Fragment, { children: [_jsxs("div", { className: "pbe-status-head", children: [_jsx("span", { className: `pbe-status-icon${failing ? ' pbe-status-icon--bad' : working ? ' pbe-status-icon--ok' : ' pbe-status-icon--warn'}`, "aria-hidden": "true", children: _jsx(Icon, { name: failing ? 'mail-problem' : 'mail-ok', size: 20 }) }), _jsxs("div", { children: [_jsx("h2", { id: "pbe-sending-title", className: "pbe-card-title", children: failing ? 'Booking emails are failing' : working ? 'Booking emails are working' : 'No email delivered yet' }), _jsx("p", { className: "pbe-card-sub", children: working ? `Last delivered ${formatAgo(status.mailer.lastSuccessAt)} (${when(status.mailer.lastSuccessAt)}).` : 'Send a test email to check the set-up.' })] })] }), failing ? (_jsxs(Notice, { kind: "warning", children: ["Last problem (", when(status.mailer.lastFailureAt), "): ", status.mailer.lastError ?? 'unknown error'] })) : null, _jsxs("p", { className: "pbe-stats-line", children: [_jsxs("span", { children: [_jsx("b", { children: status.counts.sentLast24h }), "sent in 24 h"] }), _jsxs("span", { children: [_jsx("b", { children: status.counts.codesLast24h }), "of ", settings.maxVerificationEmailsPerDay, " code emails"] }), _jsxs("span", { children: [_jsx("b", { children: status.counts.queued }), "waiting"] }), _jsxs("span", { children: [_jsx("b", { children: status.counts.failedLast7Days }), "failed in 7 days"] })] }), status.counts.retryable > 0 ? (_jsxs("div", { className: "pbe-form-actions", children: [_jsxs(Button, { icon: "retry", loading: busy === 'retry', onClick: () => void retryFailed(), children: ["Retry ", status.counts.retryable, " failed email", status.counts.retryable === 1 ? '' : 's'] }), _jsx("span", { className: "pbe-help", children: "Emails from the last 23 hours; nobody gets one twice." })] })) : null] })) : null] })] }), _jsxs("div", { className: "pbe-card", role: "region", "aria-labelledby": "pbe-test-title", children: [_jsx("div", { className: "pbe-card-head pbe-card-head--rule", children: _jsxs("div", { children: [_jsx("h2", { id: "pbe-test-title", className: "pbe-card-title", children: "Send a test email" }), _jsx("p", { className: "pbe-card-sub", children: "A delivered test email is what unlocks online booking." })] }) }), _jsxs("form", { className: "pbe-form pbe-card-form", onSubmit: sendTest, children: [_jsxs("div", { className: "pbe-test-row", children: [_jsx("div", { className: "pbe-test-field", children: _jsx(Field, { id: "pbe-test-recipient", label: "Send a test email to", help: "Use an inbox you can check now.", children: _jsx("input", { id: "pbe-test-recipient", type: "email", required: true, autoComplete: "email", value: recipient, onChange: (e) => setRecipient(e.target.value) }) }) }), _jsx("div", { className: "pbe-test-action", children: _jsx(Button, { type: "submit", variant: "primary", icon: "send", loading: busy === 'test', children: "Send test email" }) })] }), test ? _jsx(Notice, { kind: test.kind, children: test.text }) : null] }), status ? (_jsxs("details", { className: "pbe-details pbe-card-details", open: !wired || (lastStart ? !lastStart.ok : false), children: [_jsxs("summary", { children: ["Technical details", _jsx("span", { className: "pbe-push" }), wired && (!lastStart || lastStart.ok) ? _jsx(Badge, { tone: "ok", children: "Set up" }) : _jsx(Badge, { tone: "warn", children: "Needs attention" }), _jsx(Icon, { name: "down" })] }), _jsxs("div", { className: "pbe-details-body", children: [_jsxs("ul", { className: "pbe-checklist", "aria-label": "Automatic sending", children: [WIRING.map((w) => (_jsxs("li", { className: status.wiring[w.key] ? 'pbe-done' : undefined, children: [_jsx("span", { className: "pbe-checklist-mark", "aria-hidden": "true", children: status.wiring[w.key] ? _jsx(Icon, { name: "check" }) : null }), _jsxs("span", { children: [w.label, _jsx("span", { className: "pbe-sr-only", children: status.wiring[w.key] ? ' (done)' : ' (missing)' })] })] }, w.key))), lastStart ? (_jsxs("li", { className: lastStart.ok ? 'pbe-done' : 'pbe-bad', children: [_jsx("span", { className: "pbe-checklist-mark", "aria-hidden": "true", children: _jsx(Icon, { name: lastStart.ok ? 'check' : 'close' }) }), _jsxs("span", { children: ["Last start of the mailer (", when(status.wiring.lastStart?.at ?? null), "): ", lastStart.text] })] })) : null] }), !wired ? (_jsxs("p", { className: "pbe-help", children: ["Set up once from your project folder: run ", _jsx("code", { className: "pbe-inline-code", children: "npx booking-engine mailer-sql" }), ", paste its output into the Supabase SQL Editor, then add the token it returns as the Edge Function secret ", _jsx("code", { className: "pbe-inline-code", children: "BOOKING_MAILER_TOKEN" }), "."] })) : null] })] })) : null] }), status && status.recent.length ? (_jsxs("div", { className: "pbe-stack", role: "region", "aria-labelledby": "pbe-recent-title", children: [_jsx("h2", { id: "pbe-recent-title", className: "pbe-section-title", children: "Recent emails" }), _jsxs("table", { className: "pbe-table pbe-table--card", children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { scope: "col", children: "Email" }), _jsx("th", { scope: "col", children: "To" }), _jsx("th", { scope: "col", children: "Status" }), _jsx("th", { scope: "col", children: "Queued" })] }) }), _jsx("tbody", { children: (showAll ? status.recent : status.recent.slice(0, 8)).map((m) => (_jsxs("tr", { children: [_jsx("td", { children: KIND_LABELS[m.kind] ?? m.kind }), _jsx("td", { className: "pbe-tnum", children: m.recipient }), _jsxs("td", { children: [_jsx(Badge, { tone: DELIVERY[m.status].tone, children: DELIVERY[m.status].label }), m.status === 'failed' && m.lastError ? _jsx("div", { className: "pbe-help", children: m.lastError }) : null] }), _jsx("td", { className: "pbe-nowrap pbe-tnum", children: when(m.createdAt) })] }, m.id))) })] }), status.recent.length > 8 && !showAll ? (_jsxs(Button, { size: "sm", className: "pbe-table-more", onClick: () => setShowAll(true), children: ["Show all ", status.recent.length] })) : null] })) : null, _jsx(SettingsSection, { group: EMAIL_GROUP, embedded: true })] }));
|
|
182
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function HoursSection(): import("react").JSX.Element;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
import { formatClockTime, formatDate, todayInTimeZone } from '../../time.js';
|
|
4
|
+
import { useAdmin } from '../context.js';
|
|
5
|
+
import { Icon } from '../icons.js';
|
|
6
|
+
import { Button, ErrorNotice, Field, IconButton, Notice, PageHeader, SkeletonBlock, Switch, useResult } from '../ui.js';
|
|
7
|
+
const DAYS = [
|
|
8
|
+
{ weekday: 1, label: 'Monday' },
|
|
9
|
+
{ weekday: 2, label: 'Tuesday' },
|
|
10
|
+
{ weekday: 3, label: 'Wednesday' },
|
|
11
|
+
{ weekday: 4, label: 'Thursday' },
|
|
12
|
+
{ weekday: 5, label: 'Friday' },
|
|
13
|
+
{ weekday: 6, label: 'Saturday' },
|
|
14
|
+
{ weekday: 7, label: 'Sunday' },
|
|
15
|
+
];
|
|
16
|
+
function toWeek(hours) {
|
|
17
|
+
const week = { 1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [] };
|
|
18
|
+
for (const h of hours)
|
|
19
|
+
week[h.weekday].push({ start: h.start, end: h.end === '24:00' ? '00:00' : h.end });
|
|
20
|
+
return week;
|
|
21
|
+
}
|
|
22
|
+
function fromWeek(week) {
|
|
23
|
+
return DAYS.flatMap(({ weekday }) => week[weekday].map((i) => ({ weekday, start: i.start, end: i.end === '00:00' ? '24:00' : i.end })));
|
|
24
|
+
}
|
|
25
|
+
export function HoursSection() {
|
|
26
|
+
return (_jsxs("div", { className: "pbe-page", children: [_jsx(PageHeader, { id: "pbe-hours-page-title", title: "Hours & closures", sub: "When customers can book. Closures take days or hours off without touching your weekly hours." }), _jsx(WeeklyHours, {}), _jsx(Closures, {})] }));
|
|
27
|
+
}
|
|
28
|
+
function WeeklyHours() {
|
|
29
|
+
const { engine, toast } = useAdmin();
|
|
30
|
+
const [load, reload] = useResult(() => engine.admin.getWorkingHours(), []);
|
|
31
|
+
const [week, setWeek] = useState(null);
|
|
32
|
+
const [saved, setSaved] = useState('');
|
|
33
|
+
const [error, setError] = useState(null);
|
|
34
|
+
const [busy, setBusy] = useState(false);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (load.state === 'ready') {
|
|
37
|
+
setWeek(toWeek(load.data));
|
|
38
|
+
setSaved(JSON.stringify(toWeek(load.data)));
|
|
39
|
+
}
|
|
40
|
+
}, [load]);
|
|
41
|
+
function update(weekday, index, patch) {
|
|
42
|
+
if (!week)
|
|
43
|
+
return;
|
|
44
|
+
setWeek({ ...week, [weekday]: week[weekday].map((i, n) => (n === index ? { ...i, ...patch } : i)) });
|
|
45
|
+
}
|
|
46
|
+
async function save() {
|
|
47
|
+
if (!week || busy)
|
|
48
|
+
return;
|
|
49
|
+
setBusy(true);
|
|
50
|
+
setError(null);
|
|
51
|
+
const r = await engine.admin.setWorkingHours(fromWeek(week));
|
|
52
|
+
setBusy(false);
|
|
53
|
+
if (!r.ok) {
|
|
54
|
+
setError(r.error.message);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
toast({ title: 'Opening hours saved.', detail: 'Online availability is updated' });
|
|
58
|
+
reload();
|
|
59
|
+
}
|
|
60
|
+
const dirty = week ? JSON.stringify(week) !== saved : false;
|
|
61
|
+
return (_jsxs("section", { "aria-labelledby": "pbe-hours-title", className: "pbe-card", children: [_jsx("div", { className: "pbe-card-head pbe-card-head--rule", children: _jsxs("div", { children: [_jsx("h2", { id: "pbe-hours-title", className: "pbe-card-title", children: "Opening hours" }), _jsx("p", { className: "pbe-card-sub", children: "Add a second range on a day for a break. To stay open until midnight, close at 00:00." })] }) }), load.state === 'loading' ? (_jsx("div", { className: "pbe-card-body", style: { paddingTop: 20 }, children: _jsx(SkeletonBlock, { lines: 6, label: "Loading opening hours\u2026" }) })) : null, load.state === 'error' ? (_jsx("div", { className: "pbe-card-body", style: { paddingTop: 16 }, children: _jsx(ErrorNotice, { error: load.error, onRetry: reload }) })) : null, week ? (_jsxs(_Fragment, { children: [_jsx("div", { className: "pbe-hours", children: DAYS.map(({ weekday, label }) => (_jsxs("div", { className: "pbe-hours-row", children: [_jsx("div", { className: "pbe-hours-day", children: label }), _jsxs("div", { className: "pbe-hours-ranges", children: [week[weekday].length === 0 ? (_jsxs("span", { className: "pbe-hours-closed", children: [_jsx("span", { className: "pbe-dot pbe-dot--off", "aria-hidden": "true" }), "Closed"] })) : null, week[weekday].map((range, index) => (_jsxs("div", { className: "pbe-hours-range", children: [_jsx("input", { type: "time", step: 300, "aria-label": `${label} opening time ${index + 1}`, value: range.start, onChange: (e) => update(weekday, index, { start: e.target.value }) }), _jsx("span", { className: "pbe-dash", "aria-hidden": "true", children: "\u2013" }), _jsx("input", { type: "time", step: 300, "aria-label": `${label} closing time ${index + 1}`, value: range.end, onChange: (e) => update(weekday, index, { end: e.target.value }) }), _jsx(IconButton, { icon: "close", label: `Remove ${label} time range ${index + 1}`, onClick: () => setWeek({ ...week, [weekday]: week[weekday].filter((_, n) => n !== index) }) })] }, index)))] }), _jsx(Button, { size: "sm", variant: "quiet", icon: "add", onClick: () => {
|
|
62
|
+
const last = week[weekday].at(-1);
|
|
63
|
+
const next = last ? { start: last.end, end: last.end } : { start: '09:00', end: '17:00' };
|
|
64
|
+
setWeek({ ...week, [weekday]: [...week[weekday], next] });
|
|
65
|
+
}, children: week[weekday].length ? 'Add range' : 'Open this day' })] }, weekday))) }), error ? (_jsx("div", { className: "pbe-card-body", children: _jsx(Notice, { kind: "error", children: error }) })) : null, _jsxs("div", { className: "pbe-settings-foot", children: [_jsx("span", { className: "pbe-settings-state", children: dirty ? (_jsxs(_Fragment, { children: [_jsx("span", { className: "pbe-dot pbe-dot--warn", "aria-hidden": "true" }), "Unsaved changes"] })) : ('Your website offers times within these hours.') }), _jsx(Button, { variant: "primary", loading: busy, disabled: !dirty, onClick: () => void save(), children: "Save opening hours" })] })] })) : null] }));
|
|
66
|
+
}
|
|
67
|
+
function Closures() {
|
|
68
|
+
const { engine, settings, toast } = useAdmin();
|
|
69
|
+
const [load, reload] = useResult(() => engine.admin.listExceptions(), []);
|
|
70
|
+
const today = todayInTimeZone(settings.timezone);
|
|
71
|
+
const [form, setForm] = useState({ startsOn: today, endsOn: today, allDay: true, startTime: '12:00', endTime: '13:00', reason: '' });
|
|
72
|
+
const [error, setError] = useState(null);
|
|
73
|
+
const [busy, setBusy] = useState(null);
|
|
74
|
+
async function add(event) {
|
|
75
|
+
event.preventDefault();
|
|
76
|
+
if (busy)
|
|
77
|
+
return;
|
|
78
|
+
setBusy('add');
|
|
79
|
+
setError(null);
|
|
80
|
+
const r = await engine.admin.addException({
|
|
81
|
+
startsOn: form.startsOn,
|
|
82
|
+
endsOn: form.endsOn || form.startsOn,
|
|
83
|
+
startTime: form.allDay ? null : form.startTime,
|
|
84
|
+
endTime: form.allDay ? null : form.endTime === '00:00' ? '24:00' : form.endTime,
|
|
85
|
+
reason: form.reason || null,
|
|
86
|
+
});
|
|
87
|
+
setBusy(null);
|
|
88
|
+
if (!r.ok) {
|
|
89
|
+
setError(r.error.message);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
toast({ title: 'Closure added.', detail: 'Those times are no longer bookable' });
|
|
93
|
+
setForm({ ...form, reason: '' });
|
|
94
|
+
reload();
|
|
95
|
+
}
|
|
96
|
+
async function remove(id) {
|
|
97
|
+
setBusy(id);
|
|
98
|
+
const r = await engine.admin.deleteException(id);
|
|
99
|
+
setBusy(null);
|
|
100
|
+
if (!r.ok)
|
|
101
|
+
setError(r.error.message);
|
|
102
|
+
else
|
|
103
|
+
toast({ title: 'Closure removed.', detail: 'Those times can be booked again' });
|
|
104
|
+
reload();
|
|
105
|
+
}
|
|
106
|
+
const upcoming = load.state === 'ready' ? load.data.filter((e) => e.endsOn >= today).sort((a, b) => a.startsOn.localeCompare(b.startsOn)) : [];
|
|
107
|
+
const date = (d) => formatDate(d, settings.locale, { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' });
|
|
108
|
+
const clock = (t) => formatClockTime(t === '24:00' ? '00:00' : t, settings.locale);
|
|
109
|
+
return (_jsxs("section", { "aria-labelledby": "pbe-closures-title", className: "pbe-stack", children: [_jsxs("div", { children: [_jsx("h2", { id: "pbe-closures-title", className: "pbe-section-title", children: "Closures and time off" }), _jsx("p", { className: "pbe-section-sub", children: "Holidays, training days or a few blocked hours. Existing bookings are not cancelled." })] }), _jsxs("div", { className: "pbe-split pbe-split--even", children: [_jsx("form", { className: "pbe-card pbe-closure-form", onSubmit: add, "aria-label": "Add a closure", children: _jsxs("div", { className: "pbe-form", children: [error ? _jsx(Notice, { kind: "error", children: error }) : null, _jsxs("div", { className: "pbe-grid-2", children: [_jsx(Field, { id: "cl-start", label: "From", children: _jsx("input", { id: "cl-start", type: "date", required: true, value: form.startsOn, onChange: (e) => setForm({ ...form, startsOn: e.target.value }) }) }), _jsx(Field, { id: "cl-end", label: "To (inclusive)", children: _jsx("input", { id: "cl-end", type: "date", required: true, min: form.startsOn, value: form.endsOn, onChange: (e) => setForm({ ...form, endsOn: e.target.value }) }) })] }), _jsx(Switch, { id: "cl-all-day", label: "Closed all day", checked: form.allDay, onChange: (v) => setForm({ ...form, allDay: v }) }), !form.allDay ? (_jsxs("div", { className: "pbe-grid-2", children: [_jsx(Field, { id: "cl-from-time", label: "Closed from", help: "On each day of the range.", children: _jsx("input", { id: "cl-from-time", type: "time", step: 300, value: form.startTime, onChange: (e) => setForm({ ...form, startTime: e.target.value }) }) }), _jsx(Field, { id: "cl-to-time", label: "Until", children: _jsx("input", { id: "cl-to-time", type: "time", step: 300, value: form.endTime, onChange: (e) => setForm({ ...form, endTime: e.target.value }) }) })] })) : null, _jsx(Field, { id: "cl-reason", label: "Note", help: "Optional. Only you see it.", children: _jsx("input", { id: "cl-reason", maxLength: 200, value: form.reason, onChange: (e) => setForm({ ...form, reason: e.target.value }) }) }), _jsx("div", { className: "pbe-form-actions", children: _jsx(Button, { type: "submit", variant: "primary", icon: "add", loading: busy === 'add', children: "Add closure" }) })] }) }), _jsxs("div", { className: "pbe-card", "aria-label": "Upcoming closures", role: "region", children: [_jsxs("div", { className: "pbe-card-head pbe-card-head--rule", children: [_jsx("h3", { className: "pbe-card-title", children: "Upcoming" }), _jsx("span", { className: "pbe-card-sub", children: upcoming.length ? `${upcoming.length} planned` : '' })] }), load.state === 'error' ? (_jsx("div", { className: "pbe-card-body", style: { paddingTop: 16 }, children: _jsx(ErrorNotice, { error: load.error, onRetry: reload }) })) : null, load.state === 'loading' ? (_jsx("div", { className: "pbe-card-body", style: { paddingTop: 16 }, children: _jsx(SkeletonBlock, { lines: 2, label: "Loading closures\u2026" }) })) : null, upcoming.length ? (_jsx("ul", { className: "pbe-closures", children: upcoming.map((e) => (_jsxs("li", { className: "pbe-closure", children: [_jsx("span", { className: "pbe-closure-icon", "aria-hidden": "true", children: _jsx(Icon, { name: "closure" }) }), _jsxs("span", { className: "pbe-closure-text", children: [_jsxs("span", { children: [date(e.startsOn), e.endsOn !== e.startsOn ? ` – ${date(e.endsOn)}` : ''] }), _jsxs("span", { children: [e.startTime ? `${clock(e.startTime)} – ${clock(e.endTime ?? '24:00')}` : 'All day', e.reason ? ` · ${e.reason}` : ''] })] }), _jsx(Button, { size: "sm", variant: "danger-quiet", "aria-label": `Remove closure on ${date(e.startsOn)}`, loading: busy === e.id, onClick: () => void remove(e.id), children: "Remove" })] }, e.id))) })) : load.state === 'ready' ? (_jsx("p", { className: "pbe-help", style: { padding: '18px 20px 20px' }, children: "No upcoming closures. Your weekly hours apply every week." })) : null] })] })] }));
|
|
110
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function OverviewSection(): import("react").JSX.Element;
|