@open-tender/utils 0.0.32 → 0.0.34

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 (61) hide show
  1. package/dist/cjs/hooks/index.d.ts +14 -0
  2. package/dist/cjs/hooks/index.js +14 -0
  3. package/dist/cjs/hooks/useAddressForm.d.ts +13 -0
  4. package/dist/cjs/hooks/useAddressForm.js +54 -0
  5. package/dist/cjs/hooks/useAllergenForm.d.ts +9 -0
  6. package/dist/cjs/hooks/useAllergenForm.js +44 -0
  7. package/dist/cjs/hooks/useCreditCard.d.ts +14 -0
  8. package/dist/cjs/hooks/useCreditCard.js +53 -0
  9. package/dist/cjs/hooks/useCurbside.d.ts +10 -0
  10. package/dist/cjs/hooks/useCurbside.js +37 -0
  11. package/dist/cjs/hooks/useDineInForm.d.ts +7 -0
  12. package/dist/cjs/hooks/useDineInForm.js +25 -0
  13. package/dist/cjs/hooks/useDonationForm.d.ts +25 -0
  14. package/dist/cjs/hooks/useDonationForm.js +155 -0
  15. package/dist/cjs/hooks/useGiftCardAssignForm.d.ts +15 -0
  16. package/dist/cjs/hooks/useGiftCardAssignForm.js +38 -0
  17. package/dist/cjs/hooks/useGiftCardAssignOtherForm.d.ts +9 -0
  18. package/dist/cjs/hooks/useGiftCardAssignOtherForm.js +31 -0
  19. package/dist/cjs/hooks/useGiftCardForm.d.ts +13 -0
  20. package/dist/cjs/hooks/useGiftCardForm.js +48 -0
  21. package/dist/cjs/hooks/useGiftCardsForm.d.ts +23 -0
  22. package/dist/cjs/hooks/useGiftCardsForm.js +146 -0
  23. package/dist/cjs/hooks/useLatLng.d.ts +8 -0
  24. package/dist/cjs/hooks/useLatLng.js +13 -0
  25. package/dist/cjs/hooks/useLoyaltyPoints.d.ts +2 -0
  26. package/dist/cjs/hooks/useLoyaltyPoints.js +18 -0
  27. package/dist/cjs/hooks/useOrderRating.d.ts +11 -0
  28. package/dist/cjs/hooks/useOrderRating.js +32 -0
  29. package/dist/cjs/hooks/useProfileForm.d.ts +29 -0
  30. package/dist/cjs/hooks/useProfileForm.js +75 -0
  31. package/dist/esm/hooks/index.d.ts +14 -0
  32. package/dist/esm/hooks/index.js +14 -0
  33. package/dist/esm/hooks/useAddressForm.d.ts +13 -0
  34. package/dist/esm/hooks/useAddressForm.js +50 -0
  35. package/dist/esm/hooks/useAllergenForm.d.ts +9 -0
  36. package/dist/esm/hooks/useAllergenForm.js +42 -0
  37. package/dist/esm/hooks/useCreditCard.d.ts +14 -0
  38. package/dist/esm/hooks/useCreditCard.js +51 -0
  39. package/dist/esm/hooks/useCurbside.d.ts +10 -0
  40. package/dist/esm/hooks/useCurbside.js +35 -0
  41. package/dist/esm/hooks/useDineInForm.d.ts +7 -0
  42. package/dist/esm/hooks/useDineInForm.js +23 -0
  43. package/dist/esm/hooks/useDonationForm.d.ts +25 -0
  44. package/dist/esm/hooks/useDonationForm.js +153 -0
  45. package/dist/esm/hooks/useGiftCardAssignForm.d.ts +15 -0
  46. package/dist/esm/hooks/useGiftCardAssignForm.js +36 -0
  47. package/dist/esm/hooks/useGiftCardAssignOtherForm.d.ts +9 -0
  48. package/dist/esm/hooks/useGiftCardAssignOtherForm.js +29 -0
  49. package/dist/esm/hooks/useGiftCardForm.d.ts +13 -0
  50. package/dist/esm/hooks/useGiftCardForm.js +46 -0
  51. package/dist/esm/hooks/useGiftCardsForm.d.ts +23 -0
  52. package/dist/esm/hooks/useGiftCardsForm.js +144 -0
  53. package/dist/esm/hooks/useLatLng.d.ts +8 -0
  54. package/dist/esm/hooks/useLatLng.js +11 -0
  55. package/dist/esm/hooks/useLoyaltyPoints.d.ts +2 -0
  56. package/dist/esm/hooks/useLoyaltyPoints.js +16 -0
  57. package/dist/esm/hooks/useOrderRating.d.ts +11 -0
  58. package/dist/esm/hooks/useOrderRating.js +30 -0
  59. package/dist/esm/hooks/useProfileForm.d.ts +29 -0
  60. package/dist/esm/hooks/useProfileForm.js +73 -0
  61. package/package.json +4 -2
