@open-tender/utils 0.0.56 → 0.0.59

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.
Files changed (91) hide show
  1. package/dist/cjs/hooks/types.d.ts +4 -0
  2. package/dist/cjs/hooks/types.js +2 -0
  3. package/dist/cjs/hooks/useAddressForm.d.ts +2 -5
  4. package/dist/cjs/hooks/useAddressForm.js +3 -3
  5. package/dist/cjs/hooks/useAllergenForm.d.ts +5 -2
  6. package/dist/cjs/hooks/useAllergenForm.js +8 -4
  7. package/dist/cjs/hooks/useCartGuestForm.d.ts +21 -0
  8. package/dist/cjs/hooks/useCartGuestForm.js +46 -0
  9. package/dist/cjs/hooks/useCheckoutLoginForm.d.ts +12 -0
  10. package/dist/cjs/hooks/useCheckoutLoginForm.js +41 -0
  11. package/dist/cjs/hooks/useCommunicationPreferences.d.ts +0 -0
  12. package/dist/cjs/hooks/useCommunicationPreferences.js +1 -0
  13. package/dist/cjs/hooks/useCreditCard.d.ts +8 -11
  14. package/dist/cjs/hooks/useCreditCard.js +61 -41
  15. package/dist/cjs/hooks/useDineInForm.d.ts +8 -3
  16. package/dist/cjs/hooks/useDineInForm.js +8 -2
  17. package/dist/cjs/hooks/useDonationForm.d.ts +4 -1
  18. package/dist/cjs/hooks/useDonationForm.js +17 -3
  19. package/dist/cjs/hooks/useGiftCardAssignForm.d.ts +5 -7
  20. package/dist/cjs/hooks/useGiftCardAssignForm.js +11 -3
  21. package/dist/cjs/hooks/useGiftCardAssignOtherForm.d.ts +5 -1
  22. package/dist/cjs/hooks/useGiftCardAssignOtherForm.js +8 -0
  23. package/dist/cjs/hooks/useGiftCardForm.d.ts +9 -5
  24. package/dist/cjs/hooks/useGiftCardForm.js +13 -4
  25. package/dist/cjs/hooks/useGiftCardsForm.d.ts +30 -11
  26. package/dist/cjs/hooks/useGiftCardsForm.js +72 -31
  27. package/dist/cjs/hooks/useGuestForm.d.ts +13 -0
  28. package/dist/cjs/hooks/useGuestForm.js +41 -0
  29. package/dist/cjs/hooks/useOrderFulfillmentForm.d.ts +13 -0
  30. package/dist/cjs/hooks/useOrderFulfillmentForm.js +40 -0
  31. package/dist/cjs/hooks/useOrderRatingForm.d.ts +13 -0
  32. package/dist/cjs/hooks/useOrderRatingForm.js +33 -0
  33. package/dist/cjs/hooks/useProfileForm.d.ts +18 -23
  34. package/dist/cjs/hooks/useProfileForm.js +27 -28
  35. package/dist/cjs/hooks/useResetPasswordForm.d.ts +20 -0
  36. package/dist/cjs/hooks/useResetPasswordForm.js +66 -0
  37. package/dist/cjs/hooks/useSendResetForm.d.ts +12 -0
  38. package/dist/cjs/hooks/useSendResetForm.js +36 -0
  39. package/dist/cjs/hooks/useSignUpForm.d.ts +21 -0
  40. package/dist/cjs/hooks/useSignUpForm.js +91 -0
  41. package/dist/cjs/hooks/useSignUpGuestForm.d.ts +23 -0
  42. package/dist/cjs/hooks/useSignUpGuestForm.js +74 -0
  43. package/dist/cjs/hooks/useSigninForm.d.ts +12 -0
  44. package/dist/cjs/hooks/useSigninForm.js +41 -0
  45. package/dist/cjs/utils/cart.js +2 -2
  46. package/dist/esm/hooks/types.d.ts +4 -0
  47. package/dist/esm/hooks/types.js +1 -0
  48. package/dist/esm/hooks/useAddressForm.d.ts +2 -5
  49. package/dist/esm/hooks/useAddressForm.js +3 -3
  50. package/dist/esm/hooks/useAllergenForm.d.ts +5 -2
  51. package/dist/esm/hooks/useAllergenForm.js +9 -5
  52. package/dist/esm/hooks/useCartGuestForm.d.ts +21 -0
  53. package/dist/esm/hooks/useCartGuestForm.js +44 -0
  54. package/dist/esm/hooks/useCheckoutLoginForm.d.ts +12 -0
  55. package/dist/esm/hooks/useCheckoutLoginForm.js +39 -0
  56. package/dist/esm/hooks/useCommunicationPreferences.d.ts +0 -0
  57. package/dist/esm/hooks/useCommunicationPreferences.js +1 -0
  58. package/dist/esm/hooks/useCreditCard.d.ts +8 -11
  59. package/dist/esm/hooks/useCreditCard.js +62 -42
  60. package/dist/esm/hooks/useDineInForm.d.ts +8 -3
  61. package/dist/esm/hooks/useDineInForm.js +9 -3
  62. package/dist/esm/hooks/useDonationForm.d.ts +4 -1
  63. package/dist/esm/hooks/useDonationForm.js +17 -3
  64. package/dist/esm/hooks/useGiftCardAssignForm.d.ts +5 -7
  65. package/dist/esm/hooks/useGiftCardAssignForm.js +12 -4
  66. package/dist/esm/hooks/useGiftCardAssignOtherForm.d.ts +5 -1
  67. package/dist/esm/hooks/useGiftCardAssignOtherForm.js +9 -1
  68. package/dist/esm/hooks/useGiftCardForm.d.ts +9 -5
  69. package/dist/esm/hooks/useGiftCardForm.js +14 -5
  70. package/dist/esm/hooks/useGiftCardsForm.d.ts +30 -11
  71. package/dist/esm/hooks/useGiftCardsForm.js +73 -32
  72. package/dist/esm/hooks/useGuestForm.d.ts +13 -0
  73. package/dist/esm/hooks/useGuestForm.js +39 -0
  74. package/dist/esm/hooks/useOrderFulfillmentForm.d.ts +13 -0
  75. package/dist/esm/hooks/useOrderFulfillmentForm.js +38 -0
  76. package/dist/esm/hooks/useOrderRatingForm.d.ts +13 -0
  77. package/dist/esm/hooks/useOrderRatingForm.js +31 -0
  78. package/dist/esm/hooks/useProfileForm.d.ts +18 -23
  79. package/dist/esm/hooks/useProfileForm.js +28 -29
  80. package/dist/esm/hooks/useResetPasswordForm.d.ts +20 -0
  81. package/dist/esm/hooks/useResetPasswordForm.js +64 -0
  82. package/dist/esm/hooks/useSendResetForm.d.ts +12 -0
  83. package/dist/esm/hooks/useSendResetForm.js +34 -0
  84. package/dist/esm/hooks/useSignUpForm.d.ts +21 -0
  85. package/dist/esm/hooks/useSignUpForm.js +89 -0
  86. package/dist/esm/hooks/useSignUpGuestForm.d.ts +23 -0
  87. package/dist/esm/hooks/useSignUpGuestForm.js +72 -0
  88. package/dist/esm/hooks/useSigninForm.d.ts +12 -0
  89. package/dist/esm/hooks/useSigninForm.js +39 -0
  90. package/dist/esm/utils/cart.js +2 -2
  91. package/package.json +2 -2
