@open-tender/utils 0.0.57 → 0.0.60

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 (93) hide show
  1. package/dist/cjs/hooks/index.d.ts +11 -0
  2. package/dist/cjs/hooks/index.js +11 -0
  3. package/dist/cjs/hooks/types.d.ts +4 -0
  4. package/dist/cjs/hooks/types.js +2 -0
  5. package/dist/cjs/hooks/useAddressForm.d.ts +2 -5
  6. package/dist/cjs/hooks/useAddressForm.js +3 -3
  7. package/dist/cjs/hooks/useAllergenForm.d.ts +5 -2
  8. package/dist/cjs/hooks/useAllergenForm.js +8 -4
  9. package/dist/cjs/hooks/useCartGuestForm.d.ts +21 -0
  10. package/dist/cjs/hooks/useCartGuestForm.js +46 -0
  11. package/dist/cjs/hooks/useCheckoutLoginForm.d.ts +12 -0
  12. package/dist/cjs/hooks/useCheckoutLoginForm.js +41 -0
  13. package/dist/cjs/hooks/useCommunicationPreferences.d.ts +0 -0
  14. package/dist/cjs/hooks/useCommunicationPreferences.js +1 -0
  15. package/dist/cjs/hooks/useCreditCard.d.ts +8 -11
  16. package/dist/cjs/hooks/useCreditCard.js +61 -41
  17. package/dist/cjs/hooks/useDineInForm.d.ts +8 -3
  18. package/dist/cjs/hooks/useDineInForm.js +8 -2
  19. package/dist/cjs/hooks/useDonationForm.d.ts +4 -1
  20. package/dist/cjs/hooks/useDonationForm.js +17 -3
  21. package/dist/cjs/hooks/useGiftCardAssignForm.d.ts +5 -7
  22. package/dist/cjs/hooks/useGiftCardAssignForm.js +11 -3
  23. package/dist/cjs/hooks/useGiftCardAssignOtherForm.d.ts +5 -1
  24. package/dist/cjs/hooks/useGiftCardAssignOtherForm.js +8 -0
  25. package/dist/cjs/hooks/useGiftCardForm.d.ts +9 -5
  26. package/dist/cjs/hooks/useGiftCardForm.js +13 -4
  27. package/dist/cjs/hooks/useGiftCardsForm.d.ts +30 -11
  28. package/dist/cjs/hooks/useGiftCardsForm.js +72 -31
  29. package/dist/cjs/hooks/useGuestForm.d.ts +13 -0
  30. package/dist/cjs/hooks/useGuestForm.js +41 -0
  31. package/dist/cjs/hooks/useOrderFulfillmentForm.d.ts +13 -0
  32. package/dist/cjs/hooks/useOrderFulfillmentForm.js +40 -0
  33. package/dist/cjs/hooks/useOrderRatingForm.d.ts +13 -0
  34. package/dist/cjs/hooks/useOrderRatingForm.js +33 -0
  35. package/dist/cjs/hooks/useProfileForm.d.ts +18 -23
  36. package/dist/cjs/hooks/useProfileForm.js +27 -28
  37. package/dist/cjs/hooks/useResetPasswordForm.d.ts +20 -0
  38. package/dist/cjs/hooks/useResetPasswordForm.js +66 -0
  39. package/dist/cjs/hooks/useSendResetForm.d.ts +12 -0
  40. package/dist/cjs/hooks/useSendResetForm.js +36 -0
  41. package/dist/cjs/hooks/useSignUpForm.d.ts +21 -0
  42. package/dist/cjs/hooks/useSignUpForm.js +91 -0
  43. package/dist/cjs/hooks/useSignUpGuestForm.d.ts +23 -0
  44. package/dist/cjs/hooks/useSignUpGuestForm.js +74 -0
  45. package/dist/cjs/hooks/useSigninForm.d.ts +12 -0
  46. package/dist/cjs/hooks/useSigninForm.js +41 -0
  47. package/dist/esm/hooks/index.d.ts +11 -0
  48. package/dist/esm/hooks/index.js +11 -0
  49. package/dist/esm/hooks/types.d.ts +4 -0
  50. package/dist/esm/hooks/types.js +1 -0
  51. package/dist/esm/hooks/useAddressForm.d.ts +2 -5
  52. package/dist/esm/hooks/useAddressForm.js +3 -3
  53. package/dist/esm/hooks/useAllergenForm.d.ts +5 -2
  54. package/dist/esm/hooks/useAllergenForm.js +9 -5
  55. package/dist/esm/hooks/useCartGuestForm.d.ts +21 -0
  56. package/dist/esm/hooks/useCartGuestForm.js +44 -0
  57. package/dist/esm/hooks/useCheckoutLoginForm.d.ts +12 -0
  58. package/dist/esm/hooks/useCheckoutLoginForm.js +39 -0
  59. package/dist/esm/hooks/useCommunicationPreferences.d.ts +0 -0
  60. package/dist/esm/hooks/useCommunicationPreferences.js +1 -0
  61. package/dist/esm/hooks/useCreditCard.d.ts +8 -11
  62. package/dist/esm/hooks/useCreditCard.js +62 -42
  63. package/dist/esm/hooks/useDineInForm.d.ts +8 -3
  64. package/dist/esm/hooks/useDineInForm.js +9 -3
  65. package/dist/esm/hooks/useDonationForm.d.ts +4 -1
  66. package/dist/esm/hooks/useDonationForm.js +17 -3
  67. package/dist/esm/hooks/useGiftCardAssignForm.d.ts +5 -7
  68. package/dist/esm/hooks/useGiftCardAssignForm.js +12 -4
  69. package/dist/esm/hooks/useGiftCardAssignOtherForm.d.ts +5 -1
  70. package/dist/esm/hooks/useGiftCardAssignOtherForm.js +9 -1
  71. package/dist/esm/hooks/useGiftCardForm.d.ts +9 -5
  72. package/dist/esm/hooks/useGiftCardForm.js +14 -5
  73. package/dist/esm/hooks/useGiftCardsForm.d.ts +30 -11
  74. package/dist/esm/hooks/useGiftCardsForm.js +73 -32
  75. package/dist/esm/hooks/useGuestForm.d.ts +13 -0
  76. package/dist/esm/hooks/useGuestForm.js +39 -0
  77. package/dist/esm/hooks/useOrderFulfillmentForm.d.ts +13 -0
  78. package/dist/esm/hooks/useOrderFulfillmentForm.js +38 -0
  79. package/dist/esm/hooks/useOrderRatingForm.d.ts +13 -0
  80. package/dist/esm/hooks/useOrderRatingForm.js +31 -0
  81. package/dist/esm/hooks/useProfileForm.d.ts +18 -23
  82. package/dist/esm/hooks/useProfileForm.js +28 -29
  83. package/dist/esm/hooks/useResetPasswordForm.d.ts +20 -0
  84. package/dist/esm/hooks/useResetPasswordForm.js +64 -0
  85. package/dist/esm/hooks/useSendResetForm.d.ts +12 -0
  86. package/dist/esm/hooks/useSendResetForm.js +34 -0
  87. package/dist/esm/hooks/useSignUpForm.d.ts +21 -0
  88. package/dist/esm/hooks/useSignUpForm.js +89 -0
  89. package/dist/esm/hooks/useSignUpGuestForm.d.ts +23 -0
  90. package/dist/esm/hooks/useSignUpGuestForm.js +72 -0
  91. package/dist/esm/hooks/useSigninForm.d.ts +12 -0
  92. package/dist/esm/hooks/useSigninForm.js +39 -0
  93. package/package.json +2 -2
