@composable-os/pack-bookings 0.1.0-beta.11 → 0.1.0-beta.13

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.
@@ -1,5 +1,12 @@
1
1
  import type { BookingsHooks } from "@composable-os/pack-bookings/client";
2
2
  type Hooks = Pick<BookingsHooks, "useDay" | "useBookFor" | "useFindPeople">;
3
+ /**
4
+ * The front desk as the product's admin shows it (stage 47): today in the desk's own zone (the
5
+ * browser's), with a day to choose. The admin passes it nothing but the hooks.
6
+ */
7
+ export declare function FrontDesk({ bookings }: {
8
+ bookings: Hooks;
9
+ }): import("react").JSX.Element;
3
10
  export declare function Desk({ bookings, date, timeZone, }: {
4
11
  bookings: Hooks;
5
12
  /** The local date shown, YYYY-MM-DD. */
@@ -1,9 +1,21 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
- import { classTime } from "@composable-os/pack-bookings/client";
2
+ import { classTime, todayIn } from "@composable-os/pack-bookings/client";
3
3
  import { useState } from "react";
4
4
  import { Button } from "../ui-button/button.js";
5
5
  import { Input } from "../ui-input/input.js";
6
6
  import { Label } from "../ui-label/label.js";
7
+ /**
8
+ * The front desk as the product's admin shows it (stage 47): today in the desk's own zone (the
9
+ * browser's), with a day to choose. The admin passes it nothing but the hooks.
10
+ */
11
+ export function FrontDesk({ bookings }) {
12
+ const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
13
+ const [date, setDate] = useState(() => todayIn(timeZone));
14
+ return (_jsxs("div", { className: "grid gap-4", children: [_jsxs("div", { className: "grid max-w-xs gap-1", children: [_jsx(Label, { htmlFor: "desk-date", children: "Day" }), _jsx(Input, { id: "desk-date", type: "date", value: date, onChange: (event) => {
15
+ if (event.target.value)
16
+ setDate(event.target.value);
17
+ } })] }), _jsx(Desk, { bookings: bookings, date: date, timeZone: timeZone })] }));
18
+ }
7
19
  export function Desk({ bookings, date, timeZone, }) {
8
20
  const day = bookings.useDay({ date, timeZone });
9
21
  const bookFor = bookings.useBookFor();
@@ -1,4 +1,7 @@
1
+ import { type AgreementsHooks } from "@composable-os/client";
1
2
  import type { BookingsHooks } from "@composable-os/pack-bookings/client";
3
+ /** The agreements hooks the inline step uses: a member's (due, accept) and a visitor's (words). */
4
+ export type DropInAgreements = Pick<AgreementsHooks, "useDue" | "useAccept" | "useGuestVersions">;
2
5
  type Hooks = Pick<BookingsHooks, "useTimetable" | "useBuy">;
3
6
  /** What the buyer is shown before they leave for the provider's page. */
4
7
  export interface StartedDropIn {
@@ -10,9 +13,12 @@ export interface StartedDropIn {
10
13
  * `signedIn`: the buyer is a signed-in member (no email asked). `returnUrl`: where the provider
11
14
  * sends them back, on one of the deployment's allowed sites; coming back books nothing, the
12
15
  * verified payment does. `onStarted` is called before they leave (keep a guest's token).
16
+ * `agreements`: the agreements hooks, for what must be agreed to first (without them, a refusal
17
+ * only names the documents).
13
18
  */
14
- export declare function DropIn({ bookings, signedIn, returnUrl, onStarted, from, to, }: {
19
+ export declare function DropIn({ bookings, agreements, signedIn, returnUrl, onStarted, from, to, }: {
15
20
  bookings: Hooks;
21
+ agreements?: DropInAgreements;
16
22
  signedIn: boolean;
17
23
  returnUrl: string;
18
24
  onStarted?: (started: StartedDropIn) => void;
@@ -1,15 +1,33 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // Paying for one class on a gym's site (stage 31a step 8, a drop-in): the week's classes for a
3
+ // visitor, each with "Pay for this class". The host holds a place while they pay, prices it, and
4
+ // books them when the payment provider confirms it; an unpaid order lets the place go after a few
5
+ // minutes. Whether a class can be paid for, its price, the places left and the rules are the
6
+ // host's: this shows its answers and sends the buyer to the provider's page. Web only: a native
7
+ // block never buys (the block lint).
8
+ //
9
+ // What must be agreed to first (a waiver, a health form: stage 44) is the host's answer too: when
10
+ // "Pay" is refused with it, the documents are shown inline (the web-agree block's step) and "Pay"
11
+ // is tried again. A visitor's agreements travel in the buying call and are recorded with the order;
12
+ // a member's are recorded as they agree. What a visitor typed, drew and answered is held in memory
13
+ // only until that call. A minor whose purchase needs the payer's agreement is told a parent or
14
+ // guardian buys it.
15
+ import { agreementRefusal, forgetAfter, purchaseErrorWords, withAgreements, } from "@composable-os/client";
2
16
  import { classTime, formatAmount, placesLabel, refusalMessage, } from "@composable-os/pack-bookings/client";
3
17
  import { useState } from "react";
4
18
  import { Button } from "../ui-button/button.js";
5
19
  import { Input } from "../ui-input/input.js";
6
20
  import { Label } from "../ui-label/label.js";
21
+ import { useForgetOnLeave } from "../web-agree/agree.js";
22
+ import { GuestAgreementStep, MemberAgreementStep } from "../web-agree/agreement-step.js";
7
23
  /**
8
24
  * `signedIn`: the buyer is a signed-in member (no email asked). `returnUrl`: where the provider
9
25
  * sends them back, on one of the deployment's allowed sites; coming back books nothing, the
10
26
  * verified payment does. `onStarted` is called before they leave (keep a guest's token).
27
+ * `agreements`: the agreements hooks, for what must be agreed to first (without them, a refusal
28
+ * only names the documents).
11
29
  */
12
- export function DropIn({ bookings, signedIn, returnUrl, onStarted, from, to, }) {
30
+ export function DropIn({ bookings, agreements, signedIn, returnUrl, onStarted, from, to, }) {
13
31
  const timetable = bookings.useTimetable({ ...(from ? { from } : {}), ...(to ? { to } : {}) });
14
32
  const [chosen, setChosen] = useState(null);
15
33
  if (timetable.isPending)
@@ -18,37 +36,102 @@ export function DropIn({ bookings, signedIn, returnUrl, onStarted, from, to, })
18
36
  return _jsx("p", { role: "alert", children: "We could not load the timetable. Try again soon." });
19
37
  }
20
38
  if (chosen) {
21
- return (_jsx(PayForm, { klass: chosen, bookings: bookings, signedIn: signedIn, returnUrl: returnUrl, onStarted: onStarted, onBack: () => setChosen(null) }));
39
+ return (_jsx(PayForm, { klass: chosen, bookings: bookings, agreements: agreements, signedIn: signedIn, returnUrl: returnUrl, onStarted: onStarted, onBack: () => setChosen(null) }));
22
40
  }
23
41
  return (_jsxs("section", { className: "grid gap-4", "aria-label": "Pay for a class", children: [timetable.data.classes.length === 0 ? _jsx("p", { children: "No classes in these days." }) : null, _jsx("ul", { className: "grid gap-2", children: timetable.data.classes.map((klass) => (_jsxs("li", { className: "flex flex-wrap items-center justify-between gap-3 rounded-md border p-3", children: [_jsxs("div", { className: "grid gap-1", children: [_jsx("strong", { children: klass.title ?? "Class" }), _jsx("span", { children: classTime(klass) }), _jsx("span", { className: "text-muted-foreground text-sm", children: [klass.coach?.name, klass.room?.name, placesLabel(klass.places)]
24
42
  .filter(Boolean)
25
43
  .join(" · ") })] }), _jsxs(Button, { variant: "outline", onClick: () => setChosen(klass), children: ["Pay for ", klass.title ?? "this class"] })] }, klass.id))) })] }));
26
44
  }
27
- function PayForm({ klass, bookings, signedIn, returnUrl, onStarted, onBack, }) {
45
+ function PayForm({ klass, bookings, agreements, signedIn, returnUrl, onStarted, onBack, }) {
28
46
  const buy = bookings.useBuy();
29
47
  const [email, setEmail] = useState("");
30
48
  const [message, setMessage] = useState(null);
49
+ const [problem, setProblem] = useState(null);
50
+ // A member's "a copy is on its way", kept while the payment starts.
51
+ const [said, setSaid] = useState(null);
52
+ // What the host said must be agreed to first, and a minor's "a parent or guardian buys it".
53
+ const [due, setDue] = useState(null);
54
+ const [minor, setMinor] = useState(null);
55
+ // A visitor's agreements for this attempt: in this component's state only (never the app's
56
+ // cache), kept if the payment cannot start so nothing is typed twice, forgotten once it starts,
57
+ // when they go back, and when they leave the page.
58
+ const [agreed, setAgreed] = useState(null);
59
+ const [busy, setBusy] = useState(false);
60
+ useForgetOnLeave(() => setAgreed(null));
31
61
  const name = klass.title ?? "Class";
32
- const busy = buy.start.isPending || buy.checkout.isPending;
33
- async function submit(e) {
34
- e.preventDefault();
62
+ /** Starts the order (with a visitor's agreements, once given) and opens the payment page. */
63
+ async function pay(given = agreed) {
35
64
  setMessage(null);
36
- const held = await buy.start.mutateAsync({
37
- classId: klass.id,
38
- ...(signedIn ? {} : { email }),
39
- });
40
- if (held.result === "refused") {
41
- setMessage(refusalMessage(held, klass.timeZone));
42
- return;
65
+ setProblem(null);
66
+ setBusy(true);
67
+ try {
68
+ const outcome = await withAgreements((previous) => forgetAfter(buy.start, {
69
+ classId: klass.id,
70
+ ...(signedIn ? {} : { email }),
71
+ // A version replaced meanwhile: ask again with none, and the host names what is due now.
72
+ ...(!signedIn && given && previous?.kind !== "read_again" ? { agreements: given } : {}),
73
+ }));
74
+ if (outcome.kind === "due") {
75
+ setAgreed(null);
76
+ if (agreements)
77
+ setDue(outcome.documents);
78
+ else {
79
+ setProblem(`Something must be agreed to first: ${outcome.documents.map((d) => d.title).join(", ")}.`);
80
+ }
81
+ return;
82
+ }
83
+ if (outcome.kind === "minor_payer") {
84
+ setAgreed(null);
85
+ setMinor(outcome.message);
86
+ return;
87
+ }
88
+ const held = outcome.value;
89
+ if (held.result === "refused") {
90
+ setMessage(refusalMessage(held, klass.timeZone));
91
+ return;
92
+ }
93
+ // Recorded with the order: nothing of it is kept here any more.
94
+ setAgreed(null);
95
+ onStarted?.({ orderId: held.orderId, bookingId: held.bookingId, token: held.token });
96
+ setMessage(`Your place is held. ${formatAmount(held.total, held.currency)} to pay.`);
97
+ const page = await forgetAfter(buy.checkout, {
98
+ id: held.orderId,
99
+ ...(held.token ? { token: held.token } : {}),
100
+ returnUrl,
101
+ });
102
+ window.location.assign(page.url);
103
+ }
104
+ catch (error) {
105
+ const refusal = agreementRefusal(error);
106
+ if (refusal?.kind === "read_again" || refusal?.kind === "changed")
107
+ setAgreed(null);
108
+ setProblem(purchaseErrorWords(error) ?? "We could not start the payment. Try again soon.");
43
109
  }
44
- onStarted?.({ orderId: held.orderId, bookingId: held.bookingId, token: held.token });
45
- setMessage(`Your place is held. ${formatAmount(held.total, held.currency)} to pay.`);
46
- const page = await buy.checkout.mutateAsync({
47
- id: held.orderId,
48
- ...(held.token ? { token: held.token } : {}),
49
- returnUrl,
50
- });
51
- window.location.assign(page.url);
110
+ finally {
111
+ setBusy(false);
112
+ }
113
+ }
114
+ async function submit(e) {
115
+ e.preventDefault();
116
+ await pay();
117
+ }
118
+ if (minor) {
119
+ return (_jsxs("section", { className: "grid gap-3", "aria-label": `Pay for ${name}`, children: [_jsx("p", { role: "alert", children: minor }), _jsx(Button, { type: "button", variant: "outline", onClick: onBack, children: "Back" })] }));
120
+ }
121
+ if (due && agreements) {
122
+ const back = () => setDue(null);
123
+ return signedIn ? (_jsx(MemberAgreementStep, { agreements: agreements, documents: due, continueLabel: "Agree and pay", onCancel: back, onAgreed: (copy) => {
124
+ setDue(null);
125
+ setSaid(copy);
126
+ void pay(null);
127
+ } })) : (_jsx(GuestAgreementStep, { agreements: agreements, documents: due, onCancel: back, onAgreed: (given) => {
128
+ setDue(null);
129
+ setAgreed(given);
130
+ void pay(given);
131
+ } }));
52
132
  }
53
- return (_jsxs("form", { onSubmit: submit, className: "grid gap-4", "aria-label": `Pay for ${name}`, children: [_jsxs("div", { className: "grid gap-1", children: [_jsx("strong", { children: name }), _jsx("span", { children: classTime(klass) })] }), signedIn ? null : (_jsxs("div", { className: "grid gap-1", children: [_jsx(Label, { htmlFor: "drop-in-email", children: "Your email" }), _jsx(Input, { id: "drop-in-email", type: "email", autoComplete: "email", required: true, value: email, onChange: (e) => setEmail(e.target.value) })] })), message ? _jsx("p", { role: "status", children: message }) : null, buy.start.error || buy.checkout.error ? (_jsx("p", { role: "alert", children: "We could not start the payment. Try again soon." })) : null, _jsxs("div", { className: "flex flex-wrap gap-2", children: [_jsx(Button, { type: "submit", disabled: busy, children: busy ? "Opening the payment page…" : "Pay" }), _jsx(Button, { type: "button", variant: "outline", onClick: onBack, children: "Back" })] })] }));
133
+ return (_jsxs("form", { onSubmit: submit, className: "grid gap-4", "aria-label": `Pay for ${name}`, children: [_jsxs("div", { className: "grid gap-1", children: [_jsx("strong", { children: name }), _jsx("span", { children: classTime(klass) })] }), signedIn ? null : (_jsxs("div", { className: "grid gap-1", children: [_jsx(Label, { htmlFor: "drop-in-email", children: "Your email" }), _jsx(Input, { id: "drop-in-email", type: "email", autoComplete: "email", required: true, value: email, onChange: (e) => setEmail(e.target.value) })] })), said ? _jsx("p", { role: "status", children: said }) : null, agreed ? (_jsx("p", { className: "text-sm", "data-agreed": agreed.length, children: "What you agreed to is kept for this payment: press Pay to try again." })) : null, message ? _jsx("p", { role: "status", children: message }) : null, problem ? _jsx("p", { role: "alert", children: problem }) : null, _jsxs("div", { className: "flex flex-wrap gap-2", children: [_jsx(Button, { type: "submit", disabled: busy, children: busy ? "Opening the payment page…" : "Pay" }), _jsx(Button, { type: "button", variant: "outline", onClick: () => {
134
+ setAgreed(null);
135
+ onBack();
136
+ }, children: "Back" })] })] }));
54
137
  }
@@ -0,0 +1,12 @@
1
+ import type { BookingsHooks } from "@composable-os/pack-bookings/client";
2
+ type Hooks = Pick<BookingsHooks, "useDay" | "useRoster" | "useMarkAttended" | "useUndoAttended" | "useMarkNoShow">;
3
+ /**
4
+ * `date` is today's date where the gym is (YYYY-MM-DD) and `timeZone` the gym's zone: the app
5
+ * passes them; which classes a coach may see is the host's (its role's today).
6
+ */
7
+ export declare function ClassListScreen({ bookings, date, timeZone, }: {
8
+ bookings: Hooks;
9
+ date: string;
10
+ timeZone: string;
11
+ }): import("react").JSX.Element;
12
+ export {};
@@ -0,0 +1,59 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { classTime } from "@composable-os/pack-bookings/client";
3
+ import { useState } from "react";
4
+ import { View } from "react-native";
5
+ import { Button } from "../native-base/button.js";
6
+ import { List } from "../native-base/list.js";
7
+ import { Row } from "../native-base/row.js";
8
+ import { Screen } from "../native-base/screen.js";
9
+ import { Text } from "../native-base/text.js";
10
+ const STATE_LABEL = {
11
+ booked: "Booked",
12
+ attended: "Came",
13
+ no_show: "Did not come",
14
+ waiting: "Waiting",
15
+ };
16
+ /**
17
+ * `date` is today's date where the gym is (YYYY-MM-DD) and `timeZone` the gym's zone: the app
18
+ * passes them; which classes a coach may see is the host's (its role's today).
19
+ */
20
+ export function ClassListScreen({ bookings, date, timeZone, }) {
21
+ const day = bookings.useDay({ date, timeZone });
22
+ const [classId, setClassId] = useState(null);
23
+ if (classId) {
24
+ return _jsx(ClassRoster, { bookings: bookings, classId: classId, onBack: () => setClassId(null) });
25
+ }
26
+ return (_jsxs(Screen, { title: "Today's classes", children: [day.isPending ? (_jsx(Text, { tone: "muted", accessibilityRole: "progressbar", children: "Loading the classes\u2026" })) : null, day.error ? _jsx(Text, { accessibilityRole: "alert", children: "We could not load today's classes." }) : null, day.data ? (_jsx(List, { empty: "No classes today.", children: day.data.classes.map((klass) => (_jsx(Row, { title: klass.title ?? "Class", detail: [
27
+ classTime(klass),
28
+ klass.room,
29
+ `${klass.booked} booked of ${klass.places ?? "unlimited"}`,
30
+ klass.state === "cancelled" ? "Cancelled" : null,
31
+ ]
32
+ .filter(Boolean)
33
+ .join(" · "), onPress: () => setClassId(klass.id) }, klass.id))) })) : null] }));
34
+ }
35
+ function ClassRoster({ bookings, classId, onBack, }) {
36
+ const list = bookings.useRoster({ classId });
37
+ const tick = bookings.useMarkAttended();
38
+ const untick = bookings.useUndoAttended();
39
+ const noShow = bookings.useMarkNoShow();
40
+ const [message, setMessage] = useState(null);
41
+ const say = async (done) => {
42
+ setMessage(null);
43
+ try {
44
+ setMessage((await done).message);
45
+ }
46
+ catch {
47
+ setMessage("That did not work. Try again.");
48
+ }
49
+ await list.refetch?.();
50
+ };
51
+ const busy = tick.isPending || untick.isPending || noShow.isPending;
52
+ return (_jsxs(Screen, { title: list.data?.class.title ?? "Class list", children: [_jsx(Button, { label: "Back to today's classes", quiet: true, onPress: onBack }), list.isPending ? (_jsx(Text, { tone: "muted", accessibilityRole: "progressbar", children: "Loading the list\u2026" })) : null, list.error ? _jsx(Text, { accessibilityRole: "alert", children: "We could not load the list." }) : null, message ? _jsx(Text, { accessibilityRole: "alert", children: message }) : null, list.data ? (_jsxs(View, { children: [_jsxs(Text, { children: [classTime(list.data.class), " \u00B7 ", list.data.class.booked, " booked of", " ", list.data.class.places ?? "unlimited"] }), _jsx(List, { empty: "Nobody is booked yet.", children: list.data.bookings
53
+ // Cancelled and moved bookings are not on the list a coach ticks.
54
+ .filter((b) => b.state && b.state in STATE_LABEL)
55
+ .map((booking) => {
56
+ const name = booking.person?.name ?? "No name";
57
+ return (_jsxs(View, { children: [_jsx(Row, { title: name, detail: `${STATE_LABEL[booking.state ?? ""]}${booking.markedBy ? ` (${booking.markedBy})` : ""}${booking.healthNote ? " · Health note" : ""}` }), booking.state === "booked" || booking.state === "no_show" ? (_jsx(Button, { label: `Tick ${name}`, busy: busy, onPress: () => say(tick.mutateAsync({ bookingId: booking.id })) })) : null, booking.state === "booked" ? (_jsx(Button, { label: `${name} did not come`, quiet: true, busy: busy, onPress: () => say(noShow.mutateAsync({ bookingId: booking.id })) })) : null, booking.state === "attended" ? (_jsx(Button, { label: `Untick ${name}`, quiet: true, busy: busy, onPress: () => say(untick.mutateAsync({ bookingId: booking.id })) })) : null] }, booking.id));
58
+ }) })] })) : null] }));
59
+ }
@@ -1,6 +1,11 @@
1
+ import { type AgreementsHooks } from "@composable-os/client";
1
2
  import type { BookingsHooks } from "@composable-os/pack-bookings/client";
2
3
  type Hooks = Pick<BookingsHooks, "useTimetable" | "useMyBookings" | "useBook" | "useCancel" | "useFamily">;
3
- export declare function TimetableScreen({ bookings }: {
4
+ /** The agreements hooks the inline step uses. */
5
+ export type TimetableAgreements = Pick<AgreementsHooks, "useDue" | "useAccept">;
6
+ export declare function TimetableScreen({ bookings, agreements, }: {
4
7
  bookings: Hooks;
8
+ /** For what must be agreed to first, shown inline. Without it, the refusal is only named. */
9
+ agreements?: TimetableAgreements;
5
10
  }): import("react").JSX.Element;
6
11
  export {};
@@ -1,12 +1,24 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // The timetable in the gym's app (stage 31a, decision 9): book and cancel with the membership or
3
+ // pass a member already holds. Nothing is bought here: a member with nothing to spend is told so
4
+ // by the host, and buys on the website. Whether booking is open, the places left and whether a
5
+ // cancel is in time are the host's answers, shown as they come. A parent the gym linked to a child
6
+ // picks who they are booking for (ADR 0540); the child's own membership or pass pays.
7
+ //
8
+ // What must be agreed to first (stage 44) is the host's answer too: when "Book" is refused with
9
+ // it, the documents are shown in the class's place (the native-agree block's step), agreed to one
10
+ // by one, and "Book" is tried again.
11
+ import { purchaseErrorWords, withAgreements, } from "@composable-os/client";
2
12
  import { bookingFor, classTime, placesLabel, refusalMessage, } from "@composable-os/pack-bookings/client";
3
13
  import { useState } from "react";
4
- import { Text, View } from "react-native";
14
+ import { View } from "react-native";
15
+ import { AgreementStepModal } from "../native-agree/agreement-step.js";
5
16
  import { Button } from "../native-base/button.js";
6
17
  import { List } from "../native-base/list.js";
7
18
  import { Row } from "../native-base/row.js";
8
19
  import { Screen } from "../native-base/screen.js";
9
- export function TimetableScreen({ bookings }) {
20
+ import { Text } from "../native-base/text.js";
21
+ export function TimetableScreen({ bookings, agreements, }) {
10
22
  const timetable = bookings.useTimetable({});
11
23
  const family = bookings.useFamily();
12
24
  const people = family.data?.people ?? [];
@@ -22,40 +34,88 @@ export function TimetableScreen({ bookings }) {
22
34
  return (_jsxs(Screen, { title: "Timetable", children: [people.length > 1 ? (_jsxs(View, { children: [_jsx(Text, { children: "Booking for" }), people.map((person) => (_jsx(Button, { label: person.self ? "Me" : person.name, quiet: (chosen?.personId ?? null) !== (person.self ? null : person.personId), onPress: () => {
23
35
  setPersonId(person.self ? null : person.personId);
24
36
  setMessage(null);
25
- } }, person.personId)))] })) : null, message ? _jsx(Text, { accessibilityRole: "alert", children: message }) : null, timetable.isPending ? (_jsx(Text, { accessibilityRole: "progressbar", children: "Loading the timetable\u2026" })) : null, timetable.error ? (_jsx(Text, { accessibilityRole: "alert", children: "We could not load the timetable." })) : null, timetable.data ? (_jsx(List, { empty: "No classes in these days.", children: timetable.data.classes.map((klass) => (_jsx(ClassItem, { klass: klass, booking: bookingFor(klass.id, mine.data?.bookings), forPerson: chosen ? { id: chosen.personId, name: chosen.name } : null, bookings: bookings, onDone: done }, klass.id))) })) : null] }));
37
+ } }, person.personId)))] })) : null, message ? _jsx(Text, { accessibilityRole: "alert", children: message }) : null, timetable.isPending ? (_jsx(Text, { tone: "muted", accessibilityRole: "progressbar", children: "Loading the timetable\u2026" })) : null, timetable.error ? (_jsx(Text, { accessibilityRole: "alert", children: "We could not load the timetable." })) : null, timetable.data ? (_jsx(List, { empty: "No classes in these days.", children: timetable.data.classes.map((klass) => (_jsx(ClassItem
38
+ // One item per person and class: a cancel started for one person is never confirmed
39
+ // for another after "Booking for" changes (as the web timetable).
40
+ , { klass: klass, booking: bookingFor(klass.id, mine.data?.bookings), forPerson: chosen ? { id: chosen.personId, name: chosen.name } : null, bookings: bookings, agreements: agreements, onDone: done }, `${chosen?.personId ?? "self"}:${klass.id}`))) })) : null] }));
26
41
  }
27
- function ClassItem({ klass, booking, forPerson, bookings, onDone, }) {
42
+ /** When a cancel fails with no answer to show (offline, the host down): it can be tried again. */
43
+ const CANCEL_FAILED = "We could not cancel that just now. Check the connection and try again.";
44
+ function ClassItem({ klass, booking, forPerson, bookings, agreements, onDone, }) {
28
45
  const book = bookings.useBook();
29
46
  const cancel = bookings.useCancel();
30
47
  const [preview, setPreview] = useState(null);
48
+ // What the host said must be agreed to first.
49
+ const [due, setDue] = useState(null);
31
50
  const name = klass.title ?? "Class";
32
- return (_jsxs(View, { children: [_jsx(Row, { title: name, detail: [classTime(klass), klass.coach?.name, placesLabel(klass.places)]
51
+ /** Books; `said` is what agreeing just now told them (where the copy went), said first. */
52
+ async function bookIt(said = null) {
53
+ const first = said ? `${said} ` : "";
54
+ let outcome;
55
+ try {
56
+ outcome = await withAgreements(() => book.mutateAsync({
57
+ classId: klass.id,
58
+ ...(forPerson ? { forPersonId: forPerson.id } : {}),
59
+ }));
60
+ }
61
+ catch (error) {
62
+ onDone(`${first}${purchaseErrorWords(error) ?? "We could not book that just now. Try again soon."}`);
63
+ return;
64
+ }
65
+ if (outcome.kind === "due") {
66
+ if (agreements)
67
+ setDue(outcome.documents);
68
+ else {
69
+ onDone(`Something must be agreed to first: ${outcome.documents.map((d) => d.title).join(", ")}.`);
70
+ }
71
+ return;
72
+ }
73
+ if (outcome.kind === "minor_payer") {
74
+ onDone(outcome.message);
75
+ return;
76
+ }
77
+ const result = outcome.value;
78
+ onDone(first +
79
+ (result.result === "booked"
80
+ ? `Booked${forPerson ? ` for ${forPerson.name}` : ""}: ${name}, ${classTime(klass)}.`
81
+ : result.result === "waiting"
82
+ ? `${name}: ${result.message}`
83
+ : refusalMessage(result, klass.timeZone)));
84
+ }
85
+ return (_jsxs(View, { children: [due && agreements ? (_jsx(AgreementStepModal, { agreements: agreements, documents: due, continueLabel: "Agree and book", onCancel: () => setDue(null), onAgreed: (said) => {
86
+ setDue(null);
87
+ void bookIt(said);
88
+ } })) : null, _jsx(Row, { title: name, detail: [classTime(klass), klass.coach?.name, placesLabel(klass.places)]
33
89
  .filter(Boolean)
34
90
  .join(" · ") }), booking && preview ? (_jsxs(View, { children: [_jsx(Text, { children: preview.message }), _jsx(Button, { label: "Confirm cancel", busy: cancel.isPending, onPress: async () => {
35
- const result = await cancel.mutateAsync({
36
- bookingId: booking.id,
37
- confirm: true,
38
- expect: preview.state,
39
- });
91
+ let result;
92
+ try {
93
+ result = await cancel.mutateAsync({
94
+ bookingId: booking.id,
95
+ confirm: true,
96
+ expect: preview.state,
97
+ });
98
+ }
99
+ catch {
100
+ onDone(CANCEL_FAILED);
101
+ return;
102
+ }
40
103
  setPreview(null);
41
104
  onDone(result.result === "refused"
42
105
  ? refusalMessage(result, klass.timeZone)
43
106
  : `${name}: ${result.message}`);
44
107
  } }), _jsx(Button, { label: "Keep it", quiet: true, onPress: () => setPreview(null) })] })) : booking ? (_jsx(Button, { label: "Cancel", quiet: true, busy: cancel.isPending, onPress: async () => {
45
- const asked = await cancel.mutateAsync({ bookingId: booking.id });
108
+ let asked;
109
+ try {
110
+ asked = await cancel.mutateAsync({ bookingId: booking.id });
111
+ }
112
+ catch {
113
+ onDone(CANCEL_FAILED);
114
+ return;
115
+ }
46
116
  if (asked.result === "refused")
47
117
  onDone(refusalMessage(asked, klass.timeZone));
48
118
  else
49
119
  setPreview({ state: asked.state, message: asked.message });
50
- } })) : (_jsx(Button, { label: "Book", busy: book.isPending, onPress: async () => {
51
- const result = await book.mutateAsync({
52
- classId: klass.id,
53
- ...(forPerson ? { forPersonId: forPerson.id } : {}),
54
- });
55
- onDone(result.result === "booked"
56
- ? `Booked${forPerson ? ` for ${forPerson.name}` : ""}: ${name}, ${classTime(klass)}.`
57
- : result.result === "waiting"
58
- ? `${name}: ${result.message}`
59
- : refusalMessage(result, klass.timeZone));
60
- } }))] }));
120
+ } })) : (_jsx(Button, { label: "Book", busy: book.isPending, onPress: () => void bookIt() }))] }));
61
121
  }
@@ -1,7 +1,12 @@
1
+ import { type AgreementsHooks } from "@composable-os/client";
1
2
  import type { BookingsHooks } from "@composable-os/pack-bookings/client";
3
+ /** The agreements hooks the inline step uses. */
4
+ export type TimetableAgreements = Pick<AgreementsHooks, "useDue" | "useAccept">;
2
5
  type Hooks = Pick<BookingsHooks, "useTimetable" | "useMyBookings" | "useBook" | "useCancel" | "useFamily" | "useBuy">;
3
- export declare function Timetable({ bookings, from, to, payReturnUrl, }: {
6
+ export declare function Timetable({ bookings, agreements, from, to, payReturnUrl, }: {
4
7
  bookings: Hooks;
8
+ /** For what must be agreed to first, shown inline. Without it, the refusal is only named. */
9
+ agreements?: TimetableAgreements;
5
10
  /** The first moment shown (ISO 8601); the host's default is now. */
6
11
  from?: string;
7
12
  to?: string;
@@ -1,8 +1,21 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // The timetable for a gym's site (stage 31a): the week's classes, and for a signed-in member a
3
+ // Book or Cancel button on each. A parent the gym linked to a child picks who they are booking
4
+ // for (ADR 0540); the child's own membership or pass pays. A member with nothing to spend on a
5
+ // class may pay for it instead (a drop-in, when the app passes `payReturnUrl`; web only). Whether
6
+ // booking is open, whether the gym books a class, the places left, whether a cancel is in time
7
+ // and what it spends are the host's: this shows its answers.
8
+ //
9
+ // What must be agreed to first (stage 44: a waiver, a health form, a new version to agree to
10
+ // again) is the host's answer too: when "Book" or "Pay" is refused with it, the documents are shown
11
+ // inline in the class's row (the web-agree block's step), then it is tried again. A child's are
12
+ // agreed to by the parent booking for them; a member a parent agrees for is told so.
13
+ import { forgetAfter, purchaseErrorWords, withAgreements, } from "@composable-os/client";
2
14
  import { bookingFor, classTime, placesLabel, refusalMessage, } from "@composable-os/pack-bookings/client";
3
15
  import { useState } from "react";
4
16
  import { Button } from "../ui-button/button.js";
5
- export function Timetable({ bookings, from, to, payReturnUrl, }) {
17
+ import { MemberAgreementStep } from "../web-agree/agreement-step.js";
18
+ export function Timetable({ bookings, agreements, from, to, payReturnUrl, }) {
6
19
  const timetable = bookings.useTimetable({ ...(from ? { from } : {}), ...(to ? { to } : {}) });
7
20
  const family = bookings.useFamily();
8
21
  const people = family.data?.people ?? [];
@@ -23,19 +36,85 @@ export function Timetable({ bookings, from, to, payReturnUrl, }) {
23
36
  return (_jsxs("section", { className: "grid gap-4", "aria-label": "Timetable", children: [people.length > 1 ? (_jsxs("label", { className: "grid gap-1 text-sm", children: ["Booking for", _jsx("select", { className: "rounded-md border p-2", value: chosen?.personId ?? "", onChange: (event) => {
24
37
  setPersonId(event.target.value || null);
25
38
  setMessage(null);
26
- }, children: people.map((person) => (_jsx("option", { value: person.self ? "" : person.personId, children: person.self ? "Me" : person.name }, person.personId))) })] })) : null, message ? _jsx("p", { role: "status", children: message }) : null, timetable.data.classes.length === 0 ? _jsx("p", { children: "No classes in these days." }) : null, _jsx("ul", { className: "grid gap-2", children: timetable.data.classes.map((klass) => (_jsx(ClassRow, { klass: klass, booking: bookingFor(klass.id, mine.data?.bookings), forPerson: chosen ? { id: chosen.personId, name: chosen.name } : null, bookings: bookings, payReturnUrl: payReturnUrl, onDone: (text) => {
39
+ }, children: people.map((person) => (_jsx("option", { value: person.self ? "" : person.personId, children: person.self ? "Me" : person.name }, person.personId))) })] })) : null, message ? _jsx("p", { role: "status", children: message }) : null, timetable.data.classes.length === 0 ? _jsx("p", { children: "No classes in these days." }) : null, _jsx("ul", { className: "grid gap-2", children: timetable.data.classes.map((klass) => (_jsx(ClassRow
40
+ // One row per person and class: a cancel previewed for one person is never confirmed
41
+ // for another after "Booking for" changes.
42
+ , { klass: klass, booking: bookingFor(klass.id, mine.data?.bookings), forPerson: chosen ? { id: chosen.personId, name: chosen.name } : null, bookings: bookings, agreements: agreements, payReturnUrl: payReturnUrl, onDone: (text) => {
27
43
  setMessage(text);
28
44
  refresh();
29
- } }, klass.id))) })] }));
45
+ } }, `${chosen?.personId ?? "self"}:${klass.id}`))) })] }));
30
46
  }
