@open-tender/utils 0.5.4 → 0.5.5

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 { FormFields, LoginData, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useSignInForm: (email: string, loading: RequestStatus, error: RequestError, login: (email: string, password: string) => void, callback?: () => void) => {
3
+ export declare const useSignInForm: (email: string, loading: RequestStatus, error: RequestError, login: (email: string, password?: string, recaptcha_token?: string) => void, recaptchaKey: string | null, 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: FormFields;
7
8
  data: LoginData;
8
9
  errors: Record<string, string>;
@@ -3,9 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useSignInForm = void 0;
4
4
  const react_1 = require("react");
5
5
  const utils_1 = require("../utils");
6
- const useSignInForm = (email, loading, error, login, callback) => {
6
+ const useSignInForm = (email, loading, error, login, recaptchaKey, 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)({ email, password: '' });
10
11
  const [errors, setErrors] = (0, react_1.useState)({});
11
12
  const [submitting, setSubmitting] = (0, react_1.useState)(false);
@@ -34,6 +35,8 @@ const useSignInForm = (email, loading, error, login, callback) => {
34
35
  if (loading === 'idle' && submitting) {
35
36
  setSubmitting(false);
36
37
  if (error) {
38
+ if (recaptchaRef.current)
39
+ recaptchaRef.current.reset();
37
40
  setErrors((0, utils_1.makeFormErrors)(error));
38
41
  }
39
42
  else if (callback) {
@@ -45,16 +48,40 @@ const useSignInForm = (email, loading, error, login, callback) => {
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
- setSubmitting(true);
51
53
  const { email, password } = data;
52
- login(email, password);
53
- ((_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
+ login(email, password, token);
65
+ setSubmitting(true);
66
+ }
67
+ }
68
+ catch (err) {
69
+ setSubmitting(false);
70
+ setErrors({
71
+ form: 'Please complete the recaptcha before submitting'
72
+ });
73
+ }
74
+ }
75
+ else {
76
+ login(email, password);
77
+ setSubmitting(true);
78
+ }
79
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
54
80
  };
55
81
  return {
56
82
  submitRef,
57
83
  inputRef,
84
+ recaptchaRef,
58
85
  fields,
59
86
  data,
60
87
  errors,
@@ -1,8 +1,9 @@
1
1
  /// <reference types="react" />
2
2
  import { FormFields, LoginData, RequestError, RequestStatus } from '@open-tender/types';
3
- export declare const useSignInForm: (email: string, loading: RequestStatus, error: RequestError, login: (email: string, password: string) => void, callback?: () => void) => {
3
+ export declare const useSignInForm: (email: string, loading: RequestStatus, error: RequestError, login: (email: string, password?: string, recaptcha_token?: string) => void, recaptchaKey: string | null, 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: FormFields;
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 useSignInForm = (email, loading, error, login, callback) => {
3
+ export const useSignInForm = (email, loading, error, login, recaptchaKey, callback) => {
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);
@@ -31,6 +32,8 @@ export const useSignInForm = (email, loading, error, login, callback) => {
31
32
  if (loading === 'idle' && submitting) {
32
33
  setSubmitting(false);
33
34
  if (error) {
35
+ if (recaptchaRef.current)
36
+ recaptchaRef.current.reset();
34
37
  setErrors(makeFormErrors(error));
35
38
  }
36
39
  else if (callback) {
@@ -42,16 +45,40 @@ export const useSignInForm = (email, loading, error, login, callback) => {
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
- setSubmitting(true);
48
50
  const { email, password } = data;
49
- login(email, password);
50
- ((_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
+ login(email, password, token);
62
+ setSubmitting(true);
63
+ }
64
+ }
65
+ catch (err) {
66
+ setSubmitting(false);
67
+ setErrors({
68
+ form: 'Please complete the recaptcha before submitting'
69
+ });
70
+ }
71
+ }
72
+ else {
73
+ login(email, password);
74
+ setSubmitting(true);
75
+ }
76
+ ((_b = submitRef.current) === null || _b === void 0 ? void 0 : _b.blur) && ((_c = submitRef.current) === null || _c === void 0 ? void 0 : _c.blur());
51
77
  };
52
78
  return {
53
79
  submitRef,
54
80
  inputRef,
81
+ recaptchaRef,
55
82
  fields,
56
83
  data,
57
84
  errors,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.5.4",
3
+ "version": "0.5.5",
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",