@open-tender/utils 0.1.137 → 0.1.139
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/hooks/index.d.ts +1 -0
- package/dist/cjs/hooks/index.js +1 -0
- package/dist/cjs/hooks/useGuestCheckoutForm.d.ts +21 -0
- package/dist/cjs/hooks/useGuestCheckoutForm.js +87 -0
- package/dist/esm/hooks/index.d.ts +1 -0
- package/dist/esm/hooks/index.js +1 -0
- package/dist/esm/hooks/useGuestCheckoutForm.d.ts +21 -0
- package/dist/esm/hooks/useGuestCheckoutForm.js +83 -0
- package/package.json +1 -1
|
@@ -17,6 +17,7 @@ export * from './useGiftCardAssignOtherForm';
|
|
|
17
17
|
export * from './useGiftCardForm';
|
|
18
18
|
export * from './useGiftCardsForm';
|
|
19
19
|
export * from './useGuestForm';
|
|
20
|
+
export * from './useGuestCheckoutForm';
|
|
20
21
|
export * from './useImage';
|
|
21
22
|
export * from './useMenuItem';
|
|
22
23
|
export * from './useModifier';
|
package/dist/cjs/hooks/index.js
CHANGED
|
@@ -20,6 +20,7 @@ tslib_1.__exportStar(require("./useGiftCardAssignOtherForm"), exports);
|
|
|
20
20
|
tslib_1.__exportStar(require("./useGiftCardForm"), exports);
|
|
21
21
|
tslib_1.__exportStar(require("./useGiftCardsForm"), exports);
|
|
22
22
|
tslib_1.__exportStar(require("./useGuestForm"), exports);
|
|
23
|
+
tslib_1.__exportStar(require("./useGuestCheckoutForm"), exports);
|
|
23
24
|
tslib_1.__exportStar(require("./useImage"), exports);
|
|
24
25
|
tslib_1.__exportStar(require("./useMenuItem"), exports);
|
|
25
26
|
tslib_1.__exportStar(require("./useModifier"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { CheckoutFormCustomer, FormFields, RequestError, RequestStatus } from '@open-tender/types';
|
|
3
|
+
export declare const useGuestCheckoutForm: (guest: CheckoutFormCustomer, loading: RequestStatus, error: RequestError, submit: (data: CheckoutFormCustomer) => void) => {
|
|
4
|
+
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
|
+
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
|
+
fields: FormFields;
|
|
7
|
+
data: {
|
|
8
|
+
company?: string | undefined;
|
|
9
|
+
customer_id?: number | undefined;
|
|
10
|
+
email?: string | undefined;
|
|
11
|
+
first_name?: string | undefined;
|
|
12
|
+
password?: string | undefined;
|
|
13
|
+
phone?: string | undefined;
|
|
14
|
+
last_name?: string | undefined;
|
|
15
|
+
};
|
|
16
|
+
errors: Record<string, string>;
|
|
17
|
+
disabled: boolean;
|
|
18
|
+
submitting: boolean;
|
|
19
|
+
handleChange: (name: string, value: string | number | boolean) => void;
|
|
20
|
+
handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
|
|
21
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useGuestCheckoutForm = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const utils_1 = require("../utils");
|
|
6
|
+
const initialData = {
|
|
7
|
+
first_name: '',
|
|
8
|
+
last_name: '',
|
|
9
|
+
email: '',
|
|
10
|
+
phone: ''
|
|
11
|
+
};
|
|
12
|
+
const useGuestCheckoutForm = (guest, loading, error, submit) => {
|
|
13
|
+
const submitRef = (0, react_1.useRef)(null);
|
|
14
|
+
const inputRef = (0, react_1.useRef)(null);
|
|
15
|
+
const initData = Object.assign(Object.assign({}, initialData), guest);
|
|
16
|
+
const [data, setData] = (0, react_1.useState)(initData);
|
|
17
|
+
const [errors, setErrors] = (0, react_1.useState)({});
|
|
18
|
+
const [submitting, setSubmitting] = (0, react_1.useState)(false);
|
|
19
|
+
const disabled = (0, utils_1.checkEmpty)(data);
|
|
20
|
+
const fields = [
|
|
21
|
+
{
|
|
22
|
+
label: 'First Name',
|
|
23
|
+
name: 'first_name',
|
|
24
|
+
type: 'text',
|
|
25
|
+
required: true
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
label: 'Last Name',
|
|
29
|
+
name: 'last_name',
|
|
30
|
+
type: 'text',
|
|
31
|
+
required: true
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
label: 'Email',
|
|
35
|
+
name: 'email',
|
|
36
|
+
type: 'email',
|
|
37
|
+
required: true,
|
|
38
|
+
disabled: true
|
|
39
|
+
},
|
|
40
|
+
{ label: 'Phone', name: 'phone', type: 'tel', required: true }
|
|
41
|
+
];
|
|
42
|
+
const handleChange = (name, value) => {
|
|
43
|
+
const val = name === 'phone' ? (0, utils_1.makePhone)(value) : value;
|
|
44
|
+
setData(Object.assign(Object.assign({}, data), { [name]: val }));
|
|
45
|
+
};
|
|
46
|
+
const handleSubmit = (evt) => {
|
|
47
|
+
var _a, _b;
|
|
48
|
+
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
49
|
+
if (!disabled) {
|
|
50
|
+
setErrors({});
|
|
51
|
+
setSubmitting(true);
|
|
52
|
+
submit(data);
|
|
53
|
+
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
(0, react_1.useEffect)(() => {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
if (loading === 'idle') {
|
|
59
|
+
setSubmitting(false);
|
|
60
|
+
if (error) {
|
|
61
|
+
const errs = (0, utils_1.makeFormErrors)(error);
|
|
62
|
+
errs.form = 'There are one or more errors below';
|
|
63
|
+
setErrors(errs);
|
|
64
|
+
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}, [loading, error]);
|
|
68
|
+
(0, react_1.useEffect)(() => {
|
|
69
|
+
return () => {
|
|
70
|
+
setData(initialData);
|
|
71
|
+
setErrors({});
|
|
72
|
+
setSubmitting(false);
|
|
73
|
+
};
|
|
74
|
+
}, []);
|
|
75
|
+
return {
|
|
76
|
+
submitRef,
|
|
77
|
+
inputRef,
|
|
78
|
+
fields,
|
|
79
|
+
data,
|
|
80
|
+
errors,
|
|
81
|
+
disabled,
|
|
82
|
+
submitting,
|
|
83
|
+
handleChange,
|
|
84
|
+
handleSubmit
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
exports.useGuestCheckoutForm = useGuestCheckoutForm;
|
|
@@ -17,6 +17,7 @@ export * from './useGiftCardAssignOtherForm';
|
|
|
17
17
|
export * from './useGiftCardForm';
|
|
18
18
|
export * from './useGiftCardsForm';
|
|
19
19
|
export * from './useGuestForm';
|
|
20
|
+
export * from './useGuestCheckoutForm';
|
|
20
21
|
export * from './useImage';
|
|
21
22
|
export * from './useMenuItem';
|
|
22
23
|
export * from './useModifier';
|
package/dist/esm/hooks/index.js
CHANGED
|
@@ -17,6 +17,7 @@ export * from './useGiftCardAssignOtherForm';
|
|
|
17
17
|
export * from './useGiftCardForm';
|
|
18
18
|
export * from './useGiftCardsForm';
|
|
19
19
|
export * from './useGuestForm';
|
|
20
|
+
export * from './useGuestCheckoutForm';
|
|
20
21
|
export * from './useImage';
|
|
21
22
|
export * from './useMenuItem';
|
|
22
23
|
export * from './useModifier';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { CheckoutFormCustomer, FormFields, RequestError, RequestStatus } from '@open-tender/types';
|
|
3
|
+
export declare const useGuestCheckoutForm: (guest: CheckoutFormCustomer, loading: RequestStatus, error: RequestError, submit: (data: CheckoutFormCustomer) => void) => {
|
|
4
|
+
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
|
+
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
|
+
fields: FormFields;
|
|
7
|
+
data: {
|
|
8
|
+
company?: string | undefined;
|
|
9
|
+
customer_id?: number | undefined;
|
|
10
|
+
email?: string | undefined;
|
|
11
|
+
first_name?: string | undefined;
|
|
12
|
+
password?: string | undefined;
|
|
13
|
+
phone?: string | undefined;
|
|
14
|
+
last_name?: string | undefined;
|
|
15
|
+
};
|
|
16
|
+
errors: Record<string, string>;
|
|
17
|
+
disabled: boolean;
|
|
18
|
+
submitting: boolean;
|
|
19
|
+
handleChange: (name: string, value: string | number | boolean) => void;
|
|
20
|
+
handleSubmit: (evt?: React.FormEvent<HTMLFormElement>) => void;
|
|
21
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { useRef, useState, useEffect } from 'react';
|
|
2
|
+
import { checkEmpty, makeFormErrors, makePhone } from '../utils';
|
|
3
|
+
const initialData = {
|
|
4
|
+
first_name: '',
|
|
5
|
+
last_name: '',
|
|
6
|
+
email: '',
|
|
7
|
+
phone: ''
|
|
8
|
+
};
|
|
9
|
+
export const useGuestCheckoutForm = (guest, loading, error, submit) => {
|
|
10
|
+
const submitRef = useRef(null);
|
|
11
|
+
const inputRef = useRef(null);
|
|
12
|
+
const initData = Object.assign(Object.assign({}, initialData), guest);
|
|
13
|
+
const [data, setData] = useState(initData);
|
|
14
|
+
const [errors, setErrors] = useState({});
|
|
15
|
+
const [submitting, setSubmitting] = useState(false);
|
|
16
|
+
const disabled = checkEmpty(data);
|
|
17
|
+
const fields = [
|
|
18
|
+
{
|
|
19
|
+
label: 'First Name',
|
|
20
|
+
name: 'first_name',
|
|
21
|
+
type: 'text',
|
|
22
|
+
required: true
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
label: 'Last Name',
|
|
26
|
+
name: 'last_name',
|
|
27
|
+
type: 'text',
|
|
28
|
+
required: true
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
label: 'Email',
|
|
32
|
+
name: 'email',
|
|
33
|
+
type: 'email',
|
|
34
|
+
required: true,
|
|
35
|
+
disabled: true
|
|
36
|
+
},
|
|
37
|
+
{ label: 'Phone', name: 'phone', type: 'tel', required: true }
|
|
38
|
+
];
|
|
39
|
+
const handleChange = (name, value) => {
|
|
40
|
+
const val = name === 'phone' ? makePhone(value) : value;
|
|
41
|
+
setData(Object.assign(Object.assign({}, data), { [name]: val }));
|
|
42
|
+
};
|
|
43
|
+
const handleSubmit = (evt) => {
|
|
44
|
+
var _a, _b;
|
|
45
|
+
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
46
|
+
if (!disabled) {
|
|
47
|
+
setErrors({});
|
|
48
|
+
setSubmitting(true);
|
|
49
|
+
submit(data);
|
|
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
|
+
useEffect(() => {
|
|
54
|
+
var _a, _b;
|
|
55
|
+
if (loading === 'idle') {
|
|
56
|
+
setSubmitting(false);
|
|
57
|
+
if (error) {
|
|
58
|
+
const errs = makeFormErrors(error);
|
|
59
|
+
errs.form = 'There are one or more errors below';
|
|
60
|
+
setErrors(errs);
|
|
61
|
+
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}, [loading, error]);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
return () => {
|
|
67
|
+
setData(initialData);
|
|
68
|
+
setErrors({});
|
|
69
|
+
setSubmitting(false);
|
|
70
|
+
};
|
|
71
|
+
}, []);
|
|
72
|
+
return {
|
|
73
|
+
submitRef,
|
|
74
|
+
inputRef,
|
|
75
|
+
fields,
|
|
76
|
+
data,
|
|
77
|
+
errors,
|
|
78
|
+
disabled,
|
|
79
|
+
submitting,
|
|
80
|
+
handleChange,
|
|
81
|
+
handleSubmit
|
|
82
|
+
};
|
|
83
|
+
};
|
package/package.json
CHANGED