@@ -1,23 +1,42 @@
1
- import { Dispatch } from 'react';
2
- import { CardType, CreditCard, CreditCardData, CreditCards, Profile, PurchaseGiftCardData, RequestStatus } from '@open-tender/types';
3
- declare const useGiftCardsForm: (purchase: (data: any) => void, reset: () => void, loading: RequestStatus, error: any, success: boolean, setAlert: Dispatch<any>, customer: Partial<Profile> | undefined, creditCards: CreditCards | undefined, _recaptchaKey: string | null | undefined, initState: PurchaseGiftCardData, cardData: CreditCardData, cardType: CardType) => {
4
- creditCardOptions: any[];
1
+ /// <reference types="react" />
2
+ import { CardType, CreditCard, CreditCardData, CreditCards, Customer, PurchaseGiftCardData, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ interface CreditCardOption {
5
+ name: string;
6
+ value?: number;
7
+ }
8
+ interface PurchaseData {
9
+ credit_card: Partial<CreditCardData>;
10
+ email: string;
11
+ gift_cards: Array<Partial<PurchaseGiftCardData>>;
12
+ name: string;
13
+ url: string;
14
+ token?: string;
15
+ }
16
+ declare const useGiftCardsForm: <InputRef extends Field, SubmitRef extends Field>(purchase: (data: PurchaseData) => void, reset: () => void, loading: RequestStatus, error: any, success: boolean, setAlert: (data: {
17
+ type: string;
18
+ args?: any;
19
+ }) => void, customer: Partial<Customer> | undefined, creditCards: CreditCards | undefined, recaptchaKey: string | null | undefined, initState: PurchaseGiftCardData, cardData: CreditCardData, cardType: CardType, windowRef?: Window) => {
20
+ inputRef: import("react").MutableRefObject<InputRef | null>;
21
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
22
+ recaptchaRef: import("react").MutableRefObject<any>;
23
+ creditCardOptions: CreditCardOption[];
5
24
  handleName: (value: string) => void;
6
25
  handleEmail: (value: string) => void;
7
- handleChange: (id: string, value: unknown) => void;
26
+ handleChange: (name: string, value: string) => void;
8
27
  handleQuantity: (index: number, quantity: number) => void;
9
28
  handleAddAnother: () => void;
10
- handleCreditCard: (value: string) => void;
29
+ handleCreditCard: (value: number) => void;
11
30
  handleSubmit: () => void;
12
31
  handleReset: () => void;
13
- name: string | undefined;
14
- email: string | undefined;
32
+ name: string | null;
33
+ email: string | null;
15
34
  cards: PurchaseGiftCardData[];
16
35
  isNewCard: boolean;
17
36
  newCardErrors: {};
18
- creditCard: Partial<CreditCard>;
19
- setCreditCard: Dispatch<import("react").SetStateAction<Partial<CreditCard>>>;
20
- errors: any;
37
+ creditCard: CreditCard & Partial<CreditCardData>;
38
+ setCreditCard: import("react").Dispatch<import("react").SetStateAction<CreditCard & Partial<CreditCardData>>>;
39
+ errors: {};
21
40
  submitting: boolean;
22
41
  };
23
42
  export default useGiftCardsForm;
@@ -1,17 +1,19 @@
1
- import { useMemo, useState, useEffect } from 'react';
2
- import { validateCreditCard } from '../utils';
3
- const useGiftCardsForm = (purchase, reset, loading, error, success, setAlert, customer = {}, creditCards = [],
4
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
5
- _recaptchaKey = null, initState, cardData, cardType) => {
6
- const [name, setName] = useState(undefined);
7
- const [email, setEmail] = useState(undefined);
1
+ import { useMemo, useRef, useState, useEffect } from 'react';
2
+ import { validateCreditCard } from '@open-tender/utils';
3
+ const useGiftCardsForm = (purchase, reset, loading, error, success, setAlert, customer = {}, creditCards = [], recaptchaKey = null, initState, cardData, cardType, windowRef) => {
4
+ var _a, _b;
5
+ const submitRef = useRef(null);
6
+ const inputRef = useRef(null);
7
+ const recaptchaRef = useRef(null);
8
+ const [name, setName] = useState(null);
9
+ const [email, setEmail] = useState(null);
8
10
  const [cards, setCards] = useState([initState]);
9
11
  const [isNewCard, setIsNewCard] = useState(true);
10
12
  const [creditCard, setCreditCard] = useState({});
11
13
  const [creditCardOptions, setCreditCardOptions] = useState([]);
12
14
  const [errors, setErrors] = useState({});
13
15
  const [submitting, setSubmitting] = useState(false);
14
- const url = null;
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 : "";
15
17
  const newCardErrors = useMemo(() => errors
16
18
  ? Object.entries(errors)
17
19
  .filter(([key]) => key !== 'form')
@@ -22,14 +24,20 @@ _recaptchaKey = null, initState, cardData, cardType) => {
22
24
  setSubmitting(false);
23
25
  setAlert({ type: 'close' });
24
26
  if (error) {
27
+ if (recaptchaRef.current)
28
+ recaptchaRef.current.reset();
25
29
  setErrors(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);
26
34
  }
27
35
  }
28
- }, [loading, error, setAlert, success]);
36
+ }, [windowRef, loading, error, setAlert, success]);
29
37
  useEffect(() => {
30
- if (creditCards.length && loading !== 'pending') {
31
- const options = creditCards.map(i => ({
32
- label: `${i.card_type_name} ending in ${i.last4}`,
38
+ if (creditCards.length) {
39
+ const options = creditCards.map((i) => ({
40
+ name: `${i.card_type_name} ending in ${i.last4}`,
33
41
  value: i.customer_card_id,
34
42
  }));
35
43
  setCreditCardOptions(options);
@@ -39,17 +47,18 @@ _recaptchaKey = null, initState, cardData, cardType) => {
39
47
  setCreditCard(defaultCard);
40
48
  setIsNewCard(false);
41
49
  }
42
- }, [creditCards, loading]);
50
+ }, [creditCards]);
43
51
  useEffect(() => {
52
+ var _a;
44
53
  if (customer) {
45
- setEmail(customer.email);
54
+ setEmail((_a = customer.email) !== null && _a !== void 0 ? _a : null);
46
55
  setName(`${customer.first_name} ${customer.last_name}`);
47
56
  }
48
57
  }, [customer]);
49
58
  const makeGiftCards = (giftCards) => {
50
59
  return giftCards.reduce((arr, card) => {
51
- const { amount, quantity, email: mail } = card;
52
- const giftCard = !mail || !mail.length ? { amount, quantity } : card;
60
+ const { amount, quantity, email } = card;
61
+ const giftCard = !email || !email.length ? { amount, quantity } : card;
53
62
  return [...arr, giftCard];
54
63
  }, []);
55
64
  };
@@ -59,8 +68,8 @@ _recaptchaKey = null, initState, cardData, cardType) => {
59
68
  const handleEmail = (value) => {
60
69
  setEmail(value);
61
70
  };
62
- const handleChange = (id, value) => {
63
- const [field, index] = id.split('-');
71
+ const handleChange = (name, value) => {
72
+ const [field, index] = name.split('-');
64
73
  const newCards = cards.map((card, idx) => {
65
74
  return idx === parseInt(index) ? Object.assign(Object.assign({}, card), { [field]: value }) : card;
66
75
  });
@@ -69,38 +78,70 @@ _recaptchaKey = null, initState, cardData, cardType) => {
69
78
  const handleQuantity = (index, quantity) => {
70
79
  const newCards = quantity > 0
71
80
  ? cards.map((card, idx) => {
72
- return idx === index ? Object.assign(Object.assign({}, card), { quantity: quantity }) : card;
81
+ return idx === parseInt(`${index}`)
82
+ ? Object.assign(Object.assign({}, card), { quantity: quantity }) : card;
73
83
  })
74
- : cards.filter((_i, idx) => idx !== index);
84
+ : cards.filter((_, idx) => idx !== index);
75
85
  setCards(newCards);
76
86
  };
77
87
  const handleAddAnother = () => {
78
88
  setCards([...cards, initState]);
79
89
  };
80
90
  const handleCreditCard = (value) => {
81
- const customerCardId = parseInt(value);
91
+ const customerCardId = parseInt(`${value}`);
82
92
  setCreditCard({ customer_card_id: customerCardId });
83
93
  };
84
94
  const purchaseWithCaptcha = (credit_card) => {
95
+ var _a;
85
96
  const alert = {
86
97
  type: 'working',
87
98
  args: { text: 'Submitting your purchase...' },
88
99
  };
89
- setSubmitting(true);
90
- setAlert(alert);
91
- const gift_cards = makeGiftCards(cards);
92
- purchase({ credit_card, name, email, url, gift_cards });
100
+ if (recaptchaKey) {
101
+ try {
102
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
103
+ if (!token) {
104
+ setSubmitting(false);
105
+ setErrors({
106
+ form: 'Please complete the recaptcha before submitting',
107
+ });
108
+ windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
109
+ }
110
+ else {
111
+ setSubmitting(true);
112
+ setAlert(alert);
113
+ const gift_cards = makeGiftCards(cards);
114
+ purchase({ token, credit_card, name: name !== null && name !== void 0 ? name : "", email: email !== null && email !== void 0 ? email : "", url, gift_cards });
115
+ }
116
+ }
117
+ catch (err) {
118
+ setSubmitting(false);
119
+ setErrors({ form: 'Please complete the recaptcha before submitting' });
120
+ windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
121
+ }
122
+ }
123
+ else {
124
+ setSubmitting(true);
125
+ setAlert(alert);
126
+ const gift_cards = makeGiftCards(cards);
127
+ purchase({ credit_card, name: name !== null && name !== void 0 ? name : "", email: email !== null && email !== void 0 ? email : "", url, gift_cards });
128
+ }
93
129
  };
94
130
  const handleSubmit = () => {
131
+ var _a, _b, _c;
95
132
  if (!name || !email) {
96
133
  setErrors({ form: 'Both name and email are required' });
134
+ if (inputRef.current)
135
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && inputRef.current.focus();
136
+ windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
97
137
  }
98
138
  else {
99
139
  if (isNewCard) {
100
- const { card, errors: errs } = validateCreditCard(cardData, cardType);
101
- if (errs) {
102
- setErrors(Object.assign(Object.assign({}, errs), { form: 'There are one or more credit card errors below' }));
140
+ const { card, errors } = validateCreditCard(cardData, cardType);
141
+ if (errors) {
142
+ setErrors(Object.assign(Object.assign({}, errors), { form: 'There are one or more credit card errors below' }));
103
143
  setSubmitting(false);
144
+ windowRef === null || windowRef === void 0 ? void 0 : windowRef.scrollTo(0, 0);
104
145
  }
105
146
  else {
106
147
  purchaseWithCaptcha(card);
@@ -109,18 +150,18 @@ _recaptchaKey = null, initState, cardData, cardType) => {
109
150
  else {
110
151
  purchaseWithCaptcha(creditCard);
111
152
  }
153
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
112
154
  }
113
155
  };
114
156
  const handleReset = () => {
115
- if (!customer) {
116
- setName(undefined);
117
- setEmail(undefined);
118
- }
119
157
  setCards([initState]);
120
158
  setErrors({});
121
159
  reset();
122
160
  };
123
161
  return {
162
+ inputRef,
163
+ submitRef,
164
+ recaptchaRef,
124
165
  creditCardOptions,
125
166
  handleName,
126
167
  handleEmail,
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ import { GuestData, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useGuestForm: <TnputRef extends Field, SubmitRef extends Field>(email: string, loading: RequestStatus, errors: any, checkGuest: (email?: string) => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ inputRef: import("react").MutableRefObject<TnputRef | null>;
7
+ data: Partial<GuestData>;
8
+ errMsg: any;
9
+ submitting: boolean;
10
+ handleChange: (name: string, value: string) => void;
11
+ handleSubmit: () => void;
12
+ };
13
+ export default useGuestForm;
@@ -0,0 +1,39 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+ const useGuestForm = (email, loading, errors, checkGuest) => {
3
+ const submitRef = useRef(null);
4
+ const inputRef = useRef(null);
5
+ const [data, setData] = useState({});
6
+ const submitting = loading === 'pending';
7
+ const errMsg = errors ? errors.email || errors.form : null;
8
+ useEffect(() => {
9
+ var _a, _b;
10
+ if (loading === 'idle') {
11
+ if (errMsg)
12
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
13
+ }
14
+ }, [loading, errMsg]);
15
+ useEffect(() => {
16
+ if (email && data.email === undefined) {
17
+ setData({ email });
18
+ }
19
+ }, [email, data.email]);
20
+ const handleChange = (name, value) => {
21
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
22
+ };
23
+ const handleSubmit = () => {
24
+ var _a, _b;
25
+ const { email } = data;
26
+ checkGuest(email);
27
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
28
+ };
29
+ return {
30
+ submitRef,
31
+ inputRef,
32
+ data,
33
+ errMsg,
34
+ submitting,
35
+ handleChange,
36
+ handleSubmit,
37
+ };
38
+ };
39
+ export default useGuestForm;
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ import { ConfigFulfillment, OrderFulfillment, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useOrderFulfillmentForm: <SubmitRef extends Field>(orderId: number, fulfillment: Partial<OrderFulfillment>, loading: RequestStatus, error: any, update: (orderId: number, data: Partial<OrderFulfillment>) => void, settings?: ConfigFulfillment, showAllFields?: boolean) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ fields: import("@open-tender/types").ConfigFulfillmentField[] | undefined;
7
+ data: Partial<OrderFulfillment>;
8
+ errors: any;
9
+ submitting: boolean;
10
+ handleChange: (name: string, type: string, value: string | null, checked: boolean | null) => void;
11
+ handleSubmit: () => void;
12
+ };
13
+ export default useOrderFulfillmentForm;
@@ -0,0 +1,38 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+ const useOrderFulfillmentForm = (orderId, fulfillment, loading, error, update, settings, showAllFields = false) => {
3
+ const submitRef = useRef(null);
4
+ const [data, setData] = useState({});
5
+ const [submitting, setSubmitting] = useState(false);
6
+ const errors = error || {};
7
+ const empty = Object.values(fulfillment).every((i) => !i);
8
+ const fields = !empty && !showAllFields
9
+ ? settings === null || settings === void 0 ? void 0 : settings.fields.filter((i) => i.name.startsWith('arrival'))
10
+ : settings === null || settings === void 0 ? void 0 : settings.fields;
11
+ useEffect(() => {
12
+ if (loading === 'idle')
13
+ setSubmitting(false);
14
+ }, [loading]);
15
+ useEffect(() => {
16
+ setData(fulfillment);
17
+ }, [fulfillment]);
18
+ const handleChange = (name, type, value, checked) => {
19
+ const inputValue = type === 'checkbox' ? checked : value;
20
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
21
+ };
22
+ const handleSubmit = () => {
23
+ var _a, _b;
24
+ setSubmitting(true);
25
+ update(orderId, Object.assign(Object.assign({}, data), { has_arrived: true }));
26
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
27
+ };
28
+ return {
29
+ submitRef,
30
+ fields,
31
+ data,
32
+ errors,
33
+ submitting,
34
+ handleChange,
35
+ handleSubmit,
36
+ };
37
+ };
38
+ export default useOrderFulfillmentForm;
@@ -0,0 +1,13 @@
1
+ /// <reference types="react" />
2
+ import { OrderRating } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useOrderRatingForm: <SubmitRef extends Field>(orderId: number, orderRating: OrderRating, updateRating: (orderId: number, rating: Partial<OrderRating>) => void, callback?: () => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ stars: number[];
7
+ rating: number;
8
+ handleRating: (star: number) => void;
9
+ comments: string;
10
+ handleComments: (value: string) => void;
11
+ handleSubmit: () => void;
12
+ };
13
+ export default useOrderRatingForm;
@@ -0,0 +1,31 @@
1
+ import { useRef, useState } from 'react';
2
+ const useOrderRatingForm = (orderId, orderRating, updateRating, callback) => {
3
+ const submitRef = useRef(null);
4
+ const [rating, setRating] = useState(orderRating.rating || 0);
5
+ const [comments, setComments] = useState(orderRating.comments || '');
6
+ const stars = [1, 2, 3, 4, 5];
7
+ const handleRating = (star) => {
8
+ setRating(star);
9
+ };
10
+ const handleComments = (value) => {
11
+ setComments(value);
12
+ };
13
+ const handleSubmit = () => {
14
+ var _a, _b;
15
+ const data = { rating, comments };
16
+ updateRating(orderId, data);
17
+ if (callback)
18
+ callback();
19
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
20
+ };
21
+ return {
22
+ submitRef,
23
+ stars,
24
+ rating,
25
+ handleRating,
26
+ comments,
27
+ handleComments,
28
+ handleSubmit,
29
+ };
30
+ };
31
+ export default useOrderRatingForm;
@@ -1,29 +1,24 @@
1
- import { Profile, RequestStatus } from '@open-tender/types';
2
- declare const useProfileForm: (profile: Partial<Profile>, loading: RequestStatus, error: any, update: (data: Partial<Profile>) => void) => {
3
- data: any;
1
+ /// <reference types="react" />
2
+ import { ConfigBrand, Profile, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ interface ProfileFormField {
5
+ label: string;
6
+ name: keyof Profile;
7
+ type: string;
8
+ required?: boolean;
9
+ autoComplete?: string;
10
+ }
11
+ declare const useProfileForm: <SubmitRef extends Field>(profile: Partial<Profile>, loading: RequestStatus, error: any, update: (data: Partial<Profile>) => void, optIns?: Partial<ConfigBrand>) => {
12
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
13
+ accepts_marketing: import("@open-tender/types").ConfigAcceptMarketing | undefined;
14
+ order_notifications: import("@open-tender/types").ConfigOrderNotifications | undefined;
15
+ data: Partial<Profile>;
4
16
  errors: any;
5
17
  submitting: boolean;
6
- fields: ({
7
- label: string;
8
- name: string;
9
- type: string;
10
- required: boolean;
11
- autoComplete?: undefined;
12
- } | {
13
- label: string;
14
- name: string;
15
- type: string;
16
- required: boolean;
17
- autoComplete: string;
18
- } | {
19
- label: string;
20
- name: string;
21
- type: string;
22
- required?: undefined;
23
- autoComplete?: undefined;
24
- })[];
18
+ fields: Partial<ProfileFormField>[];
25
19
  errMsg: string | null;
26
- handleChange: (id: string, type: string | null, value: string, checked: boolean | null) => void;
20
+ handleChange: (name: string, type: string, value?: string, checked?: boolean) => void;
21
+ handleRadio: (name: string, value: string) => void;
27
22
  handleSubmit: () => void;
28
23
  };
29
24
  export default useProfileForm;
@@ -1,57 +1,51 @@
1
- import { useState, useEffect } from 'react';
1
+ import { useRef, useState, useEffect } from 'react';
2
2
  import { makeBirthDate, makePhone, slashesToDashes } from '../utils';
3
- const useProfileForm = (profile, loading, error, update) => {
3
+ const useProfileForm = (profile, loading, error, update, optIns = {}) => {
4
+ const submitRef = useRef(null);
5
+ const { accepts_marketing, order_notifications } = optIns;
4
6
  const [data, setData] = useState({});
5
7
  const [submitting, setSubmitting] = useState(false);
6
8
  const errMsg = error ? 'There are one or more errors below.' : null;
7
9
  const errors = error || {};
8
10
  const fields = [
9
- {
10
- label: 'First Name',
11
- name: 'first_name',
12
- type: 'default',
13
- required: true,
14
- },
15
- { label: 'Last Name', name: 'last_name', type: 'default', required: true },
11
+ { label: 'First Name', name: 'first_name', type: 'text', required: true },
12
+ { label: 'Last Name', name: 'last_name', type: 'text', required: true },
16
13
  {
17
14
  label: 'Email',
18
15
  name: 'email',
19
- type: 'email-address',
16
+ type: 'email',
20
17
  required: true,
21
18
  autoComplete: 'email',
22
19
  },
23
- { label: 'Phone', name: 'phone', type: 'number-pad', required: true },
24
- { label: 'Company', name: 'company', type: 'default' },
25
- {
26
- label: 'Birth Date (mm/dd/yyyy)',
27
- name: 'birth_date',
28
- type: 'number-pad',
29
- },
20
+ { label: 'Phone', name: 'phone', type: 'tel', required: true },
21
+ { label: 'Company', name: 'company', type: 'text' },
30
22
  ];
31
23
  useEffect(() => {
32
24
  if (loading === 'idle') {
33
25
  setSubmitting(false);
26
+ if (error) {
27
+ //handle form inputs[0] focus
28
+ }
34
29
  }
35
- }, [loading]);
36
- useEffect(() => {
37
- if (errors.birth_date) {
38
- errors.birth_date = 'Invalid date. Please enter in mm/dd/yyyy format.';
39
- }
40
- }, [errors]);
30
+ }, [loading, error]);
41
31
  useEffect(() => {
42
32
  setData(profile);
43
33
  }, [profile]);
44
- const handleChange = (id, type, value, checked) => {
34
+ const handleChange = (name, type, value, checked) => {
45
35
  const inputValue = type === 'checkbox'
46
36
  ? checked
47
- : id === 'phone'
48
- ? makePhone(value)
49
- : id === 'birth_date'
50
- ? makeBirthDate(value)
37
+ : name === 'phone'
38
+ ? makePhone(value !== null && value !== void 0 ? value : '')
39
+ : name === 'birth_date'
40
+ ? makeBirthDate(value !== null && value !== void 0 ? value : '')
51
41
  : value;
52
- setData(Object.assign(Object.assign({}, data), { [id]: inputValue }));
42
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
43
+ };
44
+ const handleRadio = (name, value) => {
45
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
53
46
  };
54
47
  const handleSubmit = () => {
48
+ var _a, _b;
55
49
  setSubmitting(true);
56
50
  const updatedData = fields.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: data[i.name] })), {});
57
51
  updatedData.birth_date = data.birth_date
@@ -59,14 +53,19 @@ const useProfileForm = (profile, loading, error, update) => {
59
53
  : null;
60
54
  updatedData.gender = data.gender || null;
61
55
  update(updatedData);
56
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
62
57
  };
63
58
  return {
59
+ submitRef,
60
+ accepts_marketing,
61
+ order_notifications,
64
62
  data,
65
63
  errors,
66
64
  submitting,
67
65
  fields,
68
66
  errMsg,
69
67
  handleChange,
68
+ handleRadio,
70
69
  handleSubmit,
71
70
  };
72
71
  };
@@ -0,0 +1,20 @@
1
+ /// <reference types="react" />
2
+ import { RequestStatus, ResetPasswordData } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ interface ResetPasswordFormField {
5
+ label: string;
6
+ name: string;
7
+ type: string;
8
+ required?: boolean;
9
+ }
10
+ declare const useResetPasswordForm: <InputRef extends Field, SubmitRef extends Field>(loading: RequestStatus, error: any, reset: (newPassword: string, callback: () => void) => void, resetForm: () => void, resetToken: () => void) => {
11
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
12
+ inputRef: import("react").MutableRefObject<InputRef | null>;
13
+ fields: ResetPasswordFormField[];
14
+ data: Partial<ResetPasswordData>;
15
+ errors: {};
16
+ submitting: boolean;
17
+ handleChange: (name: string, value: string) => void;
18
+ handleSubmit: () => void;
19
+ };
20
+ export default useResetPasswordForm;
@@ -0,0 +1,64 @@
1
+ import { useState, useEffect, useRef } from 'react';
2
+ const useResetPasswordForm = (loading, error, reset, resetForm, resetToken) => {
3
+ const submitRef = useRef(null);
4
+ const inputRef = useRef(null);
5
+ const [data, setData] = useState({});
6
+ const [errors, setErrors] = useState({});
7
+ const [submitting, setSubmitting] = useState(false);
8
+ const fields = [
9
+ { label: 'New Password', name: 'new_password', type: 'password' },
10
+ { label: 'Confirm Password', name: 'confirm', type: 'password' },
11
+ ];
12
+ useEffect(() => {
13
+ setData({});
14
+ setErrors({});
15
+ resetForm();
16
+ return () => {
17
+ setData({});
18
+ setErrors({});
19
+ resetForm();
20
+ };
21
+ }, [resetForm]);
22
+ useEffect(() => {
23
+ var _a, _b;
24
+ if (loading === 'idle') {
25
+ setSubmitting(false);
26
+ if (error) {
27
+ setErrors(error);
28
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
29
+ }
30
+ }
31
+ }, [loading, error]);
32
+ const handleChange = (name, value) => {
33
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
34
+ };
35
+ const handleSubmit = () => {
36
+ var _a, _b, _c, _d, _e, _f;
37
+ const { new_password, confirm } = data;
38
+ if (!new_password || new_password.length < 8) {
39
+ setErrors({ new_password: 'Must be at least 8 characters' });
40
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
41
+ }
42
+ else if (new_password !== confirm) {
43
+ setErrors({ confirm: 'Passwords do not match' });
44
+ ((_c = inputRef.current) === null || _c === void 0 ? void 0 : _c.focus) && ((_d = inputRef.current) === null || _d === void 0 ? void 0 : _d.focus());
45
+ }
46
+ else {
47
+ setErrors({});
48
+ setSubmitting(true);
49
+ reset(new_password, resetToken);
50
+ }
51
+ ((_e = submitRef.current) === null || _e === void 0 ? void 0 : _e.blur) && ((_f = submitRef.current) === null || _f === void 0 ? void 0 : _f.blur());
52
+ };
53
+ return {
54
+ submitRef,
55
+ inputRef,
56
+ fields,
57
+ data,
58
+ errors,
59
+ submitting,
60
+ handleChange,
61
+ handleSubmit,
62
+ };
63
+ };
64
+ export default useResetPasswordForm;
@@ -0,0 +1,12 @@
1
+ /// <reference types="react" />
2
+ import { RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useSendResetForm: <InputRef extends Field, SubmitRef extends Field>(loading: RequestStatus, error: any, sendReset: (email: string, linkUrl: string) => Promise<void>, callback?: () => void, windowref?: any, linkUrlVal?: string) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ inputRef: import("react").MutableRefObject<InputRef | null>;
7
+ data: any;
8
+ errMsg: any;
9
+ handleChange: (name: string, value: string) => void;
10
+ handleSubmit: () => void;
11
+ };
12
+ export default useSendResetForm;