@wix/headless-bookings 0.0.15 → 0.0.16

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.
@@ -3,8 +3,8 @@
3
3
  * Provides low-level access to BookingService via render props and service setup
4
4
  */
5
5
  import React from 'react';
6
- import { type BookingServiceConfig, type BookingSelection } from '../../../services/booking/booking.js';
7
- import type { TimeSlot } from '@wix/auto_sdk_bookings_availability-time-slots';
6
+ import { type BookingServiceConfig, type BookingItem, type Location, type BookingServiceActions } from '../../../services/booking/booking.js';
7
+ import type { FormValues } from '@wix/form-public';
8
8
  /**
9
9
  * Props for Booking.Root component
10
10
  */
@@ -28,14 +28,22 @@ export interface RootProps {
28
28
  * ```
29
29
  */
30
30
  export declare function Root(props: RootProps): React.ReactNode;
31
+ /**
32
+ * Data exposed by Booking.Data component
33
+ * Mirrors BookingServiceConfig structure with actions
34
+ */
35
+ export interface BookingData {
36
+ bookingItems: BookingItem[];
37
+ location: Location | null;
38
+ formValues: FormValues | null;
39
+ numberOfParticipants: number;
40
+ actions: BookingServiceActions;
41
+ }
31
42
  /**
32
43
  * Props for Data render prop component
33
44
  */
34
45
  export interface DataProps {
35
- children: (data: {
36
- bookingSelections: BookingSelection[];
37
- selectedTimeSlot: TimeSlot | null;
38
- }) => React.ReactNode;
46
+ children: (data: BookingData) => React.ReactNode;
39
47
  }
40
48
  /**
41
49
  * Core component that provides access to booking data via render props.
@@ -45,9 +53,9 @@ export interface DataProps {
45
53
  * @example
46
54
  * ```tsx
47
55
  * <Booking.Data>
48
- * {({ bookingSelections }) => (
49
- * bookingSelections.length > 0 ? (
50
- * <div>{bookingSelections.map(s => s.service.name).join(', ')}</div>
56
+ * {({ bookingItems, location, formValues, numberOfParticipants, actions }) => (
57
+ * bookingItems.length > 0 ? (
58
+ * <div>{bookingItems.map(item => item.service.name).join(', ')}</div>
51
59
  * ) : (
52
60
  * <div>No services selected</div>
53
61
  * )
@@ -28,9 +28,9 @@ export function Root(props) {
28
28
  * @example
29
29
  * ```tsx
30
30
  * <Booking.Data>
31
- * {({ bookingSelections }) => (
32
- * bookingSelections.length > 0 ? (
33
- * <div>{bookingSelections.map(s => s.service.name).join(', ')}</div>
31
+ * {({ bookingItems, location, formValues, numberOfParticipants, actions }) => (
32
+ * bookingItems.length > 0 ? (
33
+ * <div>{bookingItems.map(item => item.service.name).join(', ')}</div>
34
34
  * ) : (
35
35
  * <div>No services selected</div>
36
36
  * )
@@ -41,10 +41,16 @@ export function Root(props) {
41
41
  export function Data(props) {
42
42
  const { children } = props;
43
43
  const bookingService = useService(BookingServiceDefinition);
44
- const bookingSelections = bookingService.bookingSelections.get();
45
- const selectedTimeSlot = bookingService.timeSlot.get();
44
+ const bookingItems = bookingService.bookingItems.get();
45
+ const location = bookingService.location.get();
46
+ const formValues = bookingService.formValues.get();
47
+ const numberOfParticipants = bookingService.numberOfParticipants.get();
48
+ const { actions } = bookingService;
46
49
  return children({
47
- bookingSelections,
48
- selectedTimeSlot,
50
+ bookingItems,
51
+ location,
52
+ formValues,
53
+ numberOfParticipants,
54
+ actions,
49
55
  });
50
56
  }
@@ -5,19 +5,31 @@
5
5
  */
6
6
  import { type Signal } from '@wix/services-definitions/core-services/signals';
7
7
  import type { Service } from '@wix/auto_sdk_bookings_services';
8
- import type { TimeSlot, Resource, NestedTimeSlot } from '@wix/auto_sdk_bookings_availability-time-slots';
8
+ import type { Resource, EventInfo } from '@wix/auto_sdk_bookings_availability-time-slots';
9
9
  import type { FormValues } from '@wix/form-public';
10
10
  /**
11
- * BookingSelection contains the service AND its related selections
11
+ * Minimal time slot data for booking items
12
12
  */
13
- export interface BookingSelection {
13
+ export interface BookingItemTimeSlot {
14
+ startDate: string;
15
+ endDate: string;
16
+ totalCapacity?: number;
17
+ remainingCapacity?: number;
18
+ scheduleId?: string;
19
+ eventInfo?: EventInfo;
20
+ }
21
+ /**
22
+ * BookingItem contains the service AND its related selections
23
+ */
24
+ export interface BookingItem {
14
25
  /** Unique identifier for this booking instance (allows same service multiple times) */
15
26
  instanceId: string;
27
+ /** The selected service */
16
28
  service: Service;
17
29
  /** Selected resources (staff members, rooms, etc.) */
18
- staff?: Resource;
19
- /** Per-service slot data from nested slots (for multi-service bookings) */
20
- nestedSlotData?: NestedTimeSlot;
30
+ resources: Resource[];
31
+ /** Per-item time slot data */
32
+ timeSlot?: BookingItemTimeSlot;
21
33
  }
22
34
  export interface Location {
23
35
  id: string;
@@ -27,21 +39,25 @@ export interface Location {
27
39
  * BookingService actions - setters only, no API calls
28
40
  */
29
41
  export interface BookingServiceActions {
30
- /** Clears all booking selections and sets a single service (instanceId is auto-generated) */
31
- setService(service: Omit<BookingSelection, 'instanceId'>): string;
32
- /** Adds a service and returns the generated instanceId */
33
- addService(service: Omit<BookingSelection, 'instanceId'>): string;
34
- /** Updates a booking selection. If no instanceId provided, updates the first selection. */
35
- updateBookingSelection(updates: Partial<Omit<BookingSelection, 'instanceId'>>, instanceId?: string): void;
36
- /** Removes a booking selection. If no instanceId provided, removes the first selection. */
37
- removeBookingSelection(instanceId?: string): void;
38
- clearServices(): void;
39
- setTimeSlot(slot: TimeSlot | null): void;
40
- clearTimeSlot(): void;
42
+ /** Clears all booking items and sets a single item (instanceId is auto-generated) */
43
+ setItem(item: Omit<BookingItem, 'instanceId'>): string;
44
+ /** Adds an item and returns the generated instanceId */
45
+ addItem(item: Omit<BookingItem, 'instanceId'>): string;
46
+ /** Updates a booking item. If no instanceId provided, updates the first item. */
47
+ updateItem(updates: Partial<Omit<BookingItem, 'instanceId'>>, instanceId?: string): void;
48
+ /** Removes a booking item. If no instanceId provided, removes the first item. */
49
+ removeItem(instanceId?: string): void;
50
+ /** Clears all booking items */
51
+ clearItems(): void;
52
+ /** Sets a resource by ID within a booking item. If no instanceId provided, uses the first item. */
53
+ setResourceById(resourceId: string, resource: Resource, instanceId?: string): void;
54
+ /** Gets a resource by ID from a booking item. If no instanceId provided, uses the first item. */
55
+ getResourceById(resourceId: string, instanceId?: string): Resource | undefined;
41
56
  setLocation(location: Location | null): void;
42
57
  clearLocation(): void;
43
58
  setFormValues(values: FormValues | null): void;
44
59
  clearFormValues(): void;
60
+ setNumberOfParticipants(count: number): void;
45
61
  clearAll(): void;
46
62
  }
47
63
  /**
@@ -49,14 +65,14 @@ export interface BookingServiceActions {
49
65
  * Pure state management - no API calls
50
66
  */
51
67
  export interface BookingService {
52
- /** Signal containing the booking selections (services with their related data) */
53
- bookingSelections: Signal<BookingSelection[]>;
54
- /** Signal containing the currently selected time slot */
55
- timeSlot: Signal<TimeSlot | null>;
56
- /** Signal containing the selected location (derived from timeSlot) */
68
+ /** Signal containing the booking items (services with their related data) */
69
+ bookingItems: Signal<BookingItem[]>;
70
+ /** Signal containing the selected location */
57
71
  location: Signal<Location | null>;
58
72
  /** Signal containing the form values */
59
73
  formValues: Signal<FormValues | null>;
74
+ /** Signal containing the number of participants */
75
+ numberOfParticipants: Signal<number>;
60
76
  /** Actions for modifying booking state */
61
77
  actions: BookingServiceActions;
62
78
  }
@@ -70,15 +86,17 @@ export declare const BookingServiceDefinition: string & {
70
86
  } & BookingService;
71
87
  /**
72
88
  * Configuration interface for BookingService
73
- * Note: location is NOT in config - it's derived internally from timeSlot
89
+ * Mirrors BookingService structure (without actions and Signal wrappers)
74
90
  */
75
91
  export interface BookingServiceConfig {
76
- /** Optional initial booking selections to pre-populate */
77
- initialBookingSelections?: BookingSelection[];
78
- /** Optional initial time slot to pre-populate */
79
- initialTimeSlot?: TimeSlot | null;
92
+ /** Optional initial booking items to pre-populate */
93
+ bookingItems?: BookingItem[];
94
+ /** Optional initial location */
95
+ location?: Location | null;
80
96
  /** Optional initial form values */
81
- initialFormValues?: FormValues | null;
97
+ formValues?: FormValues | null;
98
+ /** Optional initial number of participants (defaults to 1) */
99
+ numberOfParticipants?: number;
82
100
  }
83
101
  /**
84
102
  * Implementation of BookingService
@@ -98,8 +116,8 @@ export interface BookingServiceConfig {
98
116
  *
99
117
  * // In any page - access booking data
100
118
  * <Booking.Data>
101
- * {({ services, timeSlot, location, formValues, actions }) => (
102
- * <div>{services.map(s => s.service.name).join(', ')}</div>
119
+ * {({ bookingItems, location, formValues, actions }) => (
120
+ * <div>{bookingItems.map(item => item.service.name).join(', ')}</div>
103
121
  * )}
104
122
  * </Booking.Data>
105
123
  * ```
@@ -9,7 +9,7 @@ import { SignalsServiceDefinition, } from '@wix/services-definitions/core-servic
9
9
  // Types
10
10
  // ============================================================================
11
11
  /**
12
- * Generates a unique instance ID for service selections
12
+ * Generates a unique instance ID for booking items
13
13
  */
14
14
  function generateInstanceId() {
15
15
  return crypto.randomUUID();
@@ -39,8 +39,8 @@ export const BookingServiceDefinition = defineService('booking');
39
39
  *
40
40
  * // In any page - access booking data
41
41
  * <Booking.Data>
42
- * {({ services, timeSlot, location, formValues, actions }) => (
43
- * <div>{services.map(s => s.service.name).join(', ')}</div>
42
+ * {({ bookingItems, location, formValues, actions }) => (
43
+ * <div>{bookingItems.map(item => item.service.name).join(', ')}</div>
44
44
  * )}
45
45
  * </Booking.Data>
46
46
  * ```
@@ -48,67 +48,93 @@ export const BookingServiceDefinition = defineService('booking');
48
48
  export const BookingService = implementService.withConfig()(BookingServiceDefinition, ({ getService, config }) => {
49
49
  const signalsService = getService(SignalsServiceDefinition);
50
50
  // Initialize signals with config values
51
- // Ensure all initial booking selections have instanceIds
52
- const initialSelectionsWithIds = (config.initialBookingSelections || []).map((s) => ({
53
- ...s,
54
- instanceId: s.instanceId || generateInstanceId(),
51
+ // Ensure all initial booking items have instanceIds
52
+ const initialItemsWithIds = (config.bookingItems || []).map((item) => ({
53
+ ...item,
54
+ instanceId: item.instanceId || generateInstanceId(),
55
55
  }));
56
- const bookingSelectionsSignal = signalsService.signal(initialSelectionsWithIds);
57
- const timeSlotSignal = signalsService.signal(config.initialTimeSlot || null);
58
- // Location is derived internally - initialize as null
59
- const locationSignal = signalsService.signal(null);
60
- const formValuesSignal = signalsService.signal(config.initialFormValues || null);
56
+ const bookingItemsSignal = signalsService.signal(initialItemsWithIds);
57
+ const locationSignal = signalsService.signal(config.location || null);
58
+ const formValuesSignal = signalsService.signal(config.formValues || null);
59
+ const numberOfParticipantsSignal = signalsService.signal(config.numberOfParticipants ?? 1);
61
60
  // ========================================================================
62
61
  // Actions Implementation
63
62
  // ========================================================================
64
63
  const actions = {
65
- // Services actions
66
- setService(service) {
64
+ // Item management actions
65
+ setItem(item) {
67
66
  const instanceId = generateInstanceId();
68
- const newSelection = {
69
- ...service,
67
+ const newItem = {
68
+ ...item,
70
69
  instanceId,
71
70
  };
72
- bookingSelectionsSignal.set([newSelection]);
71
+ bookingItemsSignal.set([newItem]);
73
72
  return instanceId;
74
73
  },
75
- addService(service) {
74
+ addItem(item) {
76
75
  const instanceId = generateInstanceId();
77
- const current = bookingSelectionsSignal.get();
78
- const newSelection = {
79
- ...service,
76
+ const current = bookingItemsSignal.get();
77
+ const newItem = {
78
+ ...item,
80
79
  instanceId,
81
80
  };
82
- bookingSelectionsSignal.set([...current, newSelection]);
81
+ bookingItemsSignal.set([...current, newItem]);
83
82
  return instanceId;
84
83
  },
85
- updateBookingSelection(updates, instanceId) {
86
- const current = bookingSelectionsSignal.get();
87
- // If no instanceId provided, update the first selection (index 0)
84
+ updateItem(updates, instanceId) {
85
+ const current = bookingItemsSignal.get();
86
+ // If no instanceId provided, update the first item (index 0)
88
87
  const targetId = instanceId ?? current[0]?.instanceId;
89
- const updated = current.map((s) => s.instanceId === targetId ? { ...s, ...updates } : s);
90
- bookingSelectionsSignal.set(updated);
88
+ const updated = current.map((item) => item.instanceId === targetId ? { ...item, ...updates } : item);
89
+ bookingItemsSignal.set(updated);
91
90
  },
92
- removeBookingSelection(instanceId) {
93
- const current = bookingSelectionsSignal.get();
91
+ removeItem(instanceId) {
92
+ const current = bookingItemsSignal.get();
94
93
  if (!instanceId) {
95
- // Remove first selection if no instanceId provided
96
- bookingSelectionsSignal.set(current.slice(1));
94
+ // Remove first item if no instanceId provided
95
+ bookingItemsSignal.set(current.slice(1));
97
96
  }
98
97
  else {
99
- const filtered = current.filter((s) => s.instanceId !== instanceId);
100
- bookingSelectionsSignal.set(filtered);
98
+ const filtered = current.filter((item) => item.instanceId !== instanceId);
99
+ bookingItemsSignal.set(filtered);
101
100
  }
102
101
  },
103
- clearServices() {
104
- bookingSelectionsSignal.set([]);
102
+ clearItems() {
103
+ bookingItemsSignal.set([]);
105
104
  },
106
- // Time Slot actions
107
- setTimeSlot(slot) {
108
- timeSlotSignal.set(slot);
105
+ // Resource helpers
106
+ setResourceById(resourceId, resource, instanceId) {
107
+ const current = bookingItemsSignal.get();
108
+ // If no instanceId provided, use the first item
109
+ const targetId = instanceId ?? current[0]?.instanceId;
110
+ if (!targetId)
111
+ return;
112
+ const updated = current.map((item) => {
113
+ if (item.instanceId !== targetId)
114
+ return item;
115
+ // Find and replace resource by ID, or add if not found
116
+ const existingIndex = item.resources.findIndex((r) => r._id === resourceId);
117
+ const newResources = [...item.resources];
118
+ if (existingIndex >= 0) {
119
+ newResources[existingIndex] = resource;
120
+ }
121
+ else {
122
+ newResources.push(resource);
123
+ }
124
+ return { ...item, resources: newResources };
125
+ });
126
+ bookingItemsSignal.set(updated);
109
127
  },
110
- clearTimeSlot() {
111
- timeSlotSignal.set(null);
128
+ getResourceById(resourceId, instanceId) {
129
+ const current = bookingItemsSignal.get();
130
+ // If no instanceId provided, use the first item
131
+ const targetId = instanceId ?? current[0]?.instanceId;
132
+ if (!targetId)
133
+ return undefined;
134
+ const targetItem = current.find((item) => item.instanceId === targetId);
135
+ if (!targetItem)
136
+ return undefined;
137
+ return targetItem.resources.find((r) => r._id === resourceId);
112
138
  },
113
139
  // Location actions
114
140
  setLocation(location) {
@@ -124,19 +150,23 @@ export const BookingService = implementService.withConfig()(BookingServiceDefini
124
150
  clearFormValues() {
125
151
  formValuesSignal.set(null);
126
152
  },
153
+ // Number of Participants actions
154
+ setNumberOfParticipants(count) {
155
+ numberOfParticipantsSignal.set(count);
156
+ },
127
157
  // Clear all
128
158
  clearAll() {
129
- bookingSelectionsSignal.set([]);
130
- timeSlotSignal.set(null);
159
+ bookingItemsSignal.set([]);
131
160
  locationSignal.set(null);
132
161
  formValuesSignal.set(null);
162
+ numberOfParticipantsSignal.set(1);
133
163
  },
134
164
  };
135
165
  return {
136
- bookingSelections: bookingSelectionsSignal,
137
- timeSlot: timeSlotSignal,
166
+ bookingItems: bookingItemsSignal,
138
167
  location: locationSignal,
139
168
  formValues: formValuesSignal,
169
+ numberOfParticipants: numberOfParticipantsSignal,
140
170
  actions,
141
171
  };
142
172
  });
@@ -3,6 +3,6 @@
3
3
  * Main export file for all service definitions and implementations
4
4
  */
5
5
  export { ServiceService, ServiceServiceDefinition, type ServiceServiceAPI, type ServiceServiceConfig, type ServiceActions, type ServicePaymentData, loadServiceServiceInitialData, type SuccessServiceServiceConfigResult, type NotFoundServiceServiceConfigResult, } from './service/service.js';
6
- export { BookingService, BookingServiceDefinition, type BookingServiceConfig, type BookingSelection, type Location, type BookingServiceActions, } from './booking/booking.js';
6
+ export { BookingService, BookingServiceDefinition, type BookingServiceConfig, type Location, type BookingServiceActions, } from './booking/booking.js';
7
7
  export { TimeSlotListService, TimeSlotListServiceDefinition, ViewMode, loadTimeSlotListConfig, type TimeSlotListServiceAPI, type TimeSlotListServiceActions, type TimeSlotListServiceConfig, type SlotSelection, type LoadTimeSlotListConfigOptions, type LoadTimeSlotListConfigResult, } from './time-slot-list/index.js';
8
8
  export { ServiceListService, ServiceListServiceDefinition, type ServiceListServiceAPI, type ServiceListServiceConfig, type ServiceListActions, type QueryOptions, loadServiceListServiceInitialData, type SuccessServiceListServiceConfigResult, type ErrorServiceListServiceConfigResult, } from './service-list/service-list.js';
@@ -81,8 +81,8 @@ export const ServiceService = implementService.withConfig()(ServiceServiceDefini
81
81
  const currentService = serviceSignal.get();
82
82
  if (!currentService._id)
83
83
  return false;
84
- const selections = bookingService.bookingSelections.get();
85
- return selections.some((selection) => selection.service._id === currentService._id);
84
+ const bookingItems = bookingService.bookingItems.get();
85
+ return bookingItems.some(({ service }) => service._id === currentService._id);
86
86
  });
87
87
  // requiresAvailability signal - true for APPOINTMENT and CLASS types
88
88
  const requiresAvailabilitySignal = signalsService.computed(() => {
@@ -185,25 +185,28 @@ export const ServiceService = implementService.withConfig()(ServiceServiceDefini
185
185
  if (!bookableSignal.get() || !bookingService)
186
186
  return;
187
187
  const currentService = serviceSignal.get();
188
- bookingService.actions.setService({ service: currentService });
188
+ bookingService.actions.updateItem({ service: currentService });
189
189
  },
190
190
  add: () => {
191
191
  if (!bookableSignal.get() || !bookingService)
192
192
  return;
193
193
  const currentService = serviceSignal.get();
194
- bookingService.actions.addService({ service: currentService });
194
+ bookingService.actions.addItem({
195
+ service: currentService,
196
+ resources: [],
197
+ });
195
198
  },
196
199
  remove: () => {
197
200
  if (!bookableSignal.get() || !bookingService)
198
201
  return;
199
202
  const currentService = serviceSignal.get();
200
203
  // Remove all instances of this service (by serviceId)
201
- const selections = bookingService.bookingSelections.get();
202
- const instancesToRemove = selections
204
+ const bookingItems = bookingService.bookingItems.get();
205
+ const instancesToRemove = bookingItems
203
206
  .filter((s) => s.service._id === currentService._id)
204
207
  .map((s) => s.instanceId);
205
208
  instancesToRemove.forEach((instanceId) => {
206
- bookingService.actions.removeBookingSelection(instanceId);
209
+ bookingService.actions.removeItem(instanceId);
207
210
  });
208
211
  },
209
212
  };