@open-tender/utils 0.4.30 → 0.4.32

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 (245) hide show
  1. package/LICENSE.md +21 -21
  2. package/README.md +2 -2
  3. package/dist/cjs/__tests__/utils/helpers.test.d.ts +1 -1
  4. package/dist/cjs/__tests__/utils/helpers.test.js +96 -96
  5. package/dist/cjs/__tests__/utils/theme.test.d.ts +1 -1
  6. package/dist/cjs/__tests__/utils/theme.test.js +37 -37
  7. package/dist/cjs/hooks/index.d.ts +35 -35
  8. package/dist/cjs/hooks/index.js +38 -38
  9. package/dist/cjs/hooks/types.d.ts +4 -4
  10. package/dist/cjs/hooks/types.js +2 -2
  11. package/dist/cjs/hooks/useAccordion.d.ts +22 -22
  12. package/dist/cjs/hooks/useAccordion.js +56 -56
  13. package/dist/cjs/hooks/useAddressForm.d.ts +12 -12
  14. package/dist/cjs/hooks/useAddressForm.js +83 -83
  15. package/dist/cjs/hooks/useAllergenForm.d.ts +10 -10
  16. package/dist/cjs/hooks/useAllergenForm.js +58 -58
  17. package/dist/cjs/hooks/useBuilder.d.ts +14 -14
  18. package/dist/cjs/hooks/useBuilder.js +144 -144
  19. package/dist/cjs/hooks/useCartGuestForm.d.ts +12 -12
  20. package/dist/cjs/hooks/useCartGuestForm.js +58 -58
  21. package/dist/cjs/hooks/useCartPoints.d.ts +13 -13
  22. package/dist/cjs/hooks/useCartPoints.js +74 -74
  23. package/dist/cjs/hooks/useCommunicationPreferences.js +1 -1
  24. package/dist/cjs/hooks/useCreditCard.d.ts +13 -13
  25. package/dist/cjs/hooks/useCreditCard.js +63 -63
  26. package/dist/cjs/hooks/useCreditCardForm.d.ts +10 -10
  27. package/dist/cjs/hooks/useCreditCardForm.js +71 -71
  28. package/dist/cjs/hooks/useCurbside.d.ts +9 -9
  29. package/dist/cjs/hooks/useCurbside.js +39 -39
  30. package/dist/cjs/hooks/useDineInForm.d.ts +11 -11
  31. package/dist/cjs/hooks/useDineInForm.js +56 -56
  32. package/dist/cjs/hooks/useDonationForm.d.ts +28 -28
  33. package/dist/cjs/hooks/useDonationForm.js +172 -172
  34. package/dist/cjs/hooks/useFirstTimes.d.ts +18 -18
  35. package/dist/cjs/hooks/useFirstTimes.js +81 -81
  36. package/dist/cjs/hooks/useGiftCardAssignForm.d.ts +14 -14
  37. package/dist/cjs/hooks/useGiftCardAssignForm.js +57 -57
  38. package/dist/cjs/hooks/useGiftCardAssignOtherForm.d.ts +14 -14
  39. package/dist/cjs/hooks/useGiftCardAssignOtherForm.js +50 -50
  40. package/dist/cjs/hooks/useGiftCardForm.d.ts +12 -12
  41. package/dist/cjs/hooks/useGiftCardForm.js +74 -74
  42. package/dist/cjs/hooks/useGiftCardsForm.d.ts +31 -31
  43. package/dist/cjs/hooks/useGiftCardsForm.js +195 -195
  44. package/dist/cjs/hooks/useGuestCheckoutForm.d.ts +21 -21
  45. package/dist/cjs/hooks/useGuestCheckoutForm.js +89 -89
  46. package/dist/cjs/hooks/useGuestForm.d.ts +12 -12
  47. package/dist/cjs/hooks/useGuestForm.js +60 -60
  48. package/dist/cjs/hooks/useImage.d.ts +5 -5
  49. package/dist/cjs/hooks/useImage.js +33 -33
  50. package/dist/cjs/hooks/useLoginForm.d.ts +12 -12
  51. package/dist/cjs/hooks/useLoginForm.js +67 -67
  52. package/dist/cjs/hooks/useMenuItem.d.ts +57 -55
  53. package/dist/cjs/hooks/useMenuItem.js +67 -67
  54. package/dist/cjs/hooks/useModifier.d.ts +53 -51
  55. package/dist/cjs/hooks/useModifier.js +72 -72
  56. package/dist/cjs/hooks/useModifierGroup.d.ts +15 -15
  57. package/dist/cjs/hooks/useModifierGroup.js +17 -17
  58. package/dist/cjs/hooks/useOneTimePasscodeForm.d.ts +16 -16
  59. package/dist/cjs/hooks/useOneTimePasscodeForm.js +87 -87
  60. package/dist/cjs/hooks/useOrderFulfillmentForm.d.ts +11 -11
  61. package/dist/cjs/hooks/useOrderFulfillmentForm.js +65 -65
  62. package/dist/cjs/hooks/useOrderItem.d.ts +48 -46
  63. package/dist/cjs/hooks/useOrderItem.js +54 -54
  64. package/dist/cjs/hooks/useOrderRating.d.ts +10 -10
  65. package/dist/cjs/hooks/useOrderRating.js +34 -34
  66. package/dist/cjs/hooks/useOrderRatingForm.d.ts +11 -11
  67. package/dist/cjs/hooks/useOrderRatingForm.js +35 -35
  68. package/dist/cjs/hooks/useProfileForm.d.ts +15 -15
  69. package/dist/cjs/hooks/useProfileForm.js +116 -116
  70. package/dist/cjs/hooks/useResetPasswordForm.d.ts +12 -12
  71. package/dist/cjs/hooks/useResetPasswordForm.js +77 -77
  72. package/dist/cjs/hooks/useSendResetForm.d.ts +14 -14
  73. package/dist/cjs/hooks/useSendResetForm.js +57 -57
  74. package/dist/cjs/hooks/useSignUpForm.d.ts +20 -20
  75. package/dist/cjs/hooks/useSignUpForm.js +151 -151
  76. package/dist/cjs/hooks/useSignUpGuestForm.d.ts +23 -23
  77. package/dist/cjs/hooks/useSignUpGuestForm.js +122 -113
  78. package/dist/cjs/hooks/useSigninForm.d.ts +12 -12
  79. package/dist/cjs/hooks/useSigninForm.js +66 -66
  80. package/dist/cjs/index.d.ts +2 -2
  81. package/dist/cjs/index.js +5 -5
  82. package/dist/cjs/utils/cards.d.ts +20 -20
  83. package/dist/cjs/utils/cards.js +155 -155
  84. package/dist/cjs/utils/cart.d.ts +202 -198
  85. package/dist/cjs/utils/cart.js +909 -905
  86. package/dist/cjs/utils/checkout.d.ts +55 -55
  87. package/dist/cjs/utils/checkout.js +125 -125
  88. package/dist/cjs/utils/constants.d.ts +43 -43
  89. package/dist/cjs/utils/constants.js +58 -58
  90. package/dist/cjs/utils/customer.d.ts +4 -4
  91. package/dist/cjs/utils/customer.js +34 -34
  92. package/dist/cjs/utils/datetimes.d.ts +99 -99
  93. package/dist/cjs/utils/datetimes.js +601 -601
  94. package/dist/cjs/utils/errors.d.ts +51 -51
  95. package/dist/cjs/utils/errors.js +191 -191
  96. package/dist/cjs/utils/favorites.d.ts +10 -10
  97. package/dist/cjs/utils/favorites.js +119 -119
  98. package/dist/cjs/utils/googleMaps.d.ts +14 -14
  99. package/dist/cjs/utils/googleMaps.js +30 -30
  100. package/dist/cjs/utils/helpers.d.ts +63 -63
  101. package/dist/cjs/utils/helpers.js +244 -244
  102. package/dist/cjs/utils/index.d.ts +19 -19
  103. package/dist/cjs/utils/index.js +23 -23
  104. package/dist/cjs/utils/keypad.d.ts +9 -9
  105. package/dist/cjs/utils/keypad.js +105 -105
  106. package/dist/cjs/utils/maps.d.ts +226 -226
  107. package/dist/cjs/utils/maps.js +334 -334
  108. package/dist/cjs/utils/menu.d.ts +117 -113
  109. package/dist/cjs/utils/menu.js +82 -82
  110. package/dist/cjs/utils/orders.d.ts +21 -21
  111. package/dist/cjs/utils/orders.js +202 -202
  112. package/dist/cjs/utils/pizzaBuilder.d.ts +10 -10
  113. package/dist/cjs/utils/pizzaBuilder.js +43 -43
  114. package/dist/cjs/utils/refunds.d.ts +35 -35
  115. package/dist/cjs/utils/refunds.js +98 -98
  116. package/dist/cjs/utils/revenueCenters.d.ts +11 -11
  117. package/dist/cjs/utils/revenueCenters.js +125 -125
  118. package/dist/cjs/utils/rewards.d.ts +4 -4
  119. package/dist/cjs/utils/rewards.js +106 -106
  120. package/dist/cjs/utils/theme.d.ts +27 -27
  121. package/dist/cjs/utils/theme.js +266 -266
  122. package/dist/cjs/utils/version.d.ts +4 -4
  123. package/dist/cjs/utils/version.js +38 -38
  124. package/dist/esm/__tests__/utils/helpers.test.d.ts +1 -1
  125. package/dist/esm/__tests__/utils/helpers.test.js +94 -94
  126. package/dist/esm/__tests__/utils/theme.test.d.ts +1 -1
  127. package/dist/esm/__tests__/utils/theme.test.js +35 -35
  128. package/dist/esm/hooks/index.d.ts +35 -35
  129. package/dist/esm/hooks/index.js +35 -35
  130. package/dist/esm/hooks/types.d.ts +4 -4
  131. package/dist/esm/hooks/types.js +1 -1
  132. package/dist/esm/hooks/useAccordion.d.ts +22 -22
  133. package/dist/esm/hooks/useAccordion.js +52 -52
  134. package/dist/esm/hooks/useAddressForm.d.ts +12 -12
  135. package/dist/esm/hooks/useAddressForm.js +79 -79
  136. package/dist/esm/hooks/useAllergenForm.d.ts +10 -10
  137. package/dist/esm/hooks/useAllergenForm.js +54 -54
  138. package/dist/esm/hooks/useBuilder.d.ts +14 -14
  139. package/dist/esm/hooks/useBuilder.js +140 -140
  140. package/dist/esm/hooks/useCartGuestForm.d.ts +12 -12
  141. package/dist/esm/hooks/useCartGuestForm.js +54 -54
  142. package/dist/esm/hooks/useCartPoints.d.ts +13 -13
  143. package/dist/esm/hooks/useCartPoints.js +70 -70
  144. package/dist/esm/hooks/useCommunicationPreferences.js +1 -1
  145. package/dist/esm/hooks/useCreditCard.d.ts +13 -13
  146. package/dist/esm/hooks/useCreditCard.js +59 -59
  147. package/dist/esm/hooks/useCreditCardForm.d.ts +10 -10
  148. package/dist/esm/hooks/useCreditCardForm.js +67 -67
  149. package/dist/esm/hooks/useCurbside.d.ts +9 -9
  150. package/dist/esm/hooks/useCurbside.js +35 -35
  151. package/dist/esm/hooks/useDineInForm.d.ts +11 -11
  152. package/dist/esm/hooks/useDineInForm.js +52 -52
  153. package/dist/esm/hooks/useDonationForm.d.ts +28 -28
  154. package/dist/esm/hooks/useDonationForm.js +168 -168
  155. package/dist/esm/hooks/useFirstTimes.d.ts +18 -18
  156. package/dist/esm/hooks/useFirstTimes.js +77 -77
  157. package/dist/esm/hooks/useGiftCardAssignForm.d.ts +14 -14
  158. package/dist/esm/hooks/useGiftCardAssignForm.js +53 -53
  159. package/dist/esm/hooks/useGiftCardAssignOtherForm.d.ts +14 -14
  160. package/dist/esm/hooks/useGiftCardAssignOtherForm.js +46 -46
  161. package/dist/esm/hooks/useGiftCardForm.d.ts +12 -12
  162. package/dist/esm/hooks/useGiftCardForm.js +70 -70
  163. package/dist/esm/hooks/useGiftCardsForm.d.ts +31 -31
  164. package/dist/esm/hooks/useGiftCardsForm.js +191 -191
  165. package/dist/esm/hooks/useGuestCheckoutForm.d.ts +21 -21
  166. package/dist/esm/hooks/useGuestCheckoutForm.js +85 -85
  167. package/dist/esm/hooks/useGuestForm.d.ts +12 -12
  168. package/dist/esm/hooks/useGuestForm.js +56 -56
  169. package/dist/esm/hooks/useImage.d.ts +5 -5
  170. package/dist/esm/hooks/useImage.js +29 -29
  171. package/dist/esm/hooks/useLoginForm.d.ts +12 -12
  172. package/dist/esm/hooks/useLoginForm.js +63 -63
  173. package/dist/esm/hooks/useMenuItem.d.ts +57 -55
  174. package/dist/esm/hooks/useMenuItem.js +63 -63
  175. package/dist/esm/hooks/useModifier.d.ts +53 -51
  176. package/dist/esm/hooks/useModifier.js +68 -68
  177. package/dist/esm/hooks/useModifierGroup.d.ts +15 -15
  178. package/dist/esm/hooks/useModifierGroup.js +13 -13
  179. package/dist/esm/hooks/useOneTimePasscodeForm.d.ts +16 -16
  180. package/dist/esm/hooks/useOneTimePasscodeForm.js +83 -83
  181. package/dist/esm/hooks/useOrderFulfillmentForm.d.ts +11 -11
  182. package/dist/esm/hooks/useOrderFulfillmentForm.js +61 -61
  183. package/dist/esm/hooks/useOrderItem.d.ts +48 -46
  184. package/dist/esm/hooks/useOrderItem.js +50 -50
  185. package/dist/esm/hooks/useOrderRating.d.ts +10 -10
  186. package/dist/esm/hooks/useOrderRating.js +30 -30
  187. package/dist/esm/hooks/useOrderRatingForm.d.ts +11 -11
  188. package/dist/esm/hooks/useOrderRatingForm.js +31 -31
  189. package/dist/esm/hooks/useProfileForm.d.ts +15 -15
  190. package/dist/esm/hooks/useProfileForm.js +112 -112
  191. package/dist/esm/hooks/useResetPasswordForm.d.ts +12 -12
  192. package/dist/esm/hooks/useResetPasswordForm.js +73 -73
  193. package/dist/esm/hooks/useSendResetForm.d.ts +14 -14
  194. package/dist/esm/hooks/useSendResetForm.js +53 -53
  195. package/dist/esm/hooks/useSignUpForm.d.ts +20 -20
  196. package/dist/esm/hooks/useSignUpForm.js +147 -147
  197. package/dist/esm/hooks/useSignUpGuestForm.d.ts +23 -23
  198. package/dist/esm/hooks/useSignUpGuestForm.js +118 -109
  199. package/dist/esm/hooks/useSigninForm.d.ts +12 -12
  200. package/dist/esm/hooks/useSigninForm.js +62 -62
  201. package/dist/esm/index.d.ts +2 -2
  202. package/dist/esm/index.js +2 -2
  203. package/dist/esm/utils/cards.d.ts +20 -20
  204. package/dist/esm/utils/cards.js +146 -146
  205. package/dist/esm/utils/cart.d.ts +202 -198
  206. package/dist/esm/utils/cart.js +868 -864
  207. package/dist/esm/utils/checkout.d.ts +55 -55
  208. package/dist/esm/utils/checkout.js +117 -117
  209. package/dist/esm/utils/constants.d.ts +43 -43
  210. package/dist/esm/utils/constants.js +55 -55
  211. package/dist/esm/utils/customer.d.ts +4 -4
  212. package/dist/esm/utils/customer.js +29 -29
  213. package/dist/esm/utils/datetimes.d.ts +99 -99
  214. package/dist/esm/utils/datetimes.js +531 -531
  215. package/dist/esm/utils/errors.d.ts +51 -51
  216. package/dist/esm/utils/errors.js +180 -180
  217. package/dist/esm/utils/favorites.d.ts +10 -10
  218. package/dist/esm/utils/favorites.js +109 -109
  219. package/dist/esm/utils/googleMaps.d.ts +14 -14
  220. package/dist/esm/utils/googleMaps.js +26 -26
  221. package/dist/esm/utils/helpers.d.ts +63 -63
  222. package/dist/esm/utils/helpers.js +205 -205
  223. package/dist/esm/utils/index.d.ts +19 -19
  224. package/dist/esm/utils/index.js +20 -20
  225. package/dist/esm/utils/keypad.d.ts +9 -9
  226. package/dist/esm/utils/keypad.js +97 -97
  227. package/dist/esm/utils/maps.d.ts +226 -226
  228. package/dist/esm/utils/maps.js +316 -316
  229. package/dist/esm/utils/menu.d.ts +117 -113
  230. package/dist/esm/utils/menu.js +74 -74
  231. package/dist/esm/utils/orders.d.ts +21 -21
  232. package/dist/esm/utils/orders.js +185 -185
  233. package/dist/esm/utils/pizzaBuilder.d.ts +10 -10
  234. package/dist/esm/utils/pizzaBuilder.js +38 -38
  235. package/dist/esm/utils/refunds.d.ts +35 -35
  236. package/dist/esm/utils/refunds.js +90 -90
  237. package/dist/esm/utils/revenueCenters.d.ts +11 -11
  238. package/dist/esm/utils/revenueCenters.js +115 -115
  239. package/dist/esm/utils/rewards.d.ts +4 -4
  240. package/dist/esm/utils/rewards.js +100 -100
  241. package/dist/esm/utils/theme.d.ts +27 -27
  242. package/dist/esm/utils/theme.js +254 -254
  243. package/dist/esm/utils/version.d.ts +4 -4
  244. package/dist/esm/utils/version.js +34 -34
  245. package/package.json +69 -68
