@orderingstack/front-hooks 3.7.1 → 3.9.0

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.
package/dist/index.es.js CHANGED
@@ -1369,6 +1369,41 @@ function isEqual(value, other) {
1369
1369
  return baseIsEqual(value, other);
1370
1370
  }
1371
1371
  var isEqual_1 = isEqual;
1372
+ function addDiscounts(discounts) {
1373
+ let value = 0;
1374
+ discounts == null ? void 0 : discounts.forEach((discount) => value += Number(discount.discountPrice));
1375
+ return value;
1376
+ }
1377
+ function summarizeLineDiscounts(line) {
1378
+ return addDiscounts(line.discounts);
1379
+ }
1380
+ function formatPrice(value, currency, separator = ",", decimals = 2) {
1381
+ const price = Number(value);
1382
+ return `${price.toFixed(decimals).replace(".", separator)}${currency ? "\xA0" + currency : ""}`;
1383
+ }
1384
+ function isProductEnabled(item, enableKeys) {
1385
+ var _a;
1386
+ const enableKey = (_a = item._) == null ? void 0 : _a.enableKey;
1387
+ if (!enableKey)
1388
+ return true;
1389
+ if (!enableKeys || !Array.isArray(enableKeys) || enableKeys.length === 0)
1390
+ return false;
1391
+ const itemEnableKeys = enableKey.replace(/\s/g, "").split(",");
1392
+ return itemEnableKeys.some((key) => enableKeys.includes(key));
1393
+ }
1394
+ function getProductAcronym(product) {
1395
+ var _a, _b, _c, _d, _e, _f;
1396
+ const arrayName = (_b = (_a = product.details) == null ? void 0 : _a.literals) == null ? void 0 : _b.name.split(" ").filter((string) => string !== "");
1397
+ if (!arrayName)
1398
+ return "";
1399
+ if (arrayName.length === 1) {
1400
+ return ((_f = (_e = (_d = (_c = product.details) == null ? void 0 : _c.literals) == null ? void 0 : _d.name) == null ? void 0 : _e.slice) == null ? void 0 : _f.call(_e, 0, 2)) || "";
1401
+ }
1402
+ if (arrayName.length > 1) {
1403
+ return `${arrayName[0][0]}${arrayName[1][0]}`;
1404
+ }
1405
+ return "";
1406
+ }
1372
1407
  function useAvailability(originalMenu, markOnly = false, timeZone = void 0, interval = 60) {
1373
1408
  const [menu, setMenu] = useState(originalMenu ? filterTimeAvailabilityOfProducts(originalMenu, timeZone, void 0, markOnly) : originalMenu);
1374
1409
  useInterval(checkAvailability, interval * 1e3);
@@ -1382,6 +1417,32 @@ function useAvailability(originalMenu, markOnly = false, timeZone = void 0, inte
1382
1417
  useEffect(checkAvailability, [menu, originalMenu]);
1383
1418
  return menu;
1384
1419
  }
1420
+ function markHiddenByEnableKey(product, enableKeys) {
1421
+ if (!product.__) {
1422
+ product.__ = {};
1423
+ }
1424
+ product.__.isHiddenByEnableKey = !isProductEnabled(product, enableKeys);
1425
+ if (product.items && product.items.length > 0) {
1426
+ product.items = product.items.map((item) => markHiddenByEnableKey(item, enableKeys));
1427
+ }
1428
+ return product;
1429
+ }
1430
+ function markItemsHiddenByEnableKey(items, enableKeys) {
1431
+ if (!items) {
1432
+ return void 0;
1433
+ }
1434
+ return items.map((item) => markHiddenByEnableKey(item, enableKeys));
1435
+ }
1436
+ function useMenu({
1437
+ originalMenu,
1438
+ markOnly = false,
1439
+ timeZone,
1440
+ interval = 60,
1441
+ enableKeys = []
1442
+ }) {
1443
+ const menu = useAvailability(originalMenu, markOnly, timeZone, interval);
1444
+ return markItemsHiddenByEnableKey(menu, enableKeys);
1445
+ }
1385
1446
  var EPaymentStatus;
1386
1447
  (function(EPaymentStatus2) {
1387
1448
  EPaymentStatus2["UNINITIALIZED"] = "UNINITIALIZED";
@@ -1460,17 +1521,17 @@ var EOrderPaymentType;
1460
1521
  EOrderPaymentType2["RETURN"] = "RETURN";
1461
1522
  EOrderPaymentType2["EXTERNAL"] = "EXTERNAL";
1462
1523
  })(EOrderPaymentType || (EOrderPaymentType = {}));
1463
- var OrderSource;
1464
- (function(OrderSource2) {
1465
- OrderSource2["KIOSK"] = "KIOSK";
1466
- OrderSource2["WEB"] = "WEB";
1467
- OrderSource2["JUST_EAT_TAKE_AWAY"] = "JUSTEATTAKEAWAY";
1468
- OrderSource2["GLOVO"] = "GLOVO";
1469
- OrderSource2["PYSZNE"] = "PYSZNE";
1470
- OrderSource2["WOLT"] = "WOLT";
1471
- OrderSource2["UBER"] = "UBER";
1472
- OrderSource2["BOLT"] = "BOLT";
1473
- })(OrderSource || (OrderSource = {}));
1524
+ var EOrderSource;
1525
+ (function(EOrderSource2) {
1526
+ EOrderSource2["KIOSK"] = "KIOSK";
1527
+ EOrderSource2["WEB"] = "WEB";
1528
+ EOrderSource2["JUST_EAT_TAKE_AWAY"] = "JUSTEATTAKEAWAY";
1529
+ EOrderSource2["GLOVO"] = "GLOVO";
1530
+ EOrderSource2["PYSZNE"] = "PYSZNE";
1531
+ EOrderSource2["WOLT"] = "WOLT";
1532
+ EOrderSource2["UBER"] = "UBER";
1533
+ EOrderSource2["BOLT"] = "BOLT";
1534
+ })(EOrderSource || (EOrderSource = {}));
1474
1535
  var EOrderLineStatus;
1475
1536
  (function(EOrderLineStatus2) {
1476
1537
  EOrderLineStatus2["NEW"] = "NEW";
@@ -1491,12 +1552,19 @@ var EChannelName;
1491
1552
  EChannelName2["DINE_IN"] = "DINE_IN";
1492
1553
  EChannelName2["TAKE_AWAY"] = "TAKE_AWAY";
1493
1554
  EChannelName2["DELIVERY"] = "DELIVERY";
1494
- EChannelName2["GLOVO_DELIVERY"] = "GLOVO_DELIVERY";
1495
- EChannelName2["JUSTEAT_DELIVERY"] = "JUSTEAT_DELIVERY";
1496
- EChannelName2["UBER_DELIVERY"] = "UBER_DELIVERY";
1497
- EChannelName2["BOLT_DELIVERY"] = "BOLT_DELIVERY";
1498
- EChannelName2["WOLT_DELIVERY"] = "WOLT_DELIVERY";
1555
+ EChannelName2["GLOVO"] = "GLOVO";
1556
+ EChannelName2["JUSTEAT"] = "JUSTEAT";
1557
+ EChannelName2["UBER"] = "UBER";
1558
+ EChannelName2["BOLT"] = "BOLT";
1559
+ EChannelName2["WOLT"] = "WOLT";
1499
1560
  })(EChannelName || (EChannelName = {}));
1561
+ var ENotificationType;
1562
+ (function(ENotificationType2) {
1563
+ ENotificationType2["ORDER_EVENT_ERROR"] = "OrderEventError";
1564
+ ENotificationType2["ORDER_APPLIED_EVENTS"] = "OrderAppliedEvents";
1565
+ ENotificationType2["ORDER_EVENT_FISCAL"] = "OrderEventFiscal";
1566
+ ENotificationType2["ORDER_UPSELL"] = "OrderUpsell";
1567
+ })(ENotificationType || (ENotificationType = {}));
1500
1568
  function arrayEach$1(array, iteratee) {
1501
1569
  var index = -1, length = array == null ? 0 : array.length;
1502
1570
  while (++index < length) {
@@ -3065,8 +3133,8 @@ function useCms(id, intervalSeconds = 600, withLocalStorage = true, baseUrl, ten
3065
3133
  const [timestamp, setTimestamp] = useState(((_b = getFromLocalStorage()) == null ? void 0 : _b.timestamp) || 0);
3066
3134
  const [state, setState] = useState(loadedData || null);
3067
3135
  const [error, setError] = useState(false);
3068
- function getCms() {
3069
- axios.get(url).then((res) => {
3136
+ async function getCms(signal) {
3137
+ return axios.get(url, { signal }).then((res) => {
3070
3138
  if (isEmpty_1(res.data)) {
3071
3139
  throw new Error(`CMS data '${id}' is an empty object`);
3072
3140
  }
@@ -3080,10 +3148,11 @@ function useCms(id, intervalSeconds = 600, withLocalStorage = true, baseUrl, ten
3080
3148
  saveToLocalStorage(res.data);
3081
3149
  }
3082
3150
  }).catch((e) => {
3083
- console.error(e);
3084
- setError(true);
3151
+ if (e.code !== "ERR_CANCELED") {
3152
+ console.error(e);
3153
+ setError(true);
3154
+ }
3085
3155
  });
3086
- return;
3087
3156
  }
3088
3157
  function saveToLocalStorage(data) {
3089
3158
  const t = new Date().valueOf();
@@ -3098,7 +3167,8 @@ function useCms(id, intervalSeconds = 600, withLocalStorage = true, baseUrl, ten
3098
3167
  try {
3099
3168
  const value = localStorage.getItem(key);
3100
3169
  if (value) {
3101
- return JSON.parse(value);
3170
+ const val = JSON.parse(value);
3171
+ return val;
3102
3172
  }
3103
3173
  } catch (e) {
3104
3174
  console.warn(e);
@@ -3106,10 +3176,22 @@ function useCms(id, intervalSeconds = 600, withLocalStorage = true, baseUrl, ten
3106
3176
  return null;
3107
3177
  }
3108
3178
  useEffect(() => {
3179
+ console.log("CMS ID", id);
3109
3180
  if (new Date().valueOf() - timestamp > intervalSeconds * 1e3) {
3110
- getCms();
3181
+ const controller = new AbortController();
3182
+ getCms(controller.signal);
3183
+ return () => controller.abort();
3184
+ }
3185
+ const val = getFromLocalStorage();
3186
+ if (val == null ? void 0 : val.value) {
3187
+ setState(val.value);
3188
+ setTimestamp(val.timestamp);
3189
+ } else {
3190
+ const controller = new AbortController();
3191
+ getCms(controller.signal);
3192
+ return () => controller.abort();
3111
3193
  }
3112
- }, []);
3194
+ }, [id]);
3113
3195
  useInterval(getCms, intervalSeconds * 1e3);
3114
3196
  return { value: state, isError: error, isLoading: !error && !state };
3115
3197
  }
@@ -3689,17 +3771,5 @@ function getMediaUrl(url, size = 500) {
3689
3771
  function useMediaUrl(url, size = 500) {
3690
3772
  return getMediaUrl(url, size);
3691
3773
  }
3692
- function addDiscounts(discounts) {
3693
- let value = 0;
3694
- discounts == null ? void 0 : discounts.forEach((discount) => value += Number(discount.discountPrice));
3695
- return value;
3696
- }
3697
- function summarizeLineDiscounts(line) {
3698
- return addDiscounts(line.discounts);
3699
- }
3700
- function formatPrice(value, currency, separator = ",", decimals = 2) {
3701
- const price = Number(value);
3702
- return `${price.toFixed(decimals).replace(".", separator)}${currency ? "\xA0" + currency : ""}`;
3703
- }
3704
- export { addDiscounts, formatPrice, getGroupedBucketLines, getMediaUrl, groupOrderLines, isChannelOpen, summarizeLineDiscounts, useAvailability, useCms, useDisplayPrice, useGroupOrderLines, useInterval, useKioskChannelsAvailability, useKioskMediaCms, useKioskMediaCmsWithContext, useLocalizedVenueMedia, useMediaUrl, useOnClickOutside, useVenueCms };
3774
+ export { addDiscounts, formatPrice, getGroupedBucketLines, getMediaUrl, getProductAcronym, groupOrderLines, isChannelOpen, isProductEnabled, summarizeLineDiscounts, useAvailability, useCms, useDisplayPrice, useGroupOrderLines, useInterval, useKioskChannelsAvailability, useKioskMediaCms, useKioskMediaCmsWithContext, useLocalizedVenueMedia, useMediaUrl, useMenu, useOnClickOutside, useVenueCms };
3705
3775
  //# sourceMappingURL=index.es.js.map