@open-tender/utils 0.0.57 → 0.0.60

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 (93) hide show
  1. package/dist/cjs/hooks/index.d.ts +11 -0
  2. package/dist/cjs/hooks/index.js +11 -0
  3. package/dist/cjs/hooks/types.d.ts +4 -0
  4. package/dist/cjs/hooks/types.js +2 -0
  5. package/dist/cjs/hooks/useAddressForm.d.ts +2 -5
  6. package/dist/cjs/hooks/useAddressForm.js +3 -3
  7. package/dist/cjs/hooks/useAllergenForm.d.ts +5 -2
  8. package/dist/cjs/hooks/useAllergenForm.js +8 -4
  9. package/dist/cjs/hooks/useCartGuestForm.d.ts +21 -0
  10. package/dist/cjs/hooks/useCartGuestForm.js +46 -0
  11. package/dist/cjs/hooks/useCheckoutLoginForm.d.ts +12 -0
  12. package/dist/cjs/hooks/useCheckoutLoginForm.js +41 -0
  13. package/dist/cjs/hooks/useCommunicationPreferences.d.ts +0 -0
  14. package/dist/cjs/hooks/useCommunicationPreferences.js +1 -0
  15. package/dist/cjs/hooks/useCreditCard.d.ts +8 -11
  16. package/dist/cjs/hooks/useCreditCard.js +61 -41
  17. package/dist/cjs/hooks/useDineInForm.d.ts +8 -3
  18. package/dist/cjs/hooks/useDineInForm.js +8 -2
  19. package/dist/cjs/hooks/useDonationForm.d.ts +4 -1
  20. package/dist/cjs/hooks/useDonationForm.js +17 -3
  21. package/dist/cjs/hooks/useGiftCardAssignForm.d.ts +5 -7
  22. package/dist/cjs/hooks/useGiftCardAssignForm.js +11 -3
  23. package/dist/cjs/hooks/useGiftCardAssignOtherForm.d.ts +5 -1
  24. package/dist/cjs/hooks/useGiftCardAssignOtherForm.js +8 -0
  25. package/dist/cjs/hooks/useGiftCardForm.d.ts +9 -5
  26. package/dist/cjs/hooks/useGiftCardForm.js +13 -4
  27. package/dist/cjs/hooks/useGiftCardsForm.d.ts +30 -11
  28. package/dist/cjs/hooks/useGiftCardsForm.js +72 -31
  29. package/dist/cjs/hooks/useGuestForm.d.ts +13 -0
  30. package/dist/cjs/hooks/useGuestForm.js +41 -0
  31. package/dist/cjs/hooks/useOrderFulfillmentForm.d.ts +13 -0
  32. package/dist/cjs/hooks/useOrderFulfillmentForm.js +40 -0
  33. package/dist/cjs/hooks/useOrderRatingForm.d.ts +13 -0
  34. package/dist/cjs/hooks/useOrderRatingForm.js +33 -0
  35. package/dist/cjs/hooks/useProfileForm.d.ts +18 -23
  36. package/dist/cjs/hooks/useProfileForm.js +27 -28
  37. package/dist/cjs/hooks/useResetPasswordForm.d.ts +20 -0
  38. package/dist/cjs/hooks/useResetPasswordForm.js +66 -0
  39. package/dist/cjs/hooks/useSendResetForm.d.ts +12 -0
  40. package/dist/cjs/hooks/useSendResetForm.js +36 -0
  41. package/dist/cjs/hooks/useSignUpForm.d.ts +21 -0
  42. package/dist/cjs/hooks/useSignUpForm.js +91 -0
  43. package/dist/cjs/hooks/useSignUpGuestForm.d.ts +23 -0
  44. package/dist/cjs/hooks/useSignUpGuestForm.js +74 -0
  45. package/dist/cjs/hooks/useSigninForm.d.ts +12 -0
  46. package/dist/cjs/hooks/useSigninForm.js +41 -0
  47. package/dist/esm/hooks/index.d.ts +11 -0
  48. package/dist/esm/hooks/index.js +11 -0
  49. package/dist/esm/hooks/types.d.ts +4 -0
  50. package/dist/esm/hooks/types.js +1 -0
  51. package/dist/esm/hooks/useAddressForm.d.ts +2 -5
  52. package/dist/esm/hooks/useAddressForm.js +3 -3
  53. package/dist/esm/hooks/useAllergenForm.d.ts +5 -2
  54. package/dist/esm/hooks/useAllergenForm.js +9 -5
  55. package/dist/esm/hooks/useCartGuestForm.d.ts +21 -0
  56. package/dist/esm/hooks/useCartGuestForm.js +44 -0
  57. package/dist/esm/hooks/useCheckoutLoginForm.d.ts +12 -0
  58. package/dist/esm/hooks/useCheckoutLoginForm.js +39 -0
  59. package/dist/esm/hooks/useCommunicationPreferences.d.ts +0 -0
  60. package/dist/esm/hooks/useCommunicationPreferences.js +1 -0
  61. package/dist/esm/hooks/useCreditCard.d.ts +8 -11
  62. package/dist/esm/hooks/useCreditCard.js +62 -42
  63. package/dist/esm/hooks/useDineInForm.d.ts +8 -3
  64. package/dist/esm/hooks/useDineInForm.js +9 -3
  65. package/dist/esm/hooks/useDonationForm.d.ts +4 -1
  66. package/dist/esm/hooks/useDonationForm.js +17 -3
  67. package/dist/esm/hooks/useGiftCardAssignForm.d.ts +5 -7
  68. package/dist/esm/hooks/useGiftCardAssignForm.js +12 -4
  69. package/dist/esm/hooks/useGiftCardAssignOtherForm.d.ts +5 -1
  70. package/dist/esm/hooks/useGiftCardAssignOtherForm.js +9 -1
  71. package/dist/esm/hooks/useGiftCardForm.d.ts +9 -5
  72. package/dist/esm/hooks/useGiftCardForm.js +14 -5
  73. package/dist/esm/hooks/useGiftCardsForm.d.ts +30 -11
  74. package/dist/esm/hooks/useGiftCardsForm.js +73 -32
  75. package/dist/esm/hooks/useGuestForm.d.ts +13 -0
  76. package/dist/esm/hooks/useGuestForm.js +39 -0
  77. package/dist/esm/hooks/useOrderFulfillmentForm.d.ts +13 -0
  78. package/dist/esm/hooks/useOrderFulfillmentForm.js +38 -0
  79. package/dist/esm/hooks/useOrderRatingForm.d.ts +13 -0
  80. package/dist/esm/hooks/useOrderRatingForm.js +31 -0
  81. package/dist/esm/hooks/useProfileForm.d.ts +18 -23
  82. package/dist/esm/hooks/useProfileForm.js +28 -29
  83. package/dist/esm/hooks/useResetPasswordForm.d.ts +20 -0
  84. package/dist/esm/hooks/useResetPasswordForm.js +64 -0
  85. package/dist/esm/hooks/useSendResetForm.d.ts +12 -0
  86. package/dist/esm/hooks/useSendResetForm.js +34 -0
  87. package/dist/esm/hooks/useSignUpForm.d.ts +21 -0
  88. package/dist/esm/hooks/useSignUpForm.js +89 -0
  89. package/dist/esm/hooks/useSignUpGuestForm.d.ts +23 -0
  90. package/dist/esm/hooks/useSignUpGuestForm.js +72 -0
  91. package/dist/esm/hooks/useSigninForm.d.ts +12 -0
  92. package/dist/esm/hooks/useSigninForm.js +39 -0
  93. package/package.json +2 -2
