@open-tender/utils 0.5.2 → 0.5.4

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 { 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,45 @@ 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 (recaptchaKey) {
75
+ try {
76
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
77
+ if (!token) {
78
+ setSubmitting(false);
79
+ setErrors({
80
+ form: 'Please complete the recaptcha before submitting'
81
+ });
82
+ }
83
+ else {
84
+ otpSent
85
+ ? submit({ one_time_passcode, recaptcha_token: token })
86
+ : submit(Object.assign(Object.assign({}, values), { recaptcha_token: token }));
87
+ setSubmitting(true);
88
+ }
89
+ }
90
+ catch (err) {
91
+ setSubmitting(false);
92
+ setErrors({
93
+ form: 'Please complete the recaptcha before submitting'
94
+ });
95
+ }
96
+ }
97
+ else {
98
+ otpSent ? submit({ one_time_passcode }) : submit(values);
99
+ setSubmitting(true);
100
+ }
101
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
74
102
  };
75
103
  return {
76
104
  submitRef,
77
105
  inputRef,
106
+ recaptchaRef,
78
107
  fields,
79
108
  data,
80
109
  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,45 @@ 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 (recaptchaKey) {
72
+ try {
73
+ const token = (_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.getValue();
74
+ if (!token) {
75
+ setSubmitting(false);
76
+ setErrors({
77
+ form: 'Please complete the recaptcha before submitting'
78
+ });
79
+ }
80
+ else {
81
+ otpSent
82
+ ? submit({ one_time_passcode, recaptcha_token: token })
83
+ : submit(Object.assign(Object.assign({}, values), { recaptcha_token: token }));
84
+ setSubmitting(true);
85
+ }
86
+ }
87
+ catch (err) {
88
+ setSubmitting(false);
89
+ setErrors({
90
+ form: 'Please complete the recaptcha before submitting'
91
+ });
92
+ }
93
+ }
94
+ else {
95
+ otpSent ? submit({ one_time_passcode }) : submit(values);
96
+ setSubmitting(true);
97
+ }
98
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
71
99
  };
72
100
  return {
73
101
  submitRef,
74
102
  inputRef,
103
+ recaptchaRef,
75
104
  fields,
76
105
  data,
77
106
  errors,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
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",