@open-tender/utils 0.1.44 → 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.
|
@@ -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,
|
|
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,
|
|
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
|
-
}, [
|
|
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);
|
|
@@ -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,
|
|
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,
|
|
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
|
-
}, [
|
|
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