@open-tender/utils 0.5.1 → 0.5.3

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,8 +1,9 @@
1
1
  /// <reference types="react" />
2
2
  import { LoginData, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useLoginForm: (loading: RequestStatus, error: RequestError, login: (email: string, password?: string) => void, callback?: () => void, showPassword?: boolean) => {
3
+ export declare const useLoginForm: (loading: RequestStatus, error: RequestError, login: (email: string, password?: string, recaptcha_token?: string) => void, recaptchaKey: string | null, callback?: () => void, showPassword?: boolean) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ recaptchaRef: import("react").MutableRefObject<any>;
6
7
  fields: import("@open-tender/types").FormFieldType[];
7
8
  data: LoginData;
8
9
  errors: Record<string, string>;
@@ -3,9 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useLoginForm = void 0;
4
4
  const react_1 = require("react");
5
5
  const utils_1 = require("../utils");
6
- const useLoginForm = (loading, error, login, callback, showPassword = true) => {
6
+ const useLoginForm = (loading, error, login, recaptchaKey, callback, showPassword = true) => {
7
7
  const submitRef = (0, react_1.useRef)(null);
8
8
  const inputRef = (0, react_1.useRef)(null);
9
+ const recaptchaRef = (0, react_1.useRef)(null);
9
10
  const [data, setData] = (0, react_1.useState)({ email: '', password: '' });
10
11
  const [errors, setErrors] = (0, react_1.useState)({});
11
12
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
@@ -33,6 +34,8 @@ const useLoginForm = (loading, error, login, callback, showPassword = true) => {
33
34
  if (loading === 'idle' && submitting) {
34
35
  setSubmitting(false);
35
36
  if (error) {
37
+ if (recaptchaRef.current)
38
+ recaptchaRef.current.reset();
36
39
  setErrors((0, utils_1.makeFormErrors)(error));
37
40
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
38
41
  }
@@ -45,17 +48,42 @@ const useLoginForm = (loading, error, login, callback, showPassword = true) => {
45
48
  setData(Object.assign(Object.assign({}, data), { [name]: value }));
46
49
  };
47
50
  const handleSubmit = (evt) => {
48
- var _a, _b;
51
+ var _a, _b, _c;
49
52
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
50
53
  const { email, password } = data;
51
- if (email)
52
- login(email, password);
53
- setSubmitting(true);
54
- ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
54
+ if (recaptchaKey) {
55
+ try {
56
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
57
+ if (!token) {
58
+ setSubmitting(false);
59
+ setErrors({
60
+ form: 'Please complete the recaptcha before submitting'
61
+ });
62
+ }
63
+ else {
64
+ if (email)
65
+ login(email, password, token);
66
+ setSubmitting(true);
67
+ }
68
+ }
69
+ catch (err) {
70
+ setSubmitting(false);
71
+ setErrors({
72
+ form: 'Please complete the recaptcha before submitting'
73
+ });
74
+ }
75
+ }
76
+ else {
77
+ if (email)
78
+ login(email, password);
79
+ setSubmitting(true);
80
+ }
81
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
55
82
  };
56
83
  return {
57
84
  submitRef,
58
85
  inputRef,
86
+ recaptchaRef,
59
87
  fields,
60
88
  data,
61
89
  errors,
@@ -1,8 +1,9 @@
1
1
  /// <reference types="react" />
2
2
  import { CustomerSignIn, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useOneTimePasscodeForm: (otpSent: boolean, loading: RequestStatus, error: RequestError, submit: (data: CustomerSignIn) => void, callback?: () => void) => {
3
+ export declare const useOneTimePasscodeForm: (otpSent: boolean, loading: RequestStatus, error: RequestError, recaptchaKey: string | null, submit: (data: CustomerSignIn) => void, callback?: () => void) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ recaptchaRef: import("react").MutableRefObject<any>;
6
7
  fields: import("@open-tender/types").FormFieldType[];
7
8
  data: {
8
9
  identifier: string;
@@ -3,9 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useOneTimePasscodeForm = void 0;
4
4
  const react_1 = require("react");
5
5
  const utils_1 = require("../utils");
6
- const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback) => {
6
+ const useOneTimePasscodeForm = (otpSent, loading, error, recaptchaKey, submit, callback) => {
7
7
  const submitRef = (0, react_1.useRef)(null);
8
8
  const inputRef = (0, react_1.useRef)(null);
9
+ const recaptchaRef = (0, react_1.useRef)(null);
9
10
  const [data, setData] = (0, react_1.useState)({
10
11
  identifier: '',
11
12
  one_time_passcode: ''
@@ -33,6 +34,8 @@ const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback) => {
33
34
  if (loading === 'idle' && submitting) {
34
35
  setSubmitting(false);
35
36
  if (error) {
37
+ if (recaptchaRef.current)
38
+ recaptchaRef.current.reset();
36
39
  const errs = (0, utils_1.makeFormErrors)(error);
37
40
  if (errs.form.includes('not found')) {
38
41
  errs.form =
@@ -62,19 +65,49 @@ const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback) => {
62
65
  }
63
66
  };
64
67
  const handleSubmit = (evt) => {
65
- var _a, _b;
68
+ var _a, _b, _c;
66
69
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
67
- setSubmitting(true);
68
70
  const { identifier, one_time_passcode } = data;
69
71
  const values = /[a-zA-Z]/g.test(identifier)
70
72
  ? { email: identifier }
71
73
  : { phone: identifier };
72
- otpSent ? submit({ one_time_passcode }) : submit(values);
73
- ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
74
+ if (!otpSent) {
75
+ if (recaptchaKey) {
76
+ try {
77
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
78
+ if (!token) {
79
+ setSubmitting(false);
80
+ setErrors({
81
+ form: 'Please complete the recaptcha before submitting'
82
+ });
83
+ }
84
+ else {
85
+ submit(Object.assign(Object.assign({}, values), { recaptcha_token: token }));
86
+ setSubmitting(true);
87
+ }
88
+ }
89
+ catch (err) {
90
+ setSubmitting(false);
91
+ setErrors({
92
+ form: 'Please complete the recaptcha before submitting'
93
+ });
94
+ }
95
+ }
96
+ else {
97
+ submit(values);
98
+ setSubmitting(true);
99
+ }
100
+ }
101
+ else {
102
+ submit({ one_time_passcode });
103
+ setSubmitting(true);
104
+ }
105
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
74
106
  };
75
107
  return {
76
108
  submitRef,
77
109
  inputRef,
110
+ recaptchaRef,
78
111
  fields,
79
112
  data,
80
113
  errors,
@@ -1,8 +1,9 @@
1
1
  /// <reference types="react" />
2
2
  import { LoginData, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useLoginForm: (loading: RequestStatus, error: RequestError, login: (email: string, password?: string) => void, callback?: () => void, showPassword?: boolean) => {
3
+ export declare const useLoginForm: (loading: RequestStatus, error: RequestError, login: (email: string, password?: string, recaptcha_token?: string) => void, recaptchaKey: string | null, callback?: () => void, showPassword?: boolean) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ recaptchaRef: import("react").MutableRefObject<any>;
6
7
  fields: import("@open-tender/types").FormFieldType[];
7
8
  data: LoginData;
8
9
  errors: Record<string, string>;
@@ -1,8 +1,9 @@
1
1
  import { useState, useEffect, useRef } from 'react';
2
2
  import { makeFormErrors } from '../utils';
3
- export const useLoginForm = (loading, error, login, callback, showPassword = true) => {
3
+ export const useLoginForm = (loading, error, login, recaptchaKey, callback, showPassword = true) => {
4
4
  const submitRef = useRef(null);
5
5
  const inputRef = useRef(null);
6
+ const recaptchaRef = useRef(null);
6
7
  const [data, setData] = useState({ email: '', password: '' });
7
8
  const [errors, setErrors] = useState({});
8
9
  const [submitting, setSubmitting] = useState(false);
@@ -30,6 +31,8 @@ export const useLoginForm = (loading, error, login, callback, showPassword = tru
30
31
  if (loading === 'idle' && submitting) {
31
32
  setSubmitting(false);
32
33
  if (error) {
34
+ if (recaptchaRef.current)
35
+ recaptchaRef.current.reset();
33
36
  setErrors(makeFormErrors(error));
34
37
  ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.focus) && ((_b = inputRef.current) === null || _b === void 0 ? void 0 : _b.focus());
35
38
  }
@@ -42,17 +45,42 @@ export const useLoginForm = (loading, error, login, callback, showPassword = tru
42
45
  setData(Object.assign(Object.assign({}, data), { [name]: value }));
43
46
  };
44
47
  const handleSubmit = (evt) => {
45
- var _a, _b;
48
+ var _a, _b, _c;
46
49
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
47
50
  const { email, password } = data;
48
- if (email)
49
- login(email, password);
50
- setSubmitting(true);
51
- ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
51
+ if (recaptchaKey) {
52
+ try {
53
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
54
+ if (!token) {
55
+ setSubmitting(false);
56
+ setErrors({
57
+ form: 'Please complete the recaptcha before submitting'
58
+ });
59
+ }
60
+ else {
61
+ if (email)
62
+ login(email, password, token);
63
+ setSubmitting(true);
64
+ }
65
+ }
66
+ catch (err) {
67
+ setSubmitting(false);
68
+ setErrors({
69
+ form: 'Please complete the recaptcha before submitting'
70
+ });
71
+ }
72
+ }
73
+ else {
74
+ if (email)
75
+ login(email, password);
76
+ setSubmitting(true);
77
+ }
78
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
52
79
  };
53
80
  return {
54
81
  submitRef,
55
82
  inputRef,
83
+ recaptchaRef,
56
84
  fields,
57
85
  data,
58
86
  errors,
@@ -1,8 +1,9 @@
1
1
  /// <reference types="react" />
2
2
  import { CustomerSignIn, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useOneTimePasscodeForm: (otpSent: boolean, loading: RequestStatus, error: RequestError, submit: (data: CustomerSignIn) => void, callback?: () => void) => {
3
+ export declare const useOneTimePasscodeForm: (otpSent: boolean, loading: RequestStatus, error: RequestError, recaptchaKey: string | null, submit: (data: CustomerSignIn) => void, callback?: () => void) => {
4
4
  submitRef: import("react").MutableRefObject<HTMLButtonElement | null>;
5
5
  inputRef: import("react").MutableRefObject<HTMLInputElement | null>;
6
+ recaptchaRef: import("react").MutableRefObject<any>;
6
7
  fields: import("@open-tender/types").FormFieldType[];
7
8
  data: {
8
9
  identifier: string;
@@ -1,8 +1,9 @@
1
1
  import { useState, useEffect, useRef } from 'react';
2
2
  import { makeFormErrors, makePhone } from '../utils';
3
- export const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback) => {
3
+ export const useOneTimePasscodeForm = (otpSent, loading, error, recaptchaKey, submit, callback) => {
4
4
  const submitRef = useRef(null);
5
5
  const inputRef = useRef(null);
6
+ const recaptchaRef = useRef(null);
6
7
  const [data, setData] = useState({
7
8
  identifier: '',
8
9
  one_time_passcode: ''
@@ -30,6 +31,8 @@ export const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback
30
31
  if (loading === 'idle' && submitting) {
31
32
  setSubmitting(false);
32
33
  if (error) {
34
+ if (recaptchaRef.current)
35
+ recaptchaRef.current.reset();
33
36
  const errs = makeFormErrors(error);
34
37
  if (errs.form.includes('not found')) {
35
38
  errs.form =
@@ -59,19 +62,49 @@ export const useOneTimePasscodeForm = (otpSent, loading, error, submit, callback
59
62
  }
60
63
  };
61
64
  const handleSubmit = (evt) => {
62
- var _a, _b;
65
+ var _a, _b, _c;
63
66
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
64
- setSubmitting(true);
65
67
  const { identifier, one_time_passcode } = data;
66
68
  const values = /[a-zA-Z]/g.test(identifier)
67
69
  ? { email: identifier }
68
70
  : { phone: identifier };
69
- otpSent ? submit({ one_time_passcode }) : submit(values);
70
- ((_a = submitRef.current) === null || _a === void 0 ? void 0 : _a.blur) && ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur());
71
+ if (!otpSent) {
72
+ if (recaptchaKey) {
73
+ try {
74
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
75
+ if (!token) {
76
+ setSubmitting(false);
77
+ setErrors({
78
+ form: 'Please complete the recaptcha before submitting'
79
+ });
80
+ }
81
+ else {
82
+ submit(Object.assign(Object.assign({}, values), { recaptcha_token: token }));
83
+ setSubmitting(true);
84
+ }
85
+ }
86
+ catch (err) {
87
+ setSubmitting(false);
88
+ setErrors({
89
+ form: 'Please complete the recaptcha before submitting'
90
+ });
91
+ }
92
+ }
93
+ else {
94
+ submit(values);
95
+ setSubmitting(true);
96
+ }
97
+ }
98
+ else {
99
+ submit({ one_time_passcode });
100
+ setSubmitting(true);
101
+ }
102
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
71
103
  };
72
104
  return {
73
105
  submitRef,
74
106
  inputRef,
107
+ recaptchaRef,
75
108
  fields,
76
109
  data,
77
110
  errors,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "A library of utils for use with Open Tender applications that utilize our cloud-based Order API.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -36,7 +36,7 @@
36
36
  "private": false,
37
37
  "devDependencies": {
38
38
  "@emotion/react": "^11.10.4",
39
- "@open-tender/types": "^0.4.130",
39
+ "@open-tender/types": "^0.4.134",
40
40
  "@reduxjs/toolkit": "^1.8.5",
41
41
  "@types/google.maps": "^3.53.5",
42
42
  "@types/jest": "^29.5.11",