@@ -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,44 @@
1
+ import { useRef, useState, useEffect } from 'react';
2
+ const useCartGuestForm = (loading, errMsg, cartId, joinCart) => {
3
+ const submitRef = useRef(null);
4
+ const inputRef = useRef(null);
5
+ const [data, setData] = useState({ cart_id: cartId });
6
+ const [submitting, setSubmitting] = useState(false);
7
+ const fields = [
8
+ { label: 'First Name', name: 'first_name', type: 'text', required: true },
9
+ { label: 'Last Name', name: 'last_name', type: 'text', required: true },
10
+ ];
11
+ useEffect(() => {
12
+ return () => {
13
+ setData({});
14
+ };
15
+ }, []);
16
+ useEffect(() => {
17
+ var _a, _b;
18
+ if (loading === 'idle') {
19
+ setSubmitting(false);
20
+ if (errMsg) {
21
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
22
+ }
23
+ }
24
+ }, [loading, errMsg]);
25
+ const handleChange = (name, value) => {
26
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
27
+ };
28
+ const handleSubmit = () => {
29
+ var _a, _b;
30
+ setSubmitting(true);
31
+ joinCart(data);
32
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
33
+ };
34
+ return {
35
+ submitRef,
36
+ inputRef,
37
+ fields,
38
+ data,
39
+ submitting,
40
+ handleChange,
41
+ handleSubmit,
42
+ };
43
+ };
44
+ export 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,39 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+ const useLoginForm = (loading, error, login, callback) => {
3
+ const submitRef = useRef(null);
4
+ const inputRef = useRef(null);
5
+ const [hasSubmit, setHasSubmit] = useState(false);
6
+ const [data, setData] = useState({});
7
+ const submitting = loading === 'pending';
8
+ useEffect(() => {
9
+ var _a, _b;
10
+ if (loading === 'idle' && hasSubmit) {
11
+ if (error) {
12
+ setHasSubmit(false);
13
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
14
+ }
15
+ else if (callback) {
16
+ callback();
17
+ }
18
+ }
19
+ }, [loading, error, hasSubmit, callback]);
20
+ const handleChange = (name, value) => {
21
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
22
+ };
23
+ const handleSubmit = () => {
24
+ var _a, _b;
25
+ setHasSubmit(true);
26
+ const { email, password } = data;
27
+ login(email, password);
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
+ inputRef,
33
+ data,
34
+ submitting,
35
+ handleChange,
36
+ handleSubmit,
37
+ };
38
+ };
39
+ export 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,51 +1,71 @@
1
- import { useState } from 'react';
2
- import { formatCard, formatCardField, getCardType, isEmpty, validateCreditCard } from '../utils';
3
- const defaultState = {
4
- acct: '',
5
- exp: '',
6
- cvv: '',
7
- zip: '',
8
- is_default: false,
9
- };
10
- const useCreditCard = (initState, cardErrors, disabled) => {
11
- const [initCard, initCardType] = formatCard(initState !== null && initState !== void 0 ? initState : defaultState);
12
- const [data, setData] = useState(initCard || defaultState);
13
- const [cardType, setCardType] = useState(initCardType || 'OTHER');
14
- const [errors, setErrors] = useState(cardErrors !== null && cardErrors !== void 0 ? cardErrors : {});
15
- const { acct, exp, cvv, zip } = data;
16
- const isComplete = acct && exp && cvv && zip && !isEmpty(errors) ? true : false;
17
- const handleChange = (name, value) => {
18
- if (name === 'acct') {
19
- const currentType = getCardType(value.replace(/\s/g, ''));
20
- setCardType(currentType);
1
+ import { useState, useEffect, useRef } from 'react';
2
+ import { validateCreditCard } from '@open-tender/utils';
3
+ const useCreditCardForm = (loading, error, data, cardType, setErrors, addCard, callback, recaptchaKey, revenue_center_id, windowRef) => {
4
+ const submitRef = useRef(null);
5
+ const recaptchaRef = useRef(null);
6
+ const [submitting, setSubmitting] = useState(false);
7
+ useEffect(() => {
8
+ if (loading === 'idle') {
9
+ setSubmitting(false);
10
+ if (error) {
11
+ if (recaptchaRef.current)
12
+ recaptchaRef.current.reset();
13
+ setErrors(error);
14
+ if (windowRef)
15
+ windowRef.scrollTo({ top: 0 });
16
+ }
21
17
  }
22
- const val = name === 'is_default' ? value : formatCardField(name, value);
23
- setData(Object.assign(Object.assign({}, data), { [name]: val }));
24
- };
25
- const handleBlur = (name) => {
26
- const { errors: validationErrors } = validateCreditCard(data, cardType);
27
- if (validationErrors) {
28
- const error = validationErrors[name];
29
- setErrors(Object.assign(Object.assign({}, errors), { [name]: error }));
18
+ }, [windowRef, loading, error, setErrors]);
19
+ const handleSubmit = () => {
20
+ var _a, _b, _c;
21
+ setErrors({});
22
+ const { card, errors: cardErrors } = validateCreditCard(data, cardType);
23
+ if (cardErrors) {
24
+ setErrors(cardErrors);
25
+ setSubmitting(false);
26
+ if (windowRef)
27
+ windowRef.scrollTo({ top: 0 });
30
28
  }
31
29
  else {
32
- setErrors(Object.assign(Object.assign({}, errors), { [name]: null }));
30
+ const cardRc = revenue_center_id ? Object.assign(Object.assign({}, card), { revenue_center_id }) : card;
31
+ if (recaptchaKey) {
32
+ try {
33
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
34
+ if (!token) {
35
+ setSubmitting(false);
36
+ setErrors({
37
+ form: 'Please complete the recaptcha before submitting',
38
+ });
39
+ if (windowRef)
40
+ windowRef.scrollTo({ top: 0 });
41
+ }
42
+ else {
43
+ setSubmitting(true);
44
+ addCard(Object.assign(Object.assign({}, cardRc), { token }), callback);
45
+ }
46
+ }
47
+ catch (err) {
48
+ setSubmitting(false);
49
+ setErrors({
50
+ form: 'Please complete the recaptcha before submitting',
51
+ });
52
+ if (windowRef)
53
+ windowRef.scrollTo({ top: 0 });
54
+ }
55
+ }
56
+ else {
57
+ setSubmitting(true);
58
+ addCard(cardRc, callback);
59
+ }
33
60
  }
34
- };
35
- const handleReset = () => {
36
- setData(defaultState);
37
- setErrors({});
61
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
38
62
  };
39
63
  return {
64
+ submitRef,
65
+ recaptchaRef,
40
66
  data,
41
- cardType,
42
- errors,
43
- disabled,
44
- setErrors,
45
- handleChange,
46
- handleBlur,
47
- handleReset,
48
- isComplete,
67
+ submitting,
68
+ handleSubmit,
49
69
  };
50
70
  };
51
- export default useCreditCard;
71
+ export 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;
@@ -1,19 +1,25 @@
1
- import { useState } from 'react';
1
+ import { useRef, useState } from 'react';
2
2
  const useDineInForm = (initialData = {}, submitGuest) => {
3
+ const submitRef = useRef(null);
4
+ const inputRef = useRef(null);
3
5
  const [data, setData] = useState(initialData);
4
6
  const [errMsg, setErrMsg] = useState(null);
5
- const handleChange = (id, value) => {
6
- setData(Object.assign(Object.assign({}, data), { [id]: value }));
7
+ const handleChange = (name, value) => {
8
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
7
9
  };
8
10
  const handleSubmit = () => {
11
+ var _a, _b;
9
12
  if (!data.first_name || !data.last_name) {
10
13
  setErrMsg('Both first and last name are required');
11
14
  }
12
15
  else {
13
16
  submitGuest({ customer: data });
14
17
  }
18
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
15
19
  };
16
20
  return {
21
+ submitRef,
22
+ inputRef,
17
23
  data,
18
24
  errMsg,
19
25
  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;
@@ -1,6 +1,8 @@
1
1
  import { useMemo, useEffect, useRef, useState } from 'react';
2
2
  import { makeNumeric, validateCreditCard } from '../utils';
3
- const useDonationsForm = (purchase, reset, setAlert, loading, error, success, customer, creditCards, recaptchaKey, cardData, cardType) => {
3
+ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, customer, creditCards, recaptchaKey, cardData, cardType, windowref) => {
4
+ const submitRef = useRef(null);
5
+ const inputRef = useRef(null);
4
6
  const recaptchaRef = useRef(null);
5
7
  const [amount, setAmount] = useState('');
6
8
  const [email, setEmail] = useState('');
@@ -18,6 +20,7 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
18
20
  .reduce((obj, [key, value]) => (Object.assign(Object.assign({}, obj), { [key]: value })), {})
19
21
  : {}, [errors]);
20
22
  useEffect(() => {
23
+ var _a, _b;
21
24
  if (loading === 'idle') {
22
25
  setSubmitting(false);
23
26
  setAlert({ type: 'close' });
@@ -25,12 +28,14 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
25
28
  if (recaptchaRef.current)
26
29
  recaptchaRef.current.reset();
27
30
  setErrors(error);
31
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
32
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
28
33
  }
29
34
  else if (success) {
30
- //
35
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
31
36
  }
32
37
  }
33
- }, [loading, error, setAlert, success]);
38
+ }, [windowref, loading, error, setAlert, success]);
34
39
  useEffect(() => {
35
40
  if ((creditCards === null || creditCards === void 0 ? void 0 : creditCards.length) && loading !== 'pending') {
36
41
  const options = creditCards.map(i => ({
@@ -70,6 +75,7 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
70
75
  if (!token) {
71
76
  setSubmitting(false);
72
77
  setErrors({ form: 'Please complete the recaptcha before submitting' });
78
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
73
79
  }
74
80
  else {
75
81
  setSubmitting(true);
@@ -85,6 +91,7 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
85
91
  catch (err) {
86
92
  setSubmitting(false);
87
93
  setErrors({ form: 'Please complete the recaptcha before submitting' });
94
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
88
95
  }
89
96
  }
90
97
  else {
@@ -99,8 +106,11 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
99
106
  }
100
107
  };
101
108
  const handleSubmit = () => {
109
+ var _a, _b, _c;
102
110
  if (!amount || !email) {
103
111
  setErrors({ form: 'Both amount and email are required' });
112
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && inputRef.current.focus();
113
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
104
114
  }
105
115
  else {
106
116
  if (isNewCard) {
@@ -109,6 +119,7 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
109
119
  if (errs) {
110
120
  setErrors(Object.assign(Object.assign({}, errs), { form: 'There are one or more credit card errors below' }));
111
121
  setSubmitting(false);
122
+ windowref === null || windowref === void 0 ? void 0 : windowref.scrollTo(0, 0);
112
123
  }
113
124
  else {
114
125
  purchaseWithCaptcha(card);
@@ -118,6 +129,7 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
118
129
  else {
119
130
  purchaseWithCaptcha(creditCard);
120
131
  }
132
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
121
133
  }
122
134
  };
123
135
  const handleReset = () => {
@@ -132,6 +144,8 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
132
144
  reset();
133
145
  };
134
146
  return {
147
+ inputRef,
148
+ submitRef,
135
149
  recaptchaRef,
136
150
  amount,
137
151
  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,13 +1,17 @@
1
- import { useState, useEffect } from 'react';
2
- const useGiftCardAssignForm = ({ loading, error, assign, callback, }) => {
1
+ import { useState, useEffect, useRef } from 'react';
2
+ const useGiftCardAssignForm = (loading, error, assign, callback) => {
3
+ const submitRef = useRef(null);
4
+ const inputRef = useRef(null);
3
5
  const [cardNumber, setCardNumber] = useState('');
4
6
  const [errors, setErrors] = useState({});
5
7
  const [submitting, setSubmitting] = useState(false);
6
8
  useEffect(() => {
9
+ var _a, _b;
7
10
  if (loading === 'idle') {
8
11
  setSubmitting(false);
9
12
  if (error) {
10
13
  setErrors(error);
14
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
11
15
  }
12
16
  }
13
17
  }, [loading, error]);
@@ -15,17 +19,21 @@ const useGiftCardAssignForm = ({ loading, error, assign, callback, }) => {
15
19
  setCardNumber(value);
16
20
  };
17
21
  const handleSubmit = () => {
22
+ var _a, _b, _c, _d;
18
23
  setSubmitting(true);
19
24
  const card_number = parseInt(cardNumber);
20
25
  if (isNaN(card_number)) {
21
26
  setErrors({ card_number: 'Card numbers must be all digits' });
22
- setSubmitting(false);
27
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
23
28
  }
24
29
  else {
25
- assign(`${card_number}`, callback);
30
+ assign(card_number, callback);
31
+ ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur) && ((_d = submitRef.current) === null || _d === void 0 ? void 0 : _d.blur());
26
32
  }
27
33
  };
28
34
  return {
35
+ submitRef,
36
+ inputRef,
29
37
  cardNumber,
30
38
  errors,
31
39
  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;
@@ -1,13 +1,17 @@
1
- import { useState, useEffect } from 'react';
1
+ import { useState, useEffect, useRef } from 'react';
2
2
  const useGiftCardAssignOtherForm = (loading, error, giftCardId, assign, callback) => {
3
+ const submitRef = useRef(null);
4
+ const inputRef = useRef(null);
3
5
  const [email, setEmail] = useState('');
4
6
  const [errors, setErrors] = useState({});
5
7
  const [submitting, setSubmitting] = useState(false);
6
8
  useEffect(() => {
9
+ var _a, _b;
7
10
  if (loading === 'idle') {
8
11
  setSubmitting(false);
9
12
  if (error) {
10
13
  setErrors(error);
14
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
11
15
  }
12
16
  }
13
17
  }, [loading, error]);
@@ -15,10 +19,14 @@ const useGiftCardAssignOtherForm = (loading, error, giftCardId, assign, callback
15
19
  setEmail(value);
16
20
  };
17
21
  const handleSubmit = () => {
22
+ var _a, _b;
18
23
  setSubmitting(true);
19
24
  assign(giftCardId, email, callback);
25
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
20
26
  };
21
27
  return {
28
+ submitRef,
29
+ inputRef,
22
30
  email,
23
31
  errors,
24
32
  submitting,
@@ -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;
@@ -1,16 +1,19 @@
1
- import { useState, useEffect } from 'react';
1
+ import { useState, useEffect, useRef } from 'react';
2
2
  const useGiftCardForm = (giftCard, creditCards, loading, error, update, add, callback) => {
3
+ const submitRef = useRef(null);
4
+ const inputRef = useRef(null);
3
5
  const customerCardId = creditCards[0].customer_card_id;
4
6
  const [data, setData] = useState({ customer_card_id: customerCardId });
5
7
  const [errors, setErrors] = useState({});
6
8
  const [submitting, setSubmitting] = useState(false);
7
- const options = creditCards.map(i => {
9
+ const options = creditCards.map((i) => {
8
10
  return {
9
- label: `${i.card_type_name} ending in ${i.last4}`,
11
+ name: `${i.card_type_name} ending in ${i.last4}`,
10
12
  value: i.customer_card_id,
11
13
  };
12
14
  });
13
15
  useEffect(() => {
16
+ var _a, _b;
14
17
  if (loading === 'idle') {
15
18
  setSubmitting(false);
16
19
  if (error) {
@@ -18,13 +21,16 @@ const useGiftCardForm = (giftCard, creditCards, loading, error, update, add, cal
18
21
  error.amount = 'Please enter a positive dollar amount';
19
22
  }
20
23
  setErrors(error);
24
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
21
25
  }
22
26
  }
23
27
  }, [loading, error]);
24
- const handleChange = (id, value) => {
25
- setData(Object.assign(Object.assign({}, data), { [id]: value }));
28
+ const handleChange = (name, type, value, checked) => {
29
+ const inputValue = type === 'checkbox' ? checked : value;
30
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
26
31
  };
27
32
  const handleSubmit = () => {
33
+ var _a, _b;
28
34
  setSubmitting(true);
29
35
  if (!data.customer_card_id) {
30
36
  data.customer_card_id = customerCardId;
@@ -33,8 +39,11 @@ const useGiftCardForm = (giftCard, creditCards, loading, error, update, add, cal
33
39
  giftCard
34
40
  ? update(giftCard.gift_card_id, data, callback)
35
41
  : add(data, callback);
42
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
36
43
  };
37
44
  return {
45
+ submitRef,
46
+ inputRef,
38
47
  options,
39
48
  errors,
40
49
  data,