@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
@@ -2,58 +2,52 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const react_1 = require("react");
4
4
  const utils_1 = require("../utils");
5
- const useProfileForm = (profile, loading, error, update) => {
5
+ const useProfileForm = (profile, loading, error, update, optIns = {}) => {
6
+ const submitRef = (0, react_1.useRef)(null);
7
+ const { accepts_marketing, order_notifications } = optIns;
6
8
  const [data, setData] = (0, react_1.useState)({});
7
9
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
8
10
  const errMsg = error ? 'There are one or more errors below.' : null;
9
11
  const errors = error || {};
10
12
  const fields = [
11
- {
12
- label: 'First Name',
13
- name: 'first_name',
14
- type: 'default',
15
- required: true,
16
- },
17
- { label: 'Last Name', name: 'last_name', type: 'default', required: true },
13
+ { label: 'First Name', name: 'first_name', type: 'text', required: true },
14
+ { label: 'Last Name', name: 'last_name', type: 'text', required: true },
18
15
  {
19
16
  label: 'Email',
20
17
  name: 'email',
21
- type: 'email-address',
18
+ type: 'email',
22
19
  required: true,
23
20
  autoComplete: 'email',
24
21
  },
25
- { label: 'Phone', name: 'phone', type: 'number-pad', required: true },
26
- { label: 'Company', name: 'company', type: 'default' },
27
- {
28
- label: 'Birth Date (mm/dd/yyyy)',
29
- name: 'birth_date',
30
- type: 'number-pad',
31
- },
22
+ { label: 'Phone', name: 'phone', type: 'tel', required: true },
23
+ { label: 'Company', name: 'company', type: 'text' },
32
24
  ];
33
25
  (0, react_1.useEffect)(() => {
34
26
  if (loading === 'idle') {
35
27
  setSubmitting(false);
28
+ if (error) {
29
+ //handle form inputs[0] focus
30
+ }
36
31
  }
37
- }, [loading]);
38
- (0, react_1.useEffect)(() => {
39
- if (errors.birth_date) {
40
- errors.birth_date = 'Invalid date. Please enter in mm/dd/yyyy format.';
41
- }
42
- }, [errors]);
32
+ }, [loading, error]);
43
33
  (0, react_1.useEffect)(() => {
44
34
  setData(profile);
45
35
  }, [profile]);
46
- const handleChange = (id, type, value, checked) => {
36
+ const handleChange = (name, type, value, checked) => {
47
37
  const inputValue = type === 'checkbox'
48
38
  ? checked
49
- : id === 'phone'
50
- ? (0, utils_1.makePhone)(value)
51
- : id === 'birth_date'
52
- ? (0, utils_1.makeBirthDate)(value)
39
+ : name === 'phone'
40
+ ? (0, utils_1.makePhone)(value !== null && value !== void 0 ? value : '')
41
+ : name === 'birth_date'
42
+ ? (0, utils_1.makeBirthDate)(value !== null && value !== void 0 ? value : '')
53
43
  : value;
54
- setData(Object.assign(Object.assign({}, data), { [id]: inputValue }));
44
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
45
+ };
46
+ const handleRadio = (name, value) => {
47
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
55
48
  };
56
49
  const handleSubmit = () => {
50
+ var _a, _b;
57
51
  setSubmitting(true);
58
52
  const updatedData = fields.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: data[i.name] })), {});
59
53
  updatedData.birth_date = data.birth_date
@@ -61,14 +55,19 @@ const useProfileForm = (profile, loading, error, update) => {
61
55
  : null;
62
56
  updatedData.gender = data.gender || null;
63
57
  update(updatedData);
58
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
64
59
  };
65
60
  return {
61
+ submitRef,
62
+ accepts_marketing,
63
+ order_notifications,
66
64
  data,
67
65
  errors,
68
66
  submitting,
69
67
  fields,
70
68
  errMsg,
71
69
  handleChange,
70
+ handleRadio,
72
71
  handleSubmit,
73
72
  };
74
73
  };
