@open-tender/utils 0.1.144 → 0.1.146
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/useOneTimePasscodeForm.d.ts +4 -1
- package/dist/cjs/hooks/useOneTimePasscodeForm.js +22 -7
- package/dist/cjs/hooks/useSignUpGuestForm.js +0 -3
- package/dist/cjs/utils/helpers.d.ts +1 -0
- package/dist/cjs/utils/helpers.js +5 -1
- package/dist/esm/hooks/useOneTimePasscodeForm.d.ts +4 -1
- package/dist/esm/hooks/useOneTimePasscodeForm.js +23 -8
- package/dist/esm/hooks/useSignUpGuestForm.js +0 -3
- package/dist/esm/utils/helpers.d.ts +1 -0
- package/dist/esm/utils/helpers.js +3 -0
- package/package.json +1 -1
|
@@ -4,7 +4,10 @@ export declare const useOneTimePasscodeForm: (otpSent: boolean, loading: Request
|
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
6
|
fields: import("@open-tender/types").FormFieldType[];
|
|
7
|
-
data:
|
|
7
|
+
data: {
|
|
8
|
+
identifier: string;
|
|
9
|
+
one_time_passcode: string;
|
|
10
|
+
};
|
|
8
11
|
errors: Record<string, string>;
|
|
9
12
|
setErrors: import("react").Dispatch<import("react").SetStateAction<Record<string, string>>>;
|
|
10
13
|
submitting: boolean;
|
|
@@ -7,14 +7,19 @@ const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback) => {
|
|
|
7
7
|
const submitRef = (0, react_1.useRef)(null);
|
|
8
8
|
const inputRef = (0, react_1.useRef)(null);
|
|
9
9
|
const [data, setData] = (0, react_1.useState)({
|
|
10
|
-
|
|
10
|
+
identifier: '',
|
|
11
11
|
one_time_passcode: ''
|
|
12
12
|
});
|
|
13
13
|
const [errors, setErrors] = (0, react_1.useState)({});
|
|
14
14
|
const [submitting, setSubmitting] = (0, react_1.useState)(false);
|
|
15
|
-
const toRemove = otpSent ? ['
|
|
15
|
+
const toRemove = otpSent ? ['identifier'] : ['one_time_passcode'];
|
|
16
16
|
const allFields = [
|
|
17
|
-
{
|
|
17
|
+
{
|
|
18
|
+
label: 'Phone number or email address',
|
|
19
|
+
name: 'identifier',
|
|
20
|
+
type: 'text',
|
|
21
|
+
required: true
|
|
22
|
+
},
|
|
18
23
|
{
|
|
19
24
|
label: 'One Time Passcode',
|
|
20
25
|
name: 'one_time_passcode',
|
|
@@ -28,7 +33,12 @@ const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback) => {
|
|
|
28
33
|
if (loading === 'idle' && submitting) {
|
|
29
34
|
setSubmitting(false);
|
|
30
35
|
if (error) {
|
|
31
|
-
|
|
36
|
+
const errs = (0, utils_1.makeFormErrors)(error);
|
|
37
|
+
if (errs.form.includes('not found')) {
|
|
38
|
+
errs.form =
|
|
39
|
+
'Account not found. Please double check your phone or email and try again.';
|
|
40
|
+
}
|
|
41
|
+
setErrors(errs);
|
|
32
42
|
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
|
|
33
43
|
}
|
|
34
44
|
else if (callback && submitting && otpSent) {
|
|
@@ -41,15 +51,20 @@ const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback) => {
|
|
|
41
51
|
}
|
|
42
52
|
}, [loading, error, callback, submitting, otpSent]);
|
|
43
53
|
const handleChange = (name, value) => {
|
|
44
|
-
const inputValue = name === '
|
|
54
|
+
const inputValue = name === 'identifier' && typeof value === 'string' && (0, utils_1.isPhone)(value)
|
|
55
|
+
? (0, utils_1.makePhone)(value !== null && value !== void 0 ? value : '')
|
|
56
|
+
: value;
|
|
45
57
|
setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
|
|
46
58
|
};
|
|
47
59
|
const handleSubmit = (evt) => {
|
|
48
60
|
var _a, _b;
|
|
49
61
|
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
50
62
|
setSubmitting(true);
|
|
51
|
-
const {
|
|
52
|
-
|
|
63
|
+
const { identifier, one_time_passcode } = data;
|
|
64
|
+
const values = (0, utils_1.isPhone)(identifier)
|
|
65
|
+
? { phone: identifier }
|
|
66
|
+
: { email: identifier };
|
|
67
|
+
otpSent ? submit({ one_time_passcode }) : submit(values);
|
|
53
68
|
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
|
|
54
69
|
};
|
|
55
70
|
return {
|
|
@@ -10,6 +10,7 @@ export declare const serialize: (obj: Record<string | number, string | number |
|
|
|
10
10
|
export declare const capitalize: (s: string) => string;
|
|
11
11
|
export declare const cleanPhone: (phone: string) => string;
|
|
12
12
|
export declare const makePhone: (phone: string) => string;
|
|
13
|
+
export declare const isPhone: (value: string) => boolean;
|
|
13
14
|
export declare const validatePhone: (phone: string) => boolean;
|
|
14
15
|
export declare const makeBirthDate: (birthDate: string) => string | null;
|
|
15
16
|
export declare const slashesToDashes: (birthDate: string) => DateString | null;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasEntities = exports.convertStringToArray = exports.formatDollars = exports.formatQuantity = exports.addCommas = exports.displayPrice = exports.makeSlides = exports.makeImageUrl = exports.makeImageMap = exports.getHeight = exports.getWidth = exports.makeRandomNumberString = exports.checkNotFound = exports.checkGuestData = exports.checkEmpty = exports.validateEmail = exports.dashesToSlashes = exports.slashesToDashes = exports.makeBirthDate = exports.validatePhone = exports.makePhone = exports.cleanPhone = exports.capitalize = exports.serialize = exports.stripTags = exports.slugify = exports.contains = exports.isNum = exports.isEmpty = exports.isObject = exports.isString = void 0;
|
|
3
|
+
exports.hasEntities = exports.convertStringToArray = exports.formatDollars = exports.formatQuantity = exports.addCommas = exports.displayPrice = exports.makeSlides = exports.makeImageUrl = exports.makeImageMap = exports.getHeight = exports.getWidth = exports.makeRandomNumberString = exports.checkNotFound = exports.checkGuestData = exports.checkEmpty = exports.validateEmail = exports.dashesToSlashes = exports.slashesToDashes = exports.makeBirthDate = exports.validatePhone = exports.isPhone = exports.makePhone = exports.cleanPhone = exports.capitalize = exports.serialize = exports.stripTags = exports.slugify = exports.contains = exports.isNum = exports.isEmpty = exports.isObject = exports.isString = void 0;
|
|
4
4
|
const isString = (str) => {
|
|
5
5
|
return typeof str === 'string';
|
|
6
6
|
};
|
|
@@ -74,6 +74,10 @@ const makePhone = (phone) => {
|
|
|
74
74
|
return p;
|
|
75
75
|
};
|
|
76
76
|
exports.makePhone = makePhone;
|
|
77
|
+
const isPhone = (value) => {
|
|
78
|
+
return /^\d+$/.test(value.replace(/\D/g, ''));
|
|
79
|
+
};
|
|
80
|
+
exports.isPhone = isPhone;
|
|
77
81
|
const validatePhone = (phone) => {
|
|
78
82
|
var _a;
|
|
79
83
|
if (!phone)
|
|
@@ -4,7 +4,10 @@ export declare const useOneTimePasscodeForm: (otpSent: boolean, loading: Request
|
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
6
|
fields: import("@open-tender/types").FormFieldType[];
|
|
7
|
-
data:
|
|
7
|
+
data: {
|
|
8
|
+
identifier: string;
|
|
9
|
+
one_time_passcode: string;
|
|
10
|
+
};
|
|
8
11
|
errors: Record<string, string>;
|
|
9
12
|
setErrors: import("react").Dispatch<import("react").SetStateAction<Record<string, string>>>;
|
|
10
13
|
submitting: boolean;
|
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
import { useState, useEffect, useRef } from 'react';
|
|
2
|
-
import { makeFormErrors, makePhone } from '../utils';
|
|
2
|
+
import { isPhone, makeFormErrors, makePhone } from '../utils';
|
|
3
3
|
export const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback) => {
|
|
4
4
|
const submitRef = useRef(null);
|
|
5
5
|
const inputRef = useRef(null);
|
|
6
6
|
const [data, setData] = useState({
|
|
7
|
-
|
|
7
|
+
identifier: '',
|
|
8
8
|
one_time_passcode: ''
|
|
9
9
|
});
|
|
10
10
|
const [errors, setErrors] = useState({});
|
|
11
11
|
const [submitting, setSubmitting] = useState(false);
|
|
12
|
-
const toRemove = otpSent ? ['
|
|
12
|
+
const toRemove = otpSent ? ['identifier'] : ['one_time_passcode'];
|
|
13
13
|
const allFields = [
|
|
14
|
-
{
|
|
14
|
+
{
|
|
15
|
+
label: 'Phone number or email address',
|
|
16
|
+
name: 'identifier',
|
|
17
|
+
type: 'text',
|
|
18
|
+
required: true
|
|
19
|
+
},
|
|
15
20
|
{
|
|
16
21
|
label: 'One Time Passcode',
|
|
17
22
|
name: 'one_time_passcode',
|
|
@@ -25,7 +30,12 @@ export const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback
|
|
|
25
30
|
if (loading === 'idle' && submitting) {
|
|
26
31
|
setSubmitting(false);
|
|
27
32
|
if (error) {
|
|
28
|
-
|
|
33
|
+
const errs = makeFormErrors(error);
|
|
34
|
+
if (errs.form.includes('not found')) {
|
|
35
|
+
errs.form =
|
|
36
|
+
'Account not found. Please double check your phone or email and try again.';
|
|
37
|
+
}
|
|
38
|
+
setErrors(errs);
|
|
29
39
|
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
|
|
30
40
|
}
|
|
31
41
|
else if (callback && submitting && otpSent) {
|
|
@@ -38,15 +48,20 @@ export const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback
|
|
|
38
48
|
}
|
|
39
49
|
}, [loading, error, callback, submitting, otpSent]);
|
|
40
50
|
const handleChange = (name, value) => {
|
|
41
|
-
const inputValue = name === '
|
|
51
|
+
const inputValue = name === 'identifier' && typeof value === 'string' && isPhone(value)
|
|
52
|
+
? makePhone(value !== null && value !== void 0 ? value : '')
|
|
53
|
+
: value;
|
|
42
54
|
setData(Object.assign(Object.assign({}, data), { [name]: inputValue }));
|
|
43
55
|
};
|
|
44
56
|
const handleSubmit = (evt) => {
|
|
45
57
|
var _a, _b;
|
|
46
58
|
evt === null || evt === void 0 ? void 0 : evt.preventDefault();
|
|
47
59
|
setSubmitting(true);
|
|
48
|
-
const {
|
|
49
|
-
|
|
60
|
+
const { identifier, one_time_passcode } = data;
|
|
61
|
+
const values = isPhone(identifier)
|
|
62
|
+
? { phone: identifier }
|
|
63
|
+
: { email: identifier };
|
|
64
|
+
otpSent ? submit({ one_time_passcode }) : submit(values);
|
|
50
65
|
((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
|
|
51
66
|
};
|
|
52
67
|
return {
|
|
@@ -10,6 +10,7 @@ export declare const serialize: (obj: Record<string | number, string | number |
|
|
|
10
10
|
export declare const capitalize: (s: string) => string;
|
|
11
11
|
export declare const cleanPhone: (phone: string) => string;
|
|
12
12
|
export declare const makePhone: (phone: string) => string;
|
|
13
|
+
export declare const isPhone: (value: string) => boolean;
|
|
13
14
|
export declare const validatePhone: (phone: string) => boolean;
|
|
14
15
|
export declare const makeBirthDate: (birthDate: string) => string | null;
|
|
15
16
|
export declare const slashesToDashes: (birthDate: string) => DateString | null;
|
package/package.json
CHANGED