@@ -1 +1,15 @@
1
1
  export * from './menu';
2
+ export * from './useAddressForm';
3
+ export * from './useAllergenForm';
4
+ export * from './useCreditCard';
5
+ export * from './useCurbside';
6
+ export * from './useDineInForm';
7
+ export * from './useDonationForm';
8
+ export * from './useGiftCardAssignForm';
9
+ export * from './useGiftCardAssignOtherForm';
10
+ export * from './useGiftCardForm';
11
+ export * from './useGiftCardsForm';
12
+ export * from './useLatLng';
13
+ export * from './useLoyaltyPoints';
14
+ export * from './useOrderRating';
15
+ export * from './useProfileForm';
@@ -2,3 +2,17 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./menu"), exports);
5
+ tslib_1.__exportStar(require("./useAddressForm"), exports);
6
+ tslib_1.__exportStar(require("./useAllergenForm"), exports);
7
+ tslib_1.__exportStar(require("./useCreditCard"), exports);
8
+ tslib_1.__exportStar(require("./useCurbside"), exports);
9
+ tslib_1.__exportStar(require("./useDineInForm"), exports);
10
+ tslib_1.__exportStar(require("./useDonationForm"), exports);
11
+ tslib_1.__exportStar(require("./useGiftCardAssignForm"), exports);
12
+ tslib_1.__exportStar(require("./useGiftCardAssignOtherForm"), exports);
13
+ tslib_1.__exportStar(require("./useGiftCardForm"), exports);
14
+ tslib_1.__exportStar(require("./useGiftCardsForm"), exports);
15
+ tslib_1.__exportStar(require("./useLatLng"), exports);
16
+ tslib_1.__exportStar(require("./useLoyaltyPoints"), exports);
17
+ tslib_1.__exportStar(require("./useOrderRating"), exports);
18
+ tslib_1.__exportStar(require("./useProfileForm"), exports);
@@ -0,0 +1,13 @@
1
+ import { Address, RequestStatus } from '@open-tender/types';
2
+ export declare const useAddressForm: (address: Address, loading: RequestStatus, error: any, update: (addressId: number, updatedData: Address, callback: () => void) => void, callback: () => void) => {
3
+ fields: {
4
+ label: string;
5
+ name: string;
6
+ type: string;
7
+ }[];
8
+ data: Address;
9
+ errors: any;
10
+ submitting: boolean;
11
+ handleChange: (id: string, type: string, value: string | null, checked: boolean | null) => void;
12
+ handleSubmit: () => void;
13
+ };
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useAddressForm = void 0;
4
+ const react_1 = require("react");
5
+ const utils_1 = require("../utils");
6
+ const useAddressForm = (address, loading, error, update, callback) => {
7
+ const [data, setData] = (0, react_1.useState)(address);
8
+ const [errors, setErrors] = (0, react_1.useState)({});
9
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
10
+ const fields = [
11
+ { label: 'Company', name: 'company', type: 'default' },
12
+ { label: 'Contact Person', name: 'contact', type: 'default' },
13
+ { label: 'Contact Phone', name: 'phone', type: 'phone-pad' },
14
+ { label: 'Description', name: 'description', type: 'default' },
15
+ { label: 'Notes', name: 'notes', type: 'default' },
16
+ { label: 'Is Default', name: 'is_default', type: 'checkbox' },
17
+ ];
18
+ (0, react_1.useEffect)(() => {
19
+ if (loading === 'idle') {
20
+ setSubmitting(false);
21
+ if (error) {
22
+ setErrors(error);
23
+ }
24
+ }
25
+ }, [loading, error]);
26
+ const handleChange = (id, type, value, checked) => {
27
+ const inputValue = type === 'checkbox'
28
+ ? checked
29
+ : id === 'phone'
30
+ ? (0, utils_1.makePhone)(value !== null && value !== void 0 ? value : '')
31
+ : value;
32
+ setData(Object.assign(Object.assign({}, data), { [id]: inputValue }));
33
+ };
34
+ const handleSubmit = () => {
35
+ setSubmitting(true);
36
+ const updatedData = Object.assign({}, data);
37
+ delete updatedData.customer_address_id;
38
+ delete updatedData.created_at;
39
+ delete updatedData.last_used_at;
40
+ const addressId = data.customer_address_id;
41
+ if (addressId) {
42
+ update(addressId, updatedData, callback);
43
+ }
44
+ };
45
+ return {
46
+ fields,
47
+ data,
48
+ errors,
49
+ submitting,
50
+ handleChange,
51
+ handleSubmit,
52
+ };
53
+ };
54
+ exports.useAddressForm = useAddressForm;
@@ -0,0 +1,9 @@
1
+ import { Allergen, Allergens } from '@open-tender/types';
2
+ declare const useAllergenForm: (allergens: Allergens, selectedAllergens: Allergens, isLoading: boolean | undefined, error: any, setAllergens: (allergens: Allergens) => void, updateAllergens: (allergen: Partial<Allergen>[]) => void, callback?: () => void) => {
3
+ submitting: boolean;
4
+ allergenIds: number[];
5
+ formError: any;
6
+ handleChange: (id: string, checked?: boolean) => void;
7
+ handleSubmit: () => void;
8
+ };
9
+ export default useAllergenForm;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useAllergenForm = (allergens, selectedAllergens, isLoading = false, error = null, setAllergens, updateAllergens, callback) => {
5
+ const [data, setData] = (0, react_1.useState)([]);
6
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
7
+ const allergenIds = data.map(i => i.allergen_id);
8
+ const formError = error ? error.detail || error.message : null;
9
+ (0, react_1.useEffect)(() => {
10
+ // uses isLoading boolean because loading state is based on
11
+ // both brand allergens and customer allergens loading
12
+ if (!isLoading) {
13
+ setSubmitting(false);
14
+ }
15
+ }, [isLoading]);
16
+ (0, react_1.useEffect)(() => {
17
+ setData(selectedAllergens || []);
18
+ }, [selectedAllergens, allergens]);
19
+ const handleChange = (id, checked) => {
20
+ const newData = checked
21
+ ? [...data, { allergen_id: parseInt(id) }]
22
+ : data.filter(i => i.allergen_id !== parseInt(id));
23
+ setData(newData);
24
+ };
25
+ const handleSubmit = () => {
26
+ setSubmitting(updateAllergens !== null);
27
+ const newData = data.map(i => ({ allergen_id: i.allergen_id }));
28
+ setAllergens(data);
29
+ if (updateAllergens) {
30
+ updateAllergens(newData);
31
+ }
32
+ else if (callback) {
33
+ callback();
34
+ }
35
+ };
36
+ return {
37
+ submitting,
38
+ allergenIds,
39
+ formError,
40
+ handleChange,
41
+ handleSubmit,
42
+ };
43
+ };
44
+ exports.default = useAllergenForm;
@@ -0,0 +1,14 @@
1
+ /// <reference types="react" />
2
+ import { CreditCardData } from '@open-tender/types';
3
+ declare const useCreditCard: (initState?: CreditCardData, cardErrors?: any, disabled?: boolean) => {
4
+ 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;
13
+ };
14
+ export default useCreditCard;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const utils_1 = require("../utils");
5
+ const defaultState = {
6
+ acct: '',
7
+ exp: '',
8
+ cvv: '',
9
+ zip: '',
10
+ is_default: false,
11
+ };
12
+ const useCreditCard = (initState, cardErrors, disabled) => {
13
+ const [initCard, initCardType] = (0, utils_1.formatCard)(initState !== null && initState !== void 0 ? initState : defaultState);
14
+ const [data, setData] = (0, react_1.useState)(initCard || defaultState);
15
+ const [cardType, setCardType] = (0, react_1.useState)(initCardType || 'OTHER');
16
+ const [errors, setErrors] = (0, react_1.useState)(cardErrors !== null && cardErrors !== void 0 ? cardErrors : {});
17
+ const { acct, exp, cvv, zip } = data;
18
+ const isComplete = acct && exp && cvv && zip && !(0, utils_1.isEmpty)(errors) ? true : false;
19
+ const handleChange = (name, value) => {
20
+ if (name === 'acct') {
21
+ const currentType = (0, utils_1.getCardType)(value.replace(/\s/g, ''));
22
+ setCardType(currentType);
23
+ }
24
+ const val = name === 'is_default' ? value : (0, utils_1.formatCardField)(name, value);
25
+ setData(Object.assign(Object.assign({}, data), { [name]: val }));
26
+ };
27
+ const handleBlur = (name) => {
28
+ const { errors: validationErrors } = (0, utils_1.validateCreditCard)(data, cardType);
29
+ if (validationErrors) {
30
+ const error = validationErrors[name];
31
+ setErrors(Object.assign(Object.assign({}, errors), { [name]: error }));
32
+ }
33
+ else {
34
+ setErrors(Object.assign(Object.assign({}, errors), { [name]: null }));
35
+ }
36
+ };
37
+ const handleReset = () => {
38
+ setData(defaultState);
39
+ setErrors({});
40
+ };
41
+ return {
42
+ data,
43
+ cardType,
44
+ errors,
45
+ disabled,
46
+ setErrors,
47
+ handleChange,
48
+ handleBlur,
49
+ handleReset,
50
+ isComplete,
51
+ };
52
+ };
53
+ exports.default = useCreditCard;
@@ -0,0 +1,10 @@
1
+ import { ConfigFulfillment, OrderFulfillment, RequestStatus } from '@open-tender/types';
2
+ declare const useCurbside: (orderId: number, fulfillment: Partial<OrderFulfillment>, loading: RequestStatus, error: any, update: (orderId: number, data: Partial<OrderFulfillment>) => void, settings?: ConfigFulfillment, showAllFields?: boolean) => {
3
+ fields: import("@open-tender/types").ConfigFulfillmentField[] | undefined;
4
+ data: Partial<OrderFulfillment>;
5
+ errors: any;
6
+ submitting: boolean;
7
+ handleChange: (name: keyof OrderFulfillment, value: string) => void;
8
+ handleSubmit: () => void;
9
+ };
10
+ export default useCurbside;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const utils_1 = require("../utils");
5
+ const useCurbside = (orderId, fulfillment, loading, error, update, settings, showAllFields = false) => {
6
+ const [data, setData] = (0, react_1.useState)({});
7
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
8
+ const errors = error || {};
9
+ const empty = Object.values(fulfillment).every(i => !i);
10
+ const fields = !empty && !showAllFields
11
+ ? settings === null || settings === void 0 ? void 0 : settings.fields.filter(i => i.name.startsWith('arrival'))
12
+ : settings === null || settings === void 0 ? void 0 : settings.fields;
13
+ (0, react_1.useEffect)(() => {
14
+ if (loading === 'idle')
15
+ setSubmitting(false);
16
+ }, [loading]);
17
+ (0, react_1.useEffect)(() => {
18
+ if (!(0, utils_1.isEmpty)(fulfillment))
19
+ setData(fulfillment);
20
+ }, [fulfillment]);
21
+ const handleChange = (name, value) => {
22
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
23
+ };
24
+ const handleSubmit = () => {
25
+ setSubmitting(true);
26
+ update(orderId, Object.assign(Object.assign({}, data), { has_arrived: true }));
27
+ };
28
+ return {
29
+ fields,
30
+ data,
31
+ errors,
32
+ submitting,
33
+ handleChange,
34
+ handleSubmit,
35
+ };
36
+ };
37
+ exports.default = useCurbside;
@@ -0,0 +1,7 @@
1
+ declare const useDineInForm: (initialData: any, submitGuest: (data: any) => void) => {
2
+ data: any;
3
+ errMsg: string | null;
4
+ handleChange: (id: string, value: unknown) => void;
5
+ handleSubmit: () => void;
6
+ };
7
+ export default useDineInForm;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useDineInForm = (initialData = {}, submitGuest) => {
5
+ const [data, setData] = (0, react_1.useState)(initialData);
6
+ const [errMsg, setErrMsg] = (0, react_1.useState)(null);
7
+ const handleChange = (id, value) => {
8
+ setData(Object.assign(Object.assign({}, data), { [id]: value }));
9
+ };
10
+ const handleSubmit = () => {
11
+ if (!data.first_name || !data.last_name) {
12
+ setErrMsg('Both first and last name are required');
13
+ }
14
+ else {
15
+ submitGuest({ customer: data });
16
+ }
17
+ };
18
+ return {
19
+ data,
20
+ errMsg,
21
+ handleChange,
22
+ handleSubmit,
23
+ };
24
+ };
25
+ exports.default = useDineInForm;
@@ -0,0 +1,25 @@
1
+ import { Dispatch } from 'react';
2
+ import { CardType, CreditCard, CreditCardData, CreditCards, Profile, PurchaseDonationRequest, RequestStatus } from '@open-tender/types';
3
+ interface CreditCardOption {
4
+ label: string;
5
+ value: unknown;
6
+ }
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
+ recaptchaRef: import("react").MutableRefObject<any>;
9
+ amount: string | null;
10
+ handleAmount: (value: string) => void;
11
+ email: string | null;
12
+ handleEmail: (value: string) => void;
13
+ errors: any;
14
+ errMsg: any;
15
+ newCardErrors: {};
16
+ submitting: boolean;
17
+ isNewCard: boolean;
18
+ creditCard: CreditCard;
19
+ creditCardOptions: CreditCardOption[];
20
+ handleCreditCard: (value: string) => void;
21
+ handleSubmit: () => void;
22
+ handleReset: () => void;
23
+ setErrors: Dispatch<any>;
24
+ };
25
+ export default useDonationsForm;
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const utils_1 = require("../utils");
5
+ const useDonationsForm = (purchase, reset, setAlert, loading, error, success, customer, creditCards, recaptchaKey, cardData, cardType) => {
6
+ const recaptchaRef = (0, react_1.useRef)(null);
7
+ const [amount, setAmount] = (0, react_1.useState)('');
8
+ const [email, setEmail] = (0, react_1.useState)('');
9
+ const [isNewCard, setIsNewCard] = (0, react_1.useState)(true);
10
+ const [creditCard, setCreditCard] = (0, react_1.useState)({});
11
+ const [creditCardOptions, setCreditCardOptions] = (0, react_1.useState)([]);
12
+ const [errors, setErrors] = (0, react_1.useState)({});
13
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
14
+ const errMsg = errors.form && errors.form.includes('parameters')
15
+ ? 'There are one or more errors below'
16
+ : errors.form || null;
17
+ const newCardErrors = (0, react_1.useMemo)(() => errors
18
+ ? Object.entries(errors)
19
+ .filter(([key]) => key !== 'form')
20
+ .reduce((obj, [key, value]) => (Object.assign(Object.assign({}, obj), { [key]: value })), {})
21
+ : {}, [errors]);
22
+ (0, react_1.useEffect)(() => {
23
+ if (loading === 'idle') {
24
+ setSubmitting(false);
25
+ setAlert({ type: 'close' });
26
+ if (error) {
27
+ if (recaptchaRef.current)
28
+ recaptchaRef.current.reset();
29
+ setErrors(error);
30
+ }
31
+ else if (success) {
32
+ //
33
+ }
34
+ }
35
+ }, [loading, error, setAlert, success]);
36
+ (0, react_1.useEffect)(() => {
37
+ if ((creditCards === null || creditCards === void 0 ? void 0 : creditCards.length) && loading !== 'pending') {
38
+ const options = creditCards.map(i => ({
39
+ label: `${i.card_type_name} ending in ${i.last4}`,
40
+ value: i.customer_card_id,
41
+ }));
42
+ setCreditCardOptions(options);
43
+ const defaultCard = creditCards.length
44
+ ? { customer_card_id: creditCards[0].customer_card_id }
45
+ : {};
46
+ setCreditCard(defaultCard);
47
+ setIsNewCard(false);
48
+ }
49
+ }, [creditCards, loading]);
50
+ (0, react_1.useEffect)(() => {
51
+ setEmail((customer === null || customer === void 0 ? void 0 : customer.email) ? customer.email : '');
52
+ }, [customer]);
53
+ const handleAmount = (value) => {
54
+ const cleanValue = (0, utils_1.makeNumeric)(value);
55
+ setAmount(cleanValue);
56
+ };
57
+ const handleEmail = (value) => {
58
+ setEmail(value);
59
+ };
60
+ const handleCreditCard = (value) => {
61
+ const customerCardId = parseInt(value);
62
+ setCreditCard({ customer_card_id: customerCardId });
63
+ };
64
+ const purchaseWithCaptcha = (credit_card) => {
65
+ const alert = {
66
+ type: 'working',
67
+ args: { text: 'Submitting your contribution...' },
68
+ };
69
+ if (recaptchaKey) {
70
+ try {
71
+ const token = recaptchaRef.current.getValue();
72
+ if (!token) {
73
+ setSubmitting(false);
74
+ setErrors({ form: 'Please complete the recaptcha before submitting' });
75
+ }
76
+ else {
77
+ setSubmitting(true);
78
+ setAlert(alert);
79
+ purchase({
80
+ amount: amount,
81
+ email: email,
82
+ credit_card,
83
+ token: token,
84
+ });
85
+ }
86
+ }
87
+ catch (err) {
88
+ setSubmitting(false);
89
+ setErrors({ form: 'Please complete the recaptcha before submitting' });
90
+ }
91
+ }
92
+ else {
93
+ setSubmitting(true);
94
+ setAlert(alert);
95
+ purchase({
96
+ amount: amount,
97
+ email: email,
98
+ credit_card,
99
+ token: '',
100
+ });
101
+ }
102
+ };
103
+ const handleSubmit = () => {
104
+ if (!amount || !email) {
105
+ setErrors({ form: 'Both amount and email are required' });
106
+ }
107
+ else {
108
+ if (isNewCard) {
109
+ if (cardData && cardType) {
110
+ const { card, errors: errs } = (0, utils_1.validateCreditCard)(cardData, cardType);
111
+ if (errs) {
112
+ setErrors(Object.assign(Object.assign({}, errs), { form: 'There are one or more credit card errors below' }));
113
+ setSubmitting(false);
114
+ }
115
+ else {
116
+ purchaseWithCaptcha(card);
117
+ }
118
+ }
119
+ }
120
+ else {
121
+ purchaseWithCaptcha(creditCard);
122
+ }
123
+ }
124
+ };
125
+ const handleReset = () => {
126
+ setErrors({});
127
+ setAmount(null);
128
+ if (!customer)
129
+ setEmail(null);
130
+ if (!customer)
131
+ setIsNewCard(true);
132
+ setCreditCard({});
133
+ setCreditCardOptions([]);
134
+ reset();
135
+ };
136
+ return {
137
+ recaptchaRef,
138
+ amount,
139
+ handleAmount,
140
+ email,
141
+ handleEmail,
142
+ errors,
143
+ errMsg,
144
+ newCardErrors,
145
+ submitting,
146
+ isNewCard,
147
+ creditCard,
148
+ creditCardOptions,
149
+ handleCreditCard,
150
+ handleSubmit,
151
+ handleReset,
152
+ setErrors,
153
+ };
154
+ };
155
+ exports.default = useDonationsForm;
@@ -0,0 +1,15 @@
1
+ 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) => {
9
+ cardNumber: string;
10
+ errors: any;
11
+ submitting: boolean;
12
+ handleChange: (value: string) => void;
13
+ handleSubmit: () => void;
14
+ };
15
+ export default useGiftCardAssignForm;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useGiftCardAssignForm = ({ loading, error, assign, callback, }) => {
5
+ const [cardNumber, setCardNumber] = (0, react_1.useState)('');
6
+ const [errors, setErrors] = (0, react_1.useState)({});
7
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
8
+ (0, react_1.useEffect)(() => {
9
+ if (loading === 'idle') {
10
+ setSubmitting(false);
11
+ if (error) {
12
+ setErrors(error);
13
+ }
14
+ }
15
+ }, [loading, error]);
16
+ const handleChange = (value) => {
17
+ setCardNumber(value);
18
+ };
19
+ const handleSubmit = () => {
20
+ setSubmitting(true);
21
+ const card_number = parseInt(cardNumber);
22
+ if (isNaN(card_number)) {
23
+ setErrors({ card_number: 'Card numbers must be all digits' });
24
+ setSubmitting(false);
25
+ }
26
+ else {
27
+ assign(`${card_number}`, callback);
28
+ }
29
+ };
30
+ return {
31
+ cardNumber,
32
+ errors,
33
+ submitting,
34
+ handleChange,
35
+ handleSubmit,
36
+ };
37
+ };
38
+ exports.default = useGiftCardAssignForm;
@@ -0,0 +1,9 @@
1
+ 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
+ email: string;
4
+ errors: any;
5
+ submitting: boolean;
6
+ handleChange: (value: string) => void;
7
+ handleSubmit: () => void;
8
+ };
9
+ export default useGiftCardAssignOtherForm;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useGiftCardAssignOtherForm = (loading, error, giftCardId, assign, callback) => {
5
+ const [email, setEmail] = (0, react_1.useState)('');
6
+ const [errors, setErrors] = (0, react_1.useState)({});
7
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
8
+ (0, react_1.useEffect)(() => {
9
+ if (loading === 'idle') {
10
+ setSubmitting(false);
11
+ if (error) {
12
+ setErrors(error);
13
+ }
14
+ }
15
+ }, [loading, error]);
16
+ const handleChange = (value) => {
17
+ setEmail(value);
18
+ };
19
+ const handleSubmit = () => {
20
+ setSubmitting(true);
21
+ assign(giftCardId, email, callback);
22
+ };
23
+ return {
24
+ email,
25
+ errors,
26
+ submitting,
27
+ handleChange,
28
+ handleSubmit,
29
+ };
30
+ };
31
+ exports.default = useGiftCardAssignOtherForm;
@@ -0,0 +1,13 @@
1
+ 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
+ options: {
4
+ label: string;
5
+ value: number | undefined;
6
+ }[];
7
+ errors: any;
8
+ data: any;
9
+ submitting: boolean;
10
+ handleChange: (id: string, value: unknown) => void;
11
+ handleSubmit: () => void;
12
+ };
13
+ export default useGiftCardForm;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useGiftCardForm = (giftCard, creditCards, loading, error, update, add, callback) => {
5
+ const customerCardId = creditCards[0].customer_card_id;
6
+ const [data, setData] = (0, react_1.useState)({ customer_card_id: customerCardId });
7
+ const [errors, setErrors] = (0, react_1.useState)({});
8
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
9
+ const options = creditCards.map(i => {
10
+ return {
11
+ label: `${i.card_type_name} ending in ${i.last4}`,
12
+ value: i.customer_card_id,
13
+ };
14
+ });
15
+ (0, react_1.useEffect)(() => {
16
+ if (loading === 'idle') {
17
+ setSubmitting(false);
18
+ if (error) {
19
+ if (error.amount && error.amount.includes('money')) {
20
+ error.amount = 'Please enter a positive dollar amount';
21
+ }
22
+ setErrors(error);
23
+ }
24
+ }
25
+ }, [loading, error]);
26
+ const handleChange = (id, value) => {
27
+ setData(Object.assign(Object.assign({}, data), { [id]: value }));
28
+ };
29
+ const handleSubmit = () => {
30
+ setSubmitting(true);
31
+ if (!data.customer_card_id) {
32
+ data.customer_card_id = customerCardId;
33
+ }
34
+ data.customer_card_id = parseInt(`${data.customer_card_id}`);
35
+ giftCard
36
+ ? update(giftCard.gift_card_id, data, callback)
37
+ : add(data, callback);
38
+ };
39
+ return {
40
+ options,
41
+ errors,
42
+ data,
43
+ submitting,
44
+ handleChange,
45
+ handleSubmit,
46
+ };
47
+ };
48
+ exports.default = useGiftCardForm;