@open-tender/utils 0.1.140 → 0.1.142
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/useGuestCheckoutForm.d.ts +1 -1
- package/dist/cjs/hooks/useGuestCheckoutForm.js +5 -2
- package/dist/cjs/hooks/useSignUpGuestForm.d.ts +1 -1
- package/dist/cjs/hooks/useSignUpGuestForm.js +8 -2
- package/dist/cjs/utils/errors.d.ts +6 -0
- package/dist/cjs/utils/errors.js +27 -1
- package/dist/esm/hooks/useGuestCheckoutForm.d.ts +1 -1
- package/dist/esm/hooks/useGuestCheckoutForm.js +5 -2
- package/dist/esm/hooks/useSignUpGuestForm.d.ts +1 -1
- package/dist/esm/hooks/useSignUpGuestForm.js +8 -2
- package/dist/esm/utils/errors.d.ts +6 -0
- package/dist/esm/utils/errors.js +24 -0
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
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) => {
|
|
3
|
+
export declare const useGuestCheckoutForm: (guest: CheckoutFormCustomer, loading: RequestStatus, error: RequestError, guestErrors: Record<string, string> | null, submit: (data: CheckoutFormCustomer) => void) => {
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
6
|
fields: FormFields;
|
|
@@ -9,7 +9,7 @@ const initialData = {
|
|
|
9
9
|
email: '',
|
|
10
10
|
phone: ''
|
|
11
11
|
};
|
|
12
|
-
const useGuestCheckoutForm = (guest, loading, error, submit) => {
|
|
12
|
+
const useGuestCheckoutForm = (guest, loading, error, guestErrors, submit) => {
|
|
13
13
|
const submitRef = (0, react_1.useRef)(null);
|
|
14
14
|
const inputRef = (0, react_1.useRef)(null);
|
|
15
15
|
const initData = Object.assign(Object.assign({}, initialData), guest);
|
|
@@ -62,8 +62,11 @@ const useGuestCheckoutForm = (guest, loading, error, submit) => {
|
|
|
62
62
|
setErrors(errs);
|
|
63
63
|
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
|
|
64
64
|
}
|
|
65
|
+
else if (guestErrors) {
|
|
66
|
+
setErrors(guestErrors);
|
|
67
|
+
}
|
|
65
68
|
}
|
|
66
|
-
}, [loading, error]);
|
|
69
|
+
}, [loading, error, guestErrors]);
|
|
67
70
|
(0, react_1.useEffect)(() => {
|
|
68
71
|
return () => {
|
|
69
72
|
setData(initialData);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { CheckoutFormCustomer, CustomerCreate, FormFields, RequestError, RequestStatus } from '@open-tender/types';
|
|
3
|
-
export declare const useSignUpGuestForm: (email: string, guest: CheckoutFormCustomer, loading: RequestStatus, error: RequestError, signUp: (data: CustomerCreate) => void, submitGuest: (data: CheckoutFormCustomer) => void, hasThanx?: boolean) => {
|
|
3
|
+
export declare const useSignUpGuestForm: (email: string, guest: CheckoutFormCustomer, loading: RequestStatus, error: RequestError, guestErrors: Record<string, string> | null, signUp: (data: CustomerCreate) => void, submitGuest: (data: CheckoutFormCustomer) => void, hasThanx?: boolean) => {
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
6
|
fields: FormFields;
|
|
@@ -9,7 +9,7 @@ const initialData = {
|
|
|
9
9
|
phone: '',
|
|
10
10
|
password: ''
|
|
11
11
|
};
|
|
12
|
-
const useSignUpGuestForm = (email, guest, loading, error, signUp, submitGuest, hasThanx) => {
|
|
12
|
+
const useSignUpGuestForm = (email, guest, loading, error, guestErrors, signUp, submitGuest, hasThanx) => {
|
|
13
13
|
const submitRef = (0, react_1.useRef)(null);
|
|
14
14
|
const inputRef = (0, react_1.useRef)(null);
|
|
15
15
|
const initData = Object.assign(Object.assign(Object.assign({}, initialData), guest), { email });
|
|
@@ -84,8 +84,14 @@ const useSignUpGuestForm = (email, guest, loading, error, signUp, submitGuest, h
|
|
|
84
84
|
setErrors(errs);
|
|
85
85
|
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
|
|
86
86
|
}
|
|
87
|
+
else if (guestErrors) {
|
|
88
|
+
setErrors(guestErrors);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
setErrors({});
|
|
92
|
+
}
|
|
87
93
|
}
|
|
88
|
-
}, [loading, error]);
|
|
94
|
+
}, [loading, error, guestErrors]);
|
|
89
95
|
(0, react_1.useEffect)(() => {
|
|
90
96
|
return () => {
|
|
91
97
|
setData(Object.assign(Object.assign({}, initialData), { email: '' }));
|
|
@@ -9,6 +9,11 @@ export declare const handleRespError: (err: RequestError) => {
|
|
|
9
9
|
err: RequestError;
|
|
10
10
|
};
|
|
11
11
|
export declare const processError: (msg: string | null) => string;
|
|
12
|
+
interface RecordInterface {
|
|
13
|
+
[name: string]: string | RecordInterface;
|
|
14
|
+
}
|
|
15
|
+
export declare const handleFormErrorsNested: (params: Record<string, string | null>) => RecordInterface;
|
|
16
|
+
export declare const makeFormErrorsNested: (error: RequestError) => RecordInterface;
|
|
12
17
|
export declare const handleFormErrors: (params: Record<string, string | null>) => Record<string, string>;
|
|
13
18
|
export declare const makeFormErrors: (error: RequestError) => Record<string, string>;
|
|
14
19
|
export declare const handleCheckoutError: (error: CheckoutError) => CheckoutErrorMessages;
|
|
@@ -47,3 +52,4 @@ export declare const errMessages: {
|
|
|
47
52
|
distanceSurcharge: string;
|
|
48
53
|
missingAddressDeliveryZone: string;
|
|
49
54
|
};
|
|
55
|
+
export {};
|
package/dist/cjs/utils/errors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.errMessages = exports.makeCheckError = exports.handleCheckoutError = exports.makeFormErrors = exports.handleFormErrors = exports.processError = exports.handleRespError = void 0;
|
|
3
|
+
exports.errMessages = exports.makeCheckError = exports.handleCheckoutError = exports.makeFormErrors = exports.handleFormErrors = exports.makeFormErrorsNested = exports.handleFormErrorsNested = exports.processError = exports.handleRespError = void 0;
|
|
4
4
|
const helpers_1 = require("./helpers");
|
|
5
5
|
const handleRespError = (err) => {
|
|
6
6
|
const error = typeof err === 'string' ? { detail: err } : err;
|
|
@@ -55,6 +55,32 @@ const processError = (msg) => {
|
|
|
55
55
|
return msg;
|
|
56
56
|
};
|
|
57
57
|
exports.processError = processError;
|
|
58
|
+
const makeErrObj = (keys, msg) => {
|
|
59
|
+
const obj = keys.reduceRight((msg, key) => ({ [key]: msg }), msg);
|
|
60
|
+
return typeof obj === 'string' ? null : obj;
|
|
61
|
+
};
|
|
62
|
+
const handleFormErrorsNested = (params) => {
|
|
63
|
+
return Object.entries(params).reduce((obj, error) => {
|
|
64
|
+
const [field, msg] = error;
|
|
65
|
+
const message = (0, exports.processError)(msg);
|
|
66
|
+
const keys = field.replace('$.', '').split('.');
|
|
67
|
+
const errObj = makeErrObj(keys, message);
|
|
68
|
+
return errObj ? Object.assign(Object.assign({}, obj), errObj) : obj;
|
|
69
|
+
}, {});
|
|
70
|
+
};
|
|
71
|
+
exports.handleFormErrorsNested = handleFormErrorsNested;
|
|
72
|
+
const makeFormErrorsNested = (error) => {
|
|
73
|
+
let errors = {};
|
|
74
|
+
if (error === null || error === void 0 ? void 0 : error.params) {
|
|
75
|
+
errors = (0, exports.handleFormErrorsNested)(error.params);
|
|
76
|
+
errors.form = error.detail;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
errors.form = (error === null || error === void 0 ? void 0 : error.detail) || (error === null || error === void 0 ? void 0 : error.message) || '';
|
|
80
|
+
}
|
|
81
|
+
return errors;
|
|
82
|
+
};
|
|
83
|
+
exports.makeFormErrorsNested = makeFormErrorsNested;
|
|
58
84
|
const handleFormErrors = (params) => {
|
|
59
85
|
return Object.entries(params).reduce((obj, error) => {
|
|
60
86
|
const [field, msg] = error;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
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) => {
|
|
3
|
+
export declare const useGuestCheckoutForm: (guest: CheckoutFormCustomer, loading: RequestStatus, error: RequestError, guestErrors: Record<string, string> | null, submit: (data: CheckoutFormCustomer) => void) => {
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
6
|
fields: FormFields;
|
|
@@ -6,7 +6,7 @@ const initialData = {
|
|
|
6
6
|
email: '',
|
|
7
7
|
phone: ''
|
|
8
8
|
};
|
|
9
|
-
export const useGuestCheckoutForm = (guest, loading, error, submit) => {
|
|
9
|
+
export const useGuestCheckoutForm = (guest, loading, error, guestErrors, submit) => {
|
|
10
10
|
const submitRef = useRef(null);
|
|
11
11
|
const inputRef = useRef(null);
|
|
12
12
|
const initData = Object.assign(Object.assign({}, initialData), guest);
|
|
@@ -59,8 +59,11 @@ export const useGuestCheckoutForm = (guest, loading, error, submit) => {
|
|
|
59
59
|
setErrors(errs);
|
|
60
60
|
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
|
|
61
61
|
}
|
|
62
|
+
else if (guestErrors) {
|
|
63
|
+
setErrors(guestErrors);
|
|
64
|
+
}
|
|
62
65
|
}
|
|
63
|
-
}, [loading, error]);
|
|
66
|
+
}, [loading, error, guestErrors]);
|
|
64
67
|
useEffect(() => {
|
|
65
68
|
return () => {
|
|
66
69
|
setData(initialData);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { CheckoutFormCustomer, CustomerCreate, FormFields, RequestError, RequestStatus } from '@open-tender/types';
|
|
3
|
-
export declare const useSignUpGuestForm: (email: string, guest: CheckoutFormCustomer, loading: RequestStatus, error: RequestError, signUp: (data: CustomerCreate) => void, submitGuest: (data: CheckoutFormCustomer) => void, hasThanx?: boolean) => {
|
|
3
|
+
export declare const useSignUpGuestForm: (email: string, guest: CheckoutFormCustomer, loading: RequestStatus, error: RequestError, guestErrors: Record<string, string> | null, signUp: (data: CustomerCreate) => void, submitGuest: (data: CheckoutFormCustomer) => void, hasThanx?: boolean) => {
|
|
4
4
|
submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
|
|
5
5
|
inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
|
|
6
6
|
fields: FormFields;
|
|
@@ -6,7 +6,7 @@ const initialData = {
|
|
|
6
6
|
phone: '',
|
|
7
7
|
password: ''
|
|
8
8
|
};
|
|
9
|
-
export const useSignUpGuestForm = (email, guest, loading, error, signUp, submitGuest, hasThanx) => {
|
|
9
|
+
export const useSignUpGuestForm = (email, guest, loading, error, guestErrors, signUp, submitGuest, hasThanx) => {
|
|
10
10
|
const submitRef = useRef(null);
|
|
11
11
|
const inputRef = useRef(null);
|
|
12
12
|
const initData = Object.assign(Object.assign(Object.assign({}, initialData), guest), { email });
|
|
@@ -81,8 +81,14 @@ export const useSignUpGuestForm = (email, guest, loading, error, signUp, submitG
|
|
|
81
81
|
setErrors(errs);
|
|
82
82
|
((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
|
|
83
83
|
}
|
|
84
|
+
else if (guestErrors) {
|
|
85
|
+
setErrors(guestErrors);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
setErrors({});
|
|
89
|
+
}
|
|
84
90
|
}
|
|
85
|
-
}, [loading, error]);
|
|
91
|
+
}, [loading, error, guestErrors]);
|
|
86
92
|
useEffect(() => {
|
|
87
93
|
return () => {
|
|
88
94
|
setData(Object.assign(Object.assign({}, initialData), { email: '' }));
|
|
@@ -9,6 +9,11 @@ export declare const handleRespError: (err: RequestError) => {
|
|
|
9
9
|
err: RequestError;
|
|
10
10
|
};
|
|
11
11
|
export declare const processError: (msg: string | null) => string;
|
|
12
|
+
interface RecordInterface {
|
|
13
|
+
[name: string]: string | RecordInterface;
|
|
14
|
+
}
|
|
15
|
+
export declare const handleFormErrorsNested: (params: Record<string, string | null>) => RecordInterface;
|
|
16
|
+
export declare const makeFormErrorsNested: (error: RequestError) => RecordInterface;
|
|
12
17
|
export declare const handleFormErrors: (params: Record<string, string | null>) => Record<string, string>;
|
|
13
18
|
export declare const makeFormErrors: (error: RequestError) => Record<string, string>;
|
|
14
19
|
export declare const handleCheckoutError: (error: CheckoutError) => CheckoutErrorMessages;
|
|
@@ -47,3 +52,4 @@ export declare const errMessages: {
|
|
|
47
52
|
distanceSurcharge: string;
|
|
48
53
|
missingAddressDeliveryZone: string;
|
|
49
54
|
};
|
|
55
|
+
export {};
|
package/dist/esm/utils/errors.js
CHANGED
|
@@ -50,6 +50,30 @@ export const processError = (msg) => {
|
|
|
50
50
|
}
|
|
51
51
|
return msg;
|
|
52
52
|
};
|
|
53
|
+
const makeErrObj = (keys, msg) => {
|
|
54
|
+
const obj = keys.reduceRight((msg, key) => ({ [key]: msg }), msg);
|
|
55
|
+
return typeof obj === 'string' ? null : obj;
|
|
56
|
+
};
|
|
57
|
+
export const handleFormErrorsNested = (params) => {
|
|
58
|
+
return Object.entries(params).reduce((obj, error) => {
|
|
59
|
+
const [field, msg] = error;
|
|
60
|
+
const message = processError(msg);
|
|
61
|
+
const keys = field.replace('$.', '').split('.');
|
|
62
|
+
const errObj = makeErrObj(keys, message);
|
|
63
|
+
return errObj ? Object.assign(Object.assign({}, obj), errObj) : obj;
|
|
64
|
+
}, {});
|
|
65
|
+
};
|
|
66
|
+
export const makeFormErrorsNested = (error) => {
|
|
67
|
+
let errors = {};
|
|
68
|
+
if (error === null || error === void 0 ? void 0 : error.params) {
|
|
69
|
+
errors = handleFormErrorsNested(error.params);
|
|
70
|
+
errors.form = error.detail;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
errors.form = (error === null || error === void 0 ? void 0 : error.detail) || (error === null || error === void 0 ? void 0 : error.message) || '';
|
|
74
|
+
}
|
|
75
|
+
return errors;
|
|
76
|
+
};
|
|
53
77
|
export const handleFormErrors = (params) => {
|
|
54
78
|
return Object.entries(params).reduce((obj, error) => {
|
|
55
79
|
const [field, msg] = error;
|
package/package.json
CHANGED