@open-tender/utils 0.1.47 → 0.1.48
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.
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import {
|
|
3
|
-
export declare const useDineInForm: (initialData:
|
|
2
|
+
import { CheckoutFormCustomer, FormFields } from '@open-tender/types';
|
|
3
|
+
export declare const useDineInForm: (initialData: CheckoutFormCustomer | undefined, submitGuest: (data: CheckoutFormCustomer) => void) => {
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
fields: FormFields;
|
|
7
|
+
data: CheckoutFormCustomer;
|
|
8
|
+
errors: Record<string, string>;
|
|
8
9
|
handleChange: (name: string, value: string) => void;
|
|
9
|
-
handleSubmit: (evt
|
|
10
|
+
handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
|
|
10
11
|
};
|
|
@@ -2,30 +2,53 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useDineInForm = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
+
const utils_1 = require("../utils");
|
|
6
|
+
const makeDineInFormErrors = (data) => {
|
|
7
|
+
const errors = {};
|
|
8
|
+
if (!data.first_name) {
|
|
9
|
+
errors.first_name = 'Please enter a first name';
|
|
10
|
+
}
|
|
11
|
+
if (!data.last_name) {
|
|
12
|
+
errors.last_name = 'Please enter a last name';
|
|
13
|
+
}
|
|
14
|
+
if (!(0, utils_1.isEmpty)(errors)) {
|
|
15
|
+
errors.form = 'Both first and last name are required';
|
|
16
|
+
}
|
|
17
|
+
return errors;
|
|
18
|
+
};
|
|
5
19
|
const useDineInForm = (initialData = {}, submitGuest) => {
|
|
6
20
|
const submitRef = (0, react_1.useRef)(null);
|
|
7
21
|
const inputRef = (0, react_1.useRef)(null);
|
|
8
22
|
const [data, setData] = (0, react_1.useState)(initialData);
|
|
9
|
-
const [
|
|
23
|
+
const [errors, setErrors] = (0, react_1.useState)({});
|
|
24
|
+
const fields = [
|
|
25
|
+
{
|
|
26
|
+
label: 'First Name',
|
|
27
|
+
name: 'first_name',
|
|
28
|
+
type: 'text'
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
label: 'Last Name',
|
|
32
|
+
name: 'last_name',
|
|
33
|
+
type: 'text'
|
|
34
|
+
}
|
|
35
|
+
];
|
|
10
36
|
const handleChange = (name, value) => {
|
|
11
37
|
setData(Object.assign(Object.assign({}, data), { [name]: value }));
|
|
12
38
|
};
|
|
13
39
|
const handleSubmit = (evt) => {
|
|
14
40
|
var _a, _b;
|
|
15
41
|
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
submitGuest({ customer: data });
|
|
21
|
-
}
|
|
42
|
+
const errs = makeDineInFormErrors(data);
|
|
43
|
+
!(0, utils_1.isEmpty)(errs) ? setErrors(errs) : submitGuest(data);
|
|
22
44
|
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
|
|
23
45
|
};
|
|
24
46
|
return {
|
|
25
47
|
submitRef,
|
|
26
48
|
inputRef,
|
|
49
|
+
fields,
|
|
27
50
|
data,
|
|
28
|
-
|
|
51
|
+
errors,
|
|
29
52
|
handleChange,
|
|
30
53
|
handleSubmit
|
|
31
54
|
};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import {
|
|
3
|
-
export declare const useDineInForm: (initialData:
|
|
2
|
+
import { CheckoutFormCustomer, FormFields } from '@open-tender/types';
|
|
3
|
+
export declare const useDineInForm: (initialData: CheckoutFormCustomer | undefined, submitGuest: (data: CheckoutFormCustomer) => void) => {
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
fields: FormFields;
|
|
7
|
+
data: CheckoutFormCustomer;
|
|
8
|
+
errors: Record<string, string>;
|
|
8
9
|
handleChange: (name: string, value: string) => void;
|
|
9
|
-
handleSubmit: (evt
|
|
10
|
+
handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
|
|
10
11
|
};
|
|
@@ -1,28 +1,51 @@
|
|
|
1
1
|
import { useRef, useState } from 'react';
|
|
2
|
+
import { isEmpty } from '../utils';
|
|
3
|
+
const makeDineInFormErrors = (data) => {
|
|
4
|
+
const errors = {};
|
|
5
|
+
if (!data.first_name) {
|
|
6
|
+
errors.first_name = 'Please enter a first name';
|
|
7
|
+
}
|
|
8
|
+
if (!data.last_name) {
|
|
9
|
+
errors.last_name = 'Please enter a last name';
|
|
10
|
+
}
|
|
11
|
+
if (!isEmpty(errors)) {
|
|
12
|
+
errors.form = 'Both first and last name are required';
|
|
13
|
+
}
|
|
14
|
+
return errors;
|
|
15
|
+
};
|
|
2
16
|
export const useDineInForm = (initialData = {}, submitGuest) => {
|
|
3
17
|
const submitRef = useRef(null);
|
|
4
18
|
const inputRef = useRef(null);
|
|
5
19
|
const [data, setData] = useState(initialData);
|
|
6
|
-
const [
|
|
20
|
+
const [errors, setErrors] = useState({});
|
|
21
|
+
const fields = [
|
|
22
|
+
{
|
|
23
|
+
label: 'First Name',
|
|
24
|
+
name: 'first_name',
|
|
25
|
+
type: 'text'
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
label: 'Last Name',
|
|
29
|
+
name: 'last_name',
|
|
30
|
+
type: 'text'
|
|
31
|
+
}
|
|
32
|
+
];
|
|
7
33
|
const handleChange = (name, value) => {
|
|
8
34
|
setData(Object.assign(Object.assign({}, data), { [name]: value }));
|
|
9
35
|
};
|
|
10
36
|
const handleSubmit = (evt) => {
|
|
11
37
|
var _a, _b;
|
|
12
38
|
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
submitGuest({ customer: data });
|
|
18
|
-
}
|
|
39
|
+
const errs = makeDineInFormErrors(data);
|
|
40
|
+
!isEmpty(errs) ? setErrors(errs) : submitGuest(data);
|
|
19
41
|
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
|
|
20
42
|
};
|
|
21
43
|
return {
|
|
22
44
|
submitRef,
|
|
23
45
|
inputRef,
|
|
46
|
+
fields,
|
|
24
47
|
data,
|
|
25
|
-
|
|
48
|
+
errors,
|
|
26
49
|
handleChange,
|
|
27
50
|
handleSubmit
|
|
28
51
|
};
|
package/package.json
CHANGED