@@ -1,195 +1,195 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useGiftCardsForm = void 0;
4
- const react_1 = require("react");
5
- const utils_1 = require("../utils");
6
- const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, initState, customer, creditCards, recaptchaKey, cardData, cardType, url) => {
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);
12
- const [cards, setCards] = (0, react_1.useState)([initState]);
13
- const [isNewCard, setIsNewCard] = (0, react_1.useState)(true);
14
- const [creditCard, setCreditCard] = (0, react_1.useState)(null);
15
- const [creditCardOptions, setCreditCardOptions] = (0, react_1.useState)([]);
16
- const [errors, setErrors] = (0, react_1.useState)({});
17
- const [submitting, setSubmitting] = (0, react_1.useState)(false);
18
- const newCardErrors = (0, react_1.useMemo)(() => errors
19
- ? Object.entries(errors)
20
- .filter(([key]) => key !== 'form')
21
- .reduce((obj, [key, value]) => (Object.assign(Object.assign({}, obj), { [key]: value })), {})
22
- : {}, [errors]);
23
- (0, react_1.useEffect)(() => {
24
- if (loading === 'idle') {
25
- setSubmitting(false);
26
- setAlert({ type: 'close' });
27
- if (error) {
28
- if (recaptchaRef.current)
29
- recaptchaRef.current.reset();
30
- setErrors((0, utils_1.makeFormErrors)(error));
31
- }
32
- }
33
- }, [loading, error, setAlert, success]);
34
- (0, react_1.useEffect)(() => {
35
- if (creditCards === null || creditCards === void 0 ? void 0 : creditCards.length) {
36
- const options = creditCards.map(i => ({
37
- name: `${i.card_type_name} ending in ${i.last4}`,
38
- value: i.customer_card_id
39
- }));
40
- setCreditCardOptions(options);
41
- const defaultCard = creditCards.length
42
- ? { customer_card_id: creditCards[0].customer_card_id }
43
- : null;
44
- setCreditCard(defaultCard);
45
- setIsNewCard(false);
46
- }
47
- }, [creditCards]);
48
- (0, react_1.useEffect)(() => {
49
- var _a;
50
- if (customer) {
51
- setEmail((_a = customer.email) !== null && _a !== void 0 ? _a : null);
52
- setName(`${customer.first_name} ${customer.last_name}`);
53
- }
54
- }, [customer]);
55
- const makeGiftCards = (giftCards) => {
56
- return giftCards.reduce((arr, card) => {
57
- const { amount, quantity, email } = card;
58
- const giftCard = !email || !email.length ? { amount, quantity } : card;
59
- return [...arr, giftCard];
60
- }, []);
61
- };
62
- const handleName = (value) => {
63
- setName(value);
64
- };
65
- const handleEmail = (value) => {
66
- setEmail(value);
67
- };
68
- const handleChange = (name, value) => {
69
- const [field, index] = name.split('-');
70
- const newCards = cards.map((card, idx) => {
71
- return idx === parseInt(index) ? Object.assign(Object.assign({}, card), { [field]: value }) : card;
72
- });
73
- setCards(newCards);
74
- };
75
- const handleQuantity = (index, quantity) => {
76
- const newCards = quantity > 0
77
- ? cards.map((card, idx) => {
78
- return idx === parseInt(`${index}`)
79
- ? Object.assign(Object.assign({}, card), { quantity: quantity }) : card;
80
- })
81
- : cards.filter((_, idx) => idx !== index);
82
- setCards(newCards);
83
- };
84
- const handleAddAnother = () => {
85
- setCards([...cards, initState]);
86
- };
87
- const handleCreditCard = (value) => {
88
- const customerCardId = parseInt(`${value}`);
89
- setCreditCard({ customer_card_id: customerCardId });
90
- };
91
- const purchaseWithCaptcha = (credit_card) => {
92
- var _a;
93
- const alert = {
94
- type: 'working',
95
- args: { text: 'Submitting your purchase...' }
96
- };
97
- if (recaptchaKey) {
98
- try {
99
- const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
100
- if (!token) {
101
- setSubmitting(false);
102
- setErrors({
103
- form: 'Please complete the recaptcha before submitting'
104
- });
105
- }
106
- else {
107
- setSubmitting(true);
108
- setAlert(alert);
109
- const gift_cards = makeGiftCards(cards);
110
- purchase({
111
- token,
112
- credit_card,
113
- name: name !== null && name !== void 0 ? name : '',
114
- email: email !== null && email !== void 0 ? email : '',
115
- url: url !== null && url !== void 0 ? url : '',
116
- gift_cards
117
- });
118
- }
119
- }
120
- catch (err) {
121
- setSubmitting(false);
122
- setErrors({ form: 'Please complete the recaptcha before submitting' });
123
- }
124
- }
125
- else {
126
- setSubmitting(true);
127
- setAlert(alert);
128
- const gift_cards = makeGiftCards(cards);
129
- purchase({
130
- credit_card,
131
- name: name !== null && name !== void 0 ? name : '',
132
- email: email !== null && email !== void 0 ? email : '',
133
- url: url !== null && url !== void 0 ? url : '',
134
- gift_cards
135
- });
136
- }
137
- };
138
- const handleSubmit = (evt) => {
139
- var _a, _b, _c;
140
- evt === null || evt === void 0 ? void 0 : evt.preventDefault();
141
- if (!name || !email) {
142
- setErrors({ form: 'Both name and email are required' });
143
- if (inputRef.current)
144
- ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && inputRef.current.focus();
145
- }
146
- else {
147
- if (isNewCard) {
148
- if (cardData && cardType) {
149
- const { card, errors } = (0, utils_1.validateCreditCard)(cardData, cardType);
150
- if (errors) {
151
- setErrors(Object.assign(Object.assign({}, errors), { form: 'There are one or more credit card errors below' }));
152
- setSubmitting(false);
153
- }
154
- else {
155
- purchaseWithCaptcha(card);
156
- }
157
- }
158
- }
159
- else {
160
- if (creditCard)
161
- purchaseWithCaptcha(creditCard);
162
- }
163
- ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
164
- }
165
- };
166
- const handleReset = () => {
167
- setCards([initState]);
168
- setErrors({});
169
- reset();
170
- };
171
- return {
172
- inputRef,
173
- submitRef,
174
- recaptchaRef,
175
- creditCardOptions,
176
- handleName,
177
- handleEmail,
178
- handleChange,
179
- handleQuantity,
180
- handleAddAnother,
181
- handleCreditCard,
182
- handleSubmit,
183
- handleReset,
184
- name,
185
- email,
186
- cards,
187
- isNewCard,
188
- newCardErrors,
189
- creditCard,
190
- setCreditCard,
191
- errors,
192
- submitting
193
- };
194
- };
195
- exports.useGiftCardsForm = useGiftCardsForm;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useGiftCardsForm = void 0;
4
+ const react_1 = require("react");
5
+ const utils_1 = require("../utils");
6
+ const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, initState, customer, creditCards, recaptchaKey, cardData, cardType, url) => {
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);
12
+ const [cards, setCards] = (0, react_1.useState)([initState]);
13
+ const [isNewCard, setIsNewCard] = (0, react_1.useState)(true);
14
+ const [creditCard, setCreditCard] = (0, react_1.useState)(null);
15
+ const [creditCardOptions, setCreditCardOptions] = (0, react_1.useState)([]);
16
+ const [errors, setErrors] = (0, react_1.useState)({});
17
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
18
+ const newCardErrors = (0, react_1.useMemo)(() => errors
19
+ ? Object.entries(errors)
20
+ .filter(([key]) => key !== 'form')
21
+ .reduce((obj, [key, value]) => (Object.assign(Object.assign({}, obj), { [key]: value })), {})
22
+ : {}, [errors]);
23
+ (0, react_1.useEffect)(() => {
24
+ if (loading === 'idle') {
25
+ setSubmitting(false);
26
+ setAlert({ type: 'close' });
27
+ if (error) {
28
+ if (recaptchaRef.current)
29
+ recaptchaRef.current.reset();
30
+ setErrors((0, utils_1.makeFormErrors)(error));
31
+ }
32
+ }
33
+ }, [loading, error, setAlert, success]);
34
+ (0, react_1.useEffect)(() => {
35
+ if (creditCards === null || creditCards === void 0 ? void 0 : creditCards.length) {
36
+ const options = creditCards.map(i => ({
37
+ name: `${i.card_type_name} ending in ${i.last4}`,
38
+ value: i.customer_card_id
39
+ }));
40
+ setCreditCardOptions(options);
41
+ const defaultCard = creditCards.length
42
+ ? { customer_card_id: creditCards[0].customer_card_id }
43
+ : null;
44
+ setCreditCard(defaultCard);
45
+ setIsNewCard(false);
46
+ }
47
+ }, [creditCards]);
48
+ (0, react_1.useEffect)(() => {
49
+ var _a;
50
+ if (customer) {
51
+ setEmail((_a = customer.email) !== null && _a !== void 0 ? _a : null);
52
+ setName(`${customer.first_name} ${customer.last_name}`);
53
+ }
54
+ }, [customer]);
55
+ const makeGiftCards = (giftCards) => {
56
+ return giftCards.reduce((arr, card) => {
57
+ const { amount, quantity, email } = card;
58
+ const giftCard = !email || !email.length ? { amount, quantity } : card;
59
+ return [...arr, giftCard];
60
+ }, []);
61
+ };
62
+ const handleName = (value) => {
63
+ setName(value);
64
+ };
65
+ const handleEmail = (value) => {
66
+ setEmail(value);
67
+ };
68
+ const handleChange = (name, value) => {
69
+ const [field, index] = name.split('-');
70
+ const newCards = cards.map((card, idx) => {
71
+ return idx === parseInt(index) ? Object.assign(Object.assign({}, card), { [field]: value }) : card;
72
+ });
73
+ setCards(newCards);
74
+ };
75
+ const handleQuantity = (index, quantity) => {
76
+ const newCards = quantity > 0
77
+ ? cards.map((card, idx) => {
78
+ return idx === parseInt(`${index}`)
79
+ ? Object.assign(Object.assign({}, card), { quantity: quantity }) : card;
80
+ })
81
+ : cards.filter((_, idx) => idx !== index);
82
+ setCards(newCards);
83
+ };
84
+ const handleAddAnother = () => {
85
+ setCards([...cards, initState]);
86
+ };
87
+ const handleCreditCard = (value) => {
88
+ const customerCardId = parseInt(`${value}`);
89
+ setCreditCard({ customer_card_id: customerCardId });
90
+ };
91
+ const purchaseWithCaptcha = (credit_card) => {
92
+ var _a;
93
+ const alert = {
94
+ type: 'working',
95
+ args: { text: 'Submitting your purchase...' }
96
+ };
97
+ if (recaptchaKey) {
98
+ try {
99
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
100
+ if (!token) {
101
+ setSubmitting(false);
102
+ setErrors({
103
+ form: 'Please complete the recaptcha before submitting'
104
+ });
105
+ }
106
+ else {
107
+ setSubmitting(true);
108
+ setAlert(alert);
109
+ const gift_cards = makeGiftCards(cards);
110
+ purchase({
111
+ token,
112
+ credit_card,
113
+ name: name !== null && name !== void 0 ? name : '',
114
+ email: email !== null && email !== void 0 ? email : '',
115
+ url: url !== null && url !== void 0 ? url : '',
116
+ gift_cards
117
+ });
118
+ }
119
+ }
120
+ catch (err) {
121
+ setSubmitting(false);
122
+ setErrors({ form: 'Please complete the recaptcha before submitting' });
123
+ }
124
+ }
125
+ else {
126
+ setSubmitting(true);
127
+ setAlert(alert);
128
+ const gift_cards = makeGiftCards(cards);
129
+ purchase({
130
+ credit_card,
131
+ name: name !== null && name !== void 0 ? name : '',
132
+ email: email !== null && email !== void 0 ? email : '',
133
+ url: url !== null && url !== void 0 ? url : '',
134
+ gift_cards
135
+ });
136
+ }
137
+ };
138
+ const handleSubmit = (evt) => {
139
+ var _a, _b, _c;
140
+ evt === null || evt === void 0 ? void 0 : evt.preventDefault();
141
+ if (!name || !email) {
142
+ setErrors({ form: 'Both name and email are required' });
143
+ if (inputRef.current)
144
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && inputRef.current.focus();
145
+ }
146
+ else {
147
+ if (isNewCard) {
148
+ if (cardData && cardType) {
149
+ const { card, errors } = (0, utils_1.validateCreditCard)(cardData, cardType);
150
+ if (errors) {
151
+ setErrors(Object.assign(Object.assign({}, errors), { form: 'There are one or more credit card errors below' }));
152
+ setSubmitting(false);
153
+ }
154
+ else {
155
+ purchaseWithCaptcha(card);
156
+ }
157
+ }
158
+ }
159
+ else {
160
+ if (creditCard)
161
+ purchaseWithCaptcha(creditCard);
162
+ }
163
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
164
+ }
165
+ };
166
+ const handleReset = () => {
167
+ setCards([initState]);
168
+ setErrors({});
169
+ reset();
170
+ };
171
+ return {
172
+ inputRef,
173
+ submitRef,
174
+ recaptchaRef,
175
+ creditCardOptions,
176
+ handleName,
177
+ handleEmail,
178
+ handleChange,
179
+ handleQuantity,
180
+ handleAddAnother,
181
+ handleCreditCard,
182
+ handleSubmit,
183
+ handleReset,
184
+ name,
185
+ email,
186
+ cards,
187
+ isNewCard,
188
+ newCardErrors,
189
+ creditCard,
190
+ setCreditCard,
191
+ errors,
192
+ submitting
193
+ };
194
+ };
195
+ exports.useGiftCardsForm = useGiftCardsForm;
@@ -1,21 +1,21 @@
1
- /// <reference types="react" />
2
- import { CheckoutFormCustomer, FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useGuestCheckoutForm: (guest: CheckoutFormCustomer, loading: RequestStatus, error: RequestError, guestErrors: Record<string, string> | null, submit: (data: CheckoutFormCustomer) => void) => {
4
- submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
- inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
- fields: FormFields;
7
- data: {
8
- company?: string | undefined;
9
- customer_id?: number | undefined;
10
- email?: string | undefined;
11
- first_name?: string | undefined;
12
- password?: string | undefined;
13
- phone?: string | undefined;
14
- last_name?: string | undefined;
15
- };
16
- errors: Record<string, string>;
17
- disabled: boolean;
18
- submitting: boolean;
19
- handleChange: (name: string, value: string | number | boolean) => void;
20
- handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
21
- };
1
+ /// <reference types="react" />
2
+ import { CheckoutFormCustomer, FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useGuestCheckoutForm: (guest: CheckoutFormCustomer, loading: RequestStatus, error: RequestError, guestErrors: Record<string, string> | null, submit: (data: CheckoutFormCustomer) => void) => {
4
+ submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
+ inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ fields: FormFields;
7
+ data: {
8
+ company?: string | undefined;
9
+ customer_id?: number | undefined;
10
+ email?: string | undefined;
11
+ first_name?: string | undefined;
12
+ password?: string | undefined;
13
+ phone?: string | undefined;
14
+ last_name?: string | undefined;
15
+ };
16
+ errors: Record<string, string>;
17
+ disabled: boolean;
18
+ submitting: boolean;
19
+ handleChange: (name: string, value: string | number | boolean) => void;
20
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
21
+ };
@@ -1,89 +1,89 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useGuestCheckoutForm = void 0;
4
- const react_1 = require("react");
5
- const utils_1 = require("../utils");
6
- const initialData = {
7
- first_name: '',
8
- last_name: '',
9
- email: '',
10
- phone: ''
11
- };
12
- const useGuestCheckoutForm = (guest, loading, error, guestErrors, submit) => {
13
- const submitRef = (0, react_1.useRef)(null);
14
- const inputRef = (0, react_1.useRef)(null);
15
- const initData = Object.assign(Object.assign({}, initialData), guest);
16
- const [data, setData] = (0, react_1.useState)(initData);
17
- const [errors, setErrors] = (0, react_1.useState)({});
18
- const [submitting, setSubmitting] = (0, react_1.useState)(false);
19
- const disabled = (0, utils_1.checkEmpty)(data);
20
- const fields = [
21
- {
22
- label: 'First Name',
23
- name: 'first_name',
24
- type: 'text',
25
- required: true
26
- },
27
- {
28
- label: 'Last Name',
29
- name: 'last_name',
30
- type: 'text',
31
- required: true
32
- },
33
- {
34
- label: 'Email',
35
- name: 'email',
36
- type: 'email',
37
- required: true
38
- },
39
- { label: 'Phone', name: 'phone', type: 'tel', required: true }
40
- ];
41
- const handleChange = (name, value) => {
42
- const val = name === 'phone' ? (0, utils_1.makePhone)(value) : value;
43
- setData(Object.assign(Object.assign({}, data), { [name]: val }));
44
- };
45
- const handleSubmit = (evt) => {
46
- var _a, _b;
47
- evt === null || evt === void 0 ? void 0 : evt.preventDefault();
48
- if (!disabled) {
49
- setErrors({});
50
- setSubmitting(true);
51
- submit(data);
52
- ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
53
- }
54
- };
55
- (0, react_1.useEffect)(() => {
56
- var _a, _b;
57
- if (loading === 'idle') {
58
- setSubmitting(false);
59
- if (error) {
60
- const errs = (0, utils_1.makeFormErrors)(error);
61
- errs.form = 'There are one or more errors below';
62
- setErrors(errs);
63
- ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
64
- }
65
- else if (guestErrors) {
66
- setErrors(guestErrors);
67
- }
68
- }
69
- }, [loading, error, guestErrors]);
70
- (0, react_1.useEffect)(() => {
71
- return () => {
72
- setData(initialData);
73
- setErrors({});
74
- setSubmitting(false);
75
- };
76
- }, []);
77
- return {
78
- submitRef,
79
- inputRef,
80
- fields,
81
- data,
82
- errors,
83
- disabled,
84
- submitting,
85
- handleChange,
86
- handleSubmit
87
- };
88
- };
89
- exports.useGuestCheckoutForm = useGuestCheckoutForm;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useGuestCheckoutForm = void 0;
4
+ const react_1 = require("react");
5
+ const utils_1 = require("../utils");
6
+ const initialData = {
7
+ first_name: '',
8
+ last_name: '',
9
+ email: '',
10
+ phone: ''
11
+ };
12
+ const useGuestCheckoutForm = (guest, loading, error, guestErrors, submit) => {
13
+ const submitRef = (0, react_1.useRef)(null);
14
+ const inputRef = (0, react_1.useRef)(null);
15
+ const initData = Object.assign(Object.assign({}, initialData), guest);
16
+ const [data, setData] = (0, react_1.useState)(initData);
17
+ const [errors, setErrors] = (0, react_1.useState)({});
18
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
19
+ const disabled = (0, utils_1.checkEmpty)(data);
20
+ const fields = [
21
+ {
22
+ label: 'First Name',
23
+ name: 'first_name',
24
+ type: 'text',
25
+ required: true
26
+ },
27
+ {
28
+ label: 'Last Name',
29
+ name: 'last_name',
30
+ type: 'text',
31
+ required: true
32
+ },
33
+ {
34
+ label: 'Email',
35
+ name: 'email',
36
+ type: 'email',
37
+ required: true
38
+ },
39
+ { label: 'Phone', name: 'phone', type: 'tel', required: true }
40
+ ];
41
+ const handleChange = (name, value) => {
42
+ const val = name === 'phone' ? (0, utils_1.makePhone)(value) : value;
43
+ setData(Object.assign(Object.assign({}, data), { [name]: val }));
44
+ };
45
+ const handleSubmit = (evt) => {
46
+ var _a, _b;
47
+ evt === null || evt === void 0 ? void 0 : evt.preventDefault();
48
+ if (!disabled) {
49
+ setErrors({});
50
+ setSubmitting(true);
51
+ submit(data);
52
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
53
+ }
54
+ };
55
+ (0, react_1.useEffect)(() => {
56
+ var _a, _b;
57
+ if (loading === 'idle') {
58
+ setSubmitting(false);
59
+ if (error) {
60
+ const errs = (0, utils_1.makeFormErrors)(error);
61
+ errs.form = 'There are one or more errors below';
62
+ setErrors(errs);
63
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
64
+ }
65
+ else if (guestErrors) {
66
+ setErrors(guestErrors);
67
+ }
68
+ }
69
+ }, [loading, error, guestErrors]);
70
+ (0, react_1.useEffect)(() => {
71
+ return () => {
72
+ setData(initialData);
73
+ setErrors({});
74
+ setSubmitting(false);
75
+ };
76
+ }, []);
77
+ return {
78
+ submitRef,
79
+ inputRef,
80
+ fields,
81
+ data,
82
+ errors,
83
+ disabled,
84
+ submitting,
85
+ handleChange,
86
+ handleSubmit
87
+ };
88
+ };
89
+ exports.useGuestCheckoutForm = useGuestCheckoutForm;
@@ -1,12 +1,12 @@
1
- /// <reference types="react" />
2
- import { CheckoutFormCustomer, FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useGuestForm: (email: string, loading: RequestStatus, error: RequestError, checkGuest: (email: string) => void) => {
4
- submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
- inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
- fields: FormFields;
7
- data: CheckoutFormCustomer;
8
- errors: Record<string, string>;
9
- submitting: boolean;
10
- handleChange: (name: string, value: string | number | boolean) => void;
11
- handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
12
- };
1
+ /// <reference types="react" />
2
+ import { CheckoutFormCustomer, FormFields, RequestError, RequestStatus } from '@open-tender/types';
3
+ export declare const useGuestForm: (email: string, loading: RequestStatus, error: RequestError, checkGuest: (email: string) => void) => {
4
+ submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
+ inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ fields: FormFields;
7
+ data: CheckoutFormCustomer;
8
+ errors: Record<string, string>;
9
+ submitting: boolean;
10
+ handleChange: (name: string, value: string | number | boolean) => void;
11
+ handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
12
+ };