@wix/headless-restaurants-olo 0.0.44 → 0.0.46

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.
@@ -44,6 +44,24 @@ export const FulfillmentDetailsService = implementService.withConfig()(Fulfillme
44
44
  const availableTimeSlotsForDate = signalsService.signal([]);
45
45
  const schedulingType = signalsService.signal(fulfillmentsService.schedulingType?.get());
46
46
  // ========================================
47
+ // Sync selectedFulfillment from FulfillmentsService to dispatchType
48
+ // ========================================
49
+ // Sync dispatchType when selectedFulfillment changes in FulfillmentsService
50
+ // Use peek() to read dispatchType without creating a dependency, so the effect
51
+ // only runs when selectedFulfillment changes, not when dispatchType changes
52
+ signalsService.effect(() => {
53
+ const selectedFulfillment = fulfillmentsService.selectedFulfillment?.get();
54
+ const newDispatchType = selectedFulfillment?.type
55
+ ? selectedFulfillment.type
56
+ : null;
57
+ // Use peek() to read current value without subscribing to changes
58
+ const currentDispatchType = dispatchType.peek();
59
+ // Only update if different to avoid unnecessary updates and circular updates
60
+ if (currentDispatchType !== newDispatchType) {
61
+ dispatchType.set(newDispatchType);
62
+ }
63
+ });
64
+ // ========================================
47
65
  // Helper Functions
48
66
  // ========================================
49
67
  /**
@@ -115,8 +133,78 @@ export const FulfillmentDetailsService = implementService.withConfig()(Fulfillme
115
133
  asapTimeSlot.set(slot);
116
134
  }
117
135
  };
136
+ const convertAddressInputToAddress = (address) => {
137
+ if (!address)
138
+ return null;
139
+ const {
140
+ // @ts-expect-error
141
+ addressLine,
142
+ // @ts-expect-error
143
+ streetAddress,
144
+ // @ts-expect-error
145
+ formattedAddress,
146
+ // @ts-expect-error
147
+ location, ...rest } = address;
148
+ return {
149
+ streetAddress: streetAddress,
150
+ addressLine1: formattedAddress,
151
+ ...rest,
152
+ };
153
+ };
154
+ const initAddress = async (addr) => {
155
+ isLoading.set(true);
156
+ error.set(null);
157
+ try {
158
+ const commonAddress = convertAddressInputToAddress(addr);
159
+ // 1. Get first available time slot per operation with the selected address
160
+ const firstTimeSlotsResponse = await operationsSDK.calculateFirstAvailableTimeSlotPerFulfillmentType(operation.id, {
161
+ deliveryAddress: commonAddress,
162
+ });
163
+ // Find the time slots for this operation
164
+ const deliveryTimeSlot = firstTimeSlotsResponse.timeslotsPerFulfillmentType?.find((ts) => ts.fulfilmentType === DispatchType.DELIVERY);
165
+ if (!deliveryTimeSlot?.timeSlot) {
166
+ isLoading.set(false);
167
+ // TODO - localize this message
168
+ error.set('No available time slots found for the selected address');
169
+ return;
170
+ }
171
+ const firstTimeSlot = processTimeSlots([deliveryTimeSlot], DispatchType.DELIVERY)[0];
172
+ // 2. Fetch time slots for the selected address and date (using firstTimeSlot.startTime)
173
+ const targetDate = firstTimeSlot.startTime;
174
+ if (!targetDate) {
175
+ isLoading.set(false);
176
+ return;
177
+ }
178
+ await calculateAvailableTimeSlotsForDateFn(targetDate);
179
+ // Set the date and time slot
180
+ date.set(targetDate);
181
+ setTimeslot(firstTimeSlot);
182
+ // 3. Fetch available dates for the selected address
183
+ const startDate = new Date();
184
+ startDate.setHours(0, 0, 0, 0);
185
+ const endDate = new Date();
186
+ endDate.setDate(endDate.getDate() + daysAhead);
187
+ endDate.setHours(23, 59, 59, 999);
188
+ await calculateAvailableDatesInRangeFn(startDate, endDate);
189
+ }
190
+ catch (err) {
191
+ const errorMessage = err instanceof Error ? err.message : 'Failed to initialize address';
192
+ error.set(errorMessage);
193
+ console.error('Error initializing address:', err);
194
+ }
195
+ finally {
196
+ isLoading.set(false);
197
+ }
198
+ };
118
199
  const setAddress = (addr) => {
119
200
  address.set(addr);
201
+ // Initialize address: fetch first time slot, time slots for date, and available dates
202
+ if (addr) {
203
+ initAddress(addr).catch((err) => {
204
+ console.error('Error initializing address:', err);
205
+ error.set('Failed to initialize address');
206
+ });
207
+ }
120
208
  };
121
209
  const setDate = async (selectedDate) => {
122
210
  date.set(selectedDate);
@@ -129,7 +217,6 @@ export const FulfillmentDetailsService = implementService.withConfig()(Fulfillme
129
217
  }
130
218
  };
131
219
  const setDispatchType = (type) => {
132
- console.log('setDispatchType', type);
133
220
  dispatchType.set(type);
134
221
  // Re-filter available time slots based on new dispatch type
135
222
  const currentDate = date.get();
@@ -244,6 +331,7 @@ export const FulfillmentDetailsService = implementService.withConfig()(Fulfillme
244
331
  });
245
332
  const { timeslotsPerFulfillmentType } = response;
246
333
  const currentDispatchType = dispatchType.get();
334
+ // Process slots filtered by current dispatch type
247
335
  const slots = processTimeSlots(timeslotsPerFulfillmentType, currentDispatchType).reverse();
248
336
  availableTimeSlotsForDate.set(slots);
249
337
  // Auto-select first available slot if none selected
@@ -308,6 +396,12 @@ export const FulfillmentDetailsService = implementService.withConfig()(Fulfillme
308
396
  }
309
397
  return allTimeSlots.filter((slot) => slot.dispatchType === currentDispatchType);
310
398
  });
399
+ /**
400
+ * Available dispatch types from config
401
+ */
402
+ const availableDispatchTypes = signalsService.computed(() => {
403
+ return config.availableDispatchTypes ?? [];
404
+ });
311
405
  // ========================================
312
406
  // Return Service API
313
407
  // ========================================
@@ -323,6 +417,7 @@ export const FulfillmentDetailsService = implementService.withConfig()(Fulfillme
323
417
  availableDates,
324
418
  availableTimeSlotsForDate,
325
419
  availableTimeSlots,
420
+ availableDispatchTypes,
326
421
  schedulingType,
327
422
  // Actions
328
423
  getTimeslot,
@@ -355,5 +450,6 @@ export function loadFulfillmentDetailsServiceConfig(operation, options) {
355
450
  initialDispatchType: options?.initialDispatchType,
356
451
  initialAddress: options?.initialAddress,
357
452
  daysAhead: options?.daysAhead ?? 30,
453
+ availableDispatchTypes: options?.availableDispatchTypes,
358
454
  };
359
455
  }
@@ -63,17 +63,17 @@ export const ItemService = implementService.withConfig()(ItemServiceDefinition,
63
63
  : '');
64
64
  const priceVariants = config.item?.priceVariants || [];
65
65
  let initialVariant;
66
- if (!config.editItemMode) {
67
- initialVariant = priceVariants.length > 0 ? priceVariants[0] : undefined;
66
+ if (config.editItemMode && config.editingItemValues?.selectedVariantId) {
67
+ initialVariant = priceVariants.find((variant) => variant._id === config.editingItemValues?.selectedVariantId);
68
68
  }
69
69
  else {
70
- if (config.editingItemValues?.selectedVariantId) {
71
- initialVariant = priceVariants.find((variant) => variant._id === config.editingItemValues?.selectedVariantId);
72
- }
73
- else {
74
- initialVariant =
75
- priceVariants.length > 0 ? priceVariants[0] : undefined;
76
- }
70
+ initialVariant =
71
+ priceVariants.length > 0
72
+ ? priceVariants.reduce((cheapest, current) => Number(current.priceInfo?.price) <
73
+ Number(cheapest.priceInfo?.price)
74
+ ? current
75
+ : cheapest)
76
+ : undefined;
77
77
  }
78
78
  const selectedVariant = signalsService.signal(initialVariant);
79
79
  const modifierGroups = config.item?.modifierGroups || [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/headless-restaurants-olo",
3
- "version": "0.0.44",
3
+ "version": "0.0.46",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -78,5 +78,5 @@
78
78
  "groupId": "com.wixpress.headless-components"
79
79
  }
80
80
  },
81
- "falconPackageHash": "8b0e8f84677d9e75b1242a120c014e61d8e0279095afd8bf291e54aa"
81
+ "falconPackageHash": "ff154f099ed6c3d14102602cd9650342ca326344b800448bc5237940"
82
82
  }