@open-tender/utils 0.0.67 → 0.0.69
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.
- package/dist/cjs/hooks/index.d.ts +3 -0
- package/dist/cjs/hooks/index.js +3 -0
- package/dist/cjs/hooks/useCreditCard.d.ts +10 -6
- package/dist/cjs/hooks/useCreditCard.js +44 -61
- package/dist/cjs/hooks/useCreditCardForm.d.ts +9 -0
- package/dist/cjs/hooks/useCreditCardForm.js +75 -0
- package/dist/cjs/hooks/useImage.d.ts +5 -0
- package/dist/cjs/hooks/useImage.js +32 -0
- package/dist/cjs/hooks/useLevelUpForm.d.ts +11 -0
- package/dist/cjs/hooks/useLevelUpForm.js +43 -0
- package/dist/esm/hooks/index.d.ts +3 -0
- package/dist/esm/hooks/index.js +3 -0
- package/dist/esm/hooks/useCreditCard.d.ts +10 -6
- package/dist/esm/hooks/useCreditCard.js +43 -60
- package/dist/esm/hooks/useCreditCardForm.d.ts +9 -0
- package/dist/esm/hooks/useCreditCardForm.js +71 -0
- package/dist/esm/hooks/useImage.d.ts +5 -0
- package/dist/esm/hooks/useImage.js +28 -0
- package/dist/esm/hooks/useLevelUpForm.d.ts +11 -0
- package/dist/esm/hooks/useLevelUpForm.js +39 -0
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@ export * from './menu';
|
|
|
2
2
|
export * from './useAddressForm';
|
|
3
3
|
export * from './useAllergenForm';
|
|
4
4
|
export * from './useCreditCard';
|
|
5
|
+
export * from './useCreditCardForm';
|
|
5
6
|
export * from './useCurbside';
|
|
6
7
|
export * from './useDineInForm';
|
|
7
8
|
export * from './useDonationForm';
|
|
@@ -22,3 +23,5 @@ export * from './useResetPasswordForm';
|
|
|
22
23
|
export * from './useSendResetForm';
|
|
23
24
|
export * from './useSigninForm';
|
|
24
25
|
export * from './types';
|
|
26
|
+
export * from './useImage';
|
|
27
|
+
export * from './useLevelUpForm';
|
package/dist/cjs/hooks/index.js
CHANGED
|
@@ -5,6 +5,7 @@ tslib_1.__exportStar(require("./menu"), exports);
|
|
|
5
5
|
tslib_1.__exportStar(require("./useAddressForm"), exports);
|
|
6
6
|
tslib_1.__exportStar(require("./useAllergenForm"), exports);
|
|
7
7
|
tslib_1.__exportStar(require("./useCreditCard"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./useCreditCardForm"), exports);
|
|
8
9
|
tslib_1.__exportStar(require("./useCurbside"), exports);
|
|
9
10
|
tslib_1.__exportStar(require("./useDineInForm"), exports);
|
|
10
11
|
tslib_1.__exportStar(require("./useDonationForm"), exports);
|
|
@@ -25,3 +26,5 @@ tslib_1.__exportStar(require("./useResetPasswordForm"), exports);
|
|
|
25
26
|
tslib_1.__exportStar(require("./useSendResetForm"), exports);
|
|
26
27
|
tslib_1.__exportStar(require("./useSigninForm"), exports);
|
|
27
28
|
tslib_1.__exportStar(require("./types"), exports);
|
|
29
|
+
tslib_1.__exportStar(require("./useImage"), exports);
|
|
30
|
+
tslib_1.__exportStar(require("./useLevelUpForm"), exports);
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import {
|
|
3
|
-
export declare const
|
|
4
|
-
submitRef: import("react").MutableRefObject<
|
|
5
|
-
recaptchaRef: import("react").MutableRefObject<any>;
|
|
2
|
+
import { CreditCardData } from '@open-tender/types';
|
|
3
|
+
export declare const useCreditCard: (initState: any, cardErrors?: {}, disabled?: boolean) => {
|
|
4
|
+
submitRef: import("react").MutableRefObject<null>;
|
|
6
5
|
data: CreditCardData;
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
cardType: "VISA" | "MC" | "DISC" | "AMEX" | "OTHER";
|
|
7
|
+
errors: {};
|
|
8
|
+
disabled: boolean;
|
|
9
|
+
setErrors: import("react").Dispatch<import("react").SetStateAction<{}>>;
|
|
10
|
+
handleChange: (evt?: any) => void;
|
|
11
|
+
handleBlur: (evt?: any) => void;
|
|
12
|
+
isComplete: boolean;
|
|
9
13
|
};
|
|
@@ -1,75 +1,58 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.useCreditCard = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
5
|
+
const __1 = require("../");
|
|
6
|
+
const defaultState = {
|
|
7
|
+
acct: '',
|
|
8
|
+
exp: '',
|
|
9
|
+
cvv: '',
|
|
10
|
+
zip: '',
|
|
11
|
+
is_default: false,
|
|
12
|
+
};
|
|
13
|
+
const useCreditCard = (initState, cardErrors = {}, disabled = false) => {
|
|
14
|
+
const [initCard, initCardType] = (0, __1.formatCard)(initState);
|
|
7
15
|
const submitRef = (0, react_1.useRef)(null);
|
|
8
|
-
const
|
|
9
|
-
const [
|
|
10
|
-
(0, react_1.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const [data, setData] = (0, react_1.useState)(initCard || defaultState);
|
|
17
|
+
const [cardType, setCardType] = (0, react_1.useState)(initCardType || 'OTHER');
|
|
18
|
+
const [errors, setErrors] = (0, react_1.useState)(cardErrors);
|
|
19
|
+
const { acct, exp, cvv, zip } = data;
|
|
20
|
+
const isComplete = acct && exp && cvv && zip && !(0, __1.isEmpty)(errors) ? true : false;
|
|
21
|
+
const handleChange = (evt) => {
|
|
22
|
+
let { id, checked, value } = evt.target;
|
|
23
|
+
if (id === 'acct') {
|
|
24
|
+
const currentType = (0, __1.getCardType)(value.replace(/\s/g, ''));
|
|
25
|
+
setCardType(currentType);
|
|
26
|
+
}
|
|
27
|
+
if (id === 'is_default') {
|
|
28
|
+
value = checked;
|
|
20
29
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
else {
|
|
31
|
+
value = (0, __1.formatCardField)(id, value);
|
|
32
|
+
}
|
|
33
|
+
setData(Object.assign(Object.assign({}, data), { [id]: value }));
|
|
34
|
+
};
|
|
35
|
+
const handleBlur = (evt) => {
|
|
36
|
+
let { id } = evt.target;
|
|
37
|
+
const { errors: validationErrors } = (0, __1.validateCreditCard)(data, cardType);
|
|
38
|
+
if (validationErrors) {
|
|
39
|
+
const error = validationErrors[id];
|
|
40
|
+
setErrors(Object.assign(Object.assign({}, errors), { [id]: error }));
|
|
32
41
|
}
|
|
33
42
|
else {
|
|
34
|
-
|
|
35
|
-
if (recaptchaKey) {
|
|
36
|
-
try {
|
|
37
|
-
const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
|
|
38
|
-
if (!token) {
|
|
39
|
-
setSubmitting(false);
|
|
40
|
-
setErrors({
|
|
41
|
-
form: 'Please complete the recaptcha before submitting',
|
|
42
|
-
});
|
|
43
|
-
if (windowRef)
|
|
44
|
-
windowRef.scrollTo({ top: 0 });
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
setSubmitting(true);
|
|
48
|
-
addCard(Object.assign(Object.assign({}, cardRc), { token }), callback);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
catch (err) {
|
|
52
|
-
setSubmitting(false);
|
|
53
|
-
setErrors({
|
|
54
|
-
form: 'Please complete the recaptcha before submitting',
|
|
55
|
-
});
|
|
56
|
-
if (windowRef)
|
|
57
|
-
windowRef.scrollTo({ top: 0 });
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
setSubmitting(true);
|
|
62
|
-
addCard(cardRc, callback);
|
|
63
|
-
}
|
|
43
|
+
setErrors(Object.assign(Object.assign({}, errors), { [id]: null }));
|
|
64
44
|
}
|
|
65
|
-
((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
|
|
66
45
|
};
|
|
67
46
|
return {
|
|
68
47
|
submitRef,
|
|
69
|
-
recaptchaRef,
|
|
70
48
|
data,
|
|
71
|
-
|
|
72
|
-
|
|
49
|
+
cardType,
|
|
50
|
+
errors,
|
|
51
|
+
disabled,
|
|
52
|
+
setErrors,
|
|
53
|
+
handleChange,
|
|
54
|
+
handleBlur,
|
|
55
|
+
isComplete,
|
|
73
56
|
};
|
|
74
57
|
};
|
|
75
|
-
exports.
|
|
58
|
+
exports.useCreditCard = useCreditCard;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { CardType, CreditCardData, RequestStatus } from '@open-tender/types';
|
|
3
|
+
export declare const useCreditCardForm: (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) => {
|
|
4
|
+
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
|
+
recaptchaRef: import("react").MutableRefObject<any>;
|
|
6
|
+
data: CreditCardData;
|
|
7
|
+
submitting: boolean;
|
|
8
|
+
handleSubmit: (evt?: any) => void;
|
|
9
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useCreditCardForm = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const __1 = require("../");
|
|
6
|
+
const useCreditCardForm = (loading, error, data, cardType, setErrors, addCard, callback, recaptchaKey, revenue_center_id, windowRef) => {
|
|
7
|
+
const submitRef = (0, react_1.useRef)(null);
|
|
8
|
+
const recaptchaRef = (0, react_1.useRef)(null);
|
|
9
|
+
const [submitting, setSubmitting] = (0, react_1.useState)(false);
|
|
10
|
+
(0, react_1.useEffect)(() => {
|
|
11
|
+
if (loading === 'idle') {
|
|
12
|
+
setSubmitting(false);
|
|
13
|
+
if (error) {
|
|
14
|
+
if (recaptchaRef.current)
|
|
15
|
+
recaptchaRef.current.reset();
|
|
16
|
+
setErrors(error);
|
|
17
|
+
if (windowRef)
|
|
18
|
+
windowRef.scrollTo({ top: 0 });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}, [windowRef, loading, error, setErrors]);
|
|
22
|
+
const handleSubmit = (evt) => {
|
|
23
|
+
var _a, _b, _c;
|
|
24
|
+
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
25
|
+
setErrors({});
|
|
26
|
+
const { card, errors: cardErrors } = (0, __1.validateCreditCard)(data, cardType);
|
|
27
|
+
if (cardErrors) {
|
|
28
|
+
setErrors(cardErrors);
|
|
29
|
+
setSubmitting(false);
|
|
30
|
+
if (windowRef)
|
|
31
|
+
windowRef.scrollTo({ top: 0 });
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
const cardRc = revenue_center_id ? Object.assign(Object.assign({}, card), { revenue_center_id }) : card;
|
|
35
|
+
if (recaptchaKey) {
|
|
36
|
+
try {
|
|
37
|
+
const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
|
|
38
|
+
if (!token) {
|
|
39
|
+
setSubmitting(false);
|
|
40
|
+
setErrors({
|
|
41
|
+
form: 'Please complete the recaptcha before submitting',
|
|
42
|
+
});
|
|
43
|
+
if (windowRef)
|
|
44
|
+
windowRef.scrollTo({ top: 0 });
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
setSubmitting(true);
|
|
48
|
+
addCard(Object.assign(Object.assign({}, cardRc), { token }), callback);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
setSubmitting(false);
|
|
53
|
+
setErrors({
|
|
54
|
+
form: 'Please complete the recaptcha before submitting',
|
|
55
|
+
});
|
|
56
|
+
if (windowRef)
|
|
57
|
+
windowRef.scrollTo({ top: 0 });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
setSubmitting(true);
|
|
62
|
+
addCard(cardRc, callback);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
submitRef,
|
|
69
|
+
recaptchaRef,
|
|
70
|
+
data,
|
|
71
|
+
submitting,
|
|
72
|
+
handleSubmit,
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
exports.useCreditCardForm = useCreditCardForm;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useImage = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
// https://typesafe.blog/article/fetch-images-with-a-react-hook
|
|
6
|
+
const useImage = (src) => {
|
|
7
|
+
const [hasLoaded, setHasLoaded] = (0, react_1.useState)(false);
|
|
8
|
+
const [hasError, setHasError] = (0, react_1.useState)(false);
|
|
9
|
+
const [hasStartedInitialFetch, setHasStartedInitialFetch] = (0, react_1.useState)(false);
|
|
10
|
+
(0, react_1.useEffect)(() => {
|
|
11
|
+
setHasStartedInitialFetch(true);
|
|
12
|
+
setHasLoaded(false);
|
|
13
|
+
setHasError(false);
|
|
14
|
+
const image = new Image();
|
|
15
|
+
image.src = src;
|
|
16
|
+
const handleError = () => {
|
|
17
|
+
setHasError(true);
|
|
18
|
+
};
|
|
19
|
+
const handleLoad = () => {
|
|
20
|
+
setHasLoaded(true);
|
|
21
|
+
setHasError(false);
|
|
22
|
+
};
|
|
23
|
+
image.onerror = handleError;
|
|
24
|
+
image.onload = handleLoad;
|
|
25
|
+
return () => {
|
|
26
|
+
image.removeEventListener('error', handleError);
|
|
27
|
+
image.removeEventListener('load', handleLoad);
|
|
28
|
+
};
|
|
29
|
+
}, [src]);
|
|
30
|
+
return { hasLoaded, hasError, hasStartedInitialFetch };
|
|
31
|
+
};
|
|
32
|
+
exports.useImage = useImage;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { RequestStatus } from '@open-tender/types';
|
|
3
|
+
export declare const useLevelUpForm: (email: string, loading: RequestStatus, error: any, connect: (data: any, callback: any) => void, callback: any) => {
|
|
4
|
+
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
|
+
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
|
+
data: any;
|
|
7
|
+
errors: {};
|
|
8
|
+
submitting: boolean;
|
|
9
|
+
handleChange: (evt?: any) => void;
|
|
10
|
+
handleSubmit: (evt?: any) => void;
|
|
11
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useLevelUpForm = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const useLevelUpForm = (email, loading, error, connect, callback) => {
|
|
6
|
+
const submitRef = (0, react_1.useRef)(null);
|
|
7
|
+
const inputRef = (0, react_1.useRef)(null);
|
|
8
|
+
const [data, setData] = (0, react_1.useState)({ email });
|
|
9
|
+
const [errors, setErrors] = (0, react_1.useState)({});
|
|
10
|
+
const [submitting, setSubmitting] = (0, react_1.useState)(false);
|
|
11
|
+
(0, react_1.useEffect)(() => {
|
|
12
|
+
var _a;
|
|
13
|
+
if (loading === 'idle') {
|
|
14
|
+
setSubmitting(false);
|
|
15
|
+
if (error) {
|
|
16
|
+
setErrors(error);
|
|
17
|
+
(_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}, [loading, error]);
|
|
21
|
+
const handleChange = (evt) => {
|
|
22
|
+
const { id, type, value, checked } = evt === null || evt === void 0 ? void 0 : evt.target;
|
|
23
|
+
const inputValue = type === 'checkbox' ? checked : value;
|
|
24
|
+
setData(Object.assign(Object.assign({}, data), { [id]: inputValue }));
|
|
25
|
+
};
|
|
26
|
+
const handleSubmit = (evt) => {
|
|
27
|
+
var _a;
|
|
28
|
+
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
29
|
+
setSubmitting(true);
|
|
30
|
+
connect(data, callback);
|
|
31
|
+
(_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur();
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
submitRef,
|
|
35
|
+
inputRef,
|
|
36
|
+
data,
|
|
37
|
+
errors,
|
|
38
|
+
submitting,
|
|
39
|
+
handleChange,
|
|
40
|
+
handleSubmit,
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
exports.useLevelUpForm = useLevelUpForm;
|
|
@@ -2,6 +2,7 @@ export * from './menu';
|
|
|
2
2
|
export * from './useAddressForm';
|
|
3
3
|
export * from './useAllergenForm';
|
|
4
4
|
export * from './useCreditCard';
|
|
5
|
+
export * from './useCreditCardForm';
|
|
5
6
|
export * from './useCurbside';
|
|
6
7
|
export * from './useDineInForm';
|
|
7
8
|
export * from './useDonationForm';
|
|
@@ -22,3 +23,5 @@ export * from './useResetPasswordForm';
|
|
|
22
23
|
export * from './useSendResetForm';
|
|
23
24
|
export * from './useSigninForm';
|
|
24
25
|
export * from './types';
|
|
26
|
+
export * from './useImage';
|
|
27
|
+
export * from './useLevelUpForm';
|
package/dist/esm/hooks/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export * from './menu';
|
|
|
2
2
|
export * from './useAddressForm';
|
|
3
3
|
export * from './useAllergenForm';
|
|
4
4
|
export * from './useCreditCard';
|
|
5
|
+
export * from './useCreditCardForm';
|
|
5
6
|
export * from './useCurbside';
|
|
6
7
|
export * from './useDineInForm';
|
|
7
8
|
export * from './useDonationForm';
|
|
@@ -22,3 +23,5 @@ export * from './useResetPasswordForm';
|
|
|
22
23
|
export * from './useSendResetForm';
|
|
23
24
|
export * from './useSigninForm';
|
|
24
25
|
export * from './types';
|
|
26
|
+
export * from './useImage';
|
|
27
|
+
export * from './useLevelUpForm';
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import {
|
|
3
|
-
export declare const
|
|
4
|
-
submitRef: import("react").MutableRefObject<
|
|
5
|
-
recaptchaRef: import("react").MutableRefObject<any>;
|
|
2
|
+
import { CreditCardData } from '@open-tender/types';
|
|
3
|
+
export declare const useCreditCard: (initState: any, cardErrors?: {}, disabled?: boolean) => {
|
|
4
|
+
submitRef: import("react").MutableRefObject<null>;
|
|
6
5
|
data: CreditCardData;
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
cardType: "VISA" | "MC" | "DISC" | "AMEX" | "OTHER";
|
|
7
|
+
errors: {};
|
|
8
|
+
disabled: boolean;
|
|
9
|
+
setErrors: import("react").Dispatch<import("react").SetStateAction<{}>>;
|
|
10
|
+
handleChange: (evt?: any) => void;
|
|
11
|
+
handleBlur: (evt?: any) => void;
|
|
12
|
+
isComplete: boolean;
|
|
9
13
|
};
|
|
@@ -1,71 +1,54 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { validateCreditCard } from '
|
|
3
|
-
|
|
1
|
+
import { useRef, useState } from 'react';
|
|
2
|
+
import { formatCardField, formatCard, getCardType, isEmpty, validateCreditCard, } from '../';
|
|
3
|
+
const defaultState = {
|
|
4
|
+
acct: '',
|
|
5
|
+
exp: '',
|
|
6
|
+
cvv: '',
|
|
7
|
+
zip: '',
|
|
8
|
+
is_default: false,
|
|
9
|
+
};
|
|
10
|
+
export const useCreditCard = (initState, cardErrors = {}, disabled = false) => {
|
|
11
|
+
const [initCard, initCardType] = formatCard(initState);
|
|
4
12
|
const submitRef = useRef(null);
|
|
5
|
-
const
|
|
6
|
-
const [
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
const [data, setData] = useState(initCard || defaultState);
|
|
14
|
+
const [cardType, setCardType] = useState(initCardType || 'OTHER');
|
|
15
|
+
const [errors, setErrors] = useState(cardErrors);
|
|
16
|
+
const { acct, exp, cvv, zip } = data;
|
|
17
|
+
const isComplete = acct && exp && cvv && zip && !isEmpty(errors) ? true : false;
|
|
18
|
+
const handleChange = (evt) => {
|
|
19
|
+
let { id, checked, value } = evt.target;
|
|
20
|
+
if (id === 'acct') {
|
|
21
|
+
const currentType = getCardType(value.replace(/\s/g, ''));
|
|
22
|
+
setCardType(currentType);
|
|
23
|
+
}
|
|
24
|
+
if (id === 'is_default') {
|
|
25
|
+
value = checked;
|
|
17
26
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
else {
|
|
28
|
+
value = formatCardField(id, value);
|
|
29
|
+
}
|
|
30
|
+
setData(Object.assign(Object.assign({}, data), { [id]: value }));
|
|
31
|
+
};
|
|
32
|
+
const handleBlur = (evt) => {
|
|
33
|
+
let { id } = evt.target;
|
|
34
|
+
const { errors: validationErrors } = validateCreditCard(data, cardType);
|
|
35
|
+
if (validationErrors) {
|
|
36
|
+
const error = validationErrors[id];
|
|
37
|
+
setErrors(Object.assign(Object.assign({}, errors), { [id]: error }));
|
|
29
38
|
}
|
|
30
39
|
else {
|
|
31
|
-
|
|
32
|
-
if (recaptchaKey) {
|
|
33
|
-
try {
|
|
34
|
-
const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
|
|
35
|
-
if (!token) {
|
|
36
|
-
setSubmitting(false);
|
|
37
|
-
setErrors({
|
|
38
|
-
form: 'Please complete the recaptcha before submitting',
|
|
39
|
-
});
|
|
40
|
-
if (windowRef)
|
|
41
|
-
windowRef.scrollTo({ top: 0 });
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
setSubmitting(true);
|
|
45
|
-
addCard(Object.assign(Object.assign({}, cardRc), { token }), callback);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
catch (err) {
|
|
49
|
-
setSubmitting(false);
|
|
50
|
-
setErrors({
|
|
51
|
-
form: 'Please complete the recaptcha before submitting',
|
|
52
|
-
});
|
|
53
|
-
if (windowRef)
|
|
54
|
-
windowRef.scrollTo({ top: 0 });
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
setSubmitting(true);
|
|
59
|
-
addCard(cardRc, callback);
|
|
60
|
-
}
|
|
40
|
+
setErrors(Object.assign(Object.assign({}, errors), { [id]: null }));
|
|
61
41
|
}
|
|
62
|
-
((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
|
|
63
42
|
};
|
|
64
43
|
return {
|
|
65
44
|
submitRef,
|
|
66
|
-
recaptchaRef,
|
|
67
45
|
data,
|
|
68
|
-
|
|
69
|
-
|
|
46
|
+
cardType,
|
|
47
|
+
errors,
|
|
48
|
+
disabled,
|
|
49
|
+
setErrors,
|
|
50
|
+
handleChange,
|
|
51
|
+
handleBlur,
|
|
52
|
+
isComplete,
|
|
70
53
|
};
|
|
71
54
|
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { CardType, CreditCardData, RequestStatus } from '@open-tender/types';
|
|
3
|
+
export declare const useCreditCardForm: (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) => {
|
|
4
|
+
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
|
+
recaptchaRef: import("react").MutableRefObject<any>;
|
|
6
|
+
data: CreditCardData;
|
|
7
|
+
submitting: boolean;
|
|
8
|
+
handleSubmit: (evt?: any) => void;
|
|
9
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { useState, useEffect, useRef } from 'react';
|
|
2
|
+
import { validateCreditCard } from '../';
|
|
3
|
+
export 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
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}, [windowRef, loading, error, setErrors]);
|
|
19
|
+
const handleSubmit = (evt) => {
|
|
20
|
+
var _a, _b, _c;
|
|
21
|
+
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
22
|
+
setErrors({});
|
|
23
|
+
const { card, errors: cardErrors } = validateCreditCard(data, cardType);
|
|
24
|
+
if (cardErrors) {
|
|
25
|
+
setErrors(cardErrors);
|
|
26
|
+
setSubmitting(false);
|
|
27
|
+
if (windowRef)
|
|
28
|
+
windowRef.scrollTo({ top: 0 });
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
const cardRc = revenue_center_id ? Object.assign(Object.assign({}, card), { revenue_center_id }) : card;
|
|
32
|
+
if (recaptchaKey) {
|
|
33
|
+
try {
|
|
34
|
+
const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
|
|
35
|
+
if (!token) {
|
|
36
|
+
setSubmitting(false);
|
|
37
|
+
setErrors({
|
|
38
|
+
form: 'Please complete the recaptcha before submitting',
|
|
39
|
+
});
|
|
40
|
+
if (windowRef)
|
|
41
|
+
windowRef.scrollTo({ top: 0 });
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
setSubmitting(true);
|
|
45
|
+
addCard(Object.assign(Object.assign({}, cardRc), { token }), callback);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
setSubmitting(false);
|
|
50
|
+
setErrors({
|
|
51
|
+
form: 'Please complete the recaptcha before submitting',
|
|
52
|
+
});
|
|
53
|
+
if (windowRef)
|
|
54
|
+
windowRef.scrollTo({ top: 0 });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
setSubmitting(true);
|
|
59
|
+
addCard(cardRc, callback);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
submitRef,
|
|
66
|
+
recaptchaRef,
|
|
67
|
+
data,
|
|
68
|
+
submitting,
|
|
69
|
+
handleSubmit,
|
|
70
|
+
};
|
|
71
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
// https://typesafe.blog/article/fetch-images-with-a-react-hook
|
|
3
|
+
export const useImage = (src) => {
|
|
4
|
+
const [hasLoaded, setHasLoaded] = useState(false);
|
|
5
|
+
const [hasError, setHasError] = useState(false);
|
|
6
|
+
const [hasStartedInitialFetch, setHasStartedInitialFetch] = useState(false);
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
setHasStartedInitialFetch(true);
|
|
9
|
+
setHasLoaded(false);
|
|
10
|
+
setHasError(false);
|
|
11
|
+
const image = new Image();
|
|
12
|
+
image.src = src;
|
|
13
|
+
const handleError = () => {
|
|
14
|
+
setHasError(true);
|
|
15
|
+
};
|
|
16
|
+
const handleLoad = () => {
|
|
17
|
+
setHasLoaded(true);
|
|
18
|
+
setHasError(false);
|
|
19
|
+
};
|
|
20
|
+
image.onerror = handleError;
|
|
21
|
+
image.onload = handleLoad;
|
|
22
|
+
return () => {
|
|
23
|
+
image.removeEventListener('error', handleError);
|
|
24
|
+
image.removeEventListener('load', handleLoad);
|
|
25
|
+
};
|
|
26
|
+
}, [src]);
|
|
27
|
+
return { hasLoaded, hasError, hasStartedInitialFetch };
|
|
28
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { RequestStatus } from '@open-tender/types';
|
|
3
|
+
export declare const useLevelUpForm: (email: string, loading: RequestStatus, error: any, connect: (data: any, callback: any) => void, callback: any) => {
|
|
4
|
+
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
|
+
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
|
+
data: any;
|
|
7
|
+
errors: {};
|
|
8
|
+
submitting: boolean;
|
|
9
|
+
handleChange: (evt?: any) => void;
|
|
10
|
+
handleSubmit: (evt?: any) => void;
|
|
11
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useState, useEffect, useRef } from 'react';
|
|
2
|
+
export const useLevelUpForm = (email, loading, error, connect, callback) => {
|
|
3
|
+
const submitRef = useRef(null);
|
|
4
|
+
const inputRef = useRef(null);
|
|
5
|
+
const [data, setData] = useState({ email });
|
|
6
|
+
const [errors, setErrors] = useState({});
|
|
7
|
+
const [submitting, setSubmitting] = useState(false);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
var _a;
|
|
10
|
+
if (loading === 'idle') {
|
|
11
|
+
setSubmitting(false);
|
|
12
|
+
if (error) {
|
|
13
|
+
setErrors(error);
|
|
14
|
+
(_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}, [loading, error]);
|
|
18
|
+
const handleChange = (evt) => {
|
|
19
|
+
const { id, type, value, checked } = evt === null || evt === void 0 ? void 0 : evt.target;
|
|
20
|
+
const inputValue = type === 'checkbox' ? checked : value;
|
|
21
|
+
setData(Object.assign(Object.assign({}, data), { [id]: inputValue }));
|
|
22
|
+
};
|
|
23
|
+
const handleSubmit = (evt) => {
|
|
24
|
+
var _a;
|
|
25
|
+
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
26
|
+
setSubmitting(true);
|
|
27
|
+
connect(data, callback);
|
|
28
|
+
(_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur();
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
submitRef,
|
|
32
|
+
inputRef,
|
|
33
|
+
data,
|
|
34
|
+
errors,
|
|
35
|
+
submitting,
|
|
36
|
+
handleChange,
|
|
37
|
+
handleSubmit,
|
|
38
|
+
};
|
|
39
|
+
};
|
package/package.json
CHANGED