@open-tender/utils 0.1.31 → 0.1.32
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/useProfileForm.d.ts +5 -1
- package/dist/cjs/hooks/useProfileForm.js +3 -7
- package/dist/cjs/hooks/useSignUpForm.d.ts +3 -4
- package/dist/cjs/hooks/useSignUpForm.js +29 -19
- package/dist/esm/hooks/useProfileForm.d.ts +5 -1
- package/dist/esm/hooks/useProfileForm.js +1 -5
- package/dist/esm/hooks/useSignUpForm.d.ts +3 -4
- package/dist/esm/hooks/useSignUpForm.js +30 -20
- package/package.json +1 -1
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { Customer, CustomerCreate, FormFields, RequestError, RequestStatus } from '@open-tender/types';
|
|
2
|
+
import { Customer, CustomerCreate, FormFields, Gender, RequestError, RequestStatus } from '@open-tender/types';
|
|
3
|
+
export declare const genderOptions: {
|
|
4
|
+
name: string;
|
|
5
|
+
value: Gender;
|
|
6
|
+
}[];
|
|
3
7
|
export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void) => {
|
|
4
8
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
9
|
data: Customer | null;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useProfileForm = void 0;
|
|
3
|
+
exports.useProfileForm = exports.genderOptions = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
5
|
const utils_1 = require("../utils");
|
|
6
|
-
|
|
6
|
+
exports.genderOptions = [
|
|
7
7
|
{ name: 'not specified', value: 'DECLINED' },
|
|
8
8
|
{ name: 'Male', value: 'MALE' },
|
|
9
9
|
{ name: 'Female', value: 'FEMALE' }
|
|
@@ -29,15 +29,13 @@ const useProfileForm = (profile, loading, error, update
|
|
|
29
29
|
{ label: 'Phone', name: 'phone', type: 'tel', required: true },
|
|
30
30
|
{ label: 'Company', name: 'company', type: 'text' },
|
|
31
31
|
{ label: 'Birth Date (mm/dd/yyyy)', name: 'birth_date', type: 'tel' },
|
|
32
|
-
{ label: 'Gender', name: 'gender', type: 'select', options: genderOptions }
|
|
32
|
+
{ label: 'Gender', name: 'gender', type: 'select', options: exports.genderOptions }
|
|
33
33
|
];
|
|
34
34
|
(0, react_1.useEffect)(() => {
|
|
35
35
|
if (loading === 'idle') {
|
|
36
36
|
setSubmitting(false);
|
|
37
37
|
if (error) {
|
|
38
38
|
const errs = (0, utils_1.makeFormErrors)(error);
|
|
39
|
-
if (errs)
|
|
40
|
-
errs.form = 'There are one or more errors below.';
|
|
41
39
|
if (errs.birth_date) {
|
|
42
40
|
errs.birth_date = 'Invalid date. Please enter in mm/dd/yyyy format.';
|
|
43
41
|
}
|
|
@@ -72,8 +70,6 @@ const useProfileForm = (profile, loading, error, update
|
|
|
72
70
|
setSubmitting(true);
|
|
73
71
|
// remove read-only properties
|
|
74
72
|
const rest = fields.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: data[i.name] })), {});
|
|
75
|
-
// const rest = { ...data }
|
|
76
|
-
// const { customer_id, is_notification_set, is_verified, ...rest } = data
|
|
77
73
|
rest.birth_date = data.birth_date ? (0, utils_1.slashesToDashes)(data.birth_date) : null;
|
|
78
74
|
rest.gender = data.gender || null;
|
|
79
75
|
update(rest);
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { CheckoutConfig, CustomerCreate,
|
|
2
|
+
import { CheckoutConfig, CustomerCreate, RequestError } from '@open-tender/types';
|
|
3
3
|
export declare const useSignUpForm: (loading: RequestInfo, error: RequestError, signUp: (data: CustomerCreate, callback?: () => void) => void, callback?: () => void, checkConfig?: Partial<CheckoutConfig>, hasThanx?: boolean) => {
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
data: CustomerCreate;
|
|
6
6
|
errors: Record<string, string>;
|
|
7
7
|
submitting: boolean;
|
|
8
|
-
|
|
9
|
-
errMsg: string | null;
|
|
8
|
+
fields: import("@open-tender/types").FormFieldType[];
|
|
10
9
|
handleChange: (name: string, value: string | number | boolean) => void;
|
|
11
|
-
handleSubmit: (evt
|
|
10
|
+
handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
|
|
12
11
|
};
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useSignUpForm = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
5
|
const utils_1 = require("../utils");
|
|
6
|
+
const useProfileForm_1 = require("./useProfileForm");
|
|
6
7
|
const initState = {
|
|
7
8
|
first_name: '',
|
|
8
9
|
last_name: '',
|
|
@@ -10,15 +11,20 @@ const initState = {
|
|
|
10
11
|
phone: ''
|
|
11
12
|
};
|
|
12
13
|
const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}, hasThanx = false) => {
|
|
13
|
-
const { displayed, required } = checkConfig || {};
|
|
14
|
-
const companyDisplayed = displayed && displayed.customer.includes('company');
|
|
15
|
-
const companyRequired = required && required.customer.includes('company');
|
|
16
14
|
const submitRef = (0, react_1.useRef)(null);
|
|
17
15
|
const [data, setData] = (0, react_1.useState)(initState);
|
|
18
16
|
const [errors, setErrors] = (0, react_1.useState)({});
|
|
19
17
|
const [submitting, setSubmitting] = (0, react_1.useState)(false);
|
|
20
|
-
const
|
|
21
|
-
|
|
18
|
+
const { displayed, required } = checkConfig || {};
|
|
19
|
+
const companyDisplayed = displayed && displayed.customer.includes('company');
|
|
20
|
+
const companyRequired = required && required.customer.includes('company');
|
|
21
|
+
const showCompany = companyDisplayed || companyRequired;
|
|
22
|
+
const toRemove = [];
|
|
23
|
+
if (!showCompany)
|
|
24
|
+
toRemove.push('company');
|
|
25
|
+
if (hasThanx)
|
|
26
|
+
toRemove.push('password');
|
|
27
|
+
const allFields = [
|
|
22
28
|
{
|
|
23
29
|
label: 'First Name',
|
|
24
30
|
name: 'first_name',
|
|
@@ -45,20 +51,17 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}, hasTh
|
|
|
45
51
|
required: true,
|
|
46
52
|
autoComplete: 'new-password'
|
|
47
53
|
},
|
|
48
|
-
{ label: 'Phone', name: 'phone', type: 'tel', required: true }
|
|
49
|
-
|
|
50
|
-
if (companyDisplayed || companyRequired) {
|
|
51
|
-
const company = {
|
|
54
|
+
{ label: 'Phone', name: 'phone', type: 'tel', required: true },
|
|
55
|
+
{
|
|
52
56
|
label: 'Company',
|
|
53
57
|
name: 'company',
|
|
54
58
|
type: 'text',
|
|
55
59
|
required: companyRequired
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
: fields;
|
|
60
|
+
},
|
|
61
|
+
{ label: 'Birth Date (mm/dd/yyyy)', name: 'birth_date', type: 'tel' },
|
|
62
|
+
{ label: 'Gender', name: 'gender', type: 'select', options: useProfileForm_1.genderOptions }
|
|
63
|
+
];
|
|
64
|
+
const fields = allFields.filter(i => !toRemove.includes(i.name));
|
|
62
65
|
(0, react_1.useEffect)(() => {
|
|
63
66
|
return () => {
|
|
64
67
|
setData(initState);
|
|
@@ -74,7 +77,11 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}, hasTh
|
|
|
74
77
|
}
|
|
75
78
|
}, [loading, error]);
|
|
76
79
|
const handleChange = (name, value) => {
|
|
77
|
-
const inputValue = name === 'phone'
|
|
80
|
+
const inputValue = name === 'phone'
|
|
81
|
+
? (0, utils_1.makePhone)((value !== null && value !== void 0 ? value : ''))
|
|
82
|
+
: name === 'birth_date'
|
|
83
|
+
? (0, utils_1.makeBirthDate)(value)
|
|
84
|
+
: value;
|
|
78
85
|
setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
|
|
79
86
|
};
|
|
80
87
|
const handleSubmit = (evt) => {
|
|
@@ -82,7 +89,11 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}, hasTh
|
|
|
82
89
|
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
83
90
|
setErrors({});
|
|
84
91
|
setSubmitting(true);
|
|
85
|
-
|
|
92
|
+
const values = Object.assign({}, data);
|
|
93
|
+
if (values.birth_date) {
|
|
94
|
+
values.birth_date = (0, utils_1.slashesToDashes)(values.birth_date);
|
|
95
|
+
}
|
|
96
|
+
signUp(values, callback);
|
|
86
97
|
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
|
|
87
98
|
};
|
|
88
99
|
return {
|
|
@@ -90,8 +101,7 @@ const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}, hasTh
|
|
|
90
101
|
data,
|
|
91
102
|
errors,
|
|
92
103
|
submitting,
|
|
93
|
-
|
|
94
|
-
errMsg,
|
|
104
|
+
fields,
|
|
95
105
|
handleChange,
|
|
96
106
|
handleSubmit
|
|
97
107
|
};
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { Customer, CustomerCreate, FormFields, RequestError, RequestStatus } from '@open-tender/types';
|
|
2
|
+
import { Customer, CustomerCreate, FormFields, Gender, RequestError, RequestStatus } from '@open-tender/types';
|
|
3
|
+
export declare const genderOptions: {
|
|
4
|
+
name: string;
|
|
5
|
+
value: Gender;
|
|
6
|
+
}[];
|
|
3
7
|
export declare const useProfileForm: (profile: Customer | null, loading: RequestStatus, error: RequestError, update: (data: CustomerCreate) => void) => {
|
|
4
8
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
9
|
data: Customer | null;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useRef, useState, useEffect } from 'react';
|
|
2
2
|
import { makeBirthDate, makeFormErrors, makePhone, slashesToDashes } from '../utils';
|
|
3
|
-
const genderOptions = [
|
|
3
|
+
export const genderOptions = [
|
|
4
4
|
{ name: 'not specified', value: 'DECLINED' },
|
|
5
5
|
{ name: 'Male', value: 'MALE' },
|
|
6
6
|
{ name: 'Female', value: 'FEMALE' }
|
|
@@ -33,8 +33,6 @@ export const useProfileForm = (profile, loading, error, update
|
|
|
33
33
|
setSubmitting(false);
|
|
34
34
|
if (error) {
|
|
35
35
|
const errs = makeFormErrors(error);
|
|
36
|
-
if (errs)
|
|
37
|
-
errs.form = 'There are one or more errors below.';
|
|
38
36
|
if (errs.birth_date) {
|
|
39
37
|
errs.birth_date = 'Invalid date. Please enter in mm/dd/yyyy format.';
|
|
40
38
|
}
|
|
@@ -69,8 +67,6 @@ export const useProfileForm = (profile, loading, error, update
|
|
|
69
67
|
setSubmitting(true);
|
|
70
68
|
// remove read-only properties
|
|
71
69
|
const rest = fields.reduce((obj, i) => (Object.assign(Object.assign({}, obj), { [i.name]: data[i.name] })), {});
|
|
72
|
-
// const rest = { ...data }
|
|
73
|
-
// const { customer_id, is_notification_set, is_verified, ...rest } = data
|
|
74
70
|
rest.birth_date = data.birth_date ? slashesToDashes(data.birth_date) : null;
|
|
75
71
|
rest.gender = data.gender || null;
|
|
76
72
|
update(rest);
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { CheckoutConfig, CustomerCreate,
|
|
2
|
+
import { CheckoutConfig, CustomerCreate, RequestError } from '@open-tender/types';
|
|
3
3
|
export declare const useSignUpForm: (loading: RequestInfo, error: RequestError, signUp: (data: CustomerCreate, callback?: () => void) => void, callback?: () => void, checkConfig?: Partial<CheckoutConfig>, hasThanx?: boolean) => {
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
data: CustomerCreate;
|
|
6
6
|
errors: Record<string, string>;
|
|
7
7
|
submitting: boolean;
|
|
8
|
-
|
|
9
|
-
errMsg: string | null;
|
|
8
|
+
fields: import("@open-tender/types").FormFieldType[];
|
|
10
9
|
handleChange: (name: string, value: string | number | boolean) => void;
|
|
11
|
-
handleSubmit: (evt
|
|
10
|
+
handleSubmit: (evt: React.FormEvent<HTMLFormElement>) => void;
|
|
12
11
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useRef, useState, useEffect } from 'react';
|
|
2
|
-
import { makeFormErrors, makePhone } from '../utils';
|
|
2
|
+
import { makeBirthDate, makeFormErrors, makePhone, slashesToDashes } from '../utils';
|
|
3
|
+
import { genderOptions } from './useProfileForm';
|
|
3
4
|
const initState = {
|
|
4
5
|
first_name: '',
|
|
5
6
|
last_name: '',
|
|
@@ -7,15 +8,20 @@ const initState = {
|
|
|
7
8
|
phone: ''
|
|
8
9
|
};
|
|
9
10
|
export const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}, hasThanx = false) => {
|
|
10
|
-
const { displayed, required } = checkConfig || {};
|
|
11
|
-
const companyDisplayed = displayed && displayed.customer.includes('company');
|
|
12
|
-
const companyRequired = required && required.customer.includes('company');
|
|
13
11
|
const submitRef = useRef(null);
|
|
14
12
|
const [data, setData] = useState(initState);
|
|
15
13
|
const [errors, setErrors] = useState({});
|
|
16
14
|
const [submitting, setSubmitting] = useState(false);
|
|
17
|
-
const
|
|
18
|
-
|
|
15
|
+
const { displayed, required } = checkConfig || {};
|
|
16
|
+
const companyDisplayed = displayed && displayed.customer.includes('company');
|
|
17
|
+
const companyRequired = required && required.customer.includes('company');
|
|
18
|
+
const showCompany = companyDisplayed || companyRequired;
|
|
19
|
+
const toRemove = [];
|
|
20
|
+
if (!showCompany)
|
|
21
|
+
toRemove.push('company');
|
|
22
|
+
if (hasThanx)
|
|
23
|
+
toRemove.push('password');
|
|
24
|
+
const allFields = [
|
|
19
25
|
{
|
|
20
26
|
label: 'First Name',
|
|
21
27
|
name: 'first_name',
|
|
@@ -42,20 +48,17 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}
|
|
|
42
48
|
required: true,
|
|
43
49
|
autoComplete: 'new-password'
|
|
44
50
|
},
|
|
45
|
-
{ label: 'Phone', name: 'phone', type: 'tel', required: true }
|
|
46
|
-
|
|
47
|
-
if (companyDisplayed || companyRequired) {
|
|
48
|
-
const company = {
|
|
51
|
+
{ label: 'Phone', name: 'phone', type: 'tel', required: true },
|
|
52
|
+
{
|
|
49
53
|
label: 'Company',
|
|
50
54
|
name: 'company',
|
|
51
55
|
type: 'text',
|
|
52
56
|
required: companyRequired
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
: fields;
|
|
57
|
+
},
|
|
58
|
+
{ label: 'Birth Date (mm/dd/yyyy)', name: 'birth_date', type: 'tel' },
|
|
59
|
+
{ label: 'Gender', name: 'gender', type: 'select', options: genderOptions }
|
|
60
|
+
];
|
|
61
|
+
const fields = allFields.filter(i => !toRemove.includes(i.name));
|
|
59
62
|
useEffect(() => {
|
|
60
63
|
return () => {
|
|
61
64
|
setData(initState);
|
|
@@ -71,7 +74,11 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}
|
|
|
71
74
|
}
|
|
72
75
|
}, [loading, error]);
|
|
73
76
|
const handleChange = (name, value) => {
|
|
74
|
-
const inputValue = name === 'phone'
|
|
77
|
+
const inputValue = name === 'phone'
|
|
78
|
+
? makePhone((value !== null && value !== void 0 ? value : ''))
|
|
79
|
+
: name === 'birth_date'
|
|
80
|
+
? makeBirthDate(value)
|
|
81
|
+
: value;
|
|
75
82
|
setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
|
|
76
83
|
};
|
|
77
84
|
const handleSubmit = (evt) => {
|
|
@@ -79,7 +86,11 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}
|
|
|
79
86
|
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
80
87
|
setErrors({});
|
|
81
88
|
setSubmitting(true);
|
|
82
|
-
|
|
89
|
+
const values = Object.assign({}, data);
|
|
90
|
+
if (values.birth_date) {
|
|
91
|
+
values.birth_date = slashesToDashes(values.birth_date);
|
|
92
|
+
}
|
|
93
|
+
signUp(values, callback);
|
|
83
94
|
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
|
|
84
95
|
};
|
|
85
96
|
return {
|
|
@@ -87,8 +98,7 @@ export const useSignUpForm = (loading, error, signUp, callback, checkConfig = {}
|
|
|
87
98
|
data,
|
|
88
99
|
errors,
|
|
89
100
|
submitting,
|
|
90
|
-
|
|
91
|
-
errMsg,
|
|
101
|
+
fields,
|
|
92
102
|
handleChange,
|
|
93
103
|
handleSubmit
|
|
94
104
|
};
|
package/package.json
CHANGED