@composable-os/pack-bookings 0.1.0-beta.10 → 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,10 +1,28 @@
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, }) {
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
- const mine = bookings.useMyBookings({});
20
+ const family = bookings.useFamily();
21
+ const people = family.data?.people ?? [];
22
+ // Whose bookings are shown and made: the member's own unless they pick a child.
23
+ const [personId, setPersonId] = useState(null);
24
+ const chosen = people.find((p) => p.personId === personId && !p.self);
25
+ const mine = bookings.useMyBookings(chosen ? { personId: chosen.personId } : {});
8
26
  const [message, setMessage] = useState(null);
9
27
  if (timetable.isPending)
10
28
  return _jsx("p", { "aria-busy": "true", children: "Loading the timetable\u2026" });
@@ -15,16 +33,88 @@ export function Timetable({ bookings, from, to, }) {
15
33
  timetable.refetch?.();
16
34
  mine.refetch?.();
17
35
  };
18
- return (_jsxs("section", { className: "grid gap-4", "aria-label": "Timetable", children: [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), bookings: bookings, onDone: (text) => {
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) => {
37
+ setPersonId(event.target.value || null);
38
+ setMessage(null);
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) => {
19
43
  setMessage(text);
20
44
  refresh();
21
- } }, klass.id))) })] }));
45
+ } }, `${chosen?.personId ?? "self"}:${klass.id}`))) })] }));
22
46
  }
23
- function ClassRow({ klass, booking, bookings, onDone, }) {
47
+ function ClassRow({ klass, booking, forPerson, bookings, agreements, onDone, payReturnUrl, }) {
24
48
  const book = bookings.useBook();
25
49
  const cancel = bookings.useCancel();
50
+ const buy = bookings.useBuy();
51
+ // The host said nothing they hold can pay for it: they may pay for this one class.
52
+ const [canPay, setCanPay] = useState(false);
26
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);
27
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
+ }
28
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)]
29
119
  .filter(Boolean)
30
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 () => {
@@ -43,10 +133,9 @@ function ClassRow({ klass, booking, bookings, onDone, }) {
43
133
  onDone(refusalMessage(asked, klass.timeZone));
44
134
  else
45
135
  setPreview({ state: asked.state, message: asked.message });
46
- }, children: ["Cancel ", name] }))) : (_jsxs(Button, { disabled: book.isPending, onClick: async () => {
47
- const done = await book.mutateAsync({ classId: klass.id });
48
- onDone(done.result === "booked"
49
- ? `Booked: ${name}, ${classTime(klass)}.`
50
- : refusalMessage(done, klass.timeZone));
51
- }, 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] }));
52
141
  }