@@ -0,0 +1,4 @@
1
+ export interface Field {
2
+ blur?: () => void;
3
+ focus?: () => void;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,9 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import { Address, RequestStatus } from '@open-tender/types';
3
- interface Field {
4
- blur?: () => void;
5
- focus?: () => void;
6
- }
3
+ import { Field } from './types';
7
4
  interface AddressFormField {
8
5
  label: string;
9
6
  name: string;
@@ -15,7 +12,7 @@ export declare const useAddressForm: <InputRef extends Field, SubmitRef extends
15
12
  data: Address;
16
13
  errors: any;
17
14
  submitting: boolean;
18
- handleChange: (id: string, type: string, value: string | null, checked: boolean | null) => void;
15
+ handleChange: (name: string, type: string, value: string | null, checked: boolean | null) => void;
19
16
  handleSubmit: () => void;
20
17
  submitRef: import("react").MutableRefObject<SubmitRef | null>;
21
18
  inputRef: import("react").MutableRefObject<InputRef | null>;
@@ -27,13 +27,13 @@ const useAddressForm = (address, loading, error, update, callback) => {
27
27
  }
28
28
  }
29
29
  }, [loading, error]);
30
- const handleChange = (id, type, value, checked) => {
30
+ const handleChange = (name, type, value, checked) => {
31
31
  const inputValue = type === 'checkbox'
32
32
  ? checked
33
- : id === 'phone'
33
+ : name === 'phone'
34
34
  ? (0, utils_1.makePhone)(value !== null && value !== void 0 ? value : '')
35
35
  : value;
36
- setData(Object.assign(Object.assign({}, data), { [id]: inputValue }));
36
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
37
37
  };
38
38
  const handleSubmit = () => {
39
39
  var _a, _b;
@@ -1,9 +1,12 @@
1
+ /// <reference types="react" />
1
2
  import { Allergen, Allergens } from '@open-tender/types';
2
- declare const useAllergenForm: (allergens: Allergens, selectedAllergens: Allergens, isLoading: boolean | undefined, error: any, setAllergens: (allergens: Allergens) => void, updateAllergens: (allergen: Partial<Allergen>[]) => void, callback?: () => void) => {
3
+ import { Field } from './types';
4
+ declare const useAllergenForm: <SubmitRef extends Field>(allergens: Allergens, selectedAllergens: Allergens, isLoading: boolean | undefined, error: any, setAllergens: (allergens: Allergens) => void, updateAllergens: (allergen: Partial<Allergen>[]) => void, callback?: () => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
3
6
  submitting: boolean;
4
7
  allergenIds: number[];
5
8
  formError: any;
6
- handleChange: (id: string, checked?: boolean) => void;
9
+ handleChange: (name: string, checked?: boolean) => void;
7
10
  handleSubmit: () => void;
8
11
  };
9
12
  export default useAllergenForm;
@@ -2,24 +2,27 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const react_1 = require("react");
4
4
  const useAllergenForm = (allergens, selectedAllergens, isLoading = false, error = null, setAllergens, updateAllergens, callback) => {
5
+ const submitRef = (0, react_1.useRef)(null);
5
6
  const [data, setData] = (0, react_1.useState)([]);
6
7
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
7
8
  const allergenIds = data.map(i => i.allergen_id);
8
9
  const formError = error ? error.detail || error.message : null;
9
10
  (0, react_1.useEffect)(() => {
11
+ var _a, _b;
10
12
  // uses isLoading boolean because loading state is based on
11
13
  // both brand allergens and customer allergens loading
12
14
  if (!isLoading) {
13
15
  setSubmitting(false);
16
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
14
17
  }
15
18
  }, [isLoading]);
16
19
  (0, react_1.useEffect)(() => {
17
20
  setData(selectedAllergens || []);
18
21
  }, [selectedAllergens, allergens]);
19
- const handleChange = (id, checked) => {
22
+ const handleChange = (name, checked) => {
20
23
  const newData = checked
21
- ? [...data, { allergen_id: parseInt(id) }]
22
- : data.filter(i => i.allergen_id !== parseInt(id));
24
+ ? [...data, { allergen_id: parseInt(name) }]
25
+ : data.filter(i => i.allergen_id !== parseInt(name));
23
26
  setData(newData);
24
27
  };
25
28
  const handleSubmit = () => {
@@ -29,11 +32,12 @@ const useAllergenForm = (allergens, selectedAllergens, isLoading = false, error
29
32
  if (updateAllergens) {
30
33
  updateAllergens(newData);
31
34
  }
32
- else if (callback) {
35
+ if (callback) {
33
36
  callback();
34
37
  }
35
38
  };
36
39
  return {
40
+ submitRef,
37
41
  submitting,
38
42
  allergenIds,
39
43
  formError,
@@ -0,0 +1,21 @@
1
+ /// <reference types="react" />
2
+ import { RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ interface CartGuestFormField {
5
+ label?: string;
6
+ name: string;
7
+ type?: string;
8
+ required?: boolean;
9
+ }
10
+ declare const useCartGuestForm: <InputRef extends Field, SubmitRef extends Field>(loading: RequestStatus, errMsg: string, cartId: number, joinCart: (data: any, callback?: any) => void) => {
11
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
12
+ inputRef: import("react").MutableRefObject<InputRef | null>;
13
+ fields: CartGuestFormField[];
14
+ data: {
15
+ cart_id?: number | undefined;
16
+ };
17
+ submitting: boolean;
18
+ handleChange: (name: string, value: string) => void;
19
+ handleSubmit: () => void;
20
+ };
21
+ export default useCartGuestForm;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useCartGuestForm = (loading, errMsg, cartId, joinCart) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const inputRef = (0, react_1.useRef)(null);
7
+ const [data, setData] = (0, react_1.useState)({ cart_id: cartId });
8
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
9
+ const fields = [
10
+ { label: 'First Name', name: 'first_name', type: 'text', required: true },
11
+ { label: 'Last Name', name: 'last_name', type: 'text', required: true },
12
+ ];
13
+ (0, react_1.useEffect)(() => {
14
+ return () => {
15
+ setData({});
16
+ };
17
+ }, []);
18
+ (0, react_1.useEffect)(() => {
19
+ var _a, _b;
20
+ if (loading === 'idle') {
21
+ setSubmitting(false);
22
+ if (errMsg) {
23
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
24
+ }
25
+ }
26
+ }, [loading, errMsg]);
27
+ const handleChange = (name, value) => {
28
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
29
+ };
30
+ const handleSubmit = () => {
31
+ var _a, _b;
32
+ setSubmitting(true);
33
+ joinCart(data);
34
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
35
+ };
36
+ return {
37
+ submitRef,
38
+ inputRef,
39
+ fields,
40
+ data,
41
+ submitting,
42
+ handleChange,
43
+ handleSubmit,
44
+ };
45
+ };
46
+ exports.default = useCartGuestForm;
@@ -0,0 +1,12 @@
1
+ /// <reference types="react" />
2
+ import { LoginData, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useLoginForm: <InputRef extends Field, SubmitRef extends Field>(loading: RequestStatus, error: any, login: (email?: string, password?: string) => void, callback?: () => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ inputRef: import("react").MutableRefObject<InputRef | null>;
7
+ data: Partial<LoginData>;
8
+ submitting: boolean;
9
+ handleChange: (name: string, value: string) => void;
10
+ handleSubmit: () => void;
11
+ };
12
+ export default useLoginForm;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useLoginForm = (loading, error, login, callback) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const inputRef = (0, react_1.useRef)(null);
7
+ const [hasSubmit, setHasSubmit] = (0, react_1.useState)(false);
8
+ const [data, setData] = (0, react_1.useState)({});
9
+ const submitting = loading === 'pending';
10
+ (0, react_1.useEffect)(() => {
11
+ var _a, _b;
12
+ if (loading === 'idle' && hasSubmit) {
13
+ if (error) {
14
+ setHasSubmit(false);
15
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
16
+ }
17
+ else if (callback) {
18
+ callback();
19
+ }
20
+ }
21
+ }, [loading, error, hasSubmit, callback]);
22
+ const handleChange = (name, value) => {
23
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
24
+ };
25
+ const handleSubmit = () => {
26
+ var _a, _b;
27
+ setHasSubmit(true);
28
+ const { email, password } = data;
29
+ login(email, password);
30
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
31
+ };
32
+ return {
33
+ submitRef,
34
+ inputRef,
35
+ data,
36
+ submitting,
37
+ handleChange,
38
+ handleSubmit,
39
+ };
40
+ };
41
+ exports.default = useLoginForm;
@@ -0,0 +1 @@
1
+ "use strict";
@@ -1,14 +1,11 @@
1
1
  /// <reference types="react" />
2
- import { CreditCardData } from '@open-tender/types';
3
- declare const useCreditCard: (initState?: CreditCardData, cardErrors?: any, disabled?: boolean) => {
2
+ import { CardType, CreditCardData, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useCreditCardForm: <SubmitRef extends Field>(loading: RequestStatus, error: any, data: CreditCardData, cardType: CardType, setErrors: (errors: any) => void, addCard: (data: CreditCardData, callback: () => void) => void, callback: () => void, recaptchaKey: string, revenue_center_id: number | null, windowRef?: Window) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ recaptchaRef: import("react").MutableRefObject<any>;
4
7
  data: CreditCardData;
5
- cardType: "VISA" | "MC" | "DISC" | "AMEX" | "OTHER";
6
- errors: any;
7
- disabled: boolean | undefined;
8
- setErrors: import("react").Dispatch<any>;
9
- handleChange: (name: keyof CreditCardData, value: string) => void;
10
- handleBlur: (name: keyof CreditCardData) => void;
11
- handleReset: () => void;
12
- isComplete: boolean;
8
+ submitting: boolean;
9
+ handleSubmit: () => void;
13
10
  };
14
- export default useCreditCard;
11
+ export default useCreditCardForm;
@@ -1,53 +1,73 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const react_1 = require("react");
4
- const utils_1 = require("../utils");
5
- const defaultState = {
6
- acct: '',
7
- exp: '',
8
- cvv: '',
9
- zip: '',
10
- is_default: false,
11
- };
12
- const useCreditCard = (initState, cardErrors, disabled) => {
13
- const [initCard, initCardType] = (0, utils_1.formatCard)(initState !== null && initState !== void 0 ? initState : defaultState);
14
- const [data, setData] = (0, react_1.useState)(initCard || defaultState);
15
- const [cardType, setCardType] = (0, react_1.useState)(initCardType || 'OTHER');
16
- const [errors, setErrors] = (0, react_1.useState)(cardErrors !== null && cardErrors !== void 0 ? cardErrors : {});
17
- const { acct, exp, cvv, zip } = data;
18
- const isComplete = acct && exp && cvv && zip && !(0, utils_1.isEmpty)(errors) ? true : false;
19
- const handleChange = (name, value) => {
20
- if (name === 'acct') {
21
- const currentType = (0, utils_1.getCardType)(value.replace(/\s/g, ''));
22
- setCardType(currentType);
4
+ const utils_1 = require("@open-tender/utils");
5
+ const useCreditCardForm = (loading, error, data, cardType, setErrors, addCard, callback, recaptchaKey, revenue_center_id, windowRef) => {
6
+ const submitRef = (0, react_1.useRef)(null);
7
+ const recaptchaRef = (0, react_1.useRef)(null);
8
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
9
+ (0, react_1.useEffect)(() => {
10
+ if (loading === 'idle') {
11
+ setSubmitting(false);
12
+ if (error) {
13
+ if (recaptchaRef.current)
14
+ recaptchaRef.current.reset();
15
+ setErrors(error);
16
+ if (windowRef)
17
+ windowRef.scrollTo({ top: 0 });
18
+ }
23
19
  }
24
- const val = name === 'is_default' ? value : (0, utils_1.formatCardField)(name, value);
25
- setData(Object.assign(Object.assign({}, data), { [name]: val }));
26
- };
27
- const handleBlur = (name) => {
28
- const { errors: validationErrors } = (0, utils_1.validateCreditCard)(data, cardType);
29
- if (validationErrors) {
30
- const error = validationErrors[name];
31
- setErrors(Object.assign(Object.assign({}, errors), { [name]: error }));
20
+ }, [windowRef, loading, error, setErrors]);
21
+ const handleSubmit = () => {
22
+ var _a, _b, _c;
23
+ setErrors({});
24
+ const { card, errors: cardErrors } = (0, utils_1.validateCreditCard)(data, cardType);
25
+ if (cardErrors) {
26
+ setErrors(cardErrors);
27
+ setSubmitting(false);
28
+ if (windowRef)
29
+ windowRef.scrollTo({ top: 0 });
32
30
  }
33
31
  else {
34
- setErrors(Object.assign(Object.assign({}, errors), { [name]: null }));
32
+ const cardRc = revenue_center_id ? Object.assign(Object.assign({}, card), { revenue_center_id }) : card;
33
+ if (recaptchaKey) {
34
+ try {
35
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
36
+ if (!token) {
37
+ setSubmitting(false);
38
+ setErrors({
39
+ form: 'Please complete the recaptcha before submitting',
40
+ });
41
+ if (windowRef)
42
+ windowRef.scrollTo({ top: 0 });
43
+ }
44
+ else {
45
+ setSubmitting(true);
46
+ addCard(Object.assign(Object.assign({}, cardRc), { token }), callback);
47
+ }
48
+ }
49
+ catch (err) {
50
+ setSubmitting(false);
51
+ setErrors({
52
+ form: 'Please complete the recaptcha before submitting',
53
+ });
54
+ if (windowRef)
55
+ windowRef.scrollTo({ top: 0 });
56
+ }
57
+ }
58
+ else {
59
+ setSubmitting(true);
60
+ addCard(cardRc, callback);
61
+ }
35
62
  }
36
- };
37
- const handleReset = () => {
38
- setData(defaultState);
39
- setErrors({});
63
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
40
64
  };
41
65
  return {
66
+ submitRef,
67
+ recaptchaRef,
42
68
  data,
43
- cardType,
44
- errors,
45
- disabled,
46
- setErrors,
47
- handleChange,
48
- handleBlur,
49
- handleReset,
50
- isComplete,
69
+ submitting,
70
+ handleSubmit,
51
71
  };
52
72
  };
53
- exports.default = useCreditCard;
73
+ exports.default = useCreditCardForm;
@@ -1,7 +1,12 @@
1
- declare const useDineInForm: (initialData: any, submitGuest: (data: any) => void) => {
2
- data: any;
1
+ /// <reference types="react" />
2
+ import { GuestData } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useDineInForm: <InputRef extends Field, SubmitRef extends Field>(initialData: Partial<GuestData> | undefined, submitGuest: (data: any) => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ inputRef: import("react").MutableRefObject<InputRef | null>;
7
+ data: Partial<GuestData>;
3
8
  errMsg: string | null;
4
- handleChange: (id: string, value: unknown) => void;
9
+ handleChange: (name: string, value: string) => void;
5
10
  handleSubmit: () => void;
6
11
  };
7
12
  export default useDineInForm;
@@ -2,20 +2,26 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const react_1 = require("react");
4
4
  const useDineInForm = (initialData = {}, submitGuest) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const inputRef = (0, react_1.useRef)(null);
5
7
  const [data, setData] = (0, react_1.useState)(initialData);
6
8
  const [errMsg, setErrMsg] = (0, react_1.useState)(null);
7
- const handleChange = (id, value) => {
8
- setData(Object.assign(Object.assign({}, data), { [id]: value }));
9
+ const handleChange = (name, value) => {
10
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
9
11
  };
10
12
  const handleSubmit = () => {
13
+ var _a, _b;
11
14
  if (!data.first_name || !data.last_name) {
12
15
  setErrMsg('Both first and last name are required');
13
16
  }
14
17
  else {
15
18
  submitGuest({ customer: data });
16
19
  }
20
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
17
21
  };
18
22
  return {
23
+ submitRef,
24
+ inputRef,
19
25
  data,
20
26
  errMsg,
21
27
  handleChange,
@@ -1,10 +1,13 @@
1
1
  import { Dispatch } from 'react';
2
2
  import { CardType, CreditCard, CreditCardData, CreditCards, Profile, PurchaseDonationRequest, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
3
4
  interface CreditCardOption {
4
5
  label: string;
5
6
  value: unknown;
6
7
  }
7
- declare const useDonationsForm: (purchase: (data: PurchaseDonationRequest, callback?: () => void) => void, reset: () => void, setAlert: Dispatch<any>, loading: RequestStatus, error: any, success: boolean, customer?: Partial<Profile>, creditCards?: CreditCards, recaptchaKey?: string, cardData?: CreditCardData, cardType?: CardType) => {
8
+ declare const useDonationsForm: <InputRef extends Field, SubmitRef extends Field>(purchase: (data: PurchaseDonationRequest, callback?: () => void) => void, reset: () => void, setAlert: Dispatch<any>, loading: RequestStatus, error: any, success: boolean, customer?: Partial<Profile>, creditCards?: CreditCards, recaptchaKey?: string, cardData?: CreditCardData, cardType?: CardType, windowref?: Window) => {
9
+ inputRef: import("react").MutableRefObject<InputRef | null>;
10
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
8
11
  recaptchaRef: import("react").MutableRefObject<any>;
9
12
  amount: string | null;
10
13
  handleAmount: (value: string) => void;
@@ -2,7 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const react_1 = require("react");
4
4
  const utils_1 = require("../utils");
5
- const useDonationsForm = (purchase, reset, setAlert, loading, error, success, customer, creditCards, recaptchaKey, cardData, cardType) => {
5
+ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, customer, creditCards, recaptchaKey, cardData, cardType, windowref) => {
6
+ const submitRef = (0, react_1.useRef)(null);
7
+ const inputRef = (0, react_1.useRef)(null);
6
8
  const recaptchaRef = (0, react_1.useRef)(null);
7
9
  const [amount, setAmount] = (0, react_1.useState)('');
8
10
  const [email, setEmail] = (0, react_1.useState)('');
@@ -20,6 +22,7 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
20
22
  .reduce((obj, [key, value]) => (Object.assign(Object.assign({}, obj), { [key]: value })), {})
21
23
  : {}, [errors]);
22
24
  (0, react_1.useEffect)(() => {
25
+ var _a, _b;
23
26
  if (loading === 'idle') {
24
27
  setSubmitting(false);
25
28
  setAlert({ type: 'close' });
@@ -27,12 +30,14 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
27
30
  if (recaptchaRef.current)
28
31
  recaptchaRef.current.reset();
29
32
  setErrors(error);
33
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
34
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
30
35
  }
31
36
  else if (success) {
32
- //
37
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
33
38
  }
34
39
  }
35
- }, [loading, error, setAlert, success]);
40
+ }, [windowref, loading, error, setAlert, success]);
36
41
  (0, react_1.useEffect)(() => {
37
42
  if ((creditCards === null || creditCards === void 0 ? void 0 : creditCards.length) && loading !== 'pending') {
38
43
  const options = creditCards.map(i => ({
@@ -72,6 +77,7 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
72
77
  if (!token) {
73
78
  setSubmitting(false);
74
79
  setErrors({ form: 'Please complete the recaptcha before submitting' });
80
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
75
81
  }
76
82
  else {
77
83
  setSubmitting(true);
@@ -87,6 +93,7 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
87
93
  catch (err) {
88
94
  setSubmitting(false);
89
95
  setErrors({ form: 'Please complete the recaptcha before submitting' });
96
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
90
97
  }
91
98
  }
92
99
  else {
@@ -101,8 +108,11 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
101
108
  }
102
109
  };
103
110
  const handleSubmit = () => {
111
+ var _a, _b, _c;
104
112
  if (!amount || !email) {
105
113
  setErrors({ form: 'Both amount and email are required' });
114
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && inputRef.current.focus();
115
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
106
116
  }
107
117
  else {
108
118
  if (isNewCard) {
@@ -111,6 +121,7 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
111
121
  if (errs) {
112
122
  setErrors(Object.assign(Object.assign({}, errs), { form: 'There are one or more credit card errors below' }));
113
123
  setSubmitting(false);
124
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
114
125
  }
115
126
  else {
116
127
  purchaseWithCaptcha(card);
@@ -120,6 +131,7 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
120
131
  else {
121
132
  purchaseWithCaptcha(creditCard);
122
133
  }
134
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
123
135
  }
124
136
  };
125
137
  const handleReset = () => {
@@ -134,6 +146,8 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
134
146
  reset();
135
147
  };
136
148
  return {
149
+ inputRef,
150
+ submitRef,
137
151
  recaptchaRef,
138
152
  amount,
139
153
  handleAmount,
@@ -1,11 +1,9 @@
1
+ /// <reference types="react" />
1
2
  import { RequestStatus } from '@open-tender/types';
2
- interface useGiftCardAssignFormProps {
3
- loading: RequestStatus;
4
- error: any;
5
- assign: (card: string, callback?: () => void) => void;
6
- callback: () => void;
7
- }
8
- declare const useGiftCardAssignForm: ({ loading, error, assign, callback, }: useGiftCardAssignFormProps) => {
3
+ import { Field } from './types';
4
+ declare const useGiftCardAssignForm: <InputRef extends Field, SubmitRef extends Field>(loading: RequestStatus, error: any, assign: (card_number: number, callback: () => void) => void, callback: () => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ inputRef: import("react").MutableRefObject<InputRef | null>;
9
7
  cardNumber: string;
10
8
  errors: any;
11
9
  submitting: boolean;
@@ -1,15 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const react_1 = require("react");
4
- const useGiftCardAssignForm = ({ loading, error, assign, callback, }) => {
4
+ const useGiftCardAssignForm = (loading, error, assign, callback) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const inputRef = (0, react_1.useRef)(null);
5
7
  const [cardNumber, setCardNumber] = (0, react_1.useState)('');
6
8
  const [errors, setErrors] = (0, react_1.useState)({});
7
9
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
8
10
  (0, react_1.useEffect)(() => {
11
+ var _a, _b;
9
12
  if (loading === 'idle') {
10
13
  setSubmitting(false);
11
14
  if (error) {
12
15
  setErrors(error);
16
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
13
17
  }
14
18
  }
15
19
  }, [loading, error]);
@@ -17,17 +21,21 @@ const useGiftCardAssignForm = ({ loading, error, assign, callback, }) => {
17
21
  setCardNumber(value);
18
22
  };
19
23
  const handleSubmit = () => {
24
+ var _a, _b, _c, _d;
20
25
  setSubmitting(true);
21
26
  const card_number = parseInt(cardNumber);
22
27
  if (isNaN(card_number)) {
23
28
  setErrors({ card_number: 'Card numbers must be all digits' });
24
- setSubmitting(false);
29
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
25
30
  }
26
31
  else {
27
- assign(`${card_number}`, callback);
32
+ assign(card_number, callback);
33
+ ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur) && ((_d = submitRef.current) === null || _d === void 0 ? void 0 : _d.blur());
28
34
  }
29
35
  };
30
36
  return {
37
+ submitRef,
38
+ inputRef,
31
39
  cardNumber,
32
40
  errors,
33
41
  submitting,
@@ -1,5 +1,9 @@
1
+ /// <reference types="react" />
1
2
  import { RequestStatus } from '@open-tender/types';
2
- declare const useGiftCardAssignOtherForm: (loading: RequestStatus, error: any, giftCardId: number, assign: (giftCardId: number, email: string, callback: () => void) => void, callback: () => void) => {
3
+ import { Field } from './types';
4
+ declare const useGiftCardAssignOtherForm: <InputRef extends Field, SubmitRef extends Field>(loading: RequestStatus, error: any, giftCardId: number, assign: (id: number, email: string, callback: () => void) => void, callback: () => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ inputRef: import("react").MutableRefObject<InputRef | null>;
3
7
  email: string;
4
8
  errors: any;
5
9
  submitting: boolean;
@@ -2,14 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const react_1 = require("react");
4
4
  const useGiftCardAssignOtherForm = (loading, error, giftCardId, assign, callback) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const inputRef = (0, react_1.useRef)(null);
5
7
  const [email, setEmail] = (0, react_1.useState)('');
6
8
  const [errors, setErrors] = (0, react_1.useState)({});
7
9
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
8
10
  (0, react_1.useEffect)(() => {
11
+ var _a, _b;
9
12
  if (loading === 'idle') {
10
13
  setSubmitting(false);
11
14
  if (error) {
12
15
  setErrors(error);
16
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
13
17
  }
14
18
  }
15
19
  }, [loading, error]);
@@ -17,10 +21,14 @@ const useGiftCardAssignOtherForm = (loading, error, giftCardId, assign, callback
17
21
  setEmail(value);
18
22
  };
19
23
  const handleSubmit = () => {
24
+ var _a, _b;
20
25
  setSubmitting(true);
21
26
  assign(giftCardId, email, callback);
27
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
22
28
  };
23
29
  return {
30
+ submitRef,
31
+ inputRef,
24
32
  email,
25
33
  errors,
26
34
  submitting,