@open-tender/utils 0.0.57 → 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 (89) 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/esm/hooks/types.d.ts +4 -0
  46. package/dist/esm/hooks/types.js +1 -0
  47. package/dist/esm/hooks/useAddressForm.d.ts +2 -5
  48. package/dist/esm/hooks/useAddressForm.js +3 -3
  49. package/dist/esm/hooks/useAllergenForm.d.ts +5 -2
  50. package/dist/esm/hooks/useAllergenForm.js +9 -5
  51. package/dist/esm/hooks/useCartGuestForm.d.ts +21 -0
  52. package/dist/esm/hooks/useCartGuestForm.js +44 -0
  53. package/dist/esm/hooks/useCheckoutLoginForm.d.ts +12 -0
  54. package/dist/esm/hooks/useCheckoutLoginForm.js +39 -0
  55. package/dist/esm/hooks/useCommunicationPreferences.d.ts +0 -0
  56. package/dist/esm/hooks/useCommunicationPreferences.js +1 -0
  57. package/dist/esm/hooks/useCreditCard.d.ts +8 -11
  58. package/dist/esm/hooks/useCreditCard.js +62 -42
  59. package/dist/esm/hooks/useDineInForm.d.ts +8 -3
  60. package/dist/esm/hooks/useDineInForm.js +9 -3
  61. package/dist/esm/hooks/useDonationForm.d.ts +4 -1
  62. package/dist/esm/hooks/useDonationForm.js +17 -3
  63. package/dist/esm/hooks/useGiftCardAssignForm.d.ts +5 -7
  64. package/dist/esm/hooks/useGiftCardAssignForm.js +12 -4
  65. package/dist/esm/hooks/useGiftCardAssignOtherForm.d.ts +5 -1
  66. package/dist/esm/hooks/useGiftCardAssignOtherForm.js +9 -1
  67. package/dist/esm/hooks/useGiftCardForm.d.ts +9 -5
  68. package/dist/esm/hooks/useGiftCardForm.js +14 -5
  69. package/dist/esm/hooks/useGiftCardsForm.d.ts +30 -11
  70. package/dist/esm/hooks/useGiftCardsForm.js +73 -32
  71. package/dist/esm/hooks/useGuestForm.d.ts +13 -0
  72. package/dist/esm/hooks/useGuestForm.js +39 -0
  73. package/dist/esm/hooks/useOrderFulfillmentForm.d.ts +13 -0
  74. package/dist/esm/hooks/useOrderFulfillmentForm.js +38 -0
  75. package/dist/esm/hooks/useOrderRatingForm.d.ts +13 -0
  76. package/dist/esm/hooks/useOrderRatingForm.js +31 -0
  77. package/dist/esm/hooks/useProfileForm.d.ts +18 -23
  78. package/dist/esm/hooks/useProfileForm.js +28 -29
  79. package/dist/esm/hooks/useResetPasswordForm.d.ts +20 -0
  80. package/dist/esm/hooks/useResetPasswordForm.js +64 -0
  81. package/dist/esm/hooks/useSendResetForm.d.ts +12 -0
  82. package/dist/esm/hooks/useSendResetForm.js +34 -0
  83. package/dist/esm/hooks/useSignUpForm.d.ts +21 -0
  84. package/dist/esm/hooks/useSignUpForm.js +89 -0
  85. package/dist/esm/hooks/useSignUpGuestForm.d.ts +23 -0
  86. package/dist/esm/hooks/useSignUpGuestForm.js +72 -0
  87. package/dist/esm/hooks/useSigninForm.d.ts +12 -0
  88. package/dist/esm/hooks/useSigninForm.js +39 -0
  89. package/package.json +2 -2
@@ -1,13 +1,17 @@
1
+ /// <reference types="react" />
1
2
  import { CreditCards, CustomerGiftCard, RequestStatus } from '@open-tender/types';
