@composable-os/pack-bookings 0.0.0-stage → 0.1.0-beta.10

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.
@@ -0,0 +1,137 @@
1
+ // The Bookings pack's headless entry (stage 31a step 3; ADR 0418): functions and hooks over the
2
+ // typed client, no markup. A client app imports it as `@composable-os/pack-bookings/client` and
3
+ // hands it its generated client; the bookings blocks (web and native) render what it returns.
4
+ // Web and native share this logic, not their screens.
5
+ //
6
+ // As every pack's entry: the operations are typed from the pack's own tool schemas, it takes the
7
+ // client and the app's `useQuery` and `useMutation` as arguments (no React or TanStack import),
8
+ // and it holds no rules. When booking opens, whether a cancel is late, whether a class type is
9
+ // booked by the gym, the places left and what a booking spends are the host's: a block shows the
10
+ // host's answer (`book`'s and `cancel`'s), never works one out.
11
+ import { packHooks } from "@composable-os/client";
12
+ // --- Plain functions (display only) --------------------------------------------------------------
13
+ /** A class's start and end in its own zone ("Tue 3 Mar, 06:00–07:00"), for display. */
14
+ export function classTime(klass, locale = "en-AU") {
15
+ if (!klass.startsAt)
16
+ return "";
17
+ const zone = klass.timeZone ?? undefined;
18
+ const day = new Intl.DateTimeFormat(locale, {
19
+ weekday: "short",
20
+ day: "numeric",
21
+ month: "short",
22
+ ...(zone ? { timeZone: zone } : {}),
23
+ }).format(new Date(klass.startsAt));
24
+ const time = (iso) => new Intl.DateTimeFormat(locale, {
25
+ hour: "2-digit",
26
+ minute: "2-digit",
27
+ hourCycle: "h23",
28
+ ...(zone ? { timeZone: zone } : {}),
29
+ }).format(new Date(iso));
30
+ return `${day}, ${time(klass.startsAt)}${klass.endsAt ? `–${time(klass.endsAt)}` : ""}`;
31
+ }
32
+ /** What a class's places say ("5 places left", "Full"), from the host's numbers. */
33
+ export function placesLabel(places) {
34
+ if (places.status === "sold_out")
35
+ return "Full";
36
+ if (places.available === undefined || places.available === null)
37
+ return "Places available";
38
+ return places.available === 1 ? "1 place left" : `${places.available} places left`;
39
+ }
40
+ /** The message to show for a refusal the host gave (with when booking opens, if it said). */
41
+ export function refusalMessage(result, zone, locale = "en-AU") {
42
+ if (result.reason === "not_open" && result.opensAt) {
43
+ const when = new Intl.DateTimeFormat(locale, {
44
+ weekday: "short",
45
+ day: "numeric",
46
+ month: "short",
47
+ hour: "2-digit",
48
+ minute: "2-digit",
49
+ hourCycle: "h23",
50
+ ...(zone ? { timeZone: zone } : {}),
51
+ }).format(new Date(result.opensAt));
52
+ return `Booking opens ${when}.`;
53
+ }
54
+ return result.message;
55
+ }
56
+ /** The member's live booking of a class, if any (to show Cancel instead of Book). */
57
+ export function bookingFor(classId, bookings) {
58
+ return bookings?.find((b) => b.classId === classId && b.state === "booked");
59
+ }
60
+ /**
61
+ * The operations each hook calls (ADR 0453). A customer's client gets the member's hooks (the
62
+ * timetable, book, cancel, mine), the public's only the timetable and a class, the staff client
63
+ * all of them.
64
+ */
65
+ export const BOOKINGS_HOOKS_CALL = {
66
+ useTimetable: ["x.bookings.timetable"],
67
+ useClass: ["x.bookings.classGet"],
68
+ useMyBookings: ["x.bookings.mine"],
69
+ useBook: ["x.bookings.book"],
70
+ useCancel: ["x.bookings.cancel"],
71
+ useDay: ["x.bookings.day"],
72
+ useClassList: ["x.bookings.classList"],
73
+ useBookFor: ["x.bookings.bookFor"],
74
+ useCancelFor: ["x.bookings.cancelFor"],
75
+ useReturnUse: ["x.bookings.returnUse"],
76
+ useFindPeople: ["x.bookings.findPeople"],
77
+ };
78
+ /**
79
+ * Hooks for a React or React Native app:
80
+ * `const bookings = createBookingsHooks({ useQuery, useMutation }, api)`. `api` is any generated
81
+ * client: the customer's or the staff one. The hooks it cannot back are not there.
82
+ */
83
+ export function createBookingsHooks(react, client) {
84
+ return packHooks(client, BOOKINGS_HOOKS_CALL, (api) => allBookingsHooks(react, api));
85
+ }
86
+ function allBookingsHooks(react, api) {
87
+ const { useQuery, useMutation } = react;
88
+ return {
89
+ /** The timetable between two times (default: the next 7 days). */
90
+ useTimetable(input = {}) {
91
+ return useQuery(api.query("x.bookings.timetable", input));
92
+ },
93
+ /** One scheduled class. */
94
+ useClass(input) {
95
+ return useQuery(api.query("x.bookings.classGet", input));
96
+ },
97
+ /** The signed-in member's own bookings. */
98
+ useMyBookings(input = {}) {
99
+ return useQuery(api.query("x.bookings.mine", input));
100
+ },
101
+ /** Books the member into a class; the host answers booked or why not. */
102
+ useBook() {
103
+ return useMutation(api.mutation("x.bookings.book"));
104
+ },
105
+ /** Cancels a booking: first the host's preview (in time or late), then with confirm. */
106
+ useCancel() {
107
+ return useMutation(api.mutation("x.bookings.cancel"));
108
+ },
109
+ /** Staff: one local date's classes. */
110
+ useDay(input) {
111
+ return useQuery(api.query("x.bookings.day", input));
112
+ },
113
+ /** Staff: a class's list. */
114
+ useClassList(input) {
115
+ return useQuery(api.query("x.bookings.classList", input));
116
+ },
117
+ /** Staff: book anyone, a walk-in included. */
118
+ useBookFor() {
119
+ return useMutation(api.mutation("x.bookings.bookFor"));
120
+ },
121
+ /** Staff: cancel a booking, in time or late by the host's clock. */
122
+ useCancelFor() {
123
+ return useMutation(api.mutation("x.bookings.cancelFor"));
124
+ },
125
+ /** A person on the staff: give a booking's use back after the late point (destructive). */
126
+ useReturnUse() {
127
+ return useMutation(api.mutation("x.bookings.returnUse"));
128
+ },
129
+ /** Staff: People on file matching a name or email (at least two letters). */
130
+ useFindPeople(input) {
131
+ return useQuery({
132
+ ...api.query("x.bookings.findPeople", input),
133
+ enabled: input.query.trim().length >= 2,
134
+ });
135
+ },
136
+ };
137
+ }