@open-tender/utils 0.1.78 → 0.1.79
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/useLoginForm.js +1 -2
- package/dist/cjs/hooks/useProfileForm.d.ts +1 -1
- package/dist/cjs/hooks/useProfileForm.js +52 -28
- package/dist/esm/hooks/useLoginForm.js +1 -2
- package/dist/esm/hooks/useProfileForm.d.ts +1 -1
- package/dist/esm/hooks/useProfileForm.js +52 -28
- package/package.json +1 -1
|
@@ -27,13 +27,12 @@ const useLoginForm = (loading, error, login, callback) => {
|
|
|
27
27
|
(0, react_1.useEffect)(() => {
|
|
28
28
|
var _a, _b;
|
|
29
29
|
if (loading === 'idle') {
|
|
30
|
+
setSubmitting(false);
|
|
30
31
|
if (error) {
|
|
31
|
-
setSubmitting(false);
|
|
32
32
|
setErrors((0, utils_1.makeFormErrors)(error));
|
|
33
33
|
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
|
|
34
34
|
}
|
|
35
35
|
else if (callback && submitting) {
|
|
36
|
-
setSubmitting(false);
|
|
37
36
|
callback();
|
|
38
37
|
}
|
|
39
38
|
}
|
|
@@ -4,7 +4,7 @@ export declare const genderOptions: {
|
|
|
4
4
|
name: string;
|
|
5
5
|
value: Gender;
|
|
6
6
|
}[];
|
|
7
|
-
export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void) => {
|
|
7
|
+
export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void, callback?: ((data: CustomerCreate) => void) | undefined) => {
|
|
8
8
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
9
9
|
data: Customer | null;
|
|
10
10
|
errors: Record<string, string>;
|
|
@@ -8,35 +8,46 @@ exports.genderOptions = [
|
|
|
8
8
|
{ name: 'Male', value: 'MALE' },
|
|
9
9
|
{ name: 'Female', value: 'FEMALE' }
|
|
10
10
|
];
|
|
11
|
-
const
|
|
11
|
+
const fields = [
|
|
12
|
+
{ label: 'First Name', name: 'first_name', type: 'text', required: true },
|
|
13
|
+
{ label: 'Last Name', name: 'last_name', type: 'text', required: true },
|
|
14
|
+
{
|
|
15
|
+
label: 'Email',
|
|
16
|
+
name: 'email',
|
|
17
|
+
type: 'email',
|
|
18
|
+
required: true,
|
|
19
|
+
autoComplete: 'email'
|
|
20
|
+
},
|
|
21
|
+
{ label: 'Phone', name: 'phone', type: 'tel', required: true },
|
|
22
|
+
{ label: 'Company', name: 'company', type: 'text' },
|
|
23
|
+
{ label: 'Birth Date (mm/dd/yyyy)', name: 'birth_date', type: 'tel' },
|
|
24
|
+
{
|
|
25
|
+
label: 'Gender',
|
|
26
|
+
name: 'gender',
|
|
27
|
+
type: 'select',
|
|
28
|
+
showLabel: true,
|
|
29
|
+
options: exports.genderOptions
|
|
30
|
+
}
|
|
31
|
+
];
|
|
32
|
+
const makePayload = (fields, data) => {
|
|
33
|
+
if (!data)
|
|
34
|
+
return null;
|
|
35
|
+
const rest = fields.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: data[i.name] })), {});
|
|
36
|
+
rest.birth_date = data.birth_date ? (0, utils_1.slashesToDashes)(data.birth_date) : null;
|
|
37
|
+
rest.gender = data.gender || null;
|
|
38
|
+
return rest;
|
|
39
|
+
};
|
|
40
|
+
const useProfileForm = (profile, loading, error, update, callback
|
|
12
41
|
// optIns: Partial<ConfigBrand> = {}
|
|
13
42
|
) => {
|
|
14
43
|
const submitRef = (0, react_1.useRef)(null);
|
|
15
|
-
// const { accepts_marketing, order_notifications } = optIns
|
|
16
44
|
const [data, setData] = (0, react_1.useState)(profile);
|
|
17
45
|
const [submitting, setSubmitting] = (0, react_1.useState)(false);
|
|
18
46
|
const [errors, setErrors] = (0, react_1.useState)({});
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
label: 'Email',
|
|
24
|
-
name: 'email',
|
|
25
|
-
type: 'email',
|
|
26
|
-
required: true,
|
|
27
|
-
autoComplete: 'email'
|
|
28
|
-
},
|
|
29
|
-
{ label: 'Phone', name: 'phone', type: 'tel', required: true },
|
|
30
|
-
{ label: 'Company', name: 'company', type: 'text' },
|
|
31
|
-
{ label: 'Birth Date (mm/dd/yyyy)', name: 'birth_date', type: 'tel' },
|
|
32
|
-
{
|
|
33
|
-
label: 'Gender',
|
|
34
|
-
name: 'gender',
|
|
35
|
-
type: 'select',
|
|
36
|
-
showLabel: true,
|
|
37
|
-
options: exports.genderOptions
|
|
38
|
-
}
|
|
39
|
-
];
|
|
47
|
+
// const { accepts_marketing, order_notifications } = optIns
|
|
48
|
+
// useEffect(() => {
|
|
49
|
+
// setPayload(fields, data)
|
|
50
|
+
// }, [fields, data])
|
|
40
51
|
(0, react_1.useEffect)(() => {
|
|
41
52
|
if (loading === 'idle') {
|
|
42
53
|
setSubmitting(false);
|
|
@@ -47,8 +58,13 @@ const useProfileForm = (profile, loading, error, update
|
|
|
47
58
|
}
|
|
48
59
|
setErrors(errs);
|
|
49
60
|
}
|
|
61
|
+
else if (callback && submitting) {
|
|
62
|
+
const payload = makePayload(fields, data);
|
|
63
|
+
if (payload)
|
|
64
|
+
callback(payload);
|
|
65
|
+
}
|
|
50
66
|
}
|
|
51
|
-
}, [loading, error]);
|
|
67
|
+
}, [loading, error, callback, submitting, data]);
|
|
52
68
|
(0, react_1.useEffect)(() => {
|
|
53
69
|
if (profile)
|
|
54
70
|
setData(profile);
|
|
@@ -73,10 +89,18 @@ const useProfileForm = (profile, loading, error, update
|
|
|
73
89
|
setErrors({});
|
|
74
90
|
setSubmitting(true);
|
|
75
91
|
// remove read-only properties
|
|
76
|
-
const rest = fields.reduce(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
92
|
+
// const rest: CustomerCreate = fields.reduce(
|
|
93
|
+
// (obj, i: FormFieldType) => ({
|
|
94
|
+
// ...obj,
|
|
95
|
+
// [i.name]: data[i.name as keyof Customer]
|
|
96
|
+
// }),
|
|
97
|
+
// {} as CustomerCreate
|
|
98
|
+
// )
|
|
99
|
+
// rest.birth_date = data.birth_date ? slashesToDashes(data.birth_date) : null
|
|
100
|
+
// rest.gender = data.gender || null
|
|
101
|
+
const payload = makePayload(fields, data);
|
|
102
|
+
if (payload)
|
|
103
|
+
update(payload);
|
|
80
104
|
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
|
|
81
105
|
};
|
|
82
106
|
return {
|
|
@@ -24,13 +24,12 @@ export const useLoginForm = (loading, error, login, callback) => {
|
|
|
24
24
|
useEffect(() => {
|
|
25
25
|
var _a, _b;
|
|
26
26
|
if (loading === 'idle') {
|
|
27
|
+
setSubmitting(false);
|
|
27
28
|
if (error) {
|
|
28
|
-
setSubmitting(false);
|
|
29
29
|
setErrors(makeFormErrors(error));
|
|
30
30
|
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
|
|
31
31
|
}
|
|
32
32
|
else if (callback && submitting) {
|
|
33
|
-
setSubmitting(false);
|
|
34
33
|
callback();
|
|
35
34
|
}
|
|
36
35
|
}
|
|
@@ -4,7 +4,7 @@ export declare const genderOptions: {
|
|
|
4
4
|
name: string;
|
|
5
5
|
value: Gender;
|
|
6
6
|
}[];
|
|
7
|
-
export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void) => {
|
|
7
|
+
export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void, callback?: ((data: CustomerCreate) => void) | undefined) => {
|
|
8
8
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
9
9
|
data: Customer | null;
|
|
10
10
|
errors: Record<string, string>;
|
|
@@ -5,35 +5,46 @@ export const genderOptions = [
|
|
|
5
5
|
{ name: 'Male', value: 'MALE' },
|
|
6
6
|
{ name: 'Female', value: 'FEMALE' }
|
|
7
7
|
];
|
|
8
|
-
|
|
8
|
+
const fields = [
|
|
9
|
+
{ label: 'First Name', name: 'first_name', type: 'text', required: true },
|
|
10
|
+
{ label: 'Last Name', name: 'last_name', type: 'text', required: true },
|
|
11
|
+
{
|
|
12
|
+
label: 'Email',
|
|
13
|
+
name: 'email',
|
|
14
|
+
type: 'email',
|
|
15
|
+
required: true,
|
|
16
|
+
autoComplete: 'email'
|
|
17
|
+
},
|
|
18
|
+
{ label: 'Phone', name: 'phone', type: 'tel', required: true },
|
|
19
|
+
{ label: 'Company', name: 'company', type: 'text' },
|
|
20
|
+
{ label: 'Birth Date (mm/dd/yyyy)', name: 'birth_date', type: 'tel' },
|
|
21
|
+
{
|
|
22
|
+
label: 'Gender',
|
|
23
|
+
name: 'gender',
|
|
24
|
+
type: 'select',
|
|
25
|
+
showLabel: true,
|
|
26
|
+
options: genderOptions
|
|
27
|
+
}
|
|
28
|
+
];
|
|
29
|
+
const makePayload = (fields, data) => {
|
|
30
|
+
if (!data)
|
|
31
|
+
return null;
|
|
32
|
+
const rest = fields.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: data[i.name] })), {});
|
|
33
|
+
rest.birth_date = data.birth_date ? slashesToDashes(data.birth_date) : null;
|
|
34
|
+
rest.gender = data.gender || null;
|
|
35
|
+
return rest;
|
|
36
|
+
};
|
|
37
|
+
export const useProfileForm = (profile, loading, error, update, callback
|
|
9
38
|
// optIns: Partial<ConfigBrand> = {}
|
|
10
39
|
) => {
|
|
11
40
|
const submitRef = useRef(null);
|
|
12
|
-
// const { accepts_marketing, order_notifications } = optIns
|
|
13
41
|
const [data, setData] = useState(profile);
|
|
14
42
|
const [submitting, setSubmitting] = useState(false);
|
|
15
43
|
const [errors, setErrors] = useState({});
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
label: 'Email',
|
|
21
|
-
name: 'email',
|
|
22
|
-
type: 'email',
|
|
23
|
-
required: true,
|
|
24
|
-
autoComplete: 'email'
|
|
25
|
-
},
|
|
26
|
-
{ label: 'Phone', name: 'phone', type: 'tel', required: true },
|
|
27
|
-
{ label: 'Company', name: 'company', type: 'text' },
|
|
28
|
-
{ label: 'Birth Date (mm/dd/yyyy)', name: 'birth_date', type: 'tel' },
|
|
29
|
-
{
|
|
30
|
-
label: 'Gender',
|
|
31
|
-
name: 'gender',
|
|
32
|
-
type: 'select',
|
|
33
|
-
showLabel: true,
|
|
34
|
-
options: genderOptions
|
|
35
|
-
}
|
|
36
|
-
];
|
|
44
|
+
// const { accepts_marketing, order_notifications } = optIns
|
|
45
|
+
// useEffect(() => {
|
|
46
|
+
// setPayload(fields, data)
|
|
47
|
+
// }, [fields, data])
|
|
37
48
|
useEffect(() => {
|
|
38
49
|
if (loading === 'idle') {
|
|
39
50
|
setSubmitting(false);
|
|
@@ -44,8 +55,13 @@ export const useProfileForm = (profile, loading, error, update
|
|
|
44
55
|
}
|
|
45
56
|
setErrors(errs);
|
|
46
57
|
}
|
|
58
|
+
else if (callback && submitting) {
|
|
59
|
+
const payload = makePayload(fields, data);
|
|
60
|
+
if (payload)
|
|
61
|
+
callback(payload);
|
|
62
|
+
}
|
|
47
63
|
}
|
|
48
|
-
}, [loading, error]);
|
|
64
|
+
}, [loading, error, callback, submitting, data]);
|
|
49
65
|
useEffect(() => {
|
|
50
66
|
if (profile)
|
|
51
67
|
setData(profile);
|
|
@@ -70,10 +86,18 @@ export const useProfileForm = (profile, loading, error, update
|
|
|
70
86
|
setErrors({});
|
|
71
87
|
setSubmitting(true);
|
|
72
88
|
// remove read-only properties
|
|
73
|
-
const rest = fields.reduce(
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
89
|
+
// const rest: CustomerCreate = fields.reduce(
|
|
90
|
+
// (obj, i: FormFieldType) => ({
|
|
91
|
+
// ...obj,
|
|
92
|
+
// [i.name]: data[i.name as keyof Customer]
|
|
93
|
+
// }),
|
|
94
|
+
// {} as CustomerCreate
|
|
95
|
+
// )
|
|
96
|
+
// rest.birth_date = data.birth_date ? slashesToDashes(data.birth_date) : null
|
|
97
|
+
// rest.gender = data.gender || null
|
|
98
|
+
const payload = makePayload(fields, data);
|
|
99
|
+
if (payload)
|
|
100
|
+
update(payload);
|
|
77
101
|
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
|
|
78
102
|
};
|
|
79
103
|
return {
|
package/package.json
CHANGED