@@ -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,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useResetPasswordForm = (loading, error, reset, resetForm, resetToken) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const inputRef = (0, react_1.useRef)(null);
7
+ const [data, setData] = (0, react_1.useState)({});
8
+ const [errors, setErrors] = (0, react_1.useState)({});
9
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
10
+ const fields = [
11
+ { label: 'New Password', name: 'new_password', type: 'password' },
12
+ { label: 'Confirm Password', name: 'confirm', type: 'password' },
13
+ ];
14
+ (0, react_1.useEffect)(() => {
15
+ setData({});
16
+ setErrors({});
17
+ resetForm();
18
+ return () => {
19
+ setData({});
20
+ setErrors({});
21
+ resetForm();
22
+ };
23
+ }, [resetForm]);
24
+ (0, react_1.useEffect)(() => {
25
+ var _a, _b;
26
+ if (loading === 'idle') {
27
+ setSubmitting(false);
28
+ if (error) {
29
+ setErrors(error);
30
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
31
+ }
32
+ }
33
+ }, [loading, error]);
34
+ const handleChange = (name, value) => {
35
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
36
+ };
37
+ const handleSubmit = () => {
38
+ var _a, _b, _c, _d, _e, _f;
39
+ const { new_password, confirm } = data;
40
+ if (!new_password || new_password.length < 8) {
41
+ setErrors({ new_password: 'Must be at least 8 characters' });
42
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
43
+ }
44
+ else if (new_password !== confirm) {
45
+ setErrors({ confirm: 'Passwords do not match' });
46
+ ((_c = inputRef.current) === null || _c === void 0 ? void 0 : _c.focus) && ((_d = inputRef.current) === null || _d === void 0 ? void 0 : _d.focus());
47
+ }
48
+ else {
49
+ setErrors({});
50
+ setSubmitting(true);
51
+ reset(new_password, resetToken);
52
+ }
53
+ ((_e = submitRef.current) === null || _e === void 0 ? void 0 : _e.blur) && ((_f = submitRef.current) === null || _f === void 0 ? void 0 : _f.blur());
54
+ };
55
+ return {
56
+ submitRef,
57
+ inputRef,
58
+ fields,
59
+ data,
60
+ errors,
61
+ submitting,
62
+ handleChange,
63
+ handleSubmit,
64
+ };
65
+ };
66
+ exports.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;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useSendResetForm = (loading, error, sendReset, callback, windowref, linkUrlVal) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const inputRef = (0, react_1.useRef)(null);
7
+ const [data, setData] = (0, react_1.useState)({});
8
+ const errMsg = error && error.email ? error.email : null;
9
+ (0, react_1.useEffect)(() => {
10
+ var _a, _b;
11
+ if (loading === 'idle' && error) {
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, error]);
15
+ const handleChange = (name, value) => {
16
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
17
+ };
18
+ const handleSubmit = () => {
19
+ var _a, _b;
20
+ const linkUrl = windowref ? `${windowref.location.origin}/reset-password` : `${linkUrlVal}`;
21
+ sendReset(data.email, linkUrl).then(() => {
22
+ if (callback)
23
+ callback();
24
+ });
25
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
26
+ };
27
+ return {
28
+ submitRef,
29
+ inputRef,
30
+ data,
31
+ errMsg,
32
+ handleChange,
33
+ handleSubmit,
34
+ };
35
+ };
36
+ exports.default = useSendResetForm;
@@ -0,0 +1,21 @@
1
+ /// <reference types="react" />
2
+ import { CheckoutConfig, Profile } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ interface SignUpFormField {
5
+ label?: string;
6
+ name: string;
7
+ type?: string;
8
+ required?: boolean;
9
+ autoComplete?: string;
10
+ }
11
+ declare const useSignUpForm: <SubmitRef extends Field>(loading: RequestInfo, error: any, signUp: (data: any, callback?: () => void) => void, callback?: () => void, checkConfig?: Partial<CheckoutConfig>, hasThanx?: boolean) => {
12
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
13
+ data: Partial<Profile>;
14
+ errors: any;
15
+ submitting: boolean;
16
+ formfields: SignUpFormField[];
17
+ errMsg: string | null;
18
+ handleChange: (name: string, type: string, value: string | null, checked: boolean | null) => void;
19
+ handleSubmit: () => void;
20
+ };
21
+ export default useSignUpForm;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const utils_1 = require("@open-tender/utils");
5
+ const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}, hasThanx = false) => {
6
+ const { displayed, required } = checkConfig || {};
7
+ const companyDisplayed = displayed && displayed.customer.includes('company');
8
+ const companyRequired = required && required.customer.includes('company');
9
+ const submitRef = (0, react_1.useRef)(null);
10
+ const [data, setData] = (0, react_1.useState)({});
11
+ const [errors, setErrors] = (0, react_1.useState)({});
12
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
13
+ const errMsg = error ? 'There are one or more errors below.' : null;
14
+ let fields = [
15
+ {
16
+ label: 'First Name',
17
+ name: 'first_name',
18
+ type: 'text',
19
+ required: true,
20
+ },
21
+ {
22
+ label: 'Last Name',
23
+ name: 'last_name',
24
+ type: 'text',
25
+ required: true,
26
+ },
27
+ {
28
+ label: 'Email',
29
+ name: 'email',
30
+ type: 'email',
31
+ required: true,
32
+ autoComplete: 'email',
33
+ },
34
+ {
35
+ label: 'Password',
36
+ name: 'password',
37
+ type: 'password',
38
+ required: true,
39
+ autoComplete: 'new-password',
40
+ },
41
+ { label: 'Phone', name: 'phone', type: 'tel', required: true },
42
+ ];
43
+ if (companyDisplayed || companyRequired) {
44
+ const company = {
45
+ label: 'Company',
46
+ name: 'company',
47
+ type: 'text',
48
+ required: companyRequired,
49
+ };
50
+ fields = [...fields, company];
51
+ }
52
+ const formfields = hasThanx
53
+ ? fields.filter((i) => i.name !== 'password')
54
+ : fields;
55
+ (0, react_1.useEffect)(() => {
56
+ return () => {
57
+ setData({});
58
+ setErrors({});
59
+ };
60
+ }, []);
61
+ (0, react_1.useEffect)(() => {
62
+ if (loading === 'idle') {
63
+ setSubmitting(false);
64
+ if (error) {
65
+ setErrors(error);
66
+ }
67
+ }
68
+ }, [loading, error]);
69
+ const handleChange = (name, type, value, checked) => {
70
+ const inputValue = type === 'checkbox' ? checked : name === 'phone' ? (0, utils_1.makePhone)(value !== null && value !== void 0 ? value : "") : value;
71
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
72
+ };
73
+ const handleSubmit = () => {
74
+ var _a, _b;
75
+ setErrors({});
76
+ setSubmitting(true);
77
+ signUp(data, callback);
78
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
79
+ };
80
+ return {
81
+ submitRef,
82
+ data,
83
+ errors,
84
+ submitting,
85
+ formfields,
86
+ errMsg,
87
+ handleChange,
88
+ handleSubmit,
89
+ };
90
+ };
91
+ exports.default = useSignUpForm;
@@ -0,0 +1,23 @@
1
+ /// <reference types="react" />
2
+ import { GuestData, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useSignUpGuestForm: <InputRef extends Field, SubmitRef extends Field>(email: string, guest: Partial<GuestData>, loading: RequestStatus, error: any, signUp: (data: any) => void, submitGuest: (data: Partial<GuestData>) => void, hasThanx?: boolean) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ inputRef: import("react").MutableRefObject<InputRef | null>;
7
+ data: {
8
+ first_name?: string | undefined;
9
+ email?: string | undefined;
10
+ last_name?: string | undefined;
11
+ phone?: string | undefined;
12
+ password?: string | undefined;
13
+ };
14
+ disabled: boolean;
15
+ guestDisabled: boolean;
16
+ errors: {};
17
+ errMsg: string | null;
18
+ submitting: boolean;
19
+ handleChange: (id: string, value: string) => void;
20
+ handleSubmit: () => void;
21
+ handleGuest: () => void;
22
+ };
23
+ export default useSignUpGuestForm;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const utils_1 = require("@open-tender/utils");
5
+ const initialData = {
6
+ first_name: '',
7
+ last_name: '',
8
+ phone: '',
9
+ password: '',
10
+ };
11
+ const useSignUpGuestForm = (email, guest, loading, error, signUp, submitGuest, hasThanx) => {
12
+ const submitRef = (0, react_1.useRef)(null);
13
+ const inputRef = (0, react_1.useRef)(null);
14
+ const initData = Object.assign(Object.assign({}, initialData), guest);
15
+ const [data, setData] = (0, react_1.useState)(initData);
16
+ const [errors, setErrors] = (0, react_1.useState)({});
17
+ const [submitting, setSubmitting] = (0, react_1.useState)(false);
18
+ const { guestData, guestIncomplete } = (0, utils_1.checkGuestData)(data, email);
19
+ const isIncomplete = hasThanx ? guestIncomplete : (0, utils_1.checkEmpty)(data);
20
+ const isLoading = loading === 'pending';
21
+ const disabled = isIncomplete || isLoading;
22
+ const guestDisabled = guestIncomplete || isLoading;
23
+ const errMsg = (error === null || error === void 0 ? void 0 : error.form) ? 'There are one or more errors below' : null;
24
+ const handleChange = (id, value) => {
25
+ const val = id === 'phone' ? (0, utils_1.makePhone)(value) : value;
26
+ setData(Object.assign(Object.assign({}, data), { [id]: val }));
27
+ };
28
+ (0, react_1.useEffect)(() => {
29
+ return () => {
30
+ setData({});
31
+ setErrors({});
32
+ };
33
+ }, []);
34
+ (0, react_1.useEffect)(() => {
35
+ var _a, _b;
36
+ if (loading === 'idle') {
37
+ setSubmitting(false);
38
+ if (error) {
39
+ setErrors(error);
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
+ }
43
+ }, [loading, error]);
44
+ const handleSubmit = () => {
45
+ var _a, _b;
46
+ if (!disabled) {
47
+ setErrors({});
48
+ setSubmitting(true);
49
+ signUp(Object.assign(Object.assign({}, data), { email }));
50
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
51
+ }
52
+ };
53
+ const handleGuest = () => {
54
+ if (!guestDisabled) {
55
+ setErrors({});
56
+ setSubmitting(true);
57
+ submitGuest(Object.assign(Object.assign({}, guestData), { email }));
58
+ }
59
+ };
60
+ return {
61
+ submitRef,
62
+ inputRef,
63
+ data,
64
+ disabled,
65
+ guestDisabled,
66
+ errors,
67
+ errMsg,
68
+ submitting,
69
+ handleChange,
70
+ handleSubmit,
71
+ handleGuest,
72
+ };
73
+ };
74
+ exports.default = useSignUpGuestForm;
@@ -0,0 +1,12 @@
1
+ /// <reference types="react" />
2
+ import { LoginData, RequestStatus } from '@open-tender/types';
3
+ import { Field } from './types';
4
+ declare const useSignInForm: <InputRef extends Field, SubmitRef extends Field>(email: string, loading: RequestStatus, error: any, login: (email?: string, password?: string) => void, callback?: () => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
6
+ inputRef: import("react").MutableRefObject<InputRef | null>;
7
+ data: Partial<LoginData>;
8
+ submitting: boolean;
9
+ handleChange: (name: string, value: string) => void;
10
+ handleSubmit: () => void;
11
+ };
12
+ export default useSignInForm;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const react_1 = require("react");
4
+ const useSignInForm = (email, loading, error, login, callback) => {
5
+ const submitRef = (0, react_1.useRef)(null);
6
+ const inputRef = (0, react_1.useRef)(null);
7
+ const [hasSubmit, setHasSubmit] = (0, react_1.useState)(false);
8
+ const [data, setData] = (0, react_1.useState)({ email });
9
+ const submitting = loading === 'pending';
10
+ (0, react_1.useEffect)(() => {
11
+ var _a, _b;
12
+ if (loading === 'idle' && hasSubmit) {
13
+ if (error) {
14
+ setHasSubmit(false);
15
+ ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
16
+ }
17
+ else if (callback) {
18
+ callback();
19
+ }
20
+ }
21
+ }, [loading, error, hasSubmit, callback]);
22
+ const handleChange = (name, value) => {
23
+ setData(Object.assign(Object.assign({}, data), { [name]: value }));
24
+ };
25
+ const handleSubmit = () => {
26
+ var _a, _b;
27
+ setHasSubmit(true);
28
+ const { email, password } = data;
29
+ login(email, password);
30
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
31
+ };
32
+ return {
33
+ submitRef,
34
+ inputRef,
35
+ data,
36
+ submitting,
37
+ handleChange,
38
+ handleSubmit,
39
+ };
40
+ };
41
+ exports.default = useSignInForm;
@@ -11,3 +11,14 @@ export * from './useGiftCardForm';
11
11
  export * from './useGiftCardsForm';
12
12
  export * from './useOrderRating';
13
13
  export * from './useProfileForm';
14
+ export * from './useSignUpGuestForm';
15
+ export * from './useSignUpForm';
16
+ export * from './useCartGuestForm';
17
+ export * from './useCheckoutLoginForm';
18
+ export * from './useGuestForm';
19
+ export * from './useOrderFulfillmentForm';
20
+ export * from './useOrderRatingForm';
21
+ export * from './useResetPasswordForm';
22
+ export * from './useSendResetForm';
23
+ export * from './useSigninForm';
24
+ export * from './types';
@@ -11,3 +11,14 @@ export * from './useGiftCardForm';
11
11
  export * from './useGiftCardsForm';
12
12
  export * from './useOrderRating';
13
13
  export * from './useProfileForm';
14
+ export * from './useSignUpGuestForm';
15
+ export * from './useSignUpForm';
16
+ export * from './useCartGuestForm';
17
+ export * from './useCheckoutLoginForm';
18
+ export * from './useGuestForm';
19
+ export * from './useOrderFulfillmentForm';
20
+ export * from './useOrderRatingForm';
21
+ export * from './useResetPasswordForm';
22
+ export * from './useSendResetForm';
23
+ export * from './useSigninForm';
24
+ export * from './types';
@@ -0,0 +1,4 @@
1
+ export interface Field {
2
+ blur?: () => void;
3
+ focus?: () => void;
4
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,9 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import { Address, RequestStatus } from '@open-tender/types';
3
- interface Field {
4
- blur?: () => void;
5
- focus?: () => void;
6
- }
3
+ import { Field } from './types';
7
4
  interface AddressFormField {
8
5
  label: string;
9
6
  name: string;
@@ -15,7 +12,7 @@ export declare const useAddressForm: <InputRef extends Field, SubmitRef extends
15
12
  data: Address;
16
13
  errors: any;
17
14
  submitting: boolean;
18
- handleChange: (id: string, type: string, value: string | null, checked: boolean | null) => void;
15
+ handleChange: (name: string, type: string, value: string | null, checked: boolean | null) => void;
19
16
  handleSubmit: () => void;
20
17
  submitRef: import("react").MutableRefObject<SubmitRef | null>;
21
18
  inputRef: import("react").MutableRefObject<InputRef | null>;
@@ -24,13 +24,13 @@ export const useAddressForm = (address, loading, error, update, callback) => {
24
24
  }
25
25
  }
26
26
  }, [loading, error]);