2
- declare const useGiftCardForm: (giftCard: CustomerGiftCard, creditCards: CreditCards, loading: RequestStatus, error: any, update: (id: number, data: any, callback: () => void) => void, add: (data: any, callback: () => void) => void, callback: () => void) => {
3
+ import { Field } from './types';
4
+ declare const useGiftCardForm: <InputRef extends Field, SubmitRef extends Field>(giftCard: CustomerGiftCard, creditCards: CreditCards, loading: RequestStatus, error: any, update: (id: number, data: Partial<CustomerGiftCard>, callback: () => void) => void, add: (data: Partial<CustomerGiftCard>, callback: () => void) => void, callback: () => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ inputRef: import("react").MutableRefObject<InputRef | null>;
3
7
  options: {
4
- label: string;
8
+ name: string;
5
9
  value: number | undefined;
6
10
  }[];
7
- errors: any;
8
- data: any;
11
+ errors: {};
12
+ data: Partial<CustomerGiftCard>;
9
13
  submitting: boolean;
10
- handleChange: (id: string, value: unknown) => void;
14
+ handleChange: (name: number, type: string, value: string | null, checked: boolean | null) => void;
11
15
  handleSubmit: () => void;
12
16
  };
13
17
  export default useGiftCardForm;
@@ -2,17 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const react_1 = require("react");
4
4
  const useGiftCardForm = (giftCard, creditCards, loading, error, update, add, callback) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const inputRef = (0, react_1.useRef)(null);
5
7
  const customerCardId = creditCards[0].customer_card_id;
6
8
  const [data, setData] = (0, react_1.useState)({ customer_card_id: customerCardId });
7
9
  const [errors, setErrors] = (0, react_1.useState)({});
8
10
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
9
- const options = creditCards.map(i => {
11
+ const options = creditCards.map((i) => {
10
12
  return {
11
- label: `${i.card_type_name} ending in ${i.last4}`,
13
+ name: `${i.card_type_name} ending in ${i.last4}`,
12
14
  value: i.customer_card_id,
13
15
  };
14
16
  });
15
17
  (0, react_1.useEffect)(() => {
18
+ var _a, _b;
16
19
  if (loading === 'idle') {
17
20
  setSubmitting(false);
18
21
  if (error) {
@@ -20,13 +23,16 @@ const useGiftCardForm = (giftCard, creditCards, loading, error, update, add, cal
20
23
  error.amount = 'Please enter a positive dollar amount';
21
24
  }
22
25
  setErrors(error);
26
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
23
27
  }
24
28
  }
25
29
  }, [loading, error]);
26
- const handleChange = (id, value) => {
27
- setData(Object.assign(Object.assign({}, data), { [id]: value }));
30
+ const handleChange = (name, type, value, checked) => {
31
+ const inputValue = type === 'checkbox' ? checked : value;
32
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
28
33
  };
29
34
  const handleSubmit = () => {
35
+ var _a, _b;
30
36
  setSubmitting(true);
31
37
  if (!data.customer_card_id) {
32
38
  data.customer_card_id = customerCardId;
@@ -35,8 +41,11 @@ const useGiftCardForm = (giftCard, creditCards, loading, error, update, add, cal
35
41
  giftCard
36
42
  ? update(giftCard.gift_card_id, data, callback)
37
43
  : add(data, callback);
44
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
38
45
  };
39
46
  return {
47
+ submitRef,
48
+ inputRef,
40
49
  options,
41
50
  errors,
42
51
  data,
@@ -1,23 +1,42 @@
1
- import { Dispatch } from 'react';
2
- import { CardType, CreditCard, CreditCardData, CreditCards, Profile, PurchaseGiftCardData, RequestStatus } from '@open-tender/types';
3
- declare const useGiftCardsForm: (purchase: (data: any) => void, reset: () => void, loading: RequestStatus, error: any, success: boolean, setAlert: Dispatch<any>, customer: Partial<Profile> | undefined, creditCards: CreditCards | undefined, _recaptchaKey: string | null | undefined, initState: PurchaseGiftCardData, cardData: CreditCardData, cardType: CardType) => {
4
- creditCardOptions: any[];
1
+ /// <reference types="react" />
2
+ import { CardType, CreditCard, CreditCardData, CreditCards, Customer, PurchaseGiftCardData, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ interface CreditCardOption {
5
+ name: string;
6
+ value?: number;
7
+ }
8
+ interface PurchaseData {
9
+ credit_card: Partial<CreditCardData>;
10
+ email: string;
11
+ gift_cards: Array<Partial<PurchaseGiftCardData>>;
12
+ name: string;
13
+ url: string;
14
+ token?: string;
15
+ }
16
+ declare const useGiftCardsForm: <InputRef extends Field, SubmitRef extends Field>(purchase: (data: PurchaseData) => void, reset: () => void, loading: RequestStatus, error: any, success: boolean, setAlert: (data: {
17
+ type: string;
18
+ args?: any;
19
+ }) => void, customer: Partial<Customer> | undefined, creditCards: CreditCards | undefined, recaptchaKey: string | null | undefined, initState: PurchaseGiftCardData, cardData: CreditCardData, cardType: CardType, windowRef?: Window) => {
20
+ inputRef: import("react").MutableRefObject<InputRef | null>;
21
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
22
+ recaptchaRef: import("react").MutableRefObject<any>;
23
+ creditCardOptions: CreditCardOption[];
5
24
  handleName: (value: string) => void;
6
25
  handleEmail: (value: string) => void;
7
- handleChange: (id: string, value: unknown) => void;
26
+ handleChange: (name: string, value: string) => void;
8
27
  handleQuantity: (index: number, quantity: number) => void;
9
28
  handleAddAnother: () => void;
10
- handleCreditCard: (value: string) => void;
29
+ handleCreditCard: (value: number) => void;
11
30
  handleSubmit: () => void;
12
31
  handleReset: () => void;
13
- name: string | undefined;
14
- email: string | undefined;
32
+ name: string | null;
33
+ email: string | null;
15
34
  cards: PurchaseGiftCardData[];
16
35
  isNewCard: boolean;
17
36
  newCardErrors: {};
18
- creditCard: Partial<CreditCard>;
19
- setCreditCard: Dispatch<import("react").SetStateAction<Partial<CreditCard>>>;
20
- errors: any;
37
+ creditCard: CreditCard & Partial<CreditCardData>;
38
+ setCreditCard: import("react").Dispatch<import("react").SetStateAction<CreditCard & Partial<CreditCardData>>>;
39
+ errors: {};
21
40
  submitting: boolean;
22
41
  };
23
42
  export default useGiftCardsForm;
@@ -1,19 +1,21 @@
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 useGiftCardsForm = (purchase, reset, loading, error, success, setAlert, customer = {}, creditCards = [],
6
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
7
- _recaptchaKey = null, initState, cardData, cardType) => {
8
- const [name, setName] = (0, react_1.useState)(undefined);
9
- const [email, setEmail] = (0, react_1.useState)(undefined);
4
+ const utils_1 = require("@open-tender/utils");
5
+ const useGiftCardsForm = (purchase, reset, loading, error, success, setAlert, customer = {}, creditCards = [], recaptchaKey = null, initState, cardData, cardType, windowRef) => {
6
+ var _a, _b;
7
+ const submitRef = (0, react_1.useRef)(null);
8
+ const inputRef = (0, react_1.useRef)(null);
9
+ const recaptchaRef = (0, react_1.useRef)(null);
10
+ const [name, setName] = (0, react_1.useState)(null);
11
+ const [email, setEmail] = (0, react_1.useState)(null);
10
12
  const [cards, setCards] = (0, react_1.useState)([initState]);
11
13
  const [isNewCard, setIsNewCard] = (0, react_1.useState)(true);
12
14
  const [creditCard, setCreditCard] = (0, react_1.useState)({});
13
15
  const [creditCardOptions, setCreditCardOptions] = (0, react_1.useState)([]);
14
16
  const [errors, setErrors] = (0, react_1.useState)({});
15
17
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
16
- const url = null;
18
+ const url = (_b = (_a = windowRef === null || windowRef === void 0 ? void 0 : windowRef.location) === null || _a === void 0 ? void 0 : _a.origin) !== null && _b !== void 0 ? _b : "";
17
19
  const newCardErrors = (0, react_1.useMemo)(() => errors
18
20
  ? Object.entries(errors)
19
21
  .filter(([key]) => key !== 'form')
@@ -24,14 +26,20 @@ _recaptchaKey = null, initState, cardData, cardType) => {
24
26
  setSubmitting(false);
25
27
  setAlert({ type: 'close' });
26
28
  if (error) {
29
+ if (recaptchaRef.current)
30
+ recaptchaRef.current.reset();
27
31
  setErrors(error);
32
+ windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
33
+ }
34
+ else if (success) {
35
+ windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
28
36
  }
29
37
  }
30
- }, [loading, error, setAlert, success]);
38
+ }, [windowRef, loading, error, setAlert, success]);
31
39
  (0, react_1.useEffect)(() => {
32
- if (creditCards.length && loading !== 'pending') {
33
- const options = creditCards.map(i => ({
34
- label: `${i.card_type_name} ending in ${i.last4}`,
40
+ if (creditCards.length) {
41
+ const options = creditCards.map((i) => ({
42
+ name: `${i.card_type_name} ending in ${i.last4}`,
35
43
  value: i.customer_card_id,
36
44
  }));
37
45
  setCreditCardOptions(options);
@@ -41,17 +49,18 @@ _recaptchaKey = null, initState, cardData, cardType) => {
41
49
  setCreditCard(defaultCard);
42
50
  setIsNewCard(false);
43
51
  }
44
- }, [creditCards, loading]);
52
+ }, [creditCards]);
45
53
  (0, react_1.useEffect)(() => {
54
+ var _a;
46
55
  if (customer) {
47
- setEmail(customer.email);
56
+ setEmail((_a = customer.email) !== null && _a !== void 0 ? _a : null);
48
57
  setName(`${customer.first_name} ${customer.last_name}`);
49
58
  }
50
59
  }, [customer]);
51
60
  const makeGiftCards = (giftCards) => {
52
61
  return giftCards.reduce((arr, card) => {
53
- const { amount, quantity, email: mail } = card;
54
- const giftCard = !mail || !mail.length ? { amount, quantity } : card;
62
+ const { amount, quantity, email } = card;
63
+ const giftCard = !email || !email.length ? { amount, quantity } : card;
55
64
  return [...arr, giftCard];
56
65
  }, []);
57
66
  };
@@ -61,8 +70,8 @@ _recaptchaKey = null, initState, cardData, cardType) => {
61
70
  const handleEmail = (value) => {
62
71
  setEmail(value);
63
72
  };
64
- const handleChange = (id, value) => {
65
- const [field, index] = id.split('-');
73
+ const handleChange = (name, value) => {
74
+ const [field, index] = name.split('-');
66
75
  const newCards = cards.map((card, idx) => {
67
76
  return idx === parseInt(index) ? Object.assign(Object.assign({}, card), { [field]: value }) : card;
68
77
  });
@@ -71,38 +80,70 @@ _recaptchaKey = null, initState, cardData, cardType) => {
71
80
  const handleQuantity = (index, quantity) => {
72
81
  const newCards = quantity > 0
73
82
  ? cards.map((card, idx) => {
74
- return idx === index ? Object.assign(Object.assign({}, card), { quantity: quantity }) : card;
83
+ return idx === parseInt(`${index}`)
84
+ ? Object.assign(Object.assign({}, card), { quantity: quantity }) : card;
75
85
  })
76
- : cards.filter((_i, idx) => idx !== index);
86
+ : cards.filter((_, idx) => idx !== index);
77
87
  setCards(newCards);
78
88
  };
79
89
  const handleAddAnother = () => {
80
90
  setCards([...cards, initState]);
81
91
  };
82
92
  const handleCreditCard = (value) => {
83
- const customerCardId = parseInt(value);
93
+ const customerCardId = parseInt(`${value}`);
84
94
  setCreditCard({ customer_card_id: customerCardId });
85
95
  };
86
96
  const purchaseWithCaptcha = (credit_card) => {
97
+ var _a;
87
98
  const alert = {
88
99
  type: 'working',
89
100
  args: { text: 'Submitting your purchase...' },
90
101
  };
91
- setSubmitting(true);
92
- setAlert(alert);
93
- const gift_cards = makeGiftCards(cards);
94
- purchase({ credit_card, name, email, url, gift_cards });
102
+ if (recaptchaKey) {
103
+ try {
104
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
105
+ if (!token) {
106
+ setSubmitting(false);
107
+ setErrors({
108
+ form: 'Please complete the recaptcha before submitting',
109
+ });
110
+ windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
111
+ }
112
+ else {
113
+ setSubmitting(true);
114
+ setAlert(alert);
115
+ const gift_cards = makeGiftCards(cards);
116
+ purchase({ token, credit_card, name: name !== null && name !== void 0 ? name : "", email: email !== null && email !== void 0 ? email : "", url, gift_cards });
117
+ }
118
+ }
119
+ catch (err) {
120
+ setSubmitting(false);
121
+ setErrors({ form: 'Please complete the recaptcha before submitting' });
122
+ windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
123
+ }
124
+ }
125
+ else {
126
+ setSubmitting(true);
127
+ setAlert(alert);
128
+ const gift_cards = makeGiftCards(cards);
129
+ purchase({ credit_card, name: name !== null && name !== void 0 ? name : "", email: email !== null && email !== void 0 ? email : "", url, gift_cards });
130
+ }
95
131
  };
96
132
  const handleSubmit = () => {
133
+ var _a, _b, _c;
97
134
  if (!name || !email) {
98
135
  setErrors({ form: 'Both name and email are required' });
136
+ if (inputRef.current)
137
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && inputRef.current.focus();
138
+ windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
99
139
  }
100
140
  else {
101
141
  if (isNewCard) {
102
- const { card, errors: errs } = (0, utils_1.validateCreditCard)(cardData, cardType);
103
- if (errs) {
104
- setErrors(Object.assign(Object.assign({}, errs), { form: 'There are one or more credit card errors below' }));
142
+ const { card, errors } = (0, utils_1.validateCreditCard)(cardData, cardType);
143
+ if (errors) {
144
+ setErrors(Object.assign(Object.assign({}, errors), { form: 'There are one or more credit card errors below' }));
105
145
  setSubmitting(false);
146
+ windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
106
147
  }
107
148
  else {
108
149
  purchaseWithCaptcha(card);
@@ -111,18 +152,18 @@ _recaptchaKey = null, initState, cardData, cardType) => {
111
152
  else {
112
153
  purchaseWithCaptcha(creditCard);
113
154
  }
155
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
114
156
  }
115
157
  };
116
158
  const handleReset = () => {
117
- if (!customer) {
118
- setName(undefined);
119
- setEmail(undefined);
120
- }
121
159
  setCards([initState]);
122
160
  setErrors({});
123
161
  reset();
124
162
  };
125
163
  return {
164
+ inputRef,
165
+ submitRef,
166
+ recaptchaRef,
126
167
  creditCardOptions,
127
168
  handleName,
128
169
  handleEmail,
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ import { GuestData, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useGuestForm: <TnputRef extends Field, SubmitRef extends Field>(email: string, loading: RequestStatus, errors: any, checkGuest: (email?: string) => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ inputRef: import("react").MutableRefObject<TnputRef | null>;
7
+ data: Partial<GuestData>;
8
+ errMsg: any;
9
+ submitting: boolean;
10
+ handleChange: (name: string, value: string) => void;
11
+ handleSubmit: () => void;
12
+ };
13
+ export default useGuestForm;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useGuestForm = (email, loading, errors, checkGuest) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const inputRef = (0, react_1.useRef)(null);
7
+ const [data, setData] = (0, react_1.useState)({});
8
+ const submitting = loading === 'pending';
9
+ const errMsg = errors ? errors.email || errors.form : null;
10
+ (0, react_1.useEffect)(() => {
11
+ var _a, _b;
12
+ if (loading === 'idle') {
13
+ if (errMsg)
14
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
15
+ }
16
+ }, [loading, errMsg]);
17
+ (0, react_1.useEffect)(() => {
18
+ if (email && data.email === undefined) {
19
+ setData({ email });
20
+ }
21
+ }, [email, data.email]);
22
+ const handleChange = (name, value) => {
23
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
24
+ };
25
+ const handleSubmit = () => {
26
+ var _a, _b;
27
+ const { email } = data;
28
+ checkGuest(email);
29
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
30
+ };
31
+ return {
32
+ submitRef,
33
+ inputRef,
34
+ data,
35
+ errMsg,
36
+ submitting,
37
+ handleChange,
38
+ handleSubmit,
39
+ };
40
+ };
41
+ exports.default = useGuestForm;
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ import { ConfigFulfillment, OrderFulfillment, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useOrderFulfillmentForm: <SubmitRef extends Field>(orderId: number, fulfillment: Partial<OrderFulfillment>, loading: RequestStatus, error: any, update: (orderId: number, data: Partial<OrderFulfillment>) => void, settings?: ConfigFulfillment, showAllFields?: boolean) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ fields: import("@open-tender/types").ConfigFulfillmentField[] | undefined;
7
+ data: Partial<OrderFulfillment>;
8
+ errors: any;
9
+ submitting: boolean;
10
+ handleChange: (name: string, type: string, value: string | null, checked: boolean | null) => void;
11
+ handleSubmit: () => void;
12
+ };
13
+ export default useOrderFulfillmentForm;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useOrderFulfillmentForm = (orderId, fulfillment, loading, error, update, settings, showAllFields = false) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const [data, setData] = (0, react_1.useState)({});
7
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
8
+ const errors = error || {};
9
+ const empty = Object.values(fulfillment).every((i) => !i);
10
+ const fields = !empty && !showAllFields
11
+ ? settings === null || settings === void 0 ? void 0 : settings.fields.filter((i) => i.name.startsWith('arrival'))
12
+ : settings === null || settings === void 0 ? void 0 : settings.fields;
13
+ (0, react_1.useEffect)(() => {
14
+ if (loading === 'idle')
15
+ setSubmitting(false);
16
+ }, [loading]);
17
+ (0, react_1.useEffect)(() => {
18
+ setData(fulfillment);
19
+ }, [fulfillment]);
20
+ const handleChange = (name, type, value, checked) => {
21
+ const inputValue = type === 'checkbox' ? checked : value;
22
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
23
+ };
24
+ const handleSubmit = () => {
25
+ var _a, _b;
26
+ setSubmitting(true);
27
+ update(orderId, Object.assign(Object.assign({}, data), { has_arrived: true }));
28
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
29
+ };
30
+ return {
31
+ submitRef,
32
+ fields,
33
+ data,
34
+ errors,
35
+ submitting,
36
+ handleChange,
37
+ handleSubmit,
38
+ };
39
+ };
40
+ exports.default = useOrderFulfillmentForm;
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ import { OrderRating } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useOrderRatingForm: <SubmitRef extends Field>(orderId: number, orderRating: OrderRating, updateRating: (orderId: number, rating: Partial<OrderRating>) => void, callback?: () => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ stars: number[];
7
+ rating: number;
8
+ handleRating: (star: number) => void;
9
+ comments: string;
10
+ handleComments: (value: string) => void;
11
+ handleSubmit: () => void;
12
+ };
13
+ export default useOrderRatingForm;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useOrderRatingForm = (orderId, orderRating, updateRating, callback) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const [rating, setRating] = (0, react_1.useState)(orderRating.rating || 0);
7
+ const [comments, setComments] = (0, react_1.useState)(orderRating.comments || '');
8
+ const stars = [1, 2, 3, 4, 5];
9
+ const handleRating = (star) => {
10
+ setRating(star);
11
+ };
12
+ const handleComments = (value) => {
13
+ setComments(value);
14
+ };
15
+ const handleSubmit = () => {
16
+ var _a, _b;
17
+ const data = { rating, comments };
18
+ updateRating(orderId, data);
19
+ if (callback)
20
+ callback();
21
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
22
+ };
23
+ return {
24
+ submitRef,
25
+ stars,
26
+ rating,
27
+ handleRating,
28
+ comments,
29
+ handleComments,
30
+ handleSubmit,
31
+ };
32
+ };
33
+ exports.default = useOrderRatingForm;
@@ -1,29 +1,24 @@
1
- import { Profile, RequestStatus } from '@open-tender/types';
2
- declare const useProfileForm: (profile: Partial<Profile>, loading: RequestStatus, error: any, update: (data: Partial<Profile>) => void) => {
3
- data: any;
1
+ /// <reference types="react" />
2
+ import { ConfigBrand, Profile, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ interface ProfileFormField {
5
+ label: string;
6
+ name: keyof Profile;
7
+ type: string;
8
+ required?: boolean;
9
+ autoComplete?: string;
10
+ }
11
+ declare const useProfileForm: <SubmitRef extends Field>(profile: Partial<Profile>, loading: RequestStatus, error: any, update: (data: Partial<Profile>) => void, optIns?: Partial<ConfigBrand>) => {
12
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
13
+ accepts_marketing: import("@open-tender/types").ConfigAcceptMarketing | undefined;
14
+ order_notifications: import("@open-tender/types").ConfigOrderNotifications | undefined;
15
+ data: Partial<Profile>;
4
16
  errors: any;
5
17
  submitting: boolean;
6
- fields: ({
7
- label: string;
8
- name: string;
9
- type: string;
10
- required: boolean;
11
- autoComplete?: undefined;
12
- } | {
13
- label: string;
14
- name: string;
15
- type: string;
16
- required: boolean;
17
- autoComplete: string;
18
- } | {
19
- label: string;
20
- name: string;
21
- type: string;
22
- required?: undefined;
23
- autoComplete?: undefined;
24
- })[];
18
+ fields: Partial<ProfileFormField>[];
25
19
  errMsg: string | null;
26
- handleChange: (id: string, type: string | null, value: string, checked: boolean | null) => void;
20
+ handleChange: (name: string, type: string, value?: string, checked?: boolean) => void;
21
+ handleRadio: (name: string, value: string) => void;
27
22
  handleSubmit: () => void;
28
23
  };
29
24
  export default useProfileForm;
@@ -2,58 +2,52 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const react_1 = require("react");
4
4
  const utils_1 = require("../utils");
5
- const useProfileForm = (profile, loading, error, update) => {
5
+ const useProfileForm = (profile, loading, error, update, optIns = {}) => {
6
+ const submitRef = (0, react_1.useRef)(null);
7
+ const { accepts_marketing, order_notifications } = optIns;
6
8
  const [data, setData] = (0, react_1.useState)({});
7
9
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
8
10
  const errMsg = error ? 'There are one or more errors below.' : null;
9
11
  const errors = error || {};
10
12
  const fields = [
11
- {
12
- label: 'First Name',
13
- name: 'first_name',
14
- type: 'default',
15
- required: true,
16
- },
17
- { label: 'Last Name', name: 'last_name', type: 'default', required: true },
13
+ { label: 'First Name', name: 'first_name', type: 'text', required: true },
14
+ { label: 'Last Name', name: 'last_name', type: 'text', required: true },
18
15
  {
19
16
  label: 'Email',
20
17
  name: 'email',
21
- type: 'email-address',
18
+ type: 'email',
22
19
  required: true,
23
20
  autoComplete: 'email',
24
21
  },
25
- { label: 'Phone', name: 'phone', type: 'number-pad', required: true },
26
- { label: 'Company', name: 'company', type: 'default' },
27
- {
28
- label: 'Birth Date (mm/dd/yyyy)',
29
- name: 'birth_date',
30
- type: 'number-pad',
31
- },
22
+ { label: 'Phone', name: 'phone', type: 'tel', required: true },
23
+ { label: 'Company', name: 'company', type: 'text' },
32
24
  ];
33
25
  (0, react_1.useEffect)(() => {
34
26
  if (loading === 'idle') {
35
27
  setSubmitting(false);
28
+ if (error) {
29
+ //handle form inputs[0] focus
30
+ }
36
31
  }
37
- }, [loading]);
38
- (0, react_1.useEffect)(() => {
39
- if (errors.birth_date) {
40
- errors.birth_date = 'Invalid date. Please enter in mm/dd/yyyy format.';
41
- }
42
- }, [errors]);
32
+ }, [loading, error]);
43
33
  (0, react_1.useEffect)(() => {
44
34
  setData(profile);
45
35
  }, [profile]);
46
- const handleChange = (id, type, value, checked) => {
36
+ const handleChange = (name, type, value, checked) => {
47
37
  const inputValue = type === 'checkbox'
48
38
  ? checked
49
- : id === 'phone'
50
- ? (0, utils_1.makePhone)(value)
51
- : id === 'birth_date'
52
- ? (0, utils_1.makeBirthDate)(value)
39
+ : name === 'phone'
40
+ ? (0, utils_1.makePhone)(value !== null && value !== void 0 ? value : '')
41
+ : name === 'birth_date'
42
+ ? (0, utils_1.makeBirthDate)(value !== null && value !== void 0 ? value : '')
53
43
  : value;
54
- setData(Object.assign(Object.assign({}, data), { [id]: inputValue }));
44
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
45
+ };
46
+ const handleRadio = (name, value) => {
47
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
55
48
  };
56
49
  const handleSubmit = () => {
50
+ var _a, _b;
57
51
  setSubmitting(true);
58
52
  const updatedData = fields.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: data[i.name] })), {});
59
53
  updatedData.birth_date = data.birth_date
@@ -61,14 +55,19 @@ const useProfileForm = (profile, loading, error, update) => {
61
55
  : null;
62
56
  updatedData.gender = data.gender || null;
63
57
  update(updatedData);
58
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
64
59
  };
65
60
  return {
61
+ submitRef,
62
+ accepts_marketing,
63
+ order_notifications,
66
64
  data,
67
65
  errors,
68
66
  submitting,
69
67
  fields,
70
68
  errMsg,
71
69
  handleChange,
70
+ handleRadio,
72
71
  handleSubmit,
73
72
  };
74
73
  };
@@ -0,0 +1,20 @@
1
+ /// <reference types="react" />
2
+ import { RequestStatus, ResetPasswordData } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ interface ResetPasswordFormField {
5
+ label: string;
6
+ name: string;
7
+ type: string;
8
+ required?: boolean;
9
+ }
10
+ declare const useResetPasswordForm: <InputRef extends Field, SubmitRef extends Field>(loading: RequestStatus, error: any, reset: (newPassword: string, callback: () => void) => void, resetForm: () => void, resetToken: () => void) => {
11
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
12
+ inputRef: import("react").MutableRefObject<InputRef | null>;
13
+ fields: ResetPasswordFormField[];
14
+ data: Partial<ResetPasswordData>;
15
+ errors: {};
16
+ submitting: boolean;
17
+ handleChange: (name: string, value: string) => void;
18
+ handleSubmit: () => void;
19
+ };
20
+ export default useResetPasswordForm;