@open-tender/utils 0.5.24 → 0.5.26

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.
@@ -27,7 +27,7 @@ const makePayload = (fields, data) => {
27
27
  const useProfileForm = (profile, loading, error, update, callback, optionalFields) => {
28
28
  const submitRef = (0, react_1.useRef)(null);
29
29
  const [data, setData] = (0, react_1.useState)(profile);
30
- const [submitting, setSubmitting] = (0, react_1.useState)(false);
30
+ const isSubmitting = (0, react_1.useRef)(false);
31
31
  const [errors, setErrors] = (0, react_1.useState)({});
32
32
  const toRemove = (0, react_1.useMemo)(() => {
33
33
  const removed = [];
@@ -41,7 +41,7 @@ const useProfileForm = (profile, loading, error, update, callback, optionalField
41
41
  }, [toRemove]);
42
42
  (0, react_1.useEffect)(() => {
43
43
  if (loading === 'idle') {
44
- setSubmitting(false);
44
+ isSubmitting.current = false;
45
45
  if (error) {
46
46
  const errs = (0, utils_1.makeFormErrors)(error);
47
47
  if (errs.birth_date) {
@@ -50,13 +50,13 @@ const useProfileForm = (profile, loading, error, update, callback, optionalField
50
50
  }
51
51
  setErrors(errs);
52
52
  }
53
- else if (callback && submitting) {
53
+ else if (callback && isSubmitting.current) {
54
54
  const payload = makePayload(fields, data);
55
55
  if (payload)
56
56
  callback(payload);
57
57
  }
58
58
  }
59
- }, [loading, error, callback, submitting, fields, data]);
59
+ }, [loading, error, callback, fields, data]);
60
60
  (0, react_1.useEffect)(() => {
61
61
  if (profile)
62
62
  setData(profile);
@@ -79,7 +79,7 @@ const useProfileForm = (profile, loading, error, update, callback, optionalField
79
79
  if (!data)
80
80
  return;
81
81
  setErrors({});
82
- setSubmitting(true);
82
+ isSubmitting.current = true;
83
83
  const payload = makePayload(fields, data);
84
84
  if (payload)
85
85
  update(payload);
@@ -91,7 +91,7 @@ const useProfileForm = (profile, loading, error, update, callback, optionalField
91
91
  // order_notifications,
92
92
  data,
93
93
  errors,
94
- submitting,
94
+ submitting: isSubmitting.current,
95
95
  fields,
96
96
  handleChange,
97
97
  // handleRadio,
@@ -2,15 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useSignInForm = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const react_1 = require("react");
6
5
  const utils_1 = require("../utils");
6
+ const react_1 = require("react");
7
7
  const useSignInForm = (email, loading, error, login, includeRecaptcha, callback) => {
8
8
  const submitRef = (0, react_1.useRef)(null);
9
9
  const inputRef = (0, react_1.useRef)(null);
10
10
  const recaptchaRef = (0, react_1.useRef)(null);
11
11
  const [data, setData] = (0, react_1.useState)({ email, password: '' });
12
12
  const [errors, setErrors] = (0, react_1.useState)({});
13
- const [submitting, setSubmitting] = (0, react_1.useState)(false);
13
+ const isSubmitting = (0, react_1.useRef)(false);
14
14
  const fields = [
15
15
  {
16
16
  label: 'Email',
@@ -28,13 +28,13 @@ const useSignInForm = (email, loading, error, login, includeRecaptcha, callback)
28
28
  }
29
29
  ];
30
30
  (0, react_1.useEffect)(() => {
31
- if (!data.email && email) {
32
- setData({ email, password: data.password });
31
+ if (email) {
32
+ setData(d => (Object.assign(Object.assign({}, d), { email })));
33
33
  }
34
- }, [email, data.email, data.password]);
34
+ }, [email]);
35
35
  (0, react_1.useEffect)(() => {
36
- if (loading === 'idle' && submitting) {
37
- setSubmitting(false);
36
+ if (loading === 'idle' && isSubmitting.current) {
37
+ isSubmitting.current = false;
38
38
  if (error) {
39
39
  if (recaptchaRef.current)
40
40
  recaptchaRef.current.reset();
@@ -44,14 +44,14 @@ const useSignInForm = (email, loading, error, login, includeRecaptcha, callback)
44
44
  callback();
45
45
  }
46
46
  }
47
- }, [loading, error, callback, submitting]);
47
+ }, [loading, error, callback]);
48
48
  const handleChange = (name, value) => {
49
49
  setData(Object.assign(Object.assign({}, data), { [name]: value }));
50
50
  };
51
51
  const handleSubmit = (evt) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
52
52
  var _a, _b, _c, _d, _e;
53
53
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
54
- const { email, password } = data;
54
+ const { password } = data;
55
55
  if (includeRecaptcha) {
56
56
  try {
57
57
  const isEnterpriseRecaptcha = ((_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.props.size) === 'invisible';
@@ -59,26 +59,26 @@ const useSignInForm = (email, loading, error, login, includeRecaptcha, callback)
59
59
  ? yield ((_b = recaptchaRef.current) === null || _b === void 0 ? void 0 : _b.executeAsync())
60
60
  : (_c = recaptchaRef.current) === null || _c === void 0 ? void 0 : _c.getValue();
61
61
  if (!token) {
62
- setSubmitting(false);
62
+ isSubmitting.current = false;
63
63
  setErrors({
64
64
  form: 'Please complete the recaptcha before submitting'
65
65
  });
66
66
  }
67
67
  else {
68
- login(email, password, token);
69
- setSubmitting(true);
68
+ login(data.email || email, password, token);
69
+ isSubmitting.current = true;
70
70
  }
71
71
  }
72
72
  catch (err) {
73
- setSubmitting(false);
73
+ isSubmitting.current = false;
74
74
  setErrors({
75
75
  form: 'Please complete the recaptcha before submitting'
76
76
  });
77
77
  }
78
78
  }
79
79
  else {
80
- login(email, password);
81
- setSubmitting(true);
80
+ login(data.email || email, password);
81
+ isSubmitting.current = true;
82
82
  }
83
83
  ((_d = submitRef.current) === null || _d === void 0 ? void 0 : _d.blur) && ((_e = submitRef.current) === null || _e === void 0 ? void 0 : _e.blur());
84
84
  });
@@ -89,7 +89,7 @@ const useSignInForm = (email, loading, error, login, includeRecaptcha, callback)
89
89
  fields,
90
90
  data,
91
91
  errors,
92
- submitting,
92
+ submitting: isSubmitting.current,
93
93
  handleChange,
94
94
  handleSubmit
95
95
  };
@@ -24,7 +24,7 @@ const makePayload = (fields, data) => {
24
24
  export const useProfileForm = (profile, loading, error, update, callback, optionalFields) => {
25
25
  const submitRef = useRef(null);
26
26
  const [data, setData] = useState(profile);
27
- const [submitting, setSubmitting] = useState(false);
27
+ const isSubmitting = useRef(false);
28
28
  const [errors, setErrors] = useState({});
29
29
  const toRemove = useMemo(() => {
30
30
  const removed = [];
@@ -38,7 +38,7 @@ export const useProfileForm = (profile, loading, error, update, callback, option
38
38
  }, [toRemove]);
39
39
  useEffect(() => {
40
40
  if (loading === 'idle') {
41
- setSubmitting(false);
41
+ isSubmitting.current = false;
42
42
  if (error) {
43
43
  const errs = makeFormErrors(error);
44
44
  if (errs.birth_date) {
@@ -47,13 +47,13 @@ export const useProfileForm = (profile, loading, error, update, callback, option
47
47
  }
48
48
  setErrors(errs);
49
49
  }
50
- else if (callback && submitting) {
50
+ else if (callback && isSubmitting.current) {
51
51
  const payload = makePayload(fields, data);
52
52
  if (payload)
53
53
  callback(payload);
54
54
  }
55
55
  }
56
- }, [loading, error, callback, submitting, fields, data]);
56
+ }, [loading, error, callback, fields, data]);
57
57
  useEffect(() => {
58
58
  if (profile)
59
59
  setData(profile);
@@ -76,7 +76,7 @@ export const useProfileForm = (profile, loading, error, update, callback, option
76
76
  if (!data)
77
77
  return;
78
78
  setErrors({});
79
- setSubmitting(true);
79
+ isSubmitting.current = true;
80
80
  const payload = makePayload(fields, data);
81
81
  if (payload)
82
82
  update(payload);
@@ -88,7 +88,7 @@ export const useProfileForm = (profile, loading, error, update, callback, option
88
88
  // order_notifications,
89
89
  data,
90
90
  errors,
91
- submitting,
91
+ submitting: isSubmitting.current,
92
92
  fields,
93
93
  handleChange,
94
94
  // handleRadio,
@@ -1,13 +1,13 @@
1
1
  import { __awaiter } from "tslib";
2
- import { useState, useEffect, useRef } from 'react';
3
2
  import { makeFormErrors } from '../utils';
3
+ import { useState, useEffect, useRef } from 'react';
4
4
  export const useSignInForm = (email, loading, error, login, includeRecaptcha, callback) => {
5
5
  const submitRef = useRef(null);
6
6
  const inputRef = useRef(null);
7
7
  const recaptchaRef = useRef(null);
8
8
  const [data, setData] = useState({ email, password: '' });
9
9
  const [errors, setErrors] = useState({});
10
- const [submitting, setSubmitting] = useState(false);
10
+ const isSubmitting = useRef(false);
11
11
  const fields = [
12
12
  {
13
13
  label: 'Email',
@@ -25,13 +25,13 @@ export const useSignInForm = (email, loading, error, login, includeRecaptcha, ca
25
25
  }
26
26
  ];
27
27
  useEffect(() => {
28
- if (!data.email && email) {
29
- setData({ email, password: data.password });
28
+ if (email) {
29
+ setData(d => (Object.assign(Object.assign({}, d), { email })));
30
30
  }
31
- }, [email, data.email, data.password]);
31
+ }, [email]);
32
32
  useEffect(() => {
33
- if (loading === 'idle' && submitting) {
34
- setSubmitting(false);
33
+ if (loading === 'idle' && isSubmitting.current) {
34
+ isSubmitting.current = false;
35
35
  if (error) {
36
36
  if (recaptchaRef.current)
37
37
  recaptchaRef.current.reset();
@@ -41,14 +41,14 @@ export const useSignInForm = (email, loading, error, login, includeRecaptcha, ca
41
41
  callback();
42
42
  }
43
43
  }
44
- }, [loading, error, callback, submitting]);
44
+ }, [loading, error, callback]);
45
45
  const handleChange = (name, value) => {
46
46
  setData(Object.assign(Object.assign({}, data), { [name]: value }));
47
47
  };
48
48
  const handleSubmit = (evt) => __awaiter(void 0, void 0, void 0, function* () {
49
49
  var _a, _b, _c, _d, _e;
50
50
  evt === null || evt === void 0 ? void 0 : evt.preventDefault();
51
- const { email, password } = data;
51
+ const { password } = data;
52
52
  if (includeRecaptcha) {
53
53
  try {
54
54
  const isEnterpriseRecaptcha = ((_a = recaptchaRef.current) === null || _a === void 0 ? void 0 : _a.props.size) === 'invisible';
@@ -56,26 +56,26 @@ export const useSignInForm = (email, loading, error, login, includeRecaptcha, ca
56
56
  ? yield ((_b = recaptchaRef.current) === null || _b === void 0 ? void 0 : _b.executeAsync())
57
57
  : (_c = recaptchaRef.current) === null || _c === void 0 ? void 0 : _c.getValue();
58
58
  if (!token) {
59
- setSubmitting(false);
59
+ isSubmitting.current = false;
60
60
  setErrors({
61
61
  form: 'Please complete the recaptcha before submitting'
62
62
  });
63
63
  }
64
64
  else {
65
- login(email, password, token);
66
- setSubmitting(true);
65
+ login(data.email || email, password, token);
66
+ isSubmitting.current = true;
67
67
  }
68
68
  }
69
69
  catch (err) {
70
- setSubmitting(false);
70
+ isSubmitting.current = false;
71
71
  setErrors({
72
72
  form: 'Please complete the recaptcha before submitting'
73
73
  });
74
74
  }
75
75
  }
76
76
  else {
77
- login(email, password);
78
- setSubmitting(true);
77
+ login(data.email || email, password);
78
+ isSubmitting.current = true;
79
79
  }
80
80
  ((_d = submitRef.current) === null || _d === void 0 ? void 0 : _d.blur) && ((_e = submitRef.current) === null || _e === void 0 ? void 0 : _e.blur());
81
81
  });
@@ -86,7 +86,7 @@ export const useSignInForm = (email, loading, error, login, includeRecaptcha, ca
86
86
  fields,
87
87
  data,
88
88
  errors,
89
- submitting,
89
+ submitting: isSubmitting.current,
90
90
  handleChange,
91
91
  handleSubmit
92
92
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-tender/utils",
3
- "version": "0.5.24",
3
+ "version": "0.5.26",
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",