@open-tender/utils 0.0.67 → 0.0.68

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