31
- function ClassRow({ klass, booking, forPerson, bookings, onDone, payReturnUrl, }) {
47
+ function ClassRow({ klass, booking, forPerson, bookings, agreements, onDone, payReturnUrl, }) {
32
48
  const book = bookings.useBook();
33
49
  const cancel = bookings.useCancel();
34
50
  const buy = bookings.useBuy();
35
51
  // The host said nothing they hold can pay for it: they may pay for this one class.
36
52
  const [canPay, setCanPay] = useState(false);
37
53
  const [preview, setPreview] = useState(null);
54
+ // What the host said must be agreed to first, and whether booking or paying was refused.
55
+ const [due, setDue] = useState(null);
38
56
  const name = klass.title ?? "Class";
57
+ const forWhom = forPerson ? { forPersonId: forPerson.id } : {};
58
+ /** What a refusal for agreements becomes: the inline step, or the host's words. */
59
+ function agreementsRefused(outcome, then) {
60
+ if (outcome.kind === "due") {
61
+ if (agreements)
62
+ setDue({ documents: outcome.documents, then });
63
+ else {
64
+ onDone(`Something must be agreed to first: ${outcome.documents.map((d) => d.title).join(", ")}.`);
65
+ }
66
+ return true;
67
+ }
68
+ if (outcome.kind === "minor_payer") {
69
+ onDone(outcome.message);
70
+ return true;
71
+ }
72
+ return false;
73
+ }
74
+ /** Books; `said` is what agreeing just now told them (where the copy went), said first. */
75
+ async function bookIt(said = null) {
76
+ const first = said ? `${said} ` : "";
77
+ let outcome;
78
+ try {
79
+ outcome = await withAgreements(() => book.mutateAsync({ classId: klass.id, ...forWhom }));
80
+ }
81
+ catch (error) {
82
+ onDone(`${first}${purchaseErrorWords(error) ?? "We could not book that just now. Try again soon."}`);
83
+ return;
84
+ }
85
+ if (agreementsRefused(outcome, "book") || outcome.kind !== "done")
86
+ return;
87
+ const done = outcome.value;
88
+ setCanPay(done.result === "refused" && done.reason === "no_use");
89
+ onDone(first +
90
+ (done.result === "booked"
91
+ ? `Booked${forPerson ? ` for ${forPerson.name}` : ""}: ${name}, ${classTime(klass)}.`
92
+ : done.result === "waiting"
93
+ ? `${name}: ${done.message}`
94
+ : refusalMessage(done, klass.timeZone)));
95
+ }
96
+ async function pay(said = null) {
97
+ if (!payReturnUrl)
98
+ return;
99
+ const first = said ? `${said} ` : "";
100
+ let outcome;
101
+ try {
102
+ outcome = await withAgreements(() => forgetAfter(buy.start, { classId: klass.id, ...forWhom }));
103
+ }
104
+ catch (error) {
105
+ onDone(`${first}${purchaseErrorWords(error) ?? "We could not start the payment. Try again soon."}`);
106
+ return;
107
+ }
108
+ if (agreementsRefused(outcome, "pay") || outcome.kind !== "done")
109
+ return;
110
+ const held = outcome.value;
111
+ if (held.result === "refused") {
112
+ onDone(refusalMessage(held, klass.timeZone));
113
+ return;
114
+ }
115
+ const page = await buy.checkout.mutateAsync({ id: held.orderId, returnUrl: payReturnUrl });
116
+ window.location.assign(page.url);
117
+ }
39
118
  return (_jsxs("li", { className: "flex flex-wrap items-center justify-between gap-3 rounded-md border p-3", children: [_jsxs("div", { className: "grid gap-1", children: [_jsx("strong", { children: name }), _jsx("span", { children: classTime(klass) }), _jsx("span", { className: "text-muted-foreground text-sm", children: [klass.coach?.name, klass.room?.name, placesLabel(klass.places)]
40
119
  .filter(Boolean)
41
120
  .join(" · ") })] }), booking ? (preview ? (_jsxs("div", { className: "flex flex-wrap items-center gap-2", children: [_jsx("span", { children: preview.message }), _jsx(Button, { variant: "destructive", disabled: cancel.isPending, onClick: async () => {
@@ -54,30 +133,9 @@ function ClassRow({ klass, booking, forPerson, bookings, onDone, payReturnUrl, }
54
133
  onDone(refusalMessage(asked, klass.timeZone));
55
134
  else
56
135
  setPreview({ state: asked.state, message: asked.message });
57
- }, children: ["Cancel ", name] }))) : canPay && payReturnUrl ? (_jsxs(Button, { disabled: buy.start.isPending || buy.checkout.isPending, onClick: async () => {
58
- const held = await buy.start.mutateAsync({
59
- classId: klass.id,
60
- ...(forPerson ? { forPersonId: forPerson.id } : {}),
61
- });
62
- if (held.result === "refused") {
63
- onDone(refusalMessage(held, klass.timeZone));
64
- return;
65
- }
66
- const page = await buy.checkout.mutateAsync({
67
- id: held.orderId,
68
- returnUrl: payReturnUrl,
69
- });
70
- window.location.assign(page.url);
71
- }, children: ["Pay for ", name] })) : (_jsxs(Button, { disabled: book.isPending, onClick: async () => {
72
- const done = await book.mutateAsync({
73
- classId: klass.id,
74
- ...(forPerson ? { forPersonId: forPerson.id } : {}),
75
- });
76
- setCanPay(done.result === "refused" && done.reason === "no_use");
77
- onDone(done.result === "booked"
78
- ? `Booked${forPerson ? ` for ${forPerson.name}` : ""}: ${name}, ${classTime(klass)}.`
79
- : done.result === "waiting"
80
- ? `${name}: ${done.message}`
81
- : refusalMessage(done, klass.timeZone));
82
- }, children: ["Book ", name] }))] }));
136
+ }, children: ["Cancel ", name] }))) : canPay && payReturnUrl ? (_jsxs(Button, { disabled: buy.start.isPending || buy.checkout.isPending, onClick: () => void pay(), children: ["Pay for ", name] })) : (_jsxs(Button, { disabled: book.isPending, onClick: () => void bookIt(), children: ["Book ", name] })), due && agreements ? (_jsx("div", { className: "basis-full", children: _jsx(MemberAgreementStep, { agreements: agreements, documents: due.documents, continueLabel: due.then === "pay" ? "Agree and pay" : "Agree and book", onCancel: () => setDue(null), onAgreed: (said) => {
137
+ const then = due.then;
138
+ setDue(null);
139
+ void (then === "pay" ? pay(said) : bookIt(said));
140
+ } }) })) : null] }));
83
141
  }