27
- const handleChange = (id, type, value, checked) => {
27
+ const handleChange = (name, type, value, checked) => {
28
28
  const inputValue = type === 'checkbox'
29
29
  ? checked
30
- : id === 'phone'
30
+ : name === 'phone'
31
31
  ? makePhone(value !== null && value !== void 0 ? value : '')
32
32
  : value;
33
- setData(Object.assign(Object.assign({}, data), { [id]: inputValue }));
33
+ setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
34
34
  };
35
35
  const handleSubmit = () => {
36
36
  var _a, _b;
@@ -1,9 +1,12 @@
1
+ /// <reference types="react" />
1
2
  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
+ import { Field } from './types';
4
+ declare const useAllergenForm: <SubmitRef extends Field>(allergens: Allergens, selectedAllergens: Allergens, isLoading: boolean | undefined, error: any, setAllergens: (allergens: Allergens) => void, updateAllergens: (allergen: Partial<Allergen>[]) => void, callback?: () => void) => {
5
+ submitRef: import("react").MutableRefObject<SubmitRef | null>;
3
6
  submitting: boolean;
4
7
  allergenIds: number[];
5
8
  formError: any;
6
- handleChange: (id: string, checked?: boolean) => void;
9
+ handleChange: (name: string, checked?: boolean) => void;
7
10
  handleSubmit: () => void;
8
11
  };
9
12
  export default useAllergenForm;
@@ -1,23 +1,26 @@
1
- import { useState, useEffect } from 'react';
1
+ import { useState, useEffect, useRef } from 'react';
2
2
  const useAllergenForm = (allergens, selectedAllergens, isLoading = false, error = null, setAllergens, updateAllergens, callback) => {
3
+ const submitRef = useRef(null);
3
4
  const [data, setData] = useState([]);
4
5
  const [submitting, setSubmitting] = useState(false);
5
6
  const allergenIds = data.map(i => i.allergen_id);
6
7
  const formError = error ? error.detail || error.message : null;
7
8
  useEffect(() => {
9
+ var _a, _b;
8
10
  // uses isLoading boolean because loading state is based on
9
11
  // both brand allergens and customer allergens loading
10
12
  if (!isLoading) {
11
13
  setSubmitting(false);
14
+ ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
12
15
  }
13
16
  }, [isLoading]);
14
17
  useEffect(() => {
15
18
  setData(selectedAllergens || []);
16
19
  }, [selectedAllergens, allergens]);
17
- const handleChange = (id, checked) => {
20
+ const handleChange = (name, checked) => {
18
21
  const newData = checked
19
- ? [...data, { allergen_id: parseInt(id) }]
20
- : data.filter(i => i.allergen_id !== parseInt(id));
22
+ ? [...data, { allergen_id: parseInt(name) }]
23
+ : data.filter(i => i.allergen_id !== parseInt(name));
21
24
  setData(newData);
22
25
  };
23
26
  const handleSubmit = () => {
@@ -27,11 +30,12 @@ const useAllergenForm = (allergens, selectedAllergens, isLoading = false, error
27
30
  if (updateAllergens) {
28
31
  updateAllergens(newData);
29
32
  }
30
- else if (callback) {
33
+ if (callback) {
31
34
  callback();
32
35
  }
33
36
  };
34
37
  return {
38
+ submitRef,
35
39
  submitting,
36
40
  allergenIds,
37
41
  formError,