@open-tender/utils 0.1.43 → 0.1.45

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.
@@ -22,7 +22,7 @@ export declare const useDonationsForm: (purchase: (data: DonationPurchase, callb
22
22
  value: unknown;
23
23
  }[];
24
24
  handleCreditCard: (value: string) => void;
25
- handleSubmit: () => void;
25
+ handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
26
26
  handleReset: () => void;
27
27
  setErrors: import("react").Dispatch<import("react").SetStateAction<Record<string, string>>>;
28
28
  };
@@ -108,8 +108,9 @@ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, cu
108
108
  });
109
109
  }
110
110
  };
111
- const handleSubmit = () => {
111
+ const handleSubmit = (evt) => {
112
112
  var _a, _b, _c;
113
+ evt === null || evt === void 0 ? void 0 : evt.preventDefault();
113
114
  if (!amount || !email) {
114
115
  setErrors({ form: 'Both amount and email are required' });
115
116
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && inputRef.current.focus();
@@ -1,9 +1,9 @@
1
1
  /// <reference types="react" />
2
2
  import { CardType, CreditCardData, CreditCardSaved, CreditCards, Customer, GiftCardPurchaseData, GiftCardsPurchase, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useGiftCardsForm: (purchase: (data: GiftCardsPurchase) => void, reset: () => void, setAlert: (data: {
3
+ export declare const useGiftCardsForm: (purchase: (data: GiftCardsPurchase, callback?: () => void) => void, reset: () => void, setAlert: (data: {
4
4
  type: string;
5
5
  args?: unknown;
6
- }) => void, loading: RequestStatus, error: RequestError, success: boolean, initState: GiftCardPurchaseData, customer?: Customer, creditCards?: CreditCards, recaptchaKey?: string, cardData?: CreditCardData, cardType?: CardType, windowRef?: Window) => {
6
+ }) => void, loading: RequestStatus, error: RequestError, success: boolean, initState: GiftCardPurchaseData, customer?: Customer | null, creditCards?: CreditCards, recaptchaKey?: string | null, cardData?: CreditCardData, cardType?: CardType, url?: string) => {
7
7
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
8
8
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
9
9
  recaptchaRef: import("react").MutableRefObject<any>;
@@ -17,7 +17,7 @@ export declare const useGiftCardsForm: (purchase: (data: GiftCardsPurchase) => v
17
17
  handleQuantity: (index: number, quantity: number) => void;
18
18
  handleAddAnother: () => void;
19
19
  handleCreditCard: (value: number) => void;
20
- handleSubmit: () => void;
20
+ handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
21
21
  handleReset: () => void;
22
22
  name: string | null;
23
23
  email: string | null;
@@ -3,8 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useGiftCardsForm = void 0;
4
4
  const react_1 = require("react");
5
5
  const utils_1 = require("../utils");
6
- const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, initState, customer, creditCards, recaptchaKey, cardData, cardType, windowRef) => {
7
- var _a, _b;
6
+ const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, initState, customer, creditCards, recaptchaKey, cardData, cardType, url) => {
8
7
  const submitRef = (0, react_1.useRef)(null);
9
8
  const inputRef = (0, react_1.useRef)(null);
10
9
  const recaptchaRef = (0, react_1.useRef)(null);
@@ -16,7 +15,6 @@ const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, in
16
15
  const [creditCardOptions, setCreditCardOptions] = (0, react_1.useState)([]);
17
16
  const [errors, setErrors] = (0, react_1.useState)({});
18
17
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
19
- 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 : '';
20
18
  const newCardErrors = (0, react_1.useMemo)(() => errors
21
19
  ? Object.entries(errors)
22
20
  .filter(([key]) => key !== 'form')
@@ -30,13 +28,9 @@ const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, in
30
28
  if (recaptchaRef.current)
31
29
  recaptchaRef.current.reset();
32
30
  setErrors((0, utils_1.makeFormErrors)(error));
33
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
34
- }
35
- else if (success) {
36
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
37
31
  }
38
32
  }
39
- }, [windowRef, loading, error, setAlert, success]);
33
+ }, [loading, error, setAlert, success]);
40
34
  (0, react_1.useEffect)(() => {
41
35
  if (creditCards === null || creditCards === void 0 ? void 0 : creditCards.length) {
42
36
  const options = creditCards.map(i => ({
@@ -108,7 +102,6 @@ const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, in
108
102
  setErrors({
109
103
  form: 'Please complete the recaptcha before submitting'
110
104
  });
111
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
112
105
  }
113
106
  else {
114
107
  setSubmitting(true);
@@ -119,7 +112,7 @@ const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, in
119
112
  credit_card,
120
113
  name: name !== null && name !== void 0 ? name : '',
121
114
  email: email !== null && email !== void 0 ? email : '',
122
- url,
115
+ url: url !== null && url !== void 0 ? url : '',
123
116
  gift_cards
124
117
  });
125
118
  }
@@ -127,7 +120,6 @@ const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, in
127
120
  catch (err) {
128
121
  setSubmitting(false);
129
122
  setErrors({ form: 'Please complete the recaptcha before submitting' });
130
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
131
123
  }
132
124
  }
133
125
  else {
@@ -138,18 +130,18 @@ const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, in
138
130
  credit_card,
139
131
  name: name !== null && name !== void 0 ? name : '',
140
132
  email: email !== null && email !== void 0 ? email : '',
141
- url,
133
+ url: url !== null && url !== void 0 ? url : '',
142
134
  gift_cards
143
135
  });
144
136
  }
145
137
  };
146
- const handleSubmit = () => {
138
+ const handleSubmit = (evt) => {
147
139
  var _a, _b, _c;
140
+ evt === null || evt === void 0 ? void 0 : evt.preventDefault();
148
141
  if (!name || !email) {
149
142
  setErrors({ form: 'Both name and email are required' });
150
143
  if (inputRef.current)
151
144
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && inputRef.current.focus();
152
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
153
145
  }
154
146
  else {
155
147
  if (isNewCard) {
@@ -158,7 +150,6 @@ const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, in
158
150
  if (errors) {
159
151
  setErrors(Object.assign(Object.assign({}, errors), { form: 'There are one or more credit card errors below' }));
160
152
  setSubmitting(false);
161
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
162
153
  }
163
154
  else {
164
155
  purchaseWithCaptcha(card);
@@ -22,7 +22,7 @@ export declare const useDonationsForm: (purchase: (data: DonationPurchase, callb
22
22
  value: unknown;
23
23
  }[];
24
24
  handleCreditCard: (value: string) => void;
25
- handleSubmit: () => void;
25
+ handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
26
26
  handleReset: () => void;
27
27
  setErrors: import("react").Dispatch<import("react").SetStateAction<Record<string, string>>>;
28
28
  };
@@ -105,8 +105,9 @@ export const useDonationsForm = (purchase, reset, setAlert, loading, error, succ
105
105
  });
106
106
  }
107
107
  };
108
- const handleSubmit = () => {
108
+ const handleSubmit = (evt) => {
109
109
  var _a, _b, _c;
110
+ evt === null || evt === void 0 ? void 0 : evt.preventDefault();
110
111
  if (!amount || !email) {
111
112
  setErrors({ form: 'Both amount and email are required' });
112
113
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && inputRef.current.focus();
@@ -1,9 +1,9 @@
1
1
  /// <reference types="react" />
2
2
  import { CardType, CreditCardData, CreditCardSaved, CreditCards, Customer, GiftCardPurchaseData, GiftCardsPurchase, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useGiftCardsForm: (purchase: (data: GiftCardsPurchase) => void, reset: () => void, setAlert: (data: {
3
+ export declare const useGiftCardsForm: (purchase: (data: GiftCardsPurchase, callback?: () => void) => void, reset: () => void, setAlert: (data: {
4
4
  type: string;
5
5
  args?: unknown;
6
- }) => void, loading: RequestStatus, error: RequestError, success: boolean, initState: GiftCardPurchaseData, customer?: Customer, creditCards?: CreditCards, recaptchaKey?: string, cardData?: CreditCardData, cardType?: CardType, windowRef?: Window) => {
6
+ }) => void, loading: RequestStatus, error: RequestError, success: boolean, initState: GiftCardPurchaseData, customer?: Customer | null, creditCards?: CreditCards, recaptchaKey?: string | null, cardData?: CreditCardData, cardType?: CardType, url?: string) => {
7
7
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
8
8
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
9
9
  recaptchaRef: import("react").MutableRefObject<any>;
@@ -17,7 +17,7 @@ export declare const useGiftCardsForm: (purchase: (data: GiftCardsPurchase) => v
17
17
  handleQuantity: (index: number, quantity: number) => void;
18
18
  handleAddAnother: () => void;
19
19
  handleCreditCard: (value: number) => void;
20
- handleSubmit: () => void;
20
+ handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
21
21
  handleReset: () => void;
22
22
  name: string | null;
23
23
  email: string | null;
@@ -1,7 +1,6 @@
1
1
  import { useMemo, useRef, useState, useEffect } from 'react';
2
2
  import { makeFormErrors, validateCreditCard } from '../utils';
3
- export const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, initState, customer, creditCards, recaptchaKey, cardData, cardType, windowRef) => {
4
- var _a, _b;
3
+ export const useGiftCardsForm = (purchase, reset, setAlert, loading, error, success, initState, customer, creditCards, recaptchaKey, cardData, cardType, url) => {
5
4
  const submitRef = useRef(null);
6
5
  const inputRef = useRef(null);
7
6
  const recaptchaRef = useRef(null);
@@ -13,7 +12,6 @@ export const useGiftCardsForm = (purchase, reset, setAlert, loading, error, succ
13
12
  const [creditCardOptions, setCreditCardOptions] = useState([]);
14
13
  const [errors, setErrors] = useState({});
15
14
  const [submitting, setSubmitting] = useState(false);
16
- 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
15
  const newCardErrors = useMemo(() => errors
18
16
  ? Object.entries(errors)
19
17
  .filter(([key]) => key !== 'form')
@@ -27,13 +25,9 @@ export const useGiftCardsForm = (purchase, reset, setAlert, loading, error, succ
27
25
  if (recaptchaRef.current)
28
26
  recaptchaRef.current.reset();
29
27
  setErrors(makeFormErrors(error));
30
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
31
- }
32
- else if (success) {
33
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
34
28
  }
35
29
  }
36
- }, [windowRef, loading, error, setAlert, success]);
30
+ }, [loading, error, setAlert, success]);
37
31
  useEffect(() => {
38
32
  if (creditCards === null || creditCards === void 0 ? void 0 : creditCards.length) {
39
33
  const options = creditCards.map(i => ({
@@ -105,7 +99,6 @@ export const useGiftCardsForm = (purchase, reset, setAlert, loading, error, succ
105
99
  setErrors({
106
100
  form: 'Please complete the recaptcha before submitting'
107
101
  });
108
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
109
102
  }
110
103
  else {
111
104
  setSubmitting(true);
@@ -116,7 +109,7 @@ export const useGiftCardsForm = (purchase, reset, setAlert, loading, error, succ
116
109
  credit_card,
117
110
  name: name !== null && name !== void 0 ? name : '',
118
111
  email: email !== null && email !== void 0 ? email : '',
119
- url,
112
+ url: url !== null && url !== void 0 ? url : '',
120
113
  gift_cards
121
114
  });
122
115
  }
@@ -124,7 +117,6 @@ export const useGiftCardsForm = (purchase, reset, setAlert, loading, error, succ
124
117
  catch (err) {
125
118
  setSubmitting(false);
126
119
  setErrors({ form: 'Please complete the recaptcha before submitting' });
127
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
128
120
  }
129
121
  }
130
122
  else {
@@ -135,18 +127,18 @@ export const useGiftCardsForm = (purchase, reset, setAlert, loading, error, succ
135
127
  credit_card,
136
128
  name: name !== null && name !== void 0 ? name : '',
137
129
  email: email !== null && email !== void 0 ? email : '',
138
- url,
130
+ url: url !== null && url !== void 0 ? url : '',
139
131
  gift_cards
140
132
  });
141
133
  }
142
134
  };
143
- const handleSubmit = () => {
135
+ const handleSubmit = (evt) => {
144
136
  var _a, _b, _c;
137
+ evt === null || evt === void 0 ? void 0 : evt.preventDefault();
145
138
  if (!name || !email) {
146
139
  setErrors({ form: 'Both name and email are required' });
147
140
  if (inputRef.current)
148
141
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && inputRef.current.focus();
149
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
150
142
  }
151
143
  else {
152
144
  if (isNewCard) {
@@ -155,7 +147,6 @@ export const useGiftCardsForm = (purchase, reset, setAlert, loading, error, succ
155
147
  if (errors) {
156
148
  setErrors(Object.assign(Object.assign({}, errors), { form: 'There are one or more credit card errors below' }));
157
149
  setSubmitting(false);
158
- windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
159
150
  }
160
151
  else {
161
152
  purchaseWithCaptcha(card);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.1.43",
3
+ "version": "0.1.45",
4
4
  "description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",