@open-tender/utils 0.5.1 → 0.5.2

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 { 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,
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.2",
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",