bookito-widget 0.42.16-beta.2 → 0.42.16-beta.21

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,266 +0,0 @@
1
- import {
2
- LocalizedEventData,
3
- findOccurrenceForDate,
4
- isEventBookableInAdvance,
5
- } from '@carlito/bookito-client';
6
- import { applyTimeOfDate } from '@carlito/date-utils';
7
- import { useEffect, useMemo, useState } from 'react';
8
-
9
- export function useBookableTimeRange(
10
- event: Pick<LocalizedEventData, 'timeBookability' | 'timeBookabilitySettings' | 'occurrences'>,
11
- onDate: Date
12
- ) {
13
- const canChooseTime = event.timeBookability === 'custom';
14
-
15
- // Update it every 5 seconds
16
- const [nowDate, setNowDate] = useState(new Date());
17
-
18
- useEffect(() => {
19
- const interval = setInterval(() => {
20
- setNowDate(new Date());
21
- }, 1000 * 5);
22
-
23
- return () => {
24
- clearInterval(interval);
25
- };
26
- }, []);
27
-
28
- const bookableTimeRange = useMemo(() => {
29
- if (!canChooseTime) {
30
- return undefined;
31
- }
32
-
33
- const occurrence = findOccurrenceForDate(onDate, event);
34
-
35
- if (!occurrence) {
36
- console.warn('Could not find occurrence for date', onDate);
37
- return undefined;
38
- }
39
-
40
- const startDateTime = applyTimeOfDate(onDate, new Date(occurrence.startDateTime));
41
- const endDateTime = new Date(startDateTime);
42
- endDateTime.setTime(endDateTime.getTime() + occurrence.durationMs);
43
-
44
- if (event.timeBookability === 'custom' && event.timeBookabilitySettings) {
45
- const { startTimeIncrementMs = 0, timeBlockDurationMs = 0 } = event.timeBookabilitySettings;
46
-
47
- const earliestPossibleBookable = startDateTime.getTime() - (startTimeIncrementMs ?? 0);
48
- const isPastStart = nowDate.getTime() > earliestPossibleBookable;
49
- const startBookingTime = isPastStart
50
- ? nowDate.getTime() + (startTimeIncrementMs ?? 0)
51
- : startDateTime.getTime();
52
-
53
- const safePadding = isPastStart ? 1000 * 60 * 5 : 0; // 5 minutes
54
-
55
- startDateTime.setTime(startBookingTime + safePadding);
56
- endDateTime.setTime(
57
- endDateTime.getTime() - (timeBlockDurationMs ?? 0) - (startTimeIncrementMs ?? 0)
58
- );
59
- }
60
-
61
- return {
62
- from: startDateTime,
63
- to: endDateTime,
64
- };
65
- }, [canChooseTime, onDate, event, nowDate]);
66
-
67
- return bookableTimeRange;
68
- }
69
-
70
- export function isOccurrenceAllDay(occurrence: LocalizedEventData['occurrences'][0]) {
71
- // const start = new Date(occurrence.startDateTime);
72
-
73
- return (
74
- // start.getHours() === 0 &&
75
- // start.getMinutes() === 0 &&
76
- occurrence.durationMs === 24 * 60 * 60 * 1000
77
- );
78
- }
79
-
80
- export function isEventAllDay(onDate: Date, event: Pick<LocalizedEventData, 'occurrences'>) {
81
- const occurrence = findOccurrenceForDate(onDate, event);
82
- if (!occurrence) {
83
- console.warn('No occurrence found for date', onDate, event);
84
- return false;
85
- }
86
- return isOccurrenceAllDay(occurrence);
87
- }
88
-
89
- type DerivedEventCancellationStatus =
90
- | {
91
- isCancelled: true;
92
- globallyCancelled: boolean;
93
- status: NonNullable<LocalizedEventData['cancellationStatus']>['status'];
94
- reason?: string;
95
- }
96
- | {
97
- isCancelled: false;
98
- status: undefined;
99
- reason: undefined;
100
- globallyCancelled: false;
101
- };
102
-
103
- export function isEventCancelled(
104
- onDate?: Date,
105
- event?: Pick<LocalizedEventData, 'cancellationStatus' | 'cancelledDates'>
106
- ): DerivedEventCancellationStatus {
107
- if (!event) {
108
- return {
109
- isCancelled: false,
110
- reason: undefined,
111
- status: undefined,
112
- globallyCancelled: false,
113
- };
114
- }
115
-
116
- let cancellationStatus = event?.cancellationStatus?.status;
117
- let reason = event?.cancellationStatus?.reason;
118
- const isCancelledOnDate =
119
- onDate &&
120
- event.cancelledDates?.find((cancelledDate) => {
121
- const date = new Date(cancelledDate.date);
122
- const isSameDay =
123
- date.getDate() === onDate.getDate() &&
124
- date.getMonth() === onDate.getMonth() &&
125
- date.getFullYear() === onDate.getFullYear();
126
-
127
- return (
128
- isSameDay && (cancelledDate.status === 'cancelled' || cancelledDate.status === 'deleted')
129
- );
130
- });
131
-
132
- if (isCancelledOnDate) {
133
- cancellationStatus = isCancelledOnDate.status;
134
- reason = isCancelledOnDate.reason;
135
- }
136
-
137
- const isCancelled = Boolean(
138
- cancellationStatus === 'cancelled' || cancellationStatus === 'deleted' || isCancelledOnDate
139
- );
140
-
141
- if (isCancelled) {
142
- return {
143
- globallyCancelled: Boolean(event?.cancellationStatus),
144
- isCancelled: true,
145
- status: cancellationStatus as NonNullable<typeof cancellationStatus>,
146
- reason,
147
- };
148
- }
149
-
150
- return {
151
- isCancelled: false,
152
- status: undefined,
153
- reason: undefined,
154
- globallyCancelled: false,
155
- };
156
- }
157
-
158
- type IsEventBookable = {
159
- onDate: Date;
160
- event: Pick<
161
- LocalizedEventData,
162
- | 'bookability'
163
- | 'bookableUntilBeforeStartMs'
164
- | 'maxParticipants'
165
- | 'cancellationStatus'
166
- | 'cancelledDates'
167
- | 'timeBookability'
168
- | 'timeBookabilitySettings'
169
- | 'occurrences'
170
- >;
171
- numParticipants?: number;
172
- settings?: {
173
- daysBookableInAdvance?: number | null;
174
- };
175
- };
176
-
177
- export function isEventBookable({
178
- onDate,
179
- event,
180
- settings,
181
- numParticipants = 0,
182
- }: Partial<IsEventBookable>) {
183
- if (!event || !onDate) {
184
- return {
185
- isBookable: false,
186
- };
187
- }
188
-
189
- if (
190
- !isEventBookableInAdvance({
191
- onDate,
192
- daysBookableInAdvance: settings?.daysBookableInAdvance,
193
- })
194
- ) {
195
- return {
196
- isBookable: false,
197
- };
198
- }
199
-
200
- const occurrence = findOccurrenceForDate(onDate, event);
201
-
202
- if (!occurrence) {
203
- console.warn('No occurrence found for date', onDate);
204
- return {
205
- isBookable: false,
206
- };
207
- }
208
-
209
- let stillBookable = true;
210
-
211
- const bookableUntilBeforeStartMs = event.bookableUntilBeforeStartMs;
212
- if (bookableUntilBeforeStartMs !== undefined && bookableUntilBeforeStartMs !== null) {
213
- stillBookable = onDate.getTime() - bookableUntilBeforeStartMs > Date.now();
214
- console.log({
215
- stillBookable,
216
- onDate,
217
- bookableUntilBeforeStartMs,
218
- aa: Date.now(),
219
- ss: onDate.getTime() - bookableUntilBeforeStartMs > Date.now(),
220
- });
221
- }
222
-
223
- let timeBookabilityPass = true;
224
-
225
- const eventStartDateTime = applyTimeOfDate(onDate, new Date(occurrence.startDateTime));
226
- const eventEndDateTime = eventStartDateTime.getTime() + occurrence.durationMs;
227
-
228
- if (eventEndDateTime > Date.now()) {
229
- stillBookable = true;
230
- } else {
231
- stillBookable = false;
232
- }
233
-
234
- console.log({
235
- stillBookable,
236
- eventEndDateTime,
237
- eventStartDateTime,
238
- aaa: occurrence.durationMs,
239
- aa: eventEndDateTime < Date.now(),
240
- });
241
-
242
- if (event.timeBookability === 'custom' && event.timeBookabilitySettings) {
243
- const { startTimeIncrementMs = 0, timeBlockDurationMs = 0 } = event.timeBookabilitySettings;
244
- const canBookUntil =
245
- eventEndDateTime - (startTimeIncrementMs ?? 0) - (timeBlockDurationMs ?? 0);
246
- const canStillBook = Date.now() < canBookUntil;
247
-
248
- timeBookabilityPass = canStillBook;
249
- }
250
-
251
- const hasBookability =
252
- event.bookability === 'bookable_autonomously' || event.bookability === 'bookable_on_request';
253
-
254
- const { isCancelled, reason } = isEventCancelled(onDate, event);
255
-
256
- const participantsFull = event.maxParticipants ? numParticipants > event.maxParticipants : false;
257
- const isBookable = !isCancelled && stillBookable && hasBookability && timeBookabilityPass;
258
-
259
- return {
260
- isBookable,
261
- hasBookability,
262
- isCancelled,
263
- reason,
264
- participantsFull,
265
- };
266
- }
@@ -1 +0,0 @@
1
- export { BookingForm } from './BookingForm';