@open-tender/utils 0.5.20 → 0.5.22

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,6 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import { LoginData, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useLoginForm: (loading: RequestStatus, error: RequestError, login: (email: string, password?: string, recaptcha_token?: string) => void, includeRecaptcha: boolean, callback?: () => void, showPassword?: boolean) => {
3
+ export declare const useLoginForm: (loading: RequestStatus, error: RequestError, login: (email: string, password?: string, recaptcha_token?: string) => void, recaptchaKey: string | null, callback?: () => void, showPassword?: boolean) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
6
  recaptchaRef: import("react").MutableRefObject<any>;
@@ -9,5 +9,5 @@ export declare const useLoginForm: (loading: RequestStatus, error: RequestError,
9
9
  errors: Record<string, string>;
10
10
  submitting: boolean;
11
11
  handleChange: (name: string, value: string | number | boolean) => void;
12
- handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => Promise<void>;
12
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
13
13
  };
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useLoginForm = void 0;
4
- const tslib_1 = require("tslib");
5
4
  const react_1 = require("react");
6
5
  const utils_1 = require("../utils");
7
- const useLoginForm = (loading, error, login, includeRecaptcha, callback, showPassword = true) => {
6
+ const useLoginForm = (loading, error, login, recaptchaKey, callback, showPassword = true) => {
8
7
  const submitRef = (0, react_1.useRef)(null);
9
8
  const inputRef = (0, react_1.useRef)(null);
10
9
  const recaptchaRef = (0, react_1.useRef)(null);
@@ -48,16 +47,13 @@ const useLoginForm = (loading, error, login, includeRecaptcha, callback, showPas
48
47
  const handleChange = (name, value) => {
49
48
  setData(Object.assign(Object.assign({}, data), { [name]: value }));
50
49
  };
51
- const handleSubmit = (evt) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
52
- var _a, _b, _c, _d, _e;
50
+ const handleSubmit = (evt) => {
51
+ var _a, _b, _c;
53
52
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
54
53
  const { email, password } = data;
55
- if (includeRecaptcha) {
54
+ if (recaptchaKey) {
56
55
  try {
57
- const isEnterpriseRecaptcha = ((_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.props.size) === 'invisible';
58
- const token = isEnterpriseRecaptcha
59
- ? yield ((_b = recaptchaRef.current) === null || _b === void 0 ? void 0 : _b.executeAsync())
60
- : (_c = recaptchaRef.current) === null || _c === void 0 ? void 0 : _c.getValue();
56
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
61
57
  if (!token) {
62
58
  setSubmitting(false);
63
59
  setErrors({
@@ -82,8 +78,8 @@ const useLoginForm = (loading, error, login, includeRecaptcha, callback, showPas
82
78
  login(email, password);
83
79
  setSubmitting(true);
84
80
  }
85
- ((_d = submitRef.current) === null || _d === void 0 ? void 0 : _d.blur) && ((_e = submitRef.current) === null || _e === void 0 ? void 0 : _e.blur());
86
- });
81
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
82
+ };
87
83
  return {
88
84
  submitRef,
89
85
  inputRef,
@@ -208,7 +208,7 @@ export declare const makeSimpleCart: <C extends {
208
208
  made_for: string;
209
209
  notes: string;
210
210
  }[];
211
- export declare const validateCart: (cart: Cart, categories: MenuCategories, soldOut: SoldOut) => {
211
+ export declare const validateCart: (cart: Cart, categories: MenuCategories, soldOut: SoldOut, requestedAt: RequestedAt) => {
212
212
  newCart: Cart;
213
213
  errors: CartErrors | null;
214
214
  };
@@ -731,7 +731,7 @@ const makeGroupsLookup = (item) => {
731
731
  }, {});
732
732
  };
733
733
  // TODO: refactor for nested modifiers
734
- const validateCart = (cart, categories, soldOut) => {
734
+ const validateCart = (cart, categories, soldOut, requestedAt) => {
735
735
  const itemIds = cart.map(item => item.id);
736
736
  const itemLookup = makeItemLookup(categories, itemIds);
737
737
  let errors = null;
@@ -740,7 +740,8 @@ const validateCart = (cart, categories, soldOut) => {
740
740
  let newCart = cart.map(oldItem => {
741
741
  var _a, _b, _c, _d;
742
742
  const newItem = itemLookup[oldItem.id];
743
- if (!newItem || soldOut.includes(oldItem.id)) {
743
+ if (!newItem ||
744
+ (0, exports.checkSoldOut)(oldItem.id, newItem.suspend_until, soldOut, requestedAt)) {
744
745
  missingItems.push(oldItem);
745
746
  return null;
746
747
  }
@@ -758,7 +759,7 @@ const validateCart = (cart, categories, soldOut) => {
758
759
  return i.min === 0 && !(oldGroupIds === null || oldGroupIds === void 0 ? void 0 : oldGroupIds.includes(i.id));
759
760
  })
760
761
  .map((i) => (Object.assign(Object.assign({}, i), { options: Object.values(i.options) })));
761
- const updatedGroups = (_b = oldItem.groups) === null || _b === void 0 ? void 0 : _b.map((group) => {
762
+ const updatedGroups = (_b = oldItem.groups) === null || _b === void 0 ? void 0 : _b.map(group => {
762
763
  const optionCount = group.options.reduce((t, i) => (t += i.quantity), 0);
763
764
  const newGroup = newGroups[group.id];
764
765
  // check if option group is still part of the item
@@ -772,12 +773,13 @@ const validateCart = (cart, categories, soldOut) => {
772
773
  }
773
774
  }
774
775
  else {
775
- const updatedOptions = group.options.map((option) => {
776
+ const updatedOptions = group.options.map(option => {
776
777
  const newOption = newGroup.options[option.id];
777
778
  // check to see if any OLD options aren't avaiilable on the current menu
778
779
  // or if any of the options are currently sold out
779
780
  if (!newOption ||
780
- (option.quantity > 0 && soldOut.includes(option.id))) {
781
+ (option.quantity > 0 &&
782
+ (0, exports.checkSoldOut)(option.id, option.suspend_until, soldOut, requestedAt))) {
781
783
  missingOptions.push(option);
782
784
  return option;
783
785
  }
@@ -1,6 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import { LoginData, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useLoginForm: (loading: RequestStatus, error: RequestError, login: (email: string, password?: string, recaptcha_token?: string) => void, includeRecaptcha: boolean, callback?: () => void, showPassword?: boolean) => {
3
+ export declare const useLoginForm: (loading: RequestStatus, error: RequestError, login: (email: string, password?: string, recaptcha_token?: string) => void, recaptchaKey: string | null, callback?: () => void, showPassword?: boolean) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
6
  recaptchaRef: import("react").MutableRefObject<any>;
@@ -9,5 +9,5 @@ export declare const useLoginForm: (loading: RequestStatus, error: RequestError,
9
9
  errors: Record<string, string>;
10
10
  submitting: boolean;
11
11
  handleChange: (name: string, value: string | number | boolean) => void;
12
- handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => Promise<void>;
12
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
13
13
  };
@@ -1,7 +1,6 @@
1
- import { __awaiter } from "tslib";
2
1
  import { useState, useEffect, useRef } from 'react';
3
2
  import { makeFormErrors } from '../utils';
4
- export const useLoginForm = (loading, error, login, includeRecaptcha, callback, showPassword = true) => {
3
+ export const useLoginForm = (loading, error, login, recaptchaKey, callback, showPassword = true) => {
5
4
  const submitRef = useRef(null);
6
5
  const inputRef = useRef(null);
7
6
  const recaptchaRef = useRef(null);
@@ -45,16 +44,13 @@ export const useLoginForm = (loading, error, login, includeRecaptcha, callback,
45
44
  const handleChange = (name, value) => {
46
45
  setData(Object.assign(Object.assign({}, data), { [name]: value }));
47
46
  };
48
- const handleSubmit = (evt) => __awaiter(void 0, void 0, void 0, function* () {
49
- var _a, _b, _c, _d, _e;
47
+ const handleSubmit = (evt) => {
48
+ var _a, _b, _c;
50
49
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
51
50
  const { email, password } = data;
52
- if (includeRecaptcha) {
51
+ if (recaptchaKey) {
53
52
  try {
54
- const isEnterpriseRecaptcha = ((_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.props.size) === 'invisible';
55
- const token = isEnterpriseRecaptcha
56
- ? yield ((_b = recaptchaRef.current) === null || _b === void 0 ? void 0 : _b.executeAsync())
57
- : (_c = recaptchaRef.current) === null || _c === void 0 ? void 0 : _c.getValue();
53
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
58
54
  if (!token) {
59
55
  setSubmitting(false);
60
56
  setErrors({
@@ -79,8 +75,8 @@ export const useLoginForm = (loading, error, login, includeRecaptcha, callback,
79
75
  login(email, password);
80
76
  setSubmitting(true);
81
77
  }
82
- ((_d = submitRef.current) === null || _d === void 0 ? void 0 : _d.blur) && ((_e = submitRef.current) === null || _e === void 0 ? void 0 : _e.blur());
83
- });
78
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
79
+ };
84
80
  return {
85
81
  submitRef,
86
82
  inputRef,
@@ -208,7 +208,7 @@ export declare const makeSimpleCart: <C extends {
208
208
  made_for: string;
209
209
  notes: string;
210
210
  }[];
211
- export declare const validateCart: (cart: Cart, categories: MenuCategories, soldOut: SoldOut) => {
211
+ export declare const validateCart: (cart: Cart, categories: MenuCategories, soldOut: SoldOut, requestedAt: RequestedAt) => {
212
212
  newCart: Cart;
213
213
  errors: CartErrors | null;
214
214
  };
@@ -695,7 +695,7 @@ const makeGroupsLookup = (item) => {
695
695
  }, {});
696
696
  };
697
697
  // TODO: refactor for nested modifiers
698
- export const validateCart = (cart, categories, soldOut) => {
698
+ export const validateCart = (cart, categories, soldOut, requestedAt) => {
699
699
  const itemIds = cart.map(item => item.id);
700
700
  const itemLookup = makeItemLookup(categories, itemIds);
701
701
  let errors = null;
@@ -704,7 +704,8 @@ export const validateCart = (cart, categories, soldOut) => {
704
704
  let newCart = cart.map(oldItem => {
705
705
  var _a, _b, _c, _d;
706
706
  const newItem = itemLookup[oldItem.id];
707
- if (!newItem || soldOut.includes(oldItem.id)) {
707
+ if (!newItem ||
708
+ checkSoldOut(oldItem.id, newItem.suspend_until, soldOut, requestedAt)) {
708
709
  missingItems.push(oldItem);
709
710
  return null;
710
711
  }
@@ -722,7 +723,7 @@ export const validateCart = (cart, categories, soldOut) => {
722
723
  return i.min === 0 && !(oldGroupIds === null || oldGroupIds === void 0 ? void 0 : oldGroupIds.includes(i.id));
723
724
  })
724
725
  .map((i) => (Object.assign(Object.assign({}, i), { options: Object.values(i.options) })));
725
- const updatedGroups = (_b = oldItem.groups) === null || _b === void 0 ? void 0 : _b.map((group) => {
726
+ const updatedGroups = (_b = oldItem.groups) === null || _b === void 0 ? void 0 : _b.map(group => {
726
727
  const optionCount = group.options.reduce((t, i) => (t += i.quantity), 0);
727
728
  const newGroup = newGroups[group.id];
728
729
  // check if option group is still part of the item
@@ -736,12 +737,13 @@ export const validateCart = (cart, categories, soldOut) => {
736
737
  }
737
738
  }
738
739
  else {
739
- const updatedOptions = group.options.map((option) => {
740
+ const updatedOptions = group.options.map(option => {
740
741
  const newOption = newGroup.options[option.id];
741
742
  // check to see if any OLD options aren't avaiilable on the current menu
742
743
  // or if any of the options are currently sold out
743
744
  if (!newOption ||
744
- (option.quantity > 0 && soldOut.includes(option.id))) {
745
+ (option.quantity > 0 &&
746
+ checkSoldOut(option.id, option.suspend_until, soldOut, requestedAt))) {
745
747
  missingOptions.push(option);
746
748
  return option;
747
749
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.5.20",
3
+ "version": "0.5.22",
4